1 " Language: Verilog HDL
2 " Maintainer: Chih-Tsun Huang <cthuang@larc.ee.nthu.edu.tw>
3 " Last Change: 2011 Dec 10 by Thilo Six
4 " URL: http://larc.ee.nthu.edu.tw/~cthuang/vim/indent/verilog.vim
7 " Suggestions for improvement, bug reports by
8 " Leo Butlero <lbutler@brocade.com>
11 " b:verilog_indent_modules : indenting after the declaration
13 " b:verilog_indent_width : indenting width
14 " b:verilog_indent_verbose : verbose to each indenting
17 " Only load this indent file when no other was loaded.
18 if exists("b:did_indent")
23 setlocal indentexpr=GetVerilogIndent()
24 setlocal indentkeys=!^F,o,O,0),=begin,=end,=join,=endcase
25 setlocal indentkeys+==endmodule,=endfunction,=endtask,=endspecify
26 setlocal indentkeys+==`else,=`endif
28 " Only define the function once.
29 if exists("*GetVerilogIndent")
36 function GetVerilogIndent()
38 if exists('b:verilog_indent_width')
39 let offset = b:verilog_indent_width
43 if exists('b:verilog_indent_modules')
44 let indent_modules = offset
46 let indent_modules = 0
49 " Find a non-blank line above the current line.
50 let lnum = prevnonblank(v:lnum - 1)
52 " At the start of the file use zero indent.
57 let lnum2 = prevnonblank(lnum - 1)
58 let curr_line = getline(v:lnum)
59 let last_line = getline(lnum)
60 let last_line2 = getline(lnum2)
61 let ind = indent(lnum)
62 let ind2 = indent(lnum - 1)
63 let offset_comment1 = 1
64 " Define the condition of an open statement
65 " Exclude the match of //, /* or */
66 let vlog_openstat = '\(\<or\>\|\([*/]\)\@<![*(,{><+-/%^&|!=?:]\([*/]\)\@!\)'
67 " Define the condition when the statement ends with a one-line comment
68 let vlog_comment = '\(//.*\|/\*.*\*/\s*\)'
69 if exists('b:verilog_indent_verbose')
70 let vverb_str = 'INDENT VERBOSE:'
76 " Indent accoding to last line
77 " End of multiple-line comment
78 if last_line =~ '\*/\s*$' && last_line !~ '/\*.\{-}\*/'
79 let ind = ind - offset_comment1
81 echo vverb_str "De-indent after a multiple-line comment."
84 " Indent after if/else/for/case/always/initial/specify/fork blocks
85 elseif last_line =~ '`\@<!\<\(if\|else\)\>' ||
86 \ last_line =~ '^\s*\<\(for\|case\%[[zx]]\)\>' ||
87 \ last_line =~ '^\s*\<\(always\|initial\)\>' ||
88 \ last_line =~ '^\s*\<\(specify\|fork\)\>'
89 if last_line !~ '\(;\|\<end\>\)\s*' . vlog_comment . '*$' ||
90 \ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . vlog_comment . '*$'
91 let ind = ind + offset
92 if vverb | echo vverb_str "Indent after a block statement." | endif
94 " Indent after function/task blocks
95 elseif last_line =~ '^\s*\<\(function\|task\)\>'
96 if last_line !~ '\<end\>\s*' . vlog_comment . '*$' ||
97 \ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . vlog_comment . '*$'
98 let ind = ind + offset
100 echo vverb_str "Indent after function/task block statement."
104 " Indent after module/function/task/specify/fork blocks
105 elseif last_line =~ '^\s*\<module\>'
106 let ind = ind + indent_modules
107 if vverb && indent_modules
108 echo vverb_str "Indent after module statement."
110 if last_line =~ '[(,]\s*' . vlog_comment . '*$' &&
111 \ last_line !~ '\(//\|/\*\).*[(,]\s*' . vlog_comment . '*$'
112 let ind = ind + offset
114 echo vverb_str "Indent after a multiple-line module statement."
118 " Indent after a 'begin' statement
119 elseif last_line =~ '\(\<begin\>\)\(\s*:\s*\w\+\)*' . vlog_comment . '*$' &&
120 \ last_line !~ '\(//\|/\*\).*\(\<begin\>\)' &&
121 \ ( last_line2 !~ vlog_openstat . '\s*' . vlog_comment . '*$' ||
122 \ last_line2 =~ '^\s*[^=!]\+\s*:\s*' . vlog_comment . '*$' )
123 let ind = ind + offset
124 if vverb | echo vverb_str "Indent after begin statement." | endif
126 " De-indent for the end of one-line block
127 elseif ( last_line !~ '\<begin\>' ||
128 \ last_line =~ '\(//\|/\*\).*\<begin\>' ) &&
129 \ last_line2 =~ '\<\(`\@<!if\|`\@<!else\|for\|always\|initial\)\>.*' .
130 \ vlog_comment . '*$' &&
132 \ '\(//\|/\*\).*\<\(`\@<!if\|`\@<!else\|for\|always\|initial\)\>' &&
133 \ last_line2 !~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
134 \ ( last_line2 !~ '\<begin\>' ||
135 \ last_line2 =~ '\(//\|/\*\).*\<begin\>' )
136 let ind = ind - offset
138 echo vverb_str "De-indent after the end of one-line statement."
141 " Multiple-line statement (including case statement)
143 " Ident the first open line
144 elseif last_line =~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
145 \ last_line !~ '\(//\|/\*\).*' . vlog_openstat . '\s*$' &&
146 \ last_line2 !~ vlog_openstat . '\s*' . vlog_comment . '*$'
147 let ind = ind + offset
148 if vverb | echo vverb_str "Indent after an open statement." | endif
151 " De-indent for an optional close parenthesis and a semicolon, and only
152 " if there exists precedent non-whitespace char
153 elseif last_line =~ ')*\s*;\s*' . vlog_comment . '*$' &&
154 \ last_line !~ '^\s*)*\s*;\s*' . vlog_comment . '*$' &&
155 \ last_line !~ '\(//\|/\*\).*\S)*\s*;\s*' . vlog_comment . '*$' &&
156 \ ( last_line2 =~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
157 \ last_line2 !~ ';\s*//.*$') &&
158 \ last_line2 !~ '^\s*' . vlog_comment . '$'
159 let ind = ind - offset
160 if vverb | echo vverb_str "De-indent after a close statement." | endif
163 elseif last_line =~ '^\s*`\<\(ifdef\|else\)\>'
164 let ind = ind + offset
166 echo vverb_str "Indent after a `ifdef or `else statement."
171 " Re-indent current line
173 " De-indent on the end of the block
174 " join/end/endcase/endfunction/endtask/endspecify
175 if curr_line =~ '^\s*\<\(join\|end\|endcase\)\>' ||
176 \ curr_line =~ '^\s*\<\(endfunction\|endtask\|endspecify\)\>'
177 let ind = ind - offset
178 if vverb | echo vverb_str "De-indent the end of a block." | endif
179 elseif curr_line =~ '^\s*\<endmodule\>'
180 let ind = ind - indent_modules
181 if vverb && indent_modules
182 echo vverb_str "De-indent the end of a module."
185 " De-indent on a stand-alone 'begin'
186 elseif curr_line =~ '^\s*\<begin\>'
187 if last_line !~ '^\s*\<\(function\|task\|specify\|module\)\>' &&
188 \ last_line !~ '^\s*\()*\s*;\|)\+\)\s*' . vlog_comment . '*$' &&
190 \ '\<\(`\@<!if\|`\@<!else\|for\|case\%[[zx]]\|always\|initial\)\>' ||
191 \ last_line =~ ')\s*' . vlog_comment . '*$' ||
192 \ last_line =~ vlog_openstat . '\s*' . vlog_comment . '*$' )
193 let ind = ind - offset
195 echo vverb_str "De-indent a stand alone begin statement."
199 " De-indent after the end of multiple-line statement
200 elseif curr_line =~ '^\s*)' &&
201 \ ( last_line =~ vlog_openstat . '\s*' . vlog_comment . '*$' ||
202 \ last_line !~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
203 \ last_line2 =~ vlog_openstat . '\s*' . vlog_comment . '*$' )
204 let ind = ind - offset
206 echo vverb_str "De-indent the end of a multiple statement."
209 " De-indent `else and `endif
210 elseif curr_line =~ '^\s*`\<\(else\|endif\)\>'
211 let ind = ind - offset
212 if vverb | echo vverb_str "De-indent `else and `endif statement." | endif
216 " Return the indention
220 let &cpo = s:cpo_save