On 7/20/06, Edward L. Fox <[EMAIL PROTECTED]> wrote:
Hi VIMmers,

The default sh indent script in VIM7 indent the following sample file this way:

-------------------- 8< --------------------
#!/bin/sh

SYSTEM=`uname -s`

case $SYSTEM in
    Linux)
    echo "My system is Linux"
    echo "Do Linux stuff here..."
    ;;
    FreeBSD)
    echo "My system is FreeBSD"
    echo "Do FreeBSD stuff here..."
    ;;
    *)
    echo "Unknown system : $SYSTEM"
    echo "I don't what to do..."
    ;;
esac
-------------------- 8< --------------------

But I preferred the indent of each case label be decreased to the same
level of the "case" statement, like this:

-------------------- 8< --------------------
#!/bin/sh

SYSTEM=`uname -s`

case $SYSTEM in
Linux)
    echo "My system is Linux"
    echo "Do Linux stuff here..."
    ;;
FreeBSD)
    echo "My system is FreeBSD"
    echo "Do FreeBSD stuff here..."
    ;;
*)
    echo "Unknown system : $SYSTEM"
    echo "I don't what to do..."
    ;;
esac
-------------------- 8< --------------------

So I hacked the indent script. Any suggestion and feedback are welcome. :-)

Can anyone hack the script and support this indent flavor, too?

-------------------- 8< --------------------
#!/bin/sh

SYSTEM=`uname -s`

case $SYSTEM in
    Linux)
        echo "My system is Linux"
        echo "Do Linux stuff here..."
        ;;
    FreeBSD)
        echo "My system is FreeBSD"
        echo "Do FreeBSD stuff here..."
        ;;
    *)
        echo "Unknown system : $SYSTEM"
        echo "I don't what to do..."
        ;;
esac
-------------------- 8< --------------------


Regards,


Edward L. Fox




I supported the both formats now.

:let g:sh_indent_case_labels = 0 for the first indent style.

case ...
a)
   commands
   ;;
b)
   commands
   ;;
c)
   commands
   ;;
esac

:let g:sh_indent_case_labels = 1 for the second indent style.

case ...
   a)
       commands
       ;;
   b)
       commands
       ;;
   c)
       commands
       ;;
esac

Here is the patch:

Patch unofficial

*** /usr/local/share/vim/vim70/indent/sh.vim    2006-07-24
11:12:22.000000000 +0800
--- runtime/indent/sh.vim      2006-07-30 14:20:45.000000000 +0800
***************
*** 1,7 ****
 " Vim indent file
! " Language:       Shell Script
! " Maintainer:       Nikolai Weibull <[EMAIL PROTECTED]>
! " Latest Revision:  2006-04-19

 if exists("b:did_indent")
   finish
--- 1,8 ----
 " Vim indent file
! " Language:        Shell Script
! " Maintainer:    Nikolai Weibull <[EMAIL PROTECTED]>
! " Modified:      Edward L. Fox <[EMAIL PROTECTED]>
! " Last Modified: 2006-07-30 14:20:45

 if exists("b:did_indent")
   finish
***************
*** 9,15 ****
 let b:did_indent = 1

 setlocal indentexpr=GetShIndent()
! setlocal indentkeys+==then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done
 setlocal indentkeys-=:,0#

 if exists("*GetShIndent")
--- 10,16 ----
 let b:did_indent = 1

 setlocal indentexpr=GetShIndent()
! setlocal indentkeys+==then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done,)
 setlocal indentkeys-=:,0#

 if exists("*GetShIndent")
***************
*** 27,50 ****

   " Add a 'shiftwidth' after if, while, else, case, until, for, function()
   " Skip if the line also contains the closure for the above
   let ind = indent(lnum)
   let line = getline(lnum)
   if line =~ '^\s*\(if\|then\|do\|else\|elif\|case\|while\|until\|for\)\>'
       \ || line =~ '^\s*\<\k\+\>\s*()\s*{'
       \ || line =~ '^\s*{'
     if line !~ '\(esac\|fi\|done\)\>\s*$' && line !~ '}\s*$'
       let ind = ind + &sw
     endif
   endif

   " Subtract a 'shiftwidth' on a then, do, else, esac, fi, done
   " Retain the indentation level if line matches fin (for find)
   let line = getline(v:lnum)
!   if (line =~ '^\s*\(then\|do\|else\|elif\|esac\|fi\|done\)\>' ||
line =~ '^\s*}')
       \ && line !~ '^\s*fi[ln]\>'
     let ind = ind - &sw
   endif

   return ind
 endfunction

--- 28,64 ----

   " Add a 'shiftwidth' after if, while, else, case, until, for, function()
   " Skip if the line also contains the closure for the above
+
   let ind = indent(lnum)
   let line = getline(lnum)
   if line =~ '^\s*\(if\|then\|do\|else\|elif\|case\|while\|until\|for\)\>'
       \ || line =~ '^\s*\<\k\+\>\s*()\s*{'
+     \ || line =~ '^\s*[^(]\+\s*)'
       \ || line =~ '^\s*{'
     if line !~ '\(esac\|fi\|done\)\>\s*$' && line !~ '}\s*$'
       let ind = ind + &sw
     endif
   endif

+   if line =~ '^\s*case\>' && g:sh_indent_case_labels
+       let ind = ind + &sw
+   endif
+
   " Subtract a 'shiftwidth' on a then, do, else, esac, fi, done
   " Retain the indentation level if line matches fin (for find)
   let line = getline(v:lnum)
!   if (line =~ '^\s*\(then\|do\|else\|elif\|esac\|fi\|done\)\>'
!     \ || line =~ '^\s*[^(]\+\s*)'
!     \ || line =~ '^\s*}'
!     \ )
       \ && line !~ '^\s*fi[ln]\>'
     let ind = ind - &sw
   endif

+   if line =~ '^\s*esac\>' && g:sh_indent_case_labels
+       let ind = ind - &sw
+   endif
+
   return ind
 endfunction

Reply via email to