home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim60.zip / vim60rt2.zip / vim / vim60 / indent / verilog.vim < prev    next >
Text File  |  2001-09-26  |  5KB  |  154 lines

  1. " Vim indent file
  2. " Language:    Verilog HDL
  3. " Maintainer:    Chih-Tsun Huang <cthuang@larc.ee.nthu.edu.tw>
  4. " Last Change:    Wed Aug  1 21:08:17 CST 2001
  5. " URL:        http://larc.ee.nthu.edu.tw/~cthuang/vim/indent/verilog.vim
  6. "
  7. " Buffer Variables:
  8. "     b:verilog_indent_modules : indenting after the declaration
  9. "                 of module blocks
  10. "     b:verilog_indent_width   : indenting width
  11. "
  12.  
  13. " Only load this indent file when no other was loaded.
  14. if exists("b:did_indent")
  15.   finish
  16. endif
  17. let b:did_indent = 1
  18.  
  19. setlocal indentexpr=GetVerilogIndent()
  20. setlocal indentkeys=!^F,o,O,0),=begin,=end,=join,=endcase
  21. setlocal indentkeys+==endmodule,=endfunction,=endtask,=endspecify
  22. setlocal indentkeys+==`else,=`endif
  23.  
  24. " Only define the function once.
  25. if exists("*GetVerilogIndent")
  26.   finish
  27. endif
  28.  
  29. set cpo-=C
  30.  
  31. function GetVerilogIndent()
  32.  
  33.   if exists('b:verilog_indent_width')
  34.     let offset = b:verilog_indent_width
  35.   else
  36.     let offset = &sw
  37.   endif
  38.   if exists('b:verilog_indent_modules')
  39.     let indent_modules = offset
  40.   else
  41.     let indent_modules = 0
  42.   endif
  43.  
  44.   " Find a non-blank line above the current line.
  45.   let lnum = prevnonblank(v:lnum - 1)
  46.  
  47.   " At the start of the file use zero indent.
  48.   if lnum == 0
  49.     return 0
  50.   endif
  51.  
  52.   let lnum2 = prevnonblank(lnum - 1)
  53.   let curr_line  = getline(v:lnum)
  54.   let last_line  = getline(lnum)
  55.   let last_line2 = getline(lnum2)
  56.   let ind  = indent(lnum)
  57.   let ind2 = indent(lnum - 1)
  58.   let offset_comment1 = 1
  59.  
  60.   " Indent accoding to last line
  61.   " End of multiple-line comment
  62.   if last_line =~ '\*/\s*$'
  63.     let ind = ind - offset_comment1
  64.  
  65.   " Indent after if/else/for/case/always/initial/specify/fork blocks
  66.   elseif last_line =~ '\<\(if\|else\)\>' ||
  67.     \ last_line =~ '^\s*\<\(for\|case\%[[zx]]\)\>' ||
  68.     \ last_line =~ '^\s*\<\(always\|initial\)\>' ||
  69.     \ last_line =~ '^\s*\<\(specify\|fork\)\>'
  70.     if last_line !~ '\(;\|\<end\>\)\s*$'
  71.       let ind = ind + offset
  72.     endif
  73.   " Indent after function/task blocks
  74.   elseif last_line =~ '^\s*\<\(function\|task\)\>'
  75.     if last_line !~ '\<end\>\s*$'
  76.       let ind = ind + offset
  77.     endif
  78.  
  79.   " Indent after each condition in case block
  80.   elseif last_line =~
  81.     \ '^\s*\(default\|[a-zA-Z0-9_]\+\|[0-9]\+.[dDhHbBoO][0-9a-fA-F_xXzZ?]\+\)\s*[,:]'
  82.     if last_line !~ '\(;\|\<end\>\|,\)\s*$'
  83.       let ind = ind + offset
  84.     endif
  85.  
  86.   " Indent after module/function/task/specify/fork blocks
  87.   elseif last_line =~ '^\s*\<module\>'
  88.     let ind = ind + indent_modules
  89.     if last_line =~ '[(,]\s*$'
  90.       let ind = ind + offset
  91.     endif
  92.  
  93.   " Indent after a 'begin' statement
  94.   elseif last_line =~ '\(\<begin\>\)\(\s*:\s*\w\+\)*$'
  95.     let ind = ind + offset
  96.  
  97.   " De-indent for the end of one-line block
  98.   elseif last_line !~ '\<begin\>' &&
  99.     \ last_line2 =~ '^\s*\<\(if\|else\|for\|always\|initial\)\>' &&
  100.     \ last_line2 !~ '\(\<or\>\|[*(,{><+-/%^&|!=?:]\)\s*$' &&
  101.     \ last_line2 !~ '\<begin\>'
  102.     let ind = ind - offset
  103.  
  104.   " Multiple-line statement
  105.   " Open statement
  106.   elseif last_line =~ '[*(,{><+-/%^&|!=?:]\s*$' &&
  107.     \ last_line2 !~ '[*(,{><+-/%^&|!=?:]\s*$'
  108.     let ind = ind + offset
  109.   " Close statement
  110.   elseif last_line =~ '\();\|;\|)\)\s*$' &&
  111.     \ last_line !~ '^\s*\();\|;\|)\)\s*$' &&
  112.     \ last_line2 =~ '\(\<or\>\|[*(,{><+-/%^&|!=?:]\)\s*$' &&
  113.     \ last_line2 !~ '\(//\|\*/\)\s*$'
  114.     let ind = ind - offset
  115.  
  116.   " `ifdef and `else
  117.   elseif last_line =~ '^\s*`\<\(ifdef\|else\)\>'
  118.     let ind = ind + offset
  119.  
  120.   endif
  121.  
  122.   " Re-indent current line
  123.  
  124.   " De-indent on the end of the block
  125.   " join/end/endcase/endfunction/endtask/endspecify
  126.   if curr_line =~ '^\s*\<\(join\|end\|endcase\)\>' ||
  127.     \ curr_line =~ '^\s*\<\(endfunction\|endtask\|endspecify\)\>'
  128.     let ind = ind - offset
  129.   elseif curr_line =~ '^\s*\<endmodule\>'
  130.     let ind = ind - indent_modules
  131.   " De-indent on a stand-alone 'begin'
  132.   elseif curr_line =~ '^\s*\<begin\>'
  133.     if last_line =~ '\<\(if\|else\|for\|case\%[[zx]]\|always\|initial\)\>' ||
  134.       \ last_line =~
  135.       \ '^\s*\(default\|[a-zA-Z0-9_]\+\|[0-9]\+.[dDhHbBoO][0-9a-fA-F_xXzZ?]\+\)\s*:'
  136.       let ind = ind - offset
  137.     endif
  138.  
  139.   " De-indent after the end of multiple-line statement
  140.   elseif curr_line =~ '^\s*)' && last_line !~ ')\s*$'
  141.     let ind = ind - offset
  142.  
  143.   " De-indent `else and `endif
  144.   elseif curr_line =~ '^\s*`\<\(else\|endif\)\>'
  145.     let ind = ind - offset
  146.  
  147.   endif
  148.  
  149.   " Return the indention
  150.   return ind
  151. endfunction
  152.  
  153. " vim:sw=2
  154.