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