verilog.vim 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. " Language: Verilog HDL
  2. " Maintainer: Chih-Tsun Huang <cthuang@cs.nthu.edu.tw>
  3. " Last Change: 2017 Aug 25 by Chih-Tsun Huang
  4. " 2023 Aug 28 by Vim Project (undo_indent)
  5. " URL: http://www.cs.nthu.edu.tw/~cthuang/vim/indent/verilog.vim
  6. "
  7. " Credits:
  8. " Suggestions for improvement, bug reports by
  9. " Takuya Fujiwara <tyru.exe@gmail.com>
  10. " Thilo Six <debian@Xk2c.de>
  11. " Leo Butlero <lbutler@brocade.com>
  12. "
  13. " Buffer Variables:
  14. " b:verilog_indent_modules : indenting after the declaration
  15. " of module blocks
  16. " b:verilog_indent_width : indenting width
  17. " b:verilog_indent_verbose : verbose to each indenting
  18. "
  19. " Only load this indent file when no other was loaded.
  20. if exists("b:did_indent")
  21. finish
  22. endif
  23. let b:did_indent = 1
  24. setlocal indentexpr=GetVerilogIndent()
  25. setlocal indentkeys=!^F,o,O,0),=begin,=end,=join,=endcase
  26. setlocal indentkeys+==endmodule,=endfunction,=endtask,=endspecify
  27. setlocal indentkeys+==endconfig,=endgenerate,=endprimitive,=endtable
  28. setlocal indentkeys+==`else,=`elsif,=`endif
  29. let b:undo_indent = "setlocal indentexpr< indentkeys<"
  30. " Only define the function once.
  31. if exists("*GetVerilogIndent")
  32. finish
  33. endif
  34. let s:cpo_save = &cpo
  35. set cpo&vim
  36. function GetVerilogIndent()
  37. if exists('b:verilog_indent_width')
  38. let offset = b:verilog_indent_width
  39. else
  40. let offset = shiftwidth()
  41. endif
  42. if exists('b:verilog_indent_modules')
  43. let indent_modules = offset
  44. else
  45. let indent_modules = 0
  46. endif
  47. " Find a non-blank line above the current line.
  48. let lnum = prevnonblank(v:lnum - 1)
  49. " At the start of the file use zero indent.
  50. if lnum == 0
  51. return 0
  52. endif
  53. let lnum2 = prevnonblank(lnum - 1)
  54. let curr_line = getline(v:lnum)
  55. let last_line = getline(lnum)
  56. let last_line2 = getline(lnum2)
  57. let ind = indent(lnum)
  58. let ind2 = indent(lnum - 1)
  59. let offset_comment1 = 1
  60. " Define the condition of an open statement
  61. " Exclude the match of //, /* or */
  62. let vlog_openstat = '\(\<or\>\|\([*/]\)\@<![*(,{><+-/%^&|!=?:]\([*/]\)\@!\)'
  63. " Define the condition when the statement ends with a one-line comment
  64. let vlog_comment = '\(//.*\|/\*.*\*/\s*\)'
  65. if exists('b:verilog_indent_verbose')
  66. let vverb_str = 'INDENT VERBOSE:'
  67. let vverb = 1
  68. else
  69. let vverb = 0
  70. endif
  71. " Indent according to last line
  72. " End of multiple-line comment
  73. if last_line =~ '\*/\s*$' && last_line !~ '/\*.\{-}\*/'
  74. let ind = ind - offset_comment1
  75. if vverb
  76. echo vverb_str "De-indent after a multiple-line comment."
  77. endif
  78. " Indent after if/else/for/case/always/initial/specify/fork blocks
  79. " Note: We exclude '`if' or '`else' and consider 'end else'
  80. " 'end if' is redundant here
  81. elseif last_line =~ '^\s*\(end\)\=\s*`\@<!\<\(if\|else\)\>' ||
  82. \ last_line =~ '^\s*\<\(for\|case\%[[zx]]\)\>' ||
  83. \ last_line =~ '^\s*\<\(always\|initial\)\>' ||
  84. \ last_line =~ '^\s*\<\(specify\|fork\)\>'
  85. if last_line !~ '\(;\|\<end\>\)\s*' . vlog_comment . '*$' ||
  86. \ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . vlog_comment . '*$'
  87. let ind = ind + offset
  88. if vverb | echo vverb_str "Indent after a block statement." | endif
  89. endif
  90. " Indent after function/task/config/generate/primitive/table blocks
  91. elseif last_line =~ '^\s*\<\(function\|task\|config\|generate\|primitive\|table\)\>'
  92. if last_line !~ '\<end\>\s*' . vlog_comment . '*$' ||
  93. \ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . vlog_comment . '*$'
  94. let ind = ind + offset
  95. if vverb
  96. echo vverb_str "Indent after function/task block statement."
  97. endif
  98. endif
  99. " Indent after module/function/task/specify/fork blocks
  100. elseif last_line =~ '^\s*\<module\>'
  101. let ind = ind + indent_modules
  102. if vverb && indent_modules
  103. echo vverb_str "Indent after module statement."
  104. endif
  105. if last_line =~ '[(,]\s*' . vlog_comment . '*$' &&
  106. \ last_line !~ '\(//\|/\*\).*[(,]\s*' . vlog_comment . '*$'
  107. let ind = ind + offset
  108. if vverb
  109. echo vverb_str "Indent after a multiple-line module statement."
  110. endif
  111. endif
  112. " Indent after a 'begin' statement
  113. elseif last_line =~ '\(\<begin\>\)\(\s*:\s*\w\+\)*' . vlog_comment . '*$' &&
  114. \ last_line !~ '\(//\|/\*\).*\(\<begin\>\)' &&
  115. \ ( last_line2 !~ vlog_openstat . '\s*' . vlog_comment . '*$' ||
  116. \ last_line2 =~ '^\s*[^=!]\+\s*:\s*' . vlog_comment . '*$' )
  117. let ind = ind + offset
  118. if vverb | echo vverb_str "Indent after begin statement." | endif
  119. " De-indent for the end of one-line block
  120. elseif ( last_line !~ '\<begin\>' ||
  121. \ last_line =~ '\(//\|/\*\).*\<begin\>' ) &&
  122. \ last_line2 =~ '\<\(`\@<!if\|`\@<!else\|for\|always\|initial\)\>.*' .
  123. \ vlog_comment . '*$' &&
  124. \ last_line2 !~
  125. \ '\(//\|/\*\).*\<\(`\@<!if\|`\@<!else\|for\|always\|initial\)\>' &&
  126. \ last_line2 !~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
  127. \ ( last_line2 !~ '\<begin\>' ||
  128. \ last_line2 =~ '\(//\|/\*\).*\<begin\>' )
  129. let ind = ind - offset
  130. if vverb
  131. echo vverb_str "De-indent after the end of one-line statement."
  132. endif
  133. " Multiple-line statement (including case statement)
  134. " Open statement
  135. " Ident the first open line
  136. elseif last_line =~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
  137. \ last_line !~ '\(//\|/\*\).*' . vlog_openstat . '\s*$' &&
  138. \ last_line2 !~ vlog_openstat . '\s*' . vlog_comment . '*$'
  139. let ind = ind + offset
  140. if vverb | echo vverb_str "Indent after an open statement." | endif
  141. " Close statement
  142. " De-indent for an optional close parenthesis and a semicolon, and only
  143. " if there exists precedent non-whitespace char
  144. elseif last_line =~ ')*\s*;\s*' . vlog_comment . '*$' &&
  145. \ last_line !~ '^\s*)*\s*;\s*' . vlog_comment . '*$' &&
  146. \ last_line !~ '\(//\|/\*\).*\S)*\s*;\s*' . vlog_comment . '*$' &&
  147. \ ( last_line2 =~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
  148. \ last_line2 !~ ';\s*//.*$') &&
  149. \ last_line2 !~ '^\s*' . vlog_comment . '$'
  150. let ind = ind - offset
  151. if vverb | echo vverb_str "De-indent after a close statement." | endif
  152. " `ifdef or `ifndef or `elsif or `else
  153. elseif last_line =~ '^\s*`\<\(ifn\?def\|elsif\|else\)\>'
  154. let ind = ind + offset
  155. if vverb
  156. echo vverb_str "Indent after a `ifdef or `ifndef or `elsif or `else statement."
  157. endif
  158. endif
  159. " Re-indent current line
  160. " De-indent on the end of the block
  161. " join/end/endcase/endfunction/endtask/endspecify
  162. if curr_line =~ '^\s*\<\(join\|end\|endcase\)\>' ||
  163. \ curr_line =~ '^\s*\<\(endfunction\|endtask\|endspecify\)\>' ||
  164. \ curr_line =~ '^\s*\<\(endconfig\|endgenerate\|endprimitive\|endtable\)\>'
  165. let ind = ind - offset
  166. if vverb | echo vverb_str "De-indent the end of a block." | endif
  167. elseif curr_line =~ '^\s*\<endmodule\>'
  168. let ind = ind - indent_modules
  169. if vverb && indent_modules
  170. echo vverb_str "De-indent the end of a module."
  171. endif
  172. " De-indent on a stand-alone 'begin'
  173. elseif curr_line =~ '^\s*\<begin\>'
  174. if last_line !~ '^\s*\<\(function\|task\|specify\|module\|config\|generate\|primitive\|table\)\>' &&
  175. \ last_line !~ '^\s*\()*\s*;\|)\+\)\s*' . vlog_comment . '*$' &&
  176. \ ( last_line =~
  177. \ '\<\(`\@<!if\|`\@<!else\|for\|case\%[[zx]]\|always\|initial\)\>' ||
  178. \ last_line =~ ')\s*' . vlog_comment . '*$' ||
  179. \ last_line =~ vlog_openstat . '\s*' . vlog_comment . '*$' )
  180. let ind = ind - offset
  181. if vverb
  182. echo vverb_str "De-indent a stand alone begin statement."
  183. endif
  184. endif
  185. " De-indent after the end of multiple-line statement
  186. elseif curr_line =~ '^\s*)' &&
  187. \ ( last_line =~ vlog_openstat . '\s*' . vlog_comment . '*$' ||
  188. \ last_line !~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
  189. \ last_line2 =~ vlog_openstat . '\s*' . vlog_comment . '*$' )
  190. let ind = ind - offset
  191. if vverb
  192. echo vverb_str "De-indent the end of a multiple statement."
  193. endif
  194. " De-indent `elsif or `else or `endif
  195. elseif curr_line =~ '^\s*`\<\(elsif\|else\|endif\)\>'
  196. let ind = ind - offset
  197. if vverb | echo vverb_str "De-indent `elsif or `else or `endif statement." | endif
  198. endif
  199. " Return the indentation
  200. return ind
  201. endfunction
  202. let &cpo = s:cpo_save
  203. unlet s:cpo_save
  204. " vim:sw=2