98
|
1 ;;; verilog-mode.el --- major mode for editing verilog source in Emacs
|
134
|
2 ;;
|
161
|
3 ;; $Header: /afs/informatik.uni-tuebingen.de/local/web/xemacs/xemacs-cvs/XEmacs/xemacs/lisp/modes/Attic/verilog-mode.el,v 1.6 1997/06/14 20:31:18 steve Exp $
|
98
|
4
|
|
5 ;; Copyright (C) 1996 Free Software Foundation, Inc.
|
|
6
|
|
7 ;; Author: Michael McNamara (mac@silicon-sorcery.com)
|
|
8 ;; President, Silicon Sorcery
|
|
9 ;; Keywords: languages
|
|
10
|
|
11 ;; This file is part of GNU Emacs.
|
|
12
|
|
13 ;; This program is free software; you can redistribute it and/or modify
|
|
14 ;; it under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2 of the License, or
|
|
16 ;; (at your option) any later version.
|
|
17
|
|
18 ;; This program is distributed in the hope that it will be useful,
|
|
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;; GNU General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with this program; if not, write to the Free Software
|
|
25 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
134
|
29 ;;; This mode borrows heavily from the pascal-mode and the cc-mode of emacs
|
98
|
30
|
134
|
31 ;;; USAGE
|
|
32 ;;; =====
|
98
|
33
|
134
|
34 ;;; A major mode for editing Verilog HDL source code. When you have
|
|
35 ;;; entered Verilog mode, you may get more info by pressing C-h m. You
|
|
36 ;;; may also get online help describing various functions by: C-h f
|
|
37 ;;; <Name of function you want described>
|
98
|
38
|
134
|
39 ;;; To set up automatic verilog mode, put this file in your load path,
|
|
40 ;;; and include stuff like this in your .emacs:
|
98
|
41
|
134
|
42 ; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
|
|
43 ; (setq auto-mode-alist (cons '("\\.v\\'" . verilog-mode) auto-mode-alist))
|
|
44 ; (setq auto-mode-alist (cons '("\\.dv\\'" . verilog-mode) auto-mode-alist))
|
98
|
45
|
134
|
46 ;;; If you want to customize Verilog mode to fit your needs better,
|
|
47 ;;; you may add these lines (the values of the variables presented
|
|
48 ;;; here are the defaults):
|
|
49 ;;;
|
|
50 ;;; ;; User customization for Verilog mode
|
|
51 ;;; (setq verilog-indent-level 3
|
|
52 ;;; verilog-indent-level-module 3
|
|
53 ;;; verilog-indent-level-declaration 3
|
|
54 ;;; verilog-indent-level-behavorial 3
|
|
55 ;;; verilog-case-indent 2
|
|
56 ;;; verilog-auto-newline t
|
|
57 ;;; verilog-auto-indent-on-newline t
|
|
58 ;;; verilog-tab-always-indent t
|
|
59 ;;; verilog-auto-endcomments t
|
|
60 ;;; verilog-minimum-comment-distance 40
|
|
61 ;;; verilog-indent-begin-after-if t
|
|
62 ;;; verilog-auto-lineup '(all))
|
98
|
63
|
134
|
64 ;;; KNOWN BUGS / BUGREPORTS
|
|
65 ;;; ======================= This is beta code, and likely has
|
|
66 ;;; bugs. Please report any and all bugs to me at mac@silicon-sorcery.com.
|
98
|
67 ;;
|
|
68 ;;; Code:
|
|
69
|
|
70 (provide 'verilog-mode)
|
|
71
|
|
72 ;; This variable will always hold the version number of the mode
|
161
|
73 (defconst verilog-mode-version "$$Revision: 1.6 $$"
|
98
|
74 "Version of this verilog mode.")
|
|
75
|
134
|
76 ;;
|
|
77 ;; A hack so we can support either custom, or the old defvar
|
|
78 ;;
|
|
79 (eval-and-compile
|
|
80 (condition-case ()
|
|
81 (require 'custom)
|
|
82 (error nil))
|
|
83 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
|
|
84 nil ;; We've got what we needed
|
|
85 ;; We have the old custom-library, hack around it!
|
|
86 (defmacro defgroup (&rest args)
|
|
87 nil)
|
|
88 (defmacro customize (&rest args)
|
|
89 (message "Sorry, Customise is not available with this version of emacs"))
|
|
90 (defmacro defcustom (var value doc &rest args)
|
|
91 (` (defvar (, var) (, value) (, doc))))))
|
98
|
92
|
134
|
93 (defun verilog-customize ()
|
|
94 "Link to customize screen for Verilog"
|
|
95 (interactive)
|
142
|
96 (customize 'verilog)
|
134
|
97 )
|
98
|
98
|
142
|
99 (defgroup verilog nil
|
134
|
100 "Faciliates easy editing of Verilog source text"
|
|
101 :group 'languages)
|
|
102
|
|
103 (defcustom verilog-indent-level 3
|
|
104 "*Indentation of Verilog statements with respect to containing block."
|
142
|
105 :group 'verilog
|
134
|
106 :type 'integer
|
|
107 )
|
98
|
108
|
134
|
109 (defcustom verilog-indent-level-module 3
|
|
110 "* Indentation of Module level Verilog statements. (eg always, initial)
|
|
111 Set to 0 to get initial and always statements lined up
|
|
112 on the left side of your screen."
|
142
|
113 :group 'verilog
|
134
|
114 :type 'integer
|
|
115 )
|
98
|
116
|
134
|
117 (defcustom verilog-indent-level-declaration 3
|
|
118 "*Indentation of declarations with respect to containing block.
|
|
119 Set to 0 to get them list right under containing block."
|
142
|
120 :group 'verilog
|
134
|
121 :type 'integer
|
|
122 )
|
|
123
|
|
124 (defcustom verilog-indent-level-behavorial 3
|
|
125 "*Absolute indentation of first begin in a task or function block
|
|
126 Set to 0 to get such code to start at the left side of the screen."
|
142
|
127 :group 'verilog
|
134
|
128 :type 'integer
|
|
129 )
|
98
|
130
|
134
|
131 (defcustom verilog-cexp-indent 1
|
|
132 "*Indentation of Verilog statements split across lines."
|
142
|
133 :group 'verilog
|
134
|
134 :type 'integer
|
|
135 )
|
|
136
|
|
137 (defcustom verilog-case-indent 2
|
|
138 "*Indentation for case statements."
|
142
|
139 :group 'verilog
|
134
|
140 :type 'integer
|
|
141 )
|
98
|
142
|
134
|
143 (defcustom verilog-auto-newline t
|
|
144 "*Non-nil means automatically newline after semicolons"
|
142
|
145 :group 'verilog
|
134
|
146 :type 'integer
|
|
147 )
|
|
148
|
|
149 (defcustom verilog-auto-indent-on-newline t
|
|
150 "*Non-nil means automatically indent line after newline"
|
142
|
151 :group 'verilog
|
134
|
152 :type 'integer
|
|
153 )
|
98
|
154
|
134
|
155 (defcustom verilog-tab-always-indent t
|
|
156 "*Non-nil means TAB in Verilog mode should always reindent the
|
|
157 current line, regardless of where in the line point is when the TAB
|
|
158 command is used."
|
142
|
159 :group 'verilog
|
134
|
160 :type 'integer
|
|
161 )
|
|
162
|
|
163 (defcustom verilog-indent-begin-after-if t
|
|
164 "*If true, indent begin statements following if, else, while, for
|
|
165 and repeat. otherwise, line them up."
|
142
|
166 :group 'verilog
|
134
|
167 :type 'boolean )
|
98
|
168
|
134
|
169 (defcustom verilog-auto-endcomments t
|
|
170 "*Non-nil means a comment /* ... */ is set after the ends which ends
|
|
171 cases and functions. The name of the function or case will be set
|
|
172 between the braces."
|
142
|
173 :group 'verilog
|
134
|
174 :type 'boolean )
|
|
175
|
|
176 (defcustom verilog-minimum-comment-distance 40
|
|
177 "*Minimum distance between begin and end required before a comment
|
|
178 will be inserted. Setting this variable to zero results in every
|
|
179 end aquiring a comment; the default avoids too many redundanet
|
|
180 comments in tight quarters"
|
142
|
181 :group 'verilog
|
134
|
182 :type 'integer
|
|
183 )
|
|
184
|
|
185 (defvar verilog-auto-lineup '(all)
|
|
186 "*List of contexts where auto lineup of :'s or ='s should be done.
|
|
187 Elements can be of type: 'declaration' or 'case', which will do auto
|
|
188 lineup in declarations or case-statements respectively. The word 'all'
|
|
189 will do all lineups. '(case declaration) for instance will do lineup
|
|
190 in case-statements and parameterlist, while '(all) will do all
|
|
191 lineups."
|
|
192 )
|
98
|
193
|
|
194 (defvar verilog-mode-abbrev-table nil
|
|
195 "Abbrev table in use in Verilog-mode buffers.")
|
|
196
|
|
197 (defvar verilog-font-lock-keywords-after-1930
|
|
198 '(
|
134
|
199 ;;
|
|
200 ("^\\s-*\\(function\\|task\\|module\\|macromodule\\|primitive\\)\\>"
|
98
|
201 1 font-lock-keyword-face)
|
134
|
202 ("^\\s-*\\(function\\|task\\|module\\|macromodule\\|primitive\\)\\>\\s-*\\(\\sw+\\)"
|
98
|
203 2 font-lock-function-name-face nil t)
|
134
|
204 ("\\\\\\s-*" 0 'font-lock-function-name-face)
|
|
205 ("\\(@\\)\\|\\(#\\s-*\\(\\(\[0-9\]+\\('[hdxbo][0-9_xz]*\\)?\\)\\|\\((\[^)\]*)\\|\\sw+\\)\\)\\)" 0 font-lock-type-face)
|
|
206 ("\\(`\\s-*[A-Za-z][A-Za-z0-9_]*\\)" 0 font-lock-type-face)
|
|
207 ("\\<\\(in\\(teger\\|put\\|out\\)\\|parameter\\|defparam\\|output\\|supply[01]?\\|event\\|tri\\(0\\|1\\|reg\\|and\\|or\\)?\\|w\\(ire\\|or\\|and\\)\\|time\\|re\\(al\\(time\\)?\\|g\\)\\)\\>"
|
|
208 0 font-lock-type-face)
|
|
209 ("\\(\\$[a-zA-Z][a-zA-Z0-9_\\$]*\\)\\|\\(\\<\\(begin\\|case[xz]?\\|end\\(case\\|function\\|task\\|module\\|table\\|primitive\\|specify\\)?\\|a\\(ssign\\|lways\\)\\|default\\|initial\\|table\\|\\(pos\\|neg\\)edge\\|else\\|for\\(ever\\|k\\)?\\|join\\|if\\|repeat\\|then\\|while\\|specify\\)\\>\\)"
|
98
|
210 0 font-lock-keyword-face)
|
|
211 )
|
|
212 )
|
134
|
213
|
98
|
214 (defvar verilog-font-lock-keywords-before-1930
|
|
215 '(
|
134
|
216 ("^\\s-*\\(function\\|task\\|module\\|macromodule\\|primitive\\)\\>" . 1)
|
|
217 ("^\\s-*\\(function\\|task\\|module\\|macromodule\\|primitive\\)\\>\\s-*\\(\\sw+\\)"
|
98
|
218 2 font-lock-function-name-face nil t)
|
134
|
219 ("\\(\\\\\\s-*\\)\\|\\(`[ \t]*[A-Za-z][A-Za-z0-9_]*\\)" 0 font-lock-function-name-face)
|
98
|
220 ("[@#]" . font-lock-type-face)
|
|
221 ("\\<\\(in\\(teger\\|put\\|out\\)\\|parameter\\|defparam\\|output\\|supply[01]?\\|event\\|tri\\(0\\|1\\|reg\\|and\\|or\\)?\\|w\\(ire\\|or\\|and\\)\\|time\\|re\\(al\\(time\\)?\\|g\\)\\)\\>"
|
|
222 0 font-lock-type-face)
|
|
223 ("\\(\\$[a-zA-Z][a-zA-Z0-9_\\$]*\\)\\|\\(\\<\\(begin\\|case[xz]?\\|end\\(case\\|function\\|task\\|module\\|table\\|primitive\\|specify\\)?\\|a\\(ssign\\|lways\\)\\|default\\|initial\\|table\\|\\(pos\\|neg\\)edge\\|else\\|for\\(ever\\|k\\)?\\|join\\|if\\|repeat\\|then\\|while\\|specify\\)\\>\\)" . font-lock-keyword-face)
|
|
224 )
|
|
225 )
|
|
226
|
134
|
227 ;; Insure we have certain packages
|
|
228
|
|
229 (if (fboundp 'eval-when-compile)
|
|
230 (eval-when-compile
|
|
231 (condition-case nil
|
|
232 (require 'imenu)
|
|
233 (error nil))
|
|
234 (condition-case nil
|
|
235 (require 'reporter)
|
|
236 (error nil))
|
|
237 (condition-case nil
|
|
238 (require 'easymenu)
|
|
239 (error nil))))
|
|
240
|
98
|
241 (defvar verilog-imenu-generic-expression
|
134
|
242 '((nil "^\\s-*\\(\\(m\\(odule\\|acromodule\\)\\)\\|primitive\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 3)
|
|
243 ("*Vars*" "^\\s-*\\(reg\\|wire\\)\\)\\s-+\\(\\|\\[[^\\]+]\\s-+\\)\\([-A-Za-z0-9+]+\\)" 3))
|
98
|
244 "Imenu expression for Verilog-mode. See `imenu-generic-expression'.")
|
|
245
|
|
246 (defvar verilog-mode-abbrev-table nil
|
|
247 "Abbrev table in use in Verilog-mode buffers.")
|
|
248
|
|
249
|
|
250 (define-abbrev-table 'verilog-mode-abbrev-table ())
|
|
251
|
|
252 (defvar verilog-mode-map ()
|
|
253 "Keymap used in Verilog mode.")
|
|
254 (if verilog-mode-map
|
|
255 ()
|
|
256 (setq verilog-mode-map (make-sparse-keymap))
|
|
257 (define-key verilog-mode-map ";" 'electric-verilog-semi)
|
|
258 (define-key verilog-mode-map ":" 'electric-verilog-colon)
|
|
259 (define-key verilog-mode-map "=" 'electric-verilog-equal)
|
|
260 (define-key verilog-mode-map "\`" 'electric-verilog-tick)
|
|
261 (define-key verilog-mode-map "\t" 'electric-verilog-tab)
|
|
262 (define-key verilog-mode-map "\r" 'electric-verilog-terminate-line)
|
|
263 (define-key verilog-mode-map "\M-\C-b" 'electric-verilog-backward-sexp)
|
|
264 (define-key verilog-mode-map "\M-\C-f" 'electric-verilog-forward-sexp)
|
|
265 (define-key verilog-mode-map "\M-\r" (function (lambda ()
|
134
|
266 (interactive) (electric-verilog-terminate-line 1))))
|
98
|
267 (define-key verilog-mode-map "\M-\t" 'verilog-complete-word)
|
|
268 (define-key verilog-mode-map "\M-?" 'verilog-show-completions)
|
|
269 (define-key verilog-mode-map "\M-\C-h" 'verilog-mark-defun)
|
|
270 (define-key verilog-mode-map "\C-c\C-b" 'verilog-insert-block)
|
|
271 (define-key verilog-mode-map "\C-cb" 'verilog-label-be)
|
134
|
272 (define-key verilog-mode-map "\C-ci" 'verilog-pretty-declarations)
|
|
273 (define-key verilog-mode-map "\C-cC-b" 'verilog-submit-bug-report)
|
98
|
274 (define-key verilog-mode-map "\M-*" 'verilog-star-comment)
|
134
|
275 (define-key verilog-mode-map "\C-c\C-c" 'verilog-comment-region)
|
|
276 (define-key verilog-mode-map "\C-c\C-u" 'verilog-uncomment-region)
|
98
|
277 (define-key verilog-mode-map "\M-\C-a" 'verilog-beg-of-defun)
|
|
278 (define-key verilog-mode-map "\M-\C-e" 'verilog-end-of-defun)
|
|
279 (define-key verilog-mode-map "\C-c\C-d" 'verilog-goto-defun)
|
|
280 )
|
|
281
|
134
|
282 ;; menus
|
|
283
|
|
284 (if (string-match "XEmacs" emacs-version)
|
|
285 (defvar verilog-xemacs-menu
|
|
286 '("Verilog"
|
|
287 ["Line up declarations around point" verilog-pretty-declarations t]
|
|
288 ["Redo/insert comments on every end" verilog-label-be t]
|
|
289 "----"
|
|
290 ["Beginning of function" verilog-beg-of-defun t]
|
|
291 ["End of function" verilog-end-of-defun t]
|
|
292 ["Mark function" verilog-mark-defun t]
|
|
293 "----"
|
|
294 ["Move to beginning of block" electric-verilog-backward-sexp t]
|
|
295 ["Move to end of block" electric-verilog-forward-sexp t]
|
|
296 "----"
|
|
297 ["Comment Region" verilog-comment-region t]
|
|
298 ["UnComment Region" verilog-uncomment-region t]
|
|
299 ["Multi-line comment insert" verilog-star-comment t]
|
|
300 "----"
|
|
301 ["Insert begin-end block" verilog-insert-block t]
|
|
302 ["Complete word" verilog-complete-word t]
|
|
303 "----"
|
|
304 ["Submit bug report" verilog-submit-bug-report t]
|
|
305 ["Customize Verilog Mode..." verilog-customize t]
|
|
306 "XEmacs menu for VERILOG mode."))
|
|
307 (progn
|
|
308 (easy-menu-define verilog-menu verilog-mode-map "Menu for Verilog mode"
|
|
309 '("Verilog"
|
|
310 ["Line up declarations around point" verilog-pretty-declarations t]
|
|
311 ["Redo/insert comments on every end" verilog-label-be t]
|
|
312 "----"
|
|
313 ["Beginning of function" verilog-beg-of-defun t]
|
|
314 ["End of function" verilog-end-of-defun t]
|
|
315 ["Mark function" verilog-mark-defun t]
|
|
316 "----"
|
|
317 ["Move to beginning of block" electric-verilog-backward-sexp t]
|
|
318 ["Move to end of block" electric-verilog-forward-sexp t]
|
|
319 "----"
|
|
320 ["Comment Region" verilog-comment-region t]
|
|
321 ["UnComment Region" verilog-uncomment-region t]
|
|
322 ["Multi-line comment insert" verilog-star-comment t]
|
|
323 "----"
|
|
324 ["Insert begin-end block" verilog-insert-block t]
|
|
325 ["Complete word" verilog-complete-word t]
|
|
326 "----"
|
|
327 ["Submit bug report" verilog-submit-bug-report t]
|
|
328 ["Customize Verilog Mode..." verilog-customize t]
|
|
329 ))))
|
|
330
|
|
331 (defvar verilog-mode-abbrev-table nil
|
|
332 "Abbrev table in use in Verilog-mode buffers.")
|
|
333
|
|
334 (define-abbrev-table 'verilog-mode-abbrev-table ())
|
98
|
335
|
|
336
|
|
337 ;;;
|
|
338 ;;; Regular expressions used to calculate indent, etc.
|
|
339 ;;;
|
|
340 (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
|
134
|
341 (defconst verilog-case-re "\\(\\<case[xz]?\\>\\)")
|
98
|
342 ;; Want to match
|
|
343 ;; aa :
|
|
344 ;; aa,bb :
|
|
345 ;; a[34:32] :
|
|
346 ;; a,
|
|
347 ;; b :
|
134
|
348 (defconst
|
|
349 verilog-no-indent-begin-re
|
|
350 "\\<\\(if\\|else\\|while\\|for\\|repeat\\|always\\)\\>")
|
|
351 (defconst verilog-ends-re
|
|
352 (concat
|
|
353 "\\(\\<else\\>\\)\\|"
|
|
354 "\\(\\<if\\>\\)\\|"
|
|
355 "\\(\\<end\\>\\)\\|"
|
|
356 "\\(\\<join\\>\\)\\|"
|
|
357 "\\(\\<endcase\\>\\)\\|"
|
|
358 "\\(\\<endtable\\>\\)\\|"
|
|
359 "\\(\\<endspecify\\>\\)\\|"
|
|
360 "\\(\\<endfunction\\>\\)\\|"
|
|
361 "\\(\\<endtask\\>\\)"))
|
|
362
|
|
363
|
|
364 (defconst verilog-enders-re
|
|
365 (concat "\\(\\<endcase\\>\\)\\|"
|
|
366 "\\(\\<end\\>\\)\\|"
|
|
367 "\\(\\<end\\(\\(function\\)\\|\\(task\\)\\|"
|
|
368 "\\(module\\)\\|\\(primitive\\)\\)\\>\\)"))
|
98
|
369 (defconst verilog-endcomment-reason-re
|
|
370 (concat
|
134
|
371 "\\(\\<fork\\>\\)\\|"
|
|
372 "\\(\\<begin\\>\\)\\|"
|
|
373 "\\(\\<if\\>\\)\\|"
|
|
374 "\\(\\<else\\>\\)\\|"
|
|
375 "\\(\\<end\\>.*\\<else\\>\\)\\|"
|
|
376 "\\(\\<task\\>\\)\\|"
|
|
377 "\\(\\<function\\>\\)\\|"
|
|
378 "\\(\\<initial\\>\\)\\|"
|
|
379 "\\(\\<always\\>\\(\[ \t\]*@\\)?\\)\\|"
|
|
380 "\\(\\<while\\>\\)\\|"
|
|
381 "\\(\\<for\\(ever\\)?\\>\\)\\|"
|
|
382 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
|
98
|
383 "#"))
|
|
384
|
|
385 (defconst verilog-named-block-re "begin[ \t]*:")
|
134
|
386 (defconst verilog-beg-block-re
|
|
387 ;; "begin" "case" "casex" "fork" "casez" "table" "specify" "function" "task"
|
|
388 "\\(\\<\\(begin\\>\\|case\\(\\>\\|x\\>\\|z\\>\\)\\|f\\(ork\\>\\|unction\\>\\)\\|specify\\>\\|ta\\(ble\\>\\|sk\\>\\)\\)\\)")
|
|
389
|
|
390 (defconst verilog-beg-block-re-1
|
|
391 "\\<\\(begin\\)\\|\\(case[xz]?\\)\\|\\(fork\\)\\|\\(table\\)\\|\\(specify\\)\\|\\(function\\)\\|\\(task\\)\\>")
|
|
392 (defconst verilog-end-block-re
|
|
393 ;; "end" "join" "endcase" "endtable" "endspecify" "endtask" "endfunction"
|
|
394 "\\<\\(end\\(\\>\\|case\\>\\|function\\>\\|specify\\>\\|ta\\(ble\\>\\|sk\\>\\)\\)\\|join\\>\\)")
|
|
395
|
98
|
396 (defconst verilog-end-block-re-1 "\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\>\\)\\|\\(\\<endtable\\>\\)\\|\\(\\<endspecify\\>\\)\\|\\(\\<endfunction\\>\\)\\|\\(\\<endtask\\>\\)")
|
|
397 (defconst verilog-declaration-re
|
134
|
398 ;; "input" "inout" "output" "integer" "parameter" "defparam" "event"
|
|
399 ;; "real" "reg" "realtime" "time" "tri" "tri0" "tri1" "trireg" "triand"
|
|
400 ;; "trior" "supply0" "supply1" "wire" "wor" "wand"
|
|
401 "\\(\\<\\(defparam\\>\\|event\\>\\|in\\(out\\>\\|put\\>\\|teger\\>\\)\\|output\\>\\|parameter\\>\\|re\\(al\\(\\>\\|time\\>\\)\\|g\\>\\)\\|supply\\(0\\>\\|1\\>\\)\\|t\\(ime\\>\\|ri\\(0\\>\\|1\\>\\|\\>\\|and\\>\\|or\\>\\|reg\\>\\)\\)\\|w\\(and\\>\\|ire\\>\\|or\\>\\)\\)\\)")
|
98
|
402 (defconst verilog-declaration-re-1 (concat "^[ \t]*" verilog-declaration-re "[ \t]*\\(\\[[^]]*\\][ \t]*\\)?"))
|
134
|
403 (defconst verilog-declaration-re-2 (concat "[ \t]*" verilog-declaration-re "[ \t]*\\(\\[[^]]*\\][ \t]*\\)?"))
|
|
404 (defconst verilog-defun-re
|
|
405 ;;"module" "macromodule" "primitive"
|
|
406 "\\(\\<\\(m\\(acromodule\\>\\|odule\\>\\)\\|primitive\\>\\)\\)")
|
|
407 (defconst verilog-end-defun-re
|
|
408 ;; "endmodule" "endprimitive"
|
|
409 "\\(\\<end\\(module\\>\\|primitive\\>\\)\\)")
|
98
|
410 (defconst verilog-zero-indent-re
|
|
411 (concat verilog-defun-re "\\|" verilog-end-defun-re))
|
|
412 (defconst verilog-directive-re
|
134
|
413 ;; "`else" "`ifdef" "`endif" "`define" "`undef" "`include"
|
|
414 "\\(\\<`\\(define\\>\\|e\\(lse\\>\\|ndif\\>\\)\\|i\\(fdef\\>\\|nclude\\>\\)\\|undef\\>\\)\\)")
|
98
|
415 (defconst verilog-autoindent-lines-re
|
134
|
416 ;; "macromodule" "module" "primitive" "end" "endcase" "endfunction"
|
|
417 ;; "endtask" "endmodule" "endprimitive" "endspecify" "endtable" "join"
|
|
418 ;; "begin" "else" "`else" "`ifdef" "`endif" "`define" "`undef" "`include"
|
|
419 "\\(\\<\\(`\\(define\\>\\|e\\(lse\\>\\|ndif\\>\\)\\|i\\(fdef\\>\\|nclude\\>\\)\\|undef\\>\\)\\|begin\\>\\|e\\(lse\\>\\|nd\\(\\>\\|case\\>\\|function\\>\\|module\\>\\|primitive\\>\\|specify\\>\\|ta\\(ble\\>\\|sk\\>\\)\\)\\)\\|join\\>\\|m\\(acromodule\\>\\|odule\\>\\)\\|primitive\\>\\)\\)")
|
|
420
|
98
|
421 (defconst verilog-behavorial-block-beg-re
|
|
422 "\\(\\<initial\\>\\|\\<always\\>\\|\\<function\\>\\|\\<task\\>\\)")
|
|
423 (defconst verilog-indent-reg
|
134
|
424 (concat
|
|
425 "\\(\\<begin\\>\\|\\<case[xz]?\\>\\|\\<specify\\>\\|\\<fork\\>\\|\\<table\\>\\)\\|"
|
|
426 "\\(\\<end\\>\\|\\<join\\>\\|\\<endcase\\>\\|\\<endtable\\>\\|\\<endspecify\\>\\)\\|"
|
|
427 "\\(\\<module\\>\\|\\<macromodule\\>\\|\\<primitive\\>\\|\\<initial\\>\\|\\<always\\>\\)\\|"
|
|
428 "\\(\\<endmodule\\>\\|\\<endprimitive\\>\\)\\|"
|
|
429 "\\(\\<endtask\\>\\|\\<endfunction\\>\\)\\|"
|
|
430 "\\(\\<function\\>\\|\\<task\\>\\)"
|
|
431 ;; "\\|\\(\\<if\\>\\|\\<else\\>\\)"
|
|
432 ))
|
|
433 (defconst verilog-indent-re
|
|
434 "\\(\\<\\(always\\>\\|begin\\>\\|case\\(\\>\\|x\\>\\|z\\>\\)\\|end\\(\\>\\|case\\>\\|function\\>\\|module\\>\\|primitive\\>\\|specify\\>\\|ta\\(ble\\>\\|sk\\>\\)\\)\\|f\\(ork\\>\\|unction\\>\\)\\|initial\\>\\|join\\>\\|m\\(acromodule\\>\\|odule\\>\\)\\|primitive\\>\\|specify\\>\\|ta\\(ble\\>\\|sk\\>\\)\\)\\)")
|
|
435
|
|
436 (defconst verilog-defun-level-re
|
|
437 ;; "module" "macromodule" "primitive" "initial" "always" "endtask" "endfunction"
|
|
438 "\\(\\<\\(always\\>\\|end\\(function\\>\\|task\\>\\)\\|initial\\>\\|m\\(acromodule\\>\\|odule\\>\\)\\|primitive\\>\\)\\)")
|
|
439 (defconst verilog-cpp-level-re
|
|
440 ;;"endmodule" "endprimitive"
|
|
441 "\\(\\<end\\(module\\>\\|primitive\\>\\)\\)")
|
|
442 (defconst verilog-behavorial-level-re
|
|
443 ;; "function" "task"
|
|
444 "\\(\\<\\(function\\>\\|task\\>\\)\\)")
|
98
|
445 (defconst verilog-complete-reg
|
134
|
446 ;; "always" "repeat" "case" "casex" "casez" "while" "if" "for" "forever"
|
|
447 "\\(\\<\\(always\\>\\|case\\(\\>\\|x\\>\\|z\\>\\)\\|else\\|for\\(\\>\\|ever\\>\\)\\|if\\>\\|repeat\\>\\|while\\>\\)\\)")
|
98
|
448 (defconst verilog-end-statement-re
|
|
449 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
|
|
450 verilog-end-block-re "\\)"))
|
|
451 (defconst verilog-endcase-re
|
|
452 (concat verilog-case-re "\\|"
|
|
453 "\\(endcase\\)\\|"
|
|
454 verilog-defun-re
|
|
455 ))
|
|
456 ;;; Strings used to mark beginning and end of excluded text
|
|
457 (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----")
|
|
458 (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */")
|
|
459
|
|
460 (defconst verilog-emacs-features
|
|
461 (let ((major (and (boundp 'emacs-major-version)
|
|
462 emacs-major-version))
|
|
463 (minor (and (boundp 'emacs-minor-version)
|
|
464 emacs-minor-version))
|
|
465 flavor comments)
|
|
466 ;; figure out version numbers if not already discovered
|
|
467 (and (or (not major) (not minor))
|
|
468 (string-match "\\([0-9]+\\).\\([0-9]+\\)" emacs-version)
|
|
469 (setq major (string-to-int (substring emacs-version
|
|
470 (match-beginning 1)
|
|
471 (match-end 1)))
|
|
472 minor (string-to-int (substring emacs-version
|
|
473 (match-beginning 2)
|
|
474 (match-end 2)))))
|
|
475 (if (not (and major minor))
|
|
476 (error "Cannot figure out the major and minor version numbers."))
|
|
477 ;; calculate the major version
|
|
478 (cond
|
|
479 ((= major 18) (setq major 'v18)) ;Emacs 18
|
|
480 ((= major 4) (setq major 'v18)) ;Epoch 4
|
134
|
481 ((= major 20) (setq major 'v20
|
|
482 flavor 'XEmacs))
|
98
|
483 ((= major 19) (setq major 'v19 ;Emacs 19
|
|
484 flavor (if (or (string-match "Lucid" emacs-version)
|
|
485 (string-match "XEmacs" emacs-version))
|
|
486 'XEmacs 'FSF)))
|
|
487 ;; I don't know
|
|
488 (t (error "Cannot recognize major version number: %s" major)))
|
134
|
489 ;; XEmacs 19 uses 8-bit modify-syntax-entry flags, as do all
|
|
490 ;; patched Emacs 19, Emacs 18, Epoch 4's. Only Emacs 19 uses a
|
|
491 ;; 1-bit flag. Let's be as smart as we can about figuring this
|
|
492 ;; out.
|
98
|
493 (if (or (eq major 'v20) (eq major 'v19))
|
|
494 (let ((table (copy-syntax-table)))
|
|
495 (modify-syntax-entry ?a ". 12345678" table)
|
|
496 (cond
|
|
497 ;; XEmacs pre 20 and Emacs pre 19.30 use vectors for syntax tables.
|
|
498 ((vectorp table)
|
|
499 (if (= (logand (lsh (aref table ?a) -16) 255) 255)
|
|
500 (setq comments '8-bit)
|
|
501 (setq comments '1-bit)))
|
|
502 ;; XEmacs 20 is known to be 8-bit
|
|
503 ((eq flavor 'XEmacs) (setq comments '8-bit))
|
|
504 ;; Emacs 19.30 and beyond are known to be 1-bit
|
|
505 ((eq flavor 'FSF) (setq comments '1-bit))
|
|
506 ;; Don't know what this is
|
|
507 (t (error "Couldn't figure out syntax table format."))
|
|
508 ))
|
|
509 ;; Emacs 18 has no support for dual comments
|
|
510 (setq comments 'no-dual-comments))
|
|
511 ;; lets do some minimal sanity checking.
|
|
512 (if (or
|
|
513 ;; Lemacs before 19.6 had bugs
|
|
514 (and (eq major 'v19) (eq flavor 'XEmacs) (< minor 6))
|
|
515 ;; Emacs 19 before 19.21 has known bugs
|
|
516 (and (eq major 'v19) (eq flavor 'FSF) (< minor 21))
|
|
517 )
|
|
518 (with-output-to-temp-buffer "*verilog-mode warnings*"
|
|
519 (print (format
|
134
|
520 "The version of Emacs that you are running, %s,
|
98
|
521 has known bugs in its syntax parsing routines which will affect the
|
|
522 performance of verilog-mode. You should strongly consider upgrading to the
|
|
523 latest available version. verilog-mode may continue to work, after a
|
|
524 fashion, but strange indentation errors could be encountered."
|
134
|
525 emacs-version))))
|
98
|
526 ;; Emacs 18, with no patch is not too good
|
|
527 (if (and (eq major 'v18) (eq comments 'no-dual-comments))
|
|
528 (with-output-to-temp-buffer "*verilog-mode warnings*"
|
|
529 (print (format
|
134
|
530 "The version of Emacs 18 you are running, %s,
|
98
|
531 has known deficiencies in its ability to handle the dual verilog
|
|
532 (and C++) comments, (e.g. the // and /* */ comments). This will
|
|
533 not be much of a problem for you if you only use the /* */ comments,
|
|
534 but you really should strongly consider upgrading to one of the latest
|
|
535 Emacs 19's. In Emacs 18, you may also experience performance degradations.
|
|
536 Emacs 19 has some new built-in routines which will speed things up for you.
|
|
537 Because of these inherent problems, verilog-mode is not supported
|
|
538 on emacs-18."
|
134
|
539 emacs-version))))
|
98
|
540 ;; Emacs 18 with the syntax patches are no longer supported
|
|
541 (if (and (eq major 'v18) (not (eq comments 'no-dual-comments)))
|
|
542 (with-output-to-temp-buffer "*verilog-mode warnings*"
|
|
543 (print (format
|
134
|
544 "You are running a syntax patched Emacs 18 variant. While this should
|
98
|
545 work for you, you may want to consider upgrading to Emacs 19.
|
|
546 The syntax patches are no longer supported either for verilog-mode."))))
|
|
547 (list major comments))
|
|
548 "A list of features extant in the Emacs you are using.
|
|
549 There are many flavors of Emacs out there, each with different
|
|
550 features supporting those needed by verilog-mode. Here's the current
|
|
551 supported list, along with the values for this variable:
|
|
552
|
|
553 Vanilla Emacs 18/Epoch 4: (v18 no-dual-comments)
|
|
554 Emacs 18/Epoch 4 (patch2): (v18 8-bit)
|
|
555 XEmacs (formerly Lucid) 19: (v19 8-bit)
|
134
|
556 XEmacs 20: (v20 8-bit)
|
98
|
557 Emacs 19: (v19 1-bit).")
|
|
558
|
|
559 (defconst verilog-comment-start-regexp "//\\|/\\*"
|
|
560 "Dual comment value for `comment-start-regexp'.")
|
|
561
|
|
562 (defun verilog-populate-syntax-table (table)
|
|
563 ;; Populate the syntax TABLE
|
|
564 ;; DO NOT TRY TO SET _ (UNDERSCORE) TO WORD CLASS!
|
|
565 (modify-syntax-entry ?\\ "\\" table)
|
|
566 (modify-syntax-entry ?+ "." table)
|
|
567 (modify-syntax-entry ?- "." table)
|
|
568 (modify-syntax-entry ?= "." table)
|
|
569 (modify-syntax-entry ?% "." table)
|
|
570 (modify-syntax-entry ?< "." table)
|
|
571 (modify-syntax-entry ?> "." table)
|
|
572 (modify-syntax-entry ?& "." table)
|
|
573 (modify-syntax-entry ?| "." table)
|
|
574 (modify-syntax-entry ?_ "w" table)
|
|
575 (modify-syntax-entry ?\' "." table)
|
|
576 )
|
|
577
|
|
578 (defun verilog-setup-dual-comments (table)
|
|
579 ;; Set up TABLE to handle block and line style comments
|
|
580 (cond
|
|
581 ((memq '8-bit verilog-emacs-features)
|
|
582 ;; XEmacs (formerly Lucid) has the best implementation
|
|
583 (modify-syntax-entry ?/ ". 1456" table)
|
|
584 (modify-syntax-entry ?* ". 23" table)
|
|
585 (modify-syntax-entry ?\n "> b" table)
|
|
586 ;; Give CR the same syntax as newline, for selective-display
|
|
587 (modify-syntax-entry ?\^m "> b" table))
|
|
588 ((memq '1-bit verilog-emacs-features)
|
|
589 ;; Emacs 19 does things differently, but we can work with it
|
|
590 (modify-syntax-entry ?/ ". 124b" table)
|
|
591 (modify-syntax-entry ?* ". 23" table)
|
|
592 (modify-syntax-entry ?\n "> b" table)
|
|
593 ;; Give CR the same syntax as newline, for selective-display
|
|
594 (modify-syntax-entry ?\^m "> b" table))
|
|
595 ))
|
|
596
|
|
597 (defvar verilog-mode-syntax-table nil
|
|
598 "Syntax table used in verilog-mode buffers.")
|
|
599 (if verilog-mode-syntax-table
|
|
600 ()
|
|
601 (setq verilog-mode-syntax-table (make-syntax-table))
|
|
602 (verilog-populate-syntax-table verilog-mode-syntax-table)
|
|
603 ;; add extra comment syntax
|
|
604 (verilog-setup-dual-comments verilog-mode-syntax-table)
|
|
605 )
|
134
|
606
|
|
607 (defvar verilog-font-lock-keywords nil
|
|
608 "keyword highlighting used in verilog-mode buffers.")
|
|
609 (defvar verilog-font-lock-keywords-1 nil
|
|
610 "keyword highlighting used in verilog-mode buffers.")
|
|
611 (defvar verilog-font-lock-keywords-2 nil
|
|
612 "keyword highlighting used in verilog-mode buffers.")
|
|
613 (defvar verilog-font-lock-keywords-3 nil
|
|
614 "keyword highlighting used in verilog-mode buffers.")
|
|
615 (defvar verilog-font-lock-keywords-4 nil
|
|
616 "keyword highlighting used in verilog-mode buffers.")
|
|
617 (if verilog-font-lock-keywords
|
|
618 ()
|
|
619 (cond
|
|
620 ;; We can assume 8-bit syntax table emacsen aupport new syntax
|
|
621 ((memq '8-bit verilog-emacs-features)
|
|
622 (setq verilog-font-lock-keywords verilog-font-lock-keywords-after-1930
|
|
623 verilog-font-lock-keywords-1 verilog-font-lock-keywords-after-1930
|
|
624 verilog-font-lock-keywords-2 verilog-font-lock-keywords-after-1930
|
|
625 verilog-font-lock-keywords-3 verilog-font-lock-keywords-after-1930
|
|
626 verilog-font-lock-keywords-4 verilog-font-lock-keywords-after-1930)
|
|
627 )
|
|
628 (t
|
|
629 (setq verilog-font-lock-keywords verilog-font-lock-keywords-before-1930
|
|
630 verilog-font-lock-keywords-1 verilog-font-lock-keywords-before-1930
|
|
631 verilog-font-lock-keywords-2 verilog-font-lock-keywords-before-1930
|
|
632 verilog-font-lock-keywords-3 verilog-font-lock-keywords-before-1930
|
|
633 verilog-font-lock-keywords-4 verilog-font-lock-keywords-before-1930)
|
|
634 )
|
|
635 )
|
|
636 )
|
|
637
|
98
|
638 ;;;
|
|
639 ;;; Macros
|
|
640 ;;;
|
|
641
|
|
642 (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
|
|
643 "Like re-search-forward, but skips over matches in comments or strings"
|
|
644 (set-match-data '(nil nil))
|
|
645 (while (and
|
|
646 (re-search-forward REGEXP BOUND NOERROR)
|
|
647 (and (verilog-skip-forward-comment-or-string)
|
|
648 (progn
|
|
649 (store-match-data '(nil nil))
|
|
650 (if BOUND
|
|
651 (< (point) BOUND)
|
|
652 t)
|
|
653 )
|
|
654 )
|
|
655 )
|
|
656 )
|
|
657 (match-end 0))
|
|
658
|
|
659 (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
|
|
660 "Like re-search-backward, but skips over matches in comments or strings"
|
|
661 (set-match-data '(nil nil))
|
|
662 (while (and
|
|
663 (re-search-backward REGEXP BOUND NOERROR)
|
|
664 (verilog-skip-backward-comment-or-string)
|
|
665 (not (set-match-data '(nil nil))))
|
|
666 ())
|
|
667 (match-end 0))
|
|
668
|
|
669 (defsubst verilog-get-beg-of-line (&optional arg)
|
|
670 (save-excursion
|
|
671 (beginning-of-line arg)
|
|
672 (point)))
|
|
673
|
|
674 (defsubst verilog-get-end-of-line (&optional arg)
|
|
675 (save-excursion
|
|
676 (end-of-line arg)
|
|
677 (point)))
|
|
678
|
|
679 (defun verilog-declaration-end ()
|
|
680 (search-forward ";"))
|
|
681
|
|
682 (defun electric-verilog-backward-sexp ()
|
|
683 "Move backward over a sexp"
|
|
684 (interactive)
|
|
685 ;; before that see if we are in a comment
|
|
686 (verilog-backward-sexp)
|
|
687 )
|
|
688 (defun electric-verilog-forward-sexp ()
|
|
689 "Move backward over a sexp"
|
|
690 (interactive)
|
|
691 ;; before that see if we are in a comment
|
|
692 (verilog-forward-sexp)
|
|
693 )
|
|
694
|
|
695 (defun verilog-backward-sexp ()
|
|
696 (let ((reg)
|
|
697 (elsec 1)
|
|
698 (found nil)
|
|
699 )
|
|
700 (if (not (looking-at "\\<"))
|
|
701 (forward-word -1))
|
|
702 (cond
|
|
703 ((verilog-skip-backward-comment-or-string)
|
|
704 )
|
|
705 ((looking-at "\\<else\\>")
|
|
706 (setq reg (concat
|
|
707 verilog-end-block-re
|
|
708 "\\|\\(\\<else\\>\\)"
|
|
709 "\\|\\(\\<if\\>\\)"
|
|
710 ))
|
|
711 (while (and (not found)
|
|
712 (verilog-re-search-backward reg nil 'move))
|
|
713 (cond
|
|
714 ((match-end 1) ; endblock
|
134
|
715 ; try to leap back to matching outward block by striding across
|
|
716 ; indent level changing tokens then immediately
|
|
717 ; previous line governs indentation.
|
98
|
718 (verilog-leap-to-head)
|
|
719 )
|
|
720 ((match-end 2) ; else, we're in deep
|
|
721 (setq elsec (1+ elsec))
|
|
722 )
|
|
723 ((match-end 3) ; found it
|
|
724 (setq elsec (1- elsec))
|
|
725 (if (= 0 elsec)
|
|
726 ;; Now previous line describes syntax
|
|
727 (setq found 't)
|
|
728 ))
|
|
729 )
|
|
730 )
|
|
731 )
|
134
|
732 ((looking-at verilog-end-block-re)
|
98
|
733 (verilog-leap-to-head)
|
|
734 )
|
|
735 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)")
|
|
736 (cond
|
|
737 ((match-end 1)
|
|
738 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
|
|
739 ((match-end 2)
|
|
740 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
|
|
741 (t
|
|
742 (backward-sexp 1))))
|
|
743 (t
|
|
744 (backward-sexp))
|
|
745 ) ;; cond
|
|
746 )
|
|
747 )
|
|
748 (defun verilog-forward-sexp ()
|
|
749 (let ((reg)
|
|
750 (st (point)))
|
|
751 (if (not (looking-at "\\<"))
|
|
752 (forward-word -1))
|
|
753 (cond
|
|
754 ((verilog-skip-forward-comment-or-string)
|
|
755 (verilog-forward-syntactic-ws)
|
|
756 )
|
|
757 ((looking-at verilog-beg-block-re-1);; begin|fork|case|table|specify
|
|
758 (cond
|
|
759 ((match-end 1) ; end
|
|
760 ;; Search forward for matching begin
|
|
761 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" )
|
|
762 )
|
|
763 ((match-end 2) ; endcase
|
|
764 ;; Search forward for matching case
|
|
765 (setq reg "\\(\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
|
|
766 )
|
|
767 ((match-end 3) ; join
|
|
768 ;; Search forward for matching fork
|
|
769 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\>\\)" )
|
|
770 )
|
|
771 ((match-end 4) ; endtable
|
|
772 ;; Search forward for matching table
|
|
773 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" )
|
|
774 )
|
|
775 ((match-end 5) ; endspecify
|
|
776 ;; Search forward for matching specify
|
|
777 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" )
|
|
778 )
|
|
779 ((match-end 6) ; endfunction
|
|
780 ;; Search forward for matching function
|
|
781 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" )
|
|
782 )
|
|
783 ((match-end 7) ; endspecify
|
|
784 ;; Search forward for matching task
|
|
785 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" )
|
|
786 )
|
|
787 )
|
|
788 (if (forward-word 1)
|
|
789 (catch 'skip
|
|
790 (let ((nest 1))
|
|
791 (while (verilog-re-search-forward reg nil 'move)
|
|
792 (cond
|
|
793 ((match-end 2) ; end
|
|
794 (setq nest (1- nest))
|
|
795 (if (= 0 nest)
|
|
796 (throw 'skip 1)))
|
|
797 ((match-end 1) ; begin
|
|
798 (setq nest (1+ nest)))))
|
|
799 )
|
|
800 )
|
|
801 )
|
|
802 )
|
|
803 ((looking-at "\\(\\<\\(macro\\)?module\\>\\)\\|\\(\\<primitive\\>\\)")
|
|
804 (cond
|
|
805 ((match-end 1)
|
|
806 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
|
|
807 ((match-end 2)
|
|
808 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
|
|
809 (t
|
|
810 (goto-char st)
|
|
811 (if (= (following-char) ?\) )
|
|
812 (forward-char 1)
|
|
813 (forward-sexp 1)))))
|
|
814 (t
|
|
815 (goto-char st)
|
|
816 (if (= (following-char) ?\) )
|
|
817 (forward-char 1)
|
|
818 (forward-sexp 1)))
|
|
819 ) ;; cond
|
|
820 )
|
|
821 )
|
|
822
|
|
823
|
|
824 (defun verilog-declaration-beg ()
|
|
825 (verilog-re-search-backward verilog-declaration-re (bobp) t))
|
|
826
|
|
827 (defsubst verilog-within-string ()
|
|
828 (save-excursion
|
|
829 (nth 3 (parse-partial-sexp (verilog-get-beg-of-line) (point)))))
|
|
830
|
134
|
831 (put 'verilog-mode 'font-lock-defaults
|
|
832 '((verilog-font-lock-keywords-after-1930 )
|
|
833 nil ;; nil means highlight strings & comments as well as keywords
|
|
834 nil ;; nil means keywords must match case
|
|
835 nil ;; syntax table handled elsewhere
|
|
836 verilog-beg-of-defun ;; function to move to beginning of reasonable region to highlight
|
|
837 ))
|
98
|
838
|
|
839 ;;;###autoload
|
|
840 (defun verilog-mode ()
|
|
841 "Major mode for editing Verilog code. \\<verilog-mode-map>
|
|
842 NEWLINE, TAB indents for Verilog code.
|
|
843 Delete converts tabs to spaces as it moves back.
|
|
844 Supports highlighting.
|
|
845
|
|
846 Variables controlling indentation/edit style:
|
|
847
|
|
848 verilog-indent-level (default 3)
|
|
849 Indentation of Verilog statements with respect to containing block.
|
134
|
850 verilog-indent-level-module (default 3)
|
|
851 Absolute indentation of Module level Verilog statements.
|
|
852 Set to 0 to get initial and always statements lined up
|
|
853 on the left side of your screen.
|
|
854 verilog-indent-level-declaration (default 3)
|
|
855 Indentation of declarations with respect to containing block.
|
|
856 Set to 0 to get them list right under containing block.
|
|
857 verilog-indent-level-behavorial (default 3)
|
|
858 Indentation of first begin in a task or function block
|
|
859 Set to 0 to get such code to linedup underneath the task or function keyword
|
98
|
860 verilog-cexp-indent (default 1)
|
|
861 Indentation of Verilog statements broken across lines.
|
|
862 verilog-case-indent (default 2)
|
|
863 Indentation for case statements.
|
|
864 verilog-auto-newline (default nil)
|
134
|
865 Non-nil means automatically newline after semicolons and the punctation
|
|
866 mark after an end.
|
98
|
867 verilog-auto-indent-on-newline (default t)
|
|
868 Non-nil means automatically indent line after newline
|
|
869 verilog-tab-always-indent (default t)
|
|
870 Non-nil means TAB in Verilog mode should always reindent the current line,
|
|
871 regardless of where in the line point is when the TAB command is used.
|
|
872 verilog-indent-begin-after-if (default t)
|
108
|
873 Non-nil means to indent begin statements following a preceding
|
98
|
874 if, else, while, for and repeat statements, if any. otherwise,
|
108
|
875 the begin is lined up with the preceding token. If t, you get:
|
98
|
876 if (a)
|
|
877 begin
|
|
878 otherwise you get:
|
|
879 if (a)
|
|
880 begin
|
|
881 verilog-auto-endcomments (default t)
|
134
|
882 Non-nil means a comment /* ... */ is set after the ends which ends
|
|
883 cases, tasks, functions and modules.
|
98
|
884 The type and name of the object will be set between the braces.
|
134
|
885 verilog-minimum-comment-distance (default 40)
|
|
886 Minimum distance between begin and end required before a comment
|
|
887 will be inserted. Setting this variable to zero results in every
|
|
888 end aquiring a comment; the default avoids too many redundanet
|
|
889 comments in tight quarters.
|
98
|
890 verilog-auto-lineup (default `(all))
|
|
891 List of contexts where auto lineup of :'s or ='s should be done.
|
|
892
|
|
893 Turning on Verilog mode calls the value of the variable verilog-mode-hook with
|
|
894 no args, if that value is non-nil.
|
|
895 Other useful functions are:
|
134
|
896 \\[verilog-complete-word]\t-complete word with appropriate possibilities
|
|
897 (functions, verilog keywords...)
|
|
898 \\[verilog-comment-region]\t- Put marked area in a comment, fixing
|
|
899 nested comments.
|
|
900 \\[verilog-uncomment-region]\t- Uncomment an area commented with \
|
|
901 \\[verilog-comment-region].
|
98
|
902 \\[verilog-insert-block]\t- insert begin ... end;
|
|
903 \\[verilog-star-comment]\t- insert /* ... */
|
|
904 \\[verilog-mark-defun]\t- Mark function.
|
|
905 \\[verilog-beg-of-defun]\t- Move to beginning of current function.
|
|
906 \\[verilog-end-of-defun]\t- Move to end of current function.
|
134
|
907 \\[verilog-label-be]\t- Label matching begin ... end, fork ... join
|
|
908 and case ... endcase statements;
|
98
|
909 "
|
|
910 (interactive)
|
|
911 (kill-all-local-variables)
|
|
912 (use-local-map verilog-mode-map)
|
|
913 (setq major-mode 'verilog-mode)
|
|
914 (setq mode-name "Verilog")
|
|
915 (setq local-abbrev-table verilog-mode-abbrev-table)
|
|
916 (set-syntax-table verilog-mode-syntax-table)
|
|
917 (make-local-variable 'indent-line-function)
|
|
918 (setq indent-line-function 'verilog-indent-line)
|
|
919 (setq comment-indent-function 'verilog-indent-comment)
|
|
920 (make-local-variable 'parse-sexp-ignore-comments)
|
|
921 (setq parse-sexp-ignore-comments nil)
|
|
922 (make-local-variable 'comment-start)
|
|
923 (make-local-variable 'comment-end)
|
|
924 (make-local-variable 'comment-multi-line)
|
|
925 (make-local-variable 'comment-start-skip)
|
|
926 (setq comment-start "// "
|
|
927 comment-end ""
|
|
928 comment-start-skip "/\\*+ *\\|// *"
|
|
929 comment-multi-line nil)
|
134
|
930 ;; Setting up things for font-lock
|
|
931 (if (string-match "XEmacs" emacs-version)
|
|
932 (progn
|
|
933 (if (and current-menubar
|
|
934 (not (assoc "Verilog" current-menubar)))
|
|
935 (progn
|
|
936 (set-buffer-menubar (copy-sequence current-menubar))
|
|
937 (add-submenu nil verilog-xemacs-menu))) ))
|
|
938 ;; Stuff for GNU emacs
|
|
939 (make-local-variable 'font-lock-defaults)
|
|
940 (setq font-lock-defaults
|
|
941 '((verilog-font-lock-keywords verilog-font-lock-keywords-1
|
|
942 verilog-font-lock-keywords-2
|
|
943 verilog-font-lock-keywords-3
|
|
944 verilog-font-lock-keywords-4)
|
|
945 nil t))
|
|
946 ;; Tell imenu how to handle verilog.
|
|
947 (make-local-variable 'imenu-generic-expression)
|
|
948 (setq imenu-generic-expression verilog-imenu-generic-expression)
|
|
949 ;; End GNU emacs stuff
|
98
|
950 (run-hooks 'verilog-mode-hook))
|
|
951
|
|
952
|
|
953 ;;;
|
|
954 ;;; Electric functions
|
|
955 ;;;
|
|
956 (defun electric-verilog-terminate-line (&optional arg)
|
|
957 "Terminate line and indent next line."
|
|
958 (interactive)
|
|
959 ;; before that see if we are in a comment
|
|
960 (let ((state
|
|
961 (save-excursion
|
|
962 (parse-partial-sexp (point-min) (point)))))
|
|
963 (cond
|
|
964 ((nth 7 state) ; Inside // comment
|
|
965 (if (eolp)
|
|
966 (progn
|
|
967 (delete-horizontal-space)
|
|
968 (newline))
|
|
969 (progn
|
|
970 (newline)
|
|
971 (insert-string "// ")
|
|
972 (beginning-of-line)
|
|
973 ))
|
|
974 (verilog-indent-line)
|
|
975 )
|
|
976 ((nth 4 state) ; Inside any comment (hence /**/)
|
|
977 (newline)
|
|
978 (beginning-of-line)
|
|
979 (verilog-indent-comment t)
|
|
980 )
|
|
981 ((eolp)
|
|
982 ;; First, check if current line should be indented
|
|
983 (if (save-excursion
|
|
984 (delete-horizontal-space)
|
|
985 (beginning-of-line)
|
|
986 (skip-chars-forward " \t")
|
|
987 (if (looking-at verilog-autoindent-lines-re)
|
|
988 (let ((indent-str (verilog-indent-line)))
|
|
989 ;; Maybe we should set some endcomments
|
|
990 (if verilog-auto-endcomments
|
|
991 (verilog-set-auto-endcomments indent-str arg))
|
|
992 (end-of-line)
|
|
993 (delete-horizontal-space)
|
|
994 (if arg
|
|
995 ()
|
|
996 (newline))
|
|
997 nil)
|
|
998 (progn
|
|
999 (end-of-line)
|
|
1000 (delete-horizontal-space)
|
134
|
1001 't
|
|
1002 )))
|
98
|
1003 (newline)
|
134
|
1004 (forward-line 1)
|
|
1005 )
|
98
|
1006 ;; Indent next line
|
|
1007 (if verilog-auto-indent-on-newline
|
|
1008 (verilog-indent-line))
|
|
1009 )
|
|
1010 (t
|
|
1011 (newline)
|
|
1012 )
|
|
1013 )
|
|
1014 )
|
|
1015 )
|
|
1016
|
|
1017 (defun electric-verilog-semi ()
|
|
1018 "Insert `;' character and reindent the line."
|
|
1019 (interactive)
|
|
1020 (insert last-command-char)
|
134
|
1021 (if (verilog-in-comment-or-string-p)
|
|
1022 ()
|
|
1023 (save-excursion
|
|
1024 (beginning-of-line)
|
|
1025 (verilog-indent-line))
|
|
1026 (if (and verilog-auto-newline
|
|
1027 (= 0 (verilog-parenthesis-depth)))
|
|
1028 (electric-verilog-terminate-line))))
|
98
|
1029
|
|
1030 (defun electric-verilog-colon ()
|
|
1031 "Insert `:' and do all indentions except line indent on this line."
|
|
1032 (interactive)
|
|
1033 (insert last-command-char)
|
|
1034 ;; Do nothing if within string.
|
|
1035 (if (or
|
|
1036 (verilog-within-string)
|
|
1037 (not (verilog-in-case-region-p)))
|
|
1038 ()
|
|
1039 (save-excursion
|
|
1040 (let ((p (point))
|
|
1041 (lim (progn (verilog-beg-of-statement) (point))))
|
|
1042 (goto-char p)
|
|
1043 (verilog-backward-case-item lim)
|
|
1044 (verilog-indent-line)))
|
|
1045 ;; (let ((verilog-tab-always-indent nil))
|
|
1046 ;; (verilog-indent-line))
|
|
1047 )
|
|
1048 )
|
|
1049
|
|
1050 (defun electric-verilog-equal ()
|
|
1051 "Insert `=', and do indention if within block."
|
|
1052 (interactive)
|
|
1053 (insert last-command-char)
|
|
1054 ;; Could auto line up expressions, but not yet
|
|
1055 ;; (if (eq (car (verilog-calculate-indent)) 'block)
|
|
1056 ;; (let ((verilog-tab-always-indent nil))
|
|
1057 ;; (verilog-indent-command)))
|
|
1058 )
|
|
1059
|
|
1060
|
|
1061 (defun electric-verilog-tick ()
|
|
1062 "Insert back-tick, and indent to coulmn 0 if this is a CPP directive."
|
|
1063 (interactive)
|
|
1064 (insert last-command-char)
|
134
|
1065 (if (save-excursion
|
|
1066 (beginning-of-line)
|
|
1067 (looking-at
|
|
1068 "^[ \t]*\`\\(\\<ifdef\\>\\|\\\<else\\>\\|\\<endif\\>\\|\\<define\\>\\)"))
|
98
|
1069 (save-excursion (beginning-of-line)
|
|
1070 (delete-horizontal-space))))
|
|
1071
|
|
1072 (defun electric-verilog-tab ()
|
|
1073 "Function called when TAB is pressed in Verilog mode."
|
|
1074 (interactive)
|
|
1075 ;; If verilog-tab-always-indent, indent the beginning of the line.
|
|
1076 (if verilog-tab-always-indent
|
134
|
1077 (let* (
|
|
1078 (boi-point
|
|
1079 (save-excursion
|
|
1080 (beginning-of-line)
|
|
1081 (skip-chars-forward " \t")
|
|
1082 (let (type state )
|
|
1083 (setq type (verilog-indent-line))
|
|
1084 (setq state (car type))
|
|
1085 (cond
|
|
1086 ((eq state 'block)
|
|
1087 (if (looking-at verilog-behavorial-block-beg-re )
|
|
1088 (error
|
|
1089 (concat
|
|
1090 "The reserved word \""
|
|
1091 (buffer-substring (match-beginning 0) (match-end 0))
|
|
1092 "\" must be at the behavorial level!"))))
|
|
1093 ))
|
|
1094 (back-to-indentation)
|
|
1095 (point))))
|
98
|
1096 (if (< (point) boi-point)
|
|
1097 (back-to-indentation)))
|
|
1098 (progn (insert "\t"))
|
|
1099 )
|
|
1100 )
|
|
1101
|
|
1102
|
|
1103
|
|
1104 ;;;
|
|
1105 ;;; Interactive functions
|
|
1106 ;;;
|
|
1107 (defun verilog-insert-block ()
|
|
1108 "Insert Verilog begin ... end; block in the code with right indentation."
|
|
1109 (interactive)
|
|
1110 (verilog-indent-line)
|
|
1111 (insert "begin")
|
|
1112 (electric-verilog-terminate-line)
|
|
1113 (save-excursion
|
|
1114 (electric-verilog-terminate-line)
|
|
1115 (insert "end")
|
|
1116 (beginning-of-line)
|
|
1117 (verilog-indent-line)))
|
|
1118
|
|
1119 (defun verilog-star-comment ()
|
|
1120 "Insert Verilog star comment at point."
|
|
1121 (interactive)
|
|
1122 (verilog-indent-line)
|
|
1123 (insert "/*")
|
|
1124 (save-excursion
|
|
1125 (newline)
|
|
1126 (insert " */"))
|
|
1127 (newline)
|
|
1128 (insert " * "))
|
|
1129
|
|
1130 (defun verilog-mark-defun ()
|
|
1131 "Mark the current verilog function (or procedure).
|
|
1132 This puts the mark at the end, and point at the beginning."
|
|
1133 (interactive)
|
|
1134 (push-mark (point))
|
|
1135 (verilog-end-of-defun)
|
|
1136 (push-mark (point))
|
|
1137 (verilog-beg-of-defun)
|
|
1138 (if (fboundp 'zmacs-activate-region)
|
|
1139 (zmacs-activate-region)))
|
|
1140
|
134
|
1141 (defun verilog-comment-region (start end)
|
98
|
1142 "Put the region into a Verilog comment.
|
|
1143 The comments that are in this area are \"deformed\":
|
|
1144 `*)' becomes `!(*' and `}' becomes `!{'.
|
|
1145 These deformed comments are returned to normal if you use
|
134
|
1146 \\[verilog-uncomment-region] to undo the commenting.
|
98
|
1147
|
|
1148 The commented area starts with `verilog-exclude-str-start', and ends with
|
|
1149 `verilog-include-str-end'. But if you change these variables,
|
134
|
1150 \\[verilog-uncomment-region] won't recognize the comments."
|
98
|
1151 (interactive "r")
|
|
1152 (save-excursion
|
|
1153 ;; Insert start and endcomments
|
|
1154 (goto-char end)
|
|
1155 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
|
|
1156 (not (save-excursion (skip-chars-backward " \t") (bolp))))
|
|
1157 (forward-line 1)
|
|
1158 (beginning-of-line))
|
|
1159 (insert verilog-exclude-str-end)
|
|
1160 (setq end (point))
|
|
1161 (newline)
|
|
1162 (goto-char start)
|
|
1163 (beginning-of-line)
|
|
1164 (insert verilog-exclude-str-start)
|
|
1165 (newline)
|
|
1166 ;; Replace end-comments within commented area
|
|
1167 (goto-char end)
|
|
1168 (save-excursion
|
|
1169 (while (re-search-backward "\\*/" start t)
|
134
|
1170 (replace-match "*-/" t t)))
|
|
1171 (save-excursion
|
|
1172 (let ((s+1 (1+ start)))
|
|
1173 (while (re-search-backward "/\\*" s+1 t)
|
|
1174 (replace-match "/-*" t t))))
|
98
|
1175 )
|
|
1176 )
|
|
1177
|
134
|
1178 (defun verilog-uncomment-region ()
|
98
|
1179 "Uncomment a commented area; change deformed comments back to normal.
|
|
1180 This command does nothing if the pointer is not in a commented
|
134
|
1181 area. See also `verilog-comment-region'."
|
98
|
1182 (interactive)
|
|
1183 (save-excursion
|
|
1184 (let ((start (point))
|
|
1185 (end (point)))
|
|
1186 ;; Find the boundaries of the comment
|
|
1187 (save-excursion
|
|
1188 (setq start (progn (search-backward verilog-exclude-str-start nil t)
|
|
1189 (point)))
|
|
1190 (setq end (progn (search-forward verilog-exclude-str-end nil t)
|
|
1191 (point))))
|
|
1192 ;; Check if we're really inside a comment
|
|
1193 (if (or (equal start (point)) (<= end (point)))
|
|
1194 (message "Not standing within commented area.")
|
|
1195 (progn
|
|
1196 ;; Remove endcomment
|
|
1197 (goto-char end)
|
|
1198 (beginning-of-line)
|
|
1199 (let ((pos (point)))
|
|
1200 (end-of-line)
|
|
1201 (delete-region pos (1+ (point))))
|
|
1202 ;; Change comments back to normal
|
|
1203 (save-excursion
|
134
|
1204 (while (re-search-backward "\\*-/" start t)
|
98
|
1205 (replace-match "*/" t t)))
|
134
|
1206 (save-excursion
|
|
1207 (while (re-search-backward "/-\\*" start t)
|
|
1208 (replace-match "/*" t t)))
|
98
|
1209 ;; Remove startcomment
|
|
1210 (goto-char start)
|
|
1211 (beginning-of-line)
|
|
1212 (let ((pos (point)))
|
|
1213 (end-of-line)
|
|
1214 (delete-region pos (1+ (point)))))))))
|
|
1215
|
|
1216 (defun verilog-beg-of-defun ()
|
|
1217 "Move backward to the beginning of the current function or procedure."
|
|
1218 (interactive)
|
|
1219 (verilog-re-search-backward verilog-defun-re nil 'move)
|
|
1220 )
|
|
1221 (defun verilog-end-of-defun ()
|
|
1222 (interactive)
|
|
1223 (verilog-re-search-forward verilog-end-defun-re nil 'move)
|
|
1224 )
|
|
1225
|
|
1226 (defun verilog-label-be (&optional arg)
|
134
|
1227 "Label matching begin ... end, fork ... join and case ... endcase
|
|
1228 statements in this module; With argument, first kill any existing
|
|
1229 labels."
|
98
|
1230 (interactive)
|
|
1231 (let ((cnt 0)
|
|
1232 (oldpos (point))
|
|
1233 (b (progn
|
|
1234 (verilog-beg-of-defun)
|
|
1235 (point-marker)))
|
|
1236 (e (progn
|
|
1237 (verilog-end-of-defun)
|
|
1238 (point-marker)))
|
|
1239 )
|
|
1240 (goto-char (marker-position b))
|
|
1241 (if (> (- e b) 200)
|
|
1242 (message "Relabeling module..."))
|
|
1243 (while (and
|
|
1244 (> (marker-position e) (point))
|
|
1245 (verilog-re-search-forward
|
|
1246 (concat
|
134
|
1247 "\\<end\\(\\(function\\)\\|\\(task\\)\\|\\(module\\)\\|"
|
|
1248 "\\(primitive\\)\\|\\(case\\)\\)?\\>"
|
98
|
1249 "\\|\\(`endif\\)\\|\\(`else\\)")
|
|
1250 nil 'move))
|
|
1251 (goto-char (match-beginning 0))
|
|
1252 (let ((indent-str (verilog-indent-line)))
|
|
1253 (verilog-set-auto-endcomments indent-str 't)
|
|
1254 (end-of-line)
|
|
1255 (delete-horizontal-space)
|
|
1256 )
|
|
1257 (setq cnt (1+ cnt))
|
|
1258 (if (= 9 (% cnt 10))
|
|
1259 (message "%d..." cnt))
|
|
1260 )
|
|
1261 (goto-char oldpos)
|
|
1262 (if (or
|
|
1263 (> (- e b) 200)
|
|
1264 (> cnt 20))
|
|
1265 (message "%d lines autocommented" cnt))
|
|
1266 )
|
|
1267 )
|
|
1268 (defun verilog-beg-of-statement ()
|
|
1269 "Move backward to beginning of statement"
|
|
1270 (interactive)
|
|
1271 (while (save-excursion
|
|
1272 (and
|
|
1273 (not (looking-at verilog-complete-reg))
|
134
|
1274 (verilog-backward-syntactic-ws)
|
98
|
1275 (not (or (bolp) (= (preceding-char) ?\;)))
|
|
1276 )
|
|
1277 )
|
|
1278 (skip-chars-backward " \t")
|
|
1279 (verilog-backward-token))
|
|
1280 (let ((last (point)))
|
|
1281 (while (progn
|
|
1282 (setq last (point))
|
|
1283 (and (not (looking-at verilog-complete-reg))
|
|
1284 (verilog-continued-line))))
|
|
1285 (goto-char last)
|
|
1286 (verilog-forward-syntactic-ws)
|
|
1287 )
|
|
1288 )
|
134
|
1289
|
|
1290 (defun verilog-beg-of-statement-1 ()
|
|
1291 "Move backward to beginning of statement"
|
|
1292 (interactive)
|
|
1293 (let ((pt (point)))
|
|
1294
|
|
1295 (while (and (not (looking-at verilog-complete-reg))
|
|
1296 (setq pt (point))
|
|
1297 (verilog-backward-token)
|
|
1298 (setq pt (point))
|
|
1299 (verilog-backward-syntactic-ws)
|
|
1300 (not (bolp))
|
|
1301 (not (= (preceding-char) ?\;)))
|
|
1302 )
|
|
1303 (goto-char pt)
|
|
1304 (while (progn
|
|
1305 (setq pt (point))
|
|
1306 (and (not (looking-at verilog-complete-reg))
|
|
1307 (not (= (preceding-char) ?\;))
|
|
1308 (verilog-continued-line))))
|
|
1309 (goto-char pt)
|
|
1310 (verilog-forward-syntactic-ws)
|
|
1311 )
|
|
1312 )
|
98
|
1313 (defun verilog-end-of-statement ()
|
|
1314 "Move forward to end of current statement."
|
|
1315 (interactive)
|
|
1316 (let ((nest 0) pos)
|
|
1317 (or (looking-at verilog-beg-block-re)
|
|
1318 ;; Skip to end of statement
|
|
1319 (setq pos (catch 'found
|
|
1320 (while t
|
|
1321 (forward-sexp 1)
|
|
1322 (verilog-skip-forward-comment-or-string)
|
|
1323 (cond ((looking-at "[ \t]*;")
|
|
1324 (skip-chars-forward "^;")
|
|
1325 (forward-char 1)
|
|
1326 (throw 'found (point)))
|
|
1327 ((save-excursion
|
|
1328 (forward-sexp -1)
|
|
1329 (looking-at verilog-beg-block-re))
|
|
1330 (goto-char (match-beginning 0))
|
|
1331 (throw 'found nil))
|
|
1332 ((eobp)
|
|
1333 (throw 'found (point))))))))
|
|
1334 (if (not pos)
|
|
1335 ;; Skip a whole block
|
|
1336 (catch 'found
|
|
1337 (while t
|
|
1338 (verilog-re-search-forward verilog-end-statement-re nil 'move)
|
|
1339 (setq nest (if (match-end 1)
|
|
1340 (1+ nest)
|
|
1341 (1- nest)))
|
|
1342 (cond ((eobp)
|
|
1343 (throw 'found (point)))
|
|
1344 ((= 0 nest)
|
|
1345 (throw 'found (verilog-end-of-statement))))))
|
|
1346 pos)))
|
|
1347 (defun verilog-in-case-region-p ()
|
134
|
1348 "Return TRUE if in a case region: more specifically, point @ in the
|
|
1349 line foo : @ begin"
|
98
|
1350 (interactive)
|
|
1351 (save-excursion
|
|
1352 (if (and
|
|
1353 (progn (verilog-forward-syntactic-ws)
|
|
1354 (looking-at "\\<begin\\>"))
|
|
1355 (progn (verilog-backward-syntactic-ws)
|
|
1356 (= (preceding-char) ?\:)))
|
|
1357 (catch 'found
|
|
1358 (let ((nest 1))
|
|
1359 (while t
|
134
|
1360 (verilog-re-search-backward
|
|
1361 (concat "\\(\\<module\\>\\)\\|\\(\\<case[xz]?\\>[^:]\\)\\|"
|
|
1362 "\\(\\<endcase\\>\\)\\>")
|
|
1363 nil 'move)
|
98
|
1364 (cond
|
|
1365 ((match-end 3)
|
|
1366 (setq nest (1+ nest)))
|
|
1367 ((match-end 2)
|
|
1368 (if (= nest 1)
|
|
1369 (throw 'found 1))
|
|
1370 (setq nest (1- nest))
|
|
1371 )
|
|
1372 ( t
|
|
1373 (throw 'found (= nest 0)))
|
|
1374 )
|
|
1375 )
|
|
1376 )
|
|
1377 )
|
|
1378 nil)
|
|
1379 )
|
|
1380 )
|
|
1381 (defun verilog-backward-case-item (lim)
|
|
1382 "Skip backward to nearest enclosing case item"
|
|
1383 (interactive)
|
|
1384 (let (
|
|
1385 (str 'nil)
|
134
|
1386 (lim1
|
|
1387 (progn
|
|
1388 (save-excursion
|
|
1389 (verilog-re-search-backward verilog-endcomment-reason-re
|
|
1390 lim 'move)
|
|
1391 (point)))))
|
98
|
1392 ;; Try to find the real :
|
|
1393 (if (save-excursion (search-backward ":" lim1 t))
|
|
1394 (let ((colon 0)
|
|
1395 b e )
|
134
|
1396 (while
|
|
1397 (and
|
|
1398 (< colon 1)
|
|
1399 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
|
|
1400 lim1 'move))
|
98
|
1401 (cond
|
|
1402 ((match-end 1) ;; [
|
|
1403 (setq colon (1+ colon))
|
|
1404 (if (>= colon 0)
|
|
1405 (error "unbalanced [")))
|
|
1406 ((match-end 2) ;; ]
|
|
1407 (setq colon (1- colon)))
|
|
1408
|
|
1409 ((match-end 3) ;; :
|
|
1410 (setq colon (1+ colon)))
|
|
1411
|
|
1412 )
|
|
1413 )
|
|
1414 ;; Skip back to begining of case item
|
|
1415 (skip-chars-backward "\t ")
|
|
1416 (verilog-skip-backward-comment-or-string)
|
|
1417 (setq e (point))
|
134
|
1418 (setq b
|
|
1419 (progn
|
|
1420 (if
|
|
1421 (verilog-re-search-backward
|
|
1422 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
|
|
1423 (progn
|
|
1424 (cond
|
|
1425 ((match-end 1)
|
|
1426 (goto-char (match-end 1))
|
|
1427 (verilog-forward-ws&directives)
|
|
1428 (if (looking-at "(")
|
|
1429 (progn
|
|
1430 (forward-sexp)
|
|
1431 (verilog-forward-ws&directives)
|
|
1432 ))
|
|
1433 (point))
|
|
1434 (t
|
|
1435 (goto-char (match-end 0))
|
|
1436 (verilog-forward-ws&directives)
|
|
1437 (point))
|
|
1438 ))
|
|
1439 (error "Malformed case item")
|
98
|
1440 )
|
134
|
1441 )
|
98
|
1442 )
|
|
1443 (setq str (buffer-substring b e))
|
134
|
1444 (if
|
|
1445 (setq e
|
|
1446 (string-match
|
|
1447 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
|
98
|
1448 (setq str (concat (substring str 0 e) "...")))
|
|
1449 str)
|
|
1450 'nil)
|
|
1451 )
|
|
1452 )
|
|
1453
|
|
1454
|
|
1455 ;;;
|
|
1456 ;;; Other functions
|
|
1457 ;;;
|
|
1458
|
|
1459 (defun kill-existing-comment ()
|
|
1460 "kill autocomment on this line"
|
|
1461 (save-excursion
|
|
1462 (let* (
|
|
1463 (e (progn
|
|
1464 (end-of-line)
|
|
1465 (point)))
|
|
1466 (b (progn
|
|
1467 (beginning-of-line)
|
|
1468 (search-forward "//" e t))))
|
|
1469 (if b
|
|
1470 (delete-region (- b 2) e))
|
|
1471 )
|
|
1472 )
|
|
1473 )
|
|
1474
|
|
1475 (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
|
|
1476 "Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
|
|
1477 Insert `// case expr ' if this line ends a case block.
|
|
1478 Insert `// ifdef FOO ' if this line ends code conditional on FOO.
|
|
1479 Insert `// NAME ' if this line ends a module or primitive named NAME."
|
|
1480 (save-excursion
|
|
1481 (cond
|
|
1482 (; Comment close preprocessor directives
|
|
1483 (and
|
|
1484 (looking-at "\\(`endif\\)\\|\\(`else\\)")
|
|
1485 (or kill-existing-comment
|
|
1486 (not (save-excursion
|
|
1487 (end-of-line)
|
|
1488 (search-backward "//" (verilog-get-beg-of-line) t)))))
|
|
1489 (let ( (reg "\\(`else\\)\\|\\(`ifdef\\)\\|\\(`endif\\)")
|
|
1490 (nest 1)
|
|
1491 b e
|
|
1492 (else (if (match-end 2)
|
|
1493 1
|
|
1494 0))
|
|
1495 )
|
|
1496 (end-of-line)
|
|
1497 (if kill-existing-comment
|
|
1498 (kill-existing-comment))
|
|
1499 (delete-horizontal-space)
|
|
1500 (save-excursion
|
|
1501 (backward-sexp 1)
|
|
1502 (while (and (/= nest 0)
|
|
1503 (verilog-re-search-backward reg nil 'move))
|
|
1504 (cond
|
|
1505 ((match-end 1) ; `else
|
|
1506 (if (= nest 1)
|
|
1507 (setq else 1)))
|
|
1508 ((match-end 2) ; `ifdef
|
|
1509 (setq nest (1- nest)))
|
|
1510 ((match-end 3) ; `endif
|
|
1511 (setq nest (1+ nest)))
|
|
1512 ))
|
|
1513 (if (match-end 0)
|
|
1514 (setq b (progn
|
|
1515 (skip-chars-forward "^ \t")
|
|
1516 (verilog-forward-syntactic-ws)
|
|
1517 (point))
|
|
1518 e (progn
|
|
1519 (skip-chars-forward "a-zA-Z0-9_")
|
|
1520 (point)
|
|
1521 ))))
|
|
1522 (if b
|
|
1523 (if (> (- (point) b) verilog-minimum-comment-distance)
|
|
1524 (insert (concat (if
|
|
1525 (= else 0)
|
|
1526 " // ifdef "
|
|
1527 " // !ifdef ")
|
|
1528 (buffer-substring b e))))
|
|
1529 (progn
|
|
1530 (insert " // unmatched `endif")
|
|
1531 (ding 't))
|
|
1532 )))
|
|
1533
|
|
1534 (; Comment close case/function/task/module and named block
|
|
1535 (and (looking-at "\\<end")
|
|
1536 (or kill-existing-comment
|
|
1537 (not (save-excursion
|
|
1538 (end-of-line)
|
|
1539 (search-backward "//" (verilog-get-beg-of-line) t)))))
|
|
1540 (let ((type (car indent-str)))
|
|
1541 (if (eq type 'declaration)
|
|
1542 ()
|
|
1543 (if
|
134
|
1544 (looking-at verilog-enders-re)
|
98
|
1545 (cond
|
|
1546 (;- This is a case block; search back for the start of this case
|
|
1547 (match-end 1)
|
|
1548
|
|
1549 (let ((err 't)
|
|
1550 (str "UNMATCHED!!"))
|
|
1551 (save-excursion
|
|
1552 (verilog-leap-to-head)
|
|
1553 (if (match-end 0)
|
|
1554 (progn
|
|
1555 (goto-char (match-end 1))
|
|
1556 (setq str (concat (buffer-substring (match-beginning 1) (match-end 1))
|
|
1557 (verilog-get-expr)))
|
|
1558 (setq err nil))))
|
|
1559 (end-of-line)
|
|
1560 (if kill-existing-comment
|
|
1561 (kill-existing-comment))
|
|
1562 (delete-horizontal-space)
|
|
1563 (insert (concat " // " str ))
|
|
1564 (if err (ding 't))
|
|
1565 ))
|
|
1566
|
|
1567 (;- This is a begin..end block
|
|
1568 (match-end 2)
|
|
1569 (let ((str " // UNMATCHED !!")
|
|
1570 (err 't)
|
|
1571 (here (point))
|
|
1572 there
|
|
1573 cntx
|
|
1574 )
|
|
1575 (save-excursion
|
|
1576 (verilog-leap-to-head)
|
|
1577 (setq there (point))
|
|
1578 (if (not (match-end 0))
|
|
1579 (progn
|
|
1580 (goto-char here)
|
|
1581 (end-of-line)
|
|
1582 (if kill-existing-comment
|
|
1583 (kill-existing-comment))
|
|
1584 (delete-horizontal-space)
|
|
1585 (insert str)
|
|
1586 (ding 't)
|
|
1587 )
|
|
1588 (let ( sp
|
|
1589 (lim (save-excursion (verilog-beg-of-defun) (point)))
|
|
1590 (here (point))
|
|
1591 )
|
|
1592 (cond
|
|
1593 (;-- handle named block differently
|
|
1594 (looking-at verilog-named-block-re)
|
|
1595 (search-forward ":")
|
|
1596 (setq there (point))
|
|
1597 (setq str (verilog-get-expr))
|
|
1598 (setq err nil)
|
|
1599 (setq str (concat " // block: " str )))
|
|
1600
|
|
1601 ((verilog-in-case-region-p) ;-- handle case item differently
|
|
1602 (goto-char here)
|
|
1603 (setq str (verilog-backward-case-item lim))
|
|
1604 (setq there (point))
|
|
1605 (setq err nil)
|
|
1606 (setq str (concat " // case: " str ))
|
|
1607 )
|
|
1608 (;- try to find "reason" for this begin
|
|
1609 (cond
|
|
1610 (;
|
134
|
1611 (eq here (progn
|
|
1612 (verilog-backward-token)
|
|
1613 (verilog-beg-of-statement)
|
|
1614 (point)))
|
98
|
1615 (setq err nil)
|
|
1616 (setq str ""))
|
|
1617 ((looking-at verilog-endcomment-reason-re)
|
|
1618 (setq there (match-end 0))
|
|
1619 (setq cntx (concat
|
|
1620 (buffer-substring (match-beginning 0) (match-end 0)) " "))
|
|
1621 (cond
|
|
1622 (;
|
|
1623 (match-end 2)
|
|
1624 (setq err nil)
|
|
1625 (save-excursion
|
|
1626 (goto-char sp)
|
|
1627 (if (and (verilog-continued-line)
|
|
1628 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
|
|
1629 (progn
|
|
1630 (goto-char (match-end 0))
|
|
1631 (setq there (point))
|
|
1632 (setq str
|
|
1633 (concat " // "
|
|
1634 (buffer-substring (match-beginning 0) (match-end 0)) " "
|
|
1635 (verilog-get-expr))))
|
|
1636 (setq str "")
|
|
1637 )
|
|
1638 )
|
|
1639 )
|
|
1640 (;- else
|
|
1641 (match-end 4)
|
|
1642 (let ((nest 0)
|
|
1643 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)")
|
|
1644 )
|
|
1645 (catch 'skip
|
|
1646 (while (verilog-re-search-backward reg nil 'move)
|
|
1647 (cond
|
|
1648 ((match-end 1) ; begin
|
|
1649 (setq nest (1- nest)))
|
|
1650 ((match-end 2) ; end
|
|
1651 (setq nest (1+ nest)))
|
|
1652 ((match-end 3)
|
|
1653 (if (= 0 nest)
|
|
1654 (progn
|
|
1655 (goto-char (match-end 0))
|
|
1656 (setq there (point))
|
|
1657 (setq err nil)
|
|
1658 (setq str (verilog-get-expr))
|
|
1659 (setq str (concat " // else: !if" str ))
|
|
1660 (throw 'skip 1))
|
|
1661 )))
|
|
1662 )
|
|
1663 )
|
|
1664 )
|
|
1665 )
|
134
|
1666 (;- end else
|
|
1667 (match-end 5)
|
|
1668 (goto-char there)
|
|
1669 (let ((nest 0)
|
|
1670 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)")
|
|
1671 )
|
|
1672 (catch 'skip
|
|
1673 (while (verilog-re-search-backward reg nil 'move)
|
|
1674 (cond
|
|
1675 ((match-end 1) ; begin
|
|
1676 (setq nest (1- nest)))
|
|
1677 ((match-end 2) ; end
|
|
1678 (setq nest (1+ nest)))
|
|
1679 ((match-end 3)
|
|
1680 (if (= 0 nest)
|
|
1681 (progn
|
|
1682 (goto-char (match-end 0))
|
|
1683 (setq there (point))
|
|
1684 (setq err nil)
|
|
1685 (setq str (verilog-get-expr))
|
|
1686 (setq str (concat " // else: !if" str ))
|
|
1687 (throw 'skip 1))
|
|
1688 )))
|
|
1689 )
|
|
1690 )
|
|
1691 )
|
|
1692 )
|
|
1693
|
98
|
1694 (;- task/function/initial et cetera
|
|
1695 t
|
|
1696 (match-end 0)
|
|
1697 (goto-char (match-end 0))
|
|
1698 (setq there (point))
|
|
1699 (setq err nil)
|
|
1700 (setq str (verilog-get-expr))
|
|
1701 (setq str (concat " // " cntx str )))
|
|
1702
|
|
1703 (;-- otherwise...
|
|
1704 (setq str " // auto-endcomment confused ")
|
|
1705 )
|
|
1706 )
|
|
1707 )
|
|
1708 ((and
|
|
1709 (verilog-in-case-region-p) ;-- handle case item differently
|
|
1710 (progn
|
|
1711 (setq there (point))
|
|
1712 (goto-char here)
|
|
1713 (setq str (verilog-backward-case-item lim))))
|
|
1714 (setq err nil)
|
|
1715 (setq str (concat " // case: " str ))
|
|
1716 )
|
|
1717 )
|
|
1718 )
|
|
1719 )
|
|
1720 )
|
|
1721 (goto-char here)
|
|
1722 (end-of-line)
|
|
1723 (if kill-existing-comment
|
|
1724 (kill-existing-comment))
|
|
1725 (delete-horizontal-space)
|
|
1726 (if (or err
|
|
1727 (> (- here there) verilog-minimum-comment-distance))
|
|
1728 (insert str))
|
|
1729 (if err (ding 't))
|
|
1730 )
|
|
1731 )
|
|
1732 )
|
|
1733 )
|
|
1734
|
|
1735
|
|
1736 (;- this is end{function,task,module}
|
|
1737 t
|
|
1738 (let (string reg (width nil))
|
|
1739 (end-of-line)
|
|
1740 (if kill-existing-comment
|
|
1741 (kill-existing-comment))
|
|
1742 (delete-horizontal-space)
|
|
1743 (backward-sexp)
|
|
1744 (cond
|
|
1745 ((match-end 5)
|
|
1746 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
|
|
1747 (setq width "\\([ \t]*\\[[^]]*\\]\\)?")
|
|
1748 )
|
|
1749 ((match-end 6)
|
|
1750 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)"))
|
|
1751 ((match-end 7)
|
|
1752 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
|
|
1753 ((match-end 8)
|
|
1754 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|function\\|task\\|\\(macro\\)?module\\)\\>\\)"))
|
|
1755 )
|
|
1756 (let (b e)
|
|
1757 (save-excursion
|
|
1758 (verilog-re-search-backward reg nil 'move)
|
|
1759 (cond
|
|
1760 ((match-end 1)
|
|
1761 (setq b (progn
|
|
1762 (skip-chars-forward "^ \t")
|
|
1763 (verilog-forward-ws&directives)
|
|
1764 (if (and width (looking-at width))
|
|
1765 (progn
|
|
1766 (goto-char (match-end 0))
|
|
1767 (verilog-forward-ws&directives)
|
|
1768 ))
|
|
1769 (point))
|
|
1770 e (progn
|
|
1771 (skip-chars-forward "a-zA-Z0-9_")
|
|
1772 (point)))
|
|
1773 (setq string (buffer-substring b e)))
|
|
1774 (t
|
|
1775 (ding 't)
|
|
1776 (setq string "unmactched end(function|task|module|primitive)")))))
|
|
1777 (end-of-line)
|
|
1778 (insert (concat " // " string )))
|
|
1779 )
|
|
1780 )
|
|
1781 )
|
|
1782 )
|
|
1783 )
|
|
1784 )
|
|
1785 )
|
|
1786 )
|
|
1787 )
|
|
1788
|
|
1789 (defun verilog-get-expr()
|
|
1790 "Grab expression at point, e.g, case ( a | b & (c ^d))"
|
|
1791 (let* ((b (progn
|
|
1792 (verilog-forward-syntactic-ws)
|
|
1793 (skip-chars-forward " \t")
|
|
1794 (point)))
|
|
1795 (e (let ((par 1))
|
|
1796 (cond
|
|
1797 ((looking-at "(")
|
|
1798 (forward-char 1)
|
|
1799 (while (and (/= par 0)
|
|
1800 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
|
|
1801 (cond
|
|
1802 ((match-end 1)
|
|
1803 (setq par (1+ par)))
|
|
1804 ((match-end 2)
|
|
1805 (setq par (1- par)))))
|
|
1806 (point))
|
|
1807 ((looking-at "\\[")
|
|
1808 (forward-char 1)
|
|
1809 (while (and (/= par 0)
|
|
1810 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
|
|
1811 (cond
|
|
1812 ((match-end 1)
|
|
1813 (setq par (1+ par)))
|
|
1814 ((match-end 2)
|
|
1815 (setq par (1- par)))))
|
|
1816 (verilog-forward-syntactic-ws)
|
|
1817 (skip-chars-forward "^ \t\n")
|
|
1818 (point))
|
|
1819 ((looking-at "/[/\\*]")
|
|
1820 b)
|
|
1821 ('t
|
|
1822 (skip-chars-forward "^: \t\n")
|
|
1823 (point)
|
|
1824 ))))
|
|
1825 (str (buffer-substring b e)))
|
|
1826 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
|
|
1827 (setq str (concat (substring str 0 e) "...")))
|
|
1828 str)
|
|
1829 )
|
|
1830
|
|
1831
|
|
1832 ;;;
|
|
1833 ;;; Indentation
|
|
1834 ;;;
|
|
1835 (defconst verilog-indent-alist
|
|
1836 '((block . (+ ind verilog-indent-level))
|
|
1837 (case . (+ ind verilog-case-indent))
|
|
1838 (cparenexp . (+ ind verilog-indent-level))
|
|
1839 (cexp . (+ ind verilog-indent-level))
|
134
|
1840 (defun . verilog-indent-level-module)
|
|
1841 (declaration . verilog-indent-level-declaration)
|
98
|
1842 (tf . verilog-indent-level)
|
134
|
1843 (behavorial . (+ verilog-indent-level-behavorial verilog-indent-level-module))
|
98
|
1844 (statement . ind)
|
|
1845 (cpp . 0)
|
|
1846 (comment . (verilog-indent-comment))
|
|
1847 (unknown . 3)
|
|
1848 (string . 0)))
|
|
1849
|
|
1850 (defun verilog-calculate-indent ()
|
|
1851 "Calculate the indent of the current Verilog line, through examination
|
|
1852 of previous lines. Once a line is found that is definitive as to the
|
|
1853 type of the current line, return that lines' indent level and it's
|
|
1854 type. Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
|
|
1855 (save-excursion
|
|
1856 (let* ((starting_position (point))
|
|
1857 (par 0)
|
|
1858 (begin (looking-at "[ \t]*begin\\>"))
|
|
1859 (type (catch 'nesting
|
|
1860 ;; Keep working backwards until we can figure out
|
|
1861 ;; what type of statement this is.
|
|
1862 ;; Basically we need to figure out
|
|
1863 ;; 1) if this is a continuation of the previous line;
|
|
1864 ;; 2) are we in a block scope (begin..end)
|
|
1865
|
|
1866 ;; if we are in a comment, done.
|
|
1867 (if (verilog-in-star-comment-p) (throw 'nesting 'comment))
|
|
1868
|
|
1869 ;; if we are in a parenthesized list, done.
|
|
1870 (if (verilog-in-paren) (progn (setq par 1) (throw 'nesting 'block)))
|
|
1871
|
|
1872 ;; See if we are continuing a previous line
|
|
1873 (while t
|
|
1874 ;; trap out if we crawl off the top of the buffer
|
|
1875 (if (bobp) (throw 'nesting 'cpp))
|
|
1876
|
134
|
1877 (if (verilog-continued-line-1)
|
98
|
1878 (let ((sp (point)))
|
|
1879 (if (and
|
|
1880 (not (looking-at verilog-complete-reg))
|
134
|
1881 (verilog-continued-line-1))
|
98
|
1882 (progn (goto-char sp)
|
|
1883 (throw 'nesting 'cexp))
|
|
1884 (goto-char sp))
|
134
|
1885
|
98
|
1886 (if (and begin
|
|
1887 (not verilog-indent-begin-after-if)
|
|
1888 (looking-at verilog-no-indent-begin-re))
|
|
1889 (throw 'nesting 'statement)
|
134
|
1890 (progn
|
|
1891 (throw 'nesting 'cexp)
|
|
1892 )
|
|
1893 ))
|
98
|
1894
|
|
1895 ;; not a continued line
|
|
1896 (goto-char starting_position))
|
|
1897
|
|
1898 (if (looking-at "\\<else\\>")
|
|
1899 ;; search back for governing if, striding across begin..end pairs
|
|
1900 ;; appropriately
|
134
|
1901 (let ((elsec 1))
|
|
1902 (while (verilog-re-search-backward verilog-ends-re nil 'move)
|
98
|
1903 (cond
|
134
|
1904 ((match-end 1) ; else, we're in deep
|
|
1905 (setq elsec (1+ elsec))
|
|
1906 )
|
|
1907 ((match-end 2) ; found it
|
|
1908 (setq elsec (1- elsec))
|
|
1909 (if (= 0 elsec)
|
|
1910 ;; Now previous line describes syntax
|
|
1911 (throw 'nesting 'statement)
|
|
1912 ))
|
|
1913 (t ; endblock
|
|
1914 ; try to leap back to matching outward block by striding across
|
|
1915 ; indent level changing tokens then immediately
|
|
1916 ; previous line governs indentation.
|
98
|
1917 (let ((reg)(nest 1))
|
134
|
1918 ;; (looking-at verilog-end-block-re-1);; end|join|endcase|endtable|endspecify
|
98
|
1919 (cond
|
134
|
1920 ((match-end 3) ; end
|
98
|
1921 ;; Search back for matching begin
|
|
1922 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" )
|
|
1923 )
|
134
|
1924 ((match-end 4) ; endcase
|
98
|
1925 ;; Search back for matching case
|
|
1926 (setq reg "\\(\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
|
|
1927 )
|
134
|
1928 ((match-end 5) ; join
|
98
|
1929 ;; Search back for matching fork
|
|
1930 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\>\\)" )
|
|
1931 )
|
134
|
1932 ((match-end 6) ; endtable
|
98
|
1933 ;; Search back for matching table
|
|
1934 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" )
|
|
1935 )
|
134
|
1936 ((match-end 7) ; endspecify
|
98
|
1937 ;; Search back for matching specify
|
|
1938 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" )
|
|
1939 )
|
134
|
1940 ((match-end 8) ; endfunction
|
98
|
1941 ;; Search back for matching function
|
|
1942 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" )
|
|
1943 )
|
134
|
1944 ((match-end 9) ; endtask
|
98
|
1945 ;; Search back for matching task
|
|
1946 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" )
|
|
1947 )
|
|
1948 )
|
|
1949 (catch 'skip
|
|
1950 (while (verilog-re-search-backward reg nil 'move)
|
|
1951 (cond
|
|
1952 ((match-end 1) ; begin
|
|
1953 (setq nest (1- nest))
|
|
1954 (if (= 0 nest)
|
|
1955 (throw 'skip 1)))
|
|
1956 ((match-end 2) ; end
|
|
1957 (setq nest (1+ nest)))))
|
|
1958 )
|
|
1959 )
|
|
1960 )
|
134
|
1961 )
|
98
|
1962 )
|
|
1963 )
|
|
1964 )
|
134
|
1965 (throw 'nesting (verilog-calc-1))
|
98
|
1966 )
|
|
1967 )
|
|
1968 )
|
|
1969 )
|
|
1970 ;; Return type of block and indent level.
|
|
1971 (if (not type)
|
|
1972 (setq type 'cpp))
|
|
1973 (if (> par 0) ; Unclosed Parenthesis
|
|
1974 (list 'cparenexp par)
|
134
|
1975 (cond
|
|
1976 ((eq type 'case)
|
|
1977 (list type (verilog-case-indent-level)))
|
|
1978 ((eq type 'statement)
|
|
1979 (list type (current-column)))
|
|
1980 ((eq type 'defun)
|
|
1981 (list type 0))
|
|
1982 (t
|
|
1983 (list type (verilog-indent-level)))))
|
|
1984 )
|
|
1985 )
|
|
1986 )
|
|
1987 (defun verilog-calc-1 ()
|
|
1988 ""
|
|
1989 (catch 'nesting
|
|
1990 (while (verilog-re-search-backward verilog-indent-re nil 'move)
|
|
1991 (cond
|
|
1992 ((looking-at verilog-beg-block-re-1)
|
|
1993 (cond
|
|
1994 ((match-end 2) (throw 'nesting 'case))
|
|
1995 (t (throw 'nesting 'block))))
|
|
1996
|
|
1997 ((looking-at verilog-end-block-re)
|
|
1998 (verilog-leap-to-head)
|
|
1999 (if (verilog-in-case-region-p)
|
|
2000 (progn
|
|
2001 (verilog-leap-to-case-head)
|
|
2002 (if (looking-at verilog-case-re)
|
|
2003 (throw 'nesting 'case))
|
|
2004 )))
|
|
2005
|
|
2006 ((looking-at verilog-defun-level-re)
|
|
2007 (throw 'nesting 'defun))
|
|
2008
|
|
2009 ((looking-at verilog-cpp-level-re)
|
|
2010 (throw 'nesting 'cpp))
|
|
2011
|
|
2012 ((looking-at verilog-behavorial-level-re)
|
|
2013 (throw 'nesting 'behavorial))
|
|
2014
|
|
2015 ((bobp)
|
|
2016 (throw 'nesting 'cpp))
|
|
2017 )
|
|
2018 )
|
|
2019 )
|
|
2020 )
|
|
2021
|
98
|
2022 (defun verilog-leap-to-case-head () ""
|
|
2023 (let ((nest 1))
|
|
2024 (while (/= 0 nest)
|
|
2025 (verilog-re-search-backward "\\(\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" nil 'move)
|
|
2026 (cond
|
|
2027 ((match-end 1)
|
|
2028 (setq nest (1- nest)))
|
|
2029 ((match-end 2)
|
|
2030 (setq nest (1+ nest)))
|
|
2031 ((bobp)
|
|
2032 (ding 't)
|
|
2033 (setq nest 0))
|
|
2034 )
|
|
2035 )
|
|
2036 )
|
|
2037 )
|
|
2038
|
134
|
2039 (defun verilog-leap-to-head ()
|
|
2040 "Move point to the head of this block; jump from end to matching begin,
|
|
2041 from endcase to matching case, and so on."
|
98
|
2042 (let (reg
|
|
2043 snest
|
|
2044 (nest 1))
|
134
|
2045 (cond
|
|
2046 ((looking-at "\\<end\\>")
|
|
2047 ;; Search back for matching begin
|
|
2048 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
|
|
2049 "\\(\\<endcase\\>\\)\\|\\(\\<join\\>\\)" )))
|
|
2050
|
|
2051 ((looking-at "\\<endcase\\>")
|
|
2052 ;; Search back for matching case
|
|
2053 (setq reg "\\(\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)" )
|
|
2054 )
|
|
2055 ((looking-at "\\<join\\>")
|
|
2056 ;; Search back for matching fork
|
|
2057 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\>\\)" )
|
|
2058 )
|
|
2059 ((looking-at "\\<endtable\\>")
|
|
2060 ;; Search back for matching table
|
|
2061 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" )
|
|
2062 )
|
|
2063 ((looking-at "\\<endspecify\\>")
|
|
2064 ;; Search back for matching specify
|
|
2065 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" )
|
|
2066 )
|
|
2067 ((looking-at "\\<endfunction\\>")
|
|
2068 ;; Search back for matching function
|
|
2069 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" )
|
|
2070 )
|
|
2071 ((looking-at "\\<endtask\\>")
|
|
2072 ;; Search back for matching task
|
|
2073 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" )
|
|
2074 )
|
|
2075 )
|
|
2076 (catch 'skip
|
|
2077 (let (sreg)
|
|
2078 (while (verilog-re-search-backward reg nil 'move)
|
98
|
2079 (cond
|
134
|
2080 ((match-end 1) ; begin
|
|
2081 (setq nest (1- nest))
|
|
2082 (if (= 0 nest)
|
|
2083 ;; Now previous line describes syntax
|
|
2084 (throw 'skip 1))
|
|
2085 (if (and snest
|
|
2086 (= snest nest))
|
|
2087 (setq reg sreg))
|
|
2088 )
|
|
2089 ((match-end 2) ; end
|
|
2090 (setq nest (1+ nest))
|
|
2091 )
|
|
2092 ((match-end 3)
|
|
2093 ;; endcase, jump to case
|
|
2094 (setq snest nest)
|
|
2095 (setq nest (1+ nest))
|
|
2096 (setq sreg reg)
|
98
|
2097 (setq reg "\\(\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
|
|
2098 )
|
134
|
2099 ((match-end 4)
|
|
2100 ;; join, jump to fork
|
|
2101 (setq snest nest)
|
|
2102 (setq nest (1+ nest))
|
|
2103 (setq sreg reg)
|
98
|
2104 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\>\\)" )
|
|
2105 )
|
|
2106 )
|
|
2107 )
|
134
|
2108 )
|
98
|
2109 )
|
|
2110 )
|
|
2111 )
|
134
|
2112
|
|
2113 (defun verilog-continued-line-1 ()
|
|
2114 "Return true if this is a continued line.
|
|
2115 Set point to where line starts"
|
|
2116 (let ((continued 't))
|
|
2117 (if (eq 0 (forward-line -1))
|
|
2118 (progn
|
|
2119 (end-of-line)
|
|
2120 (verilog-backward-ws&directives)
|
|
2121 (if (bobp)
|
|
2122 (setq continued nil)
|
|
2123 (setq continued (verilog-backward-token))
|
|
2124 )
|
|
2125 )
|
|
2126 (setq continued nil)
|
|
2127 )
|
|
2128 continued)
|
|
2129 )
|
|
2130
|
98
|
2131 (defun verilog-continued-line ()
|
|
2132 "Return true if this is a continued line.
|
|
2133 Set point to where line starts"
|
|
2134 (let ((continued 't))
|
|
2135 (if (eq 0 (forward-line -1))
|
|
2136 (progn
|
|
2137 (end-of-line)
|
|
2138 (verilog-backward-ws&directives)
|
|
2139 (if (bobp)
|
|
2140 (setq continued nil)
|
|
2141 (while (and continued
|
|
2142 (save-excursion
|
|
2143 (skip-chars-backward " \t")
|
|
2144 (not (bolp))))
|
134
|
2145 (setq continued (verilog-backward-token))
|
98
|
2146 ) ;; while
|
|
2147 )
|
|
2148 )
|
|
2149 (setq continued nil)
|
|
2150 )
|
|
2151 continued)
|
|
2152 )
|
|
2153
|
|
2154 (defun verilog-backward-token ()
|
|
2155 "step backward token, returning true if we are now at an end of line token"
|
|
2156 (verilog-backward-syntactic-ws)
|
|
2157 (cond
|
|
2158 ((bolp)
|
|
2159 nil)
|
|
2160 (;-- Anything ending in a ; is complete
|
|
2161 (= (preceding-char) ?\;)
|
|
2162 nil)
|
|
2163 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
|
|
2164 (= (preceding-char) ?\))
|
|
2165 (progn
|
|
2166 (backward-char)
|
|
2167 (backward-up-list 1)
|
|
2168 (verilog-backward-syntactic-ws)
|
|
2169 (forward-word -1)
|
|
2170 (not (looking-at "\\<case[xz]?\\>[^:]"))))
|
|
2171 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
|
|
2172 t
|
|
2173 (forward-word -1)
|
|
2174 (cond
|
|
2175 (
|
134
|
2176 (looking-at "\\(else\\)\\|\\(initial\\>\\)\\|\\(always\\>\\)")
|
98
|
2177 t)
|
|
2178 (
|
|
2179 (looking-at verilog-indent-reg)
|
|
2180 nil)
|
|
2181 (t
|
|
2182 (let
|
|
2183 ((back (point)))
|
|
2184 (verilog-backward-syntactic-ws)
|
|
2185 (cond
|
|
2186 ((= (preceding-char) ?\:)
|
|
2187 (backward-char)
|
|
2188 (verilog-backward-syntactic-ws)
|
|
2189 (backward-sexp)
|
|
2190 (if (looking-at "begin")
|
|
2191 nil
|
|
2192 t)
|
|
2193 )
|
|
2194 ((= (preceding-char) ?\#)
|
|
2195 (backward-char)
|
|
2196 t)
|
134
|
2197 ((= (preceding-char) ?\`)
|
|
2198 (backward-char)
|
|
2199 t)
|
98
|
2200
|
|
2201 (t
|
|
2202 (goto-char back)
|
|
2203 t)
|
|
2204 )
|
|
2205 )
|
|
2206 )
|
|
2207 )
|
|
2208 )
|
|
2209 )
|
|
2210 )
|
|
2211
|
|
2212 (defun verilog-backward-syntactic-ws (&optional lim)
|
|
2213 ;; Backward skip over syntactic whitespace for Emacs 19.
|
|
2214 (save-restriction
|
|
2215 (let* ((lim (or lim (point-min)))
|
|
2216 (here lim)
|
|
2217 bol
|
|
2218 )
|
|
2219 (if (< lim (point))
|
|
2220 (progn
|
|
2221 (narrow-to-region lim (point))
|
|
2222 (while (/= here (point))
|
|
2223 (setq here (point))
|
|
2224 (forward-comment (-(buffer-size)))
|
|
2225 (save-excursion
|
|
2226 (setq bol (progn (beginning-of-line) (point))))
|
|
2227 (search-backward "//" bol t)
|
|
2228 )))
|
134
|
2229 ))
|
|
2230 t)
|
98
|
2231
|
|
2232 (defun verilog-forward-syntactic-ws (&optional lim)
|
|
2233 ;; forward skip over syntactic whitespace for Emacs 19.
|
|
2234 (save-restriction
|
|
2235 (let* ((lim (or lim (point-max)))
|
|
2236 (here lim)
|
|
2237 )
|
|
2238 (if (> lim (point))
|
|
2239 (progn
|
|
2240 (narrow-to-region (point) lim)
|
|
2241 (while (/= here (point))
|
|
2242 (setq here (point))
|
|
2243 (forward-comment (buffer-size))
|
|
2244 )))
|
|
2245 )))
|
|
2246
|
|
2247 (defun verilog-backward-ws&directives (&optional lim)
|
|
2248 ;; Backward skip over syntactic whitespace and compiler directives for Emacs 19.
|
|
2249 (save-restriction
|
|
2250 (let* ((lim (or lim (point-min)))
|
|
2251 (here lim)
|
|
2252 jump
|
|
2253 )
|
|
2254 (if (< lim (point))
|
|
2255 (progn
|
|
2256 (let ((state
|
|
2257 (save-excursion
|
|
2258 (parse-partial-sexp (point-min) (point)))))
|
|
2259 (cond
|
|
2260 ((nth 4 state) ;; in /* */ comment
|
|
2261 (verilog-re-search-backward "/\*" nil 'move)
|
|
2262 )
|
|
2263 ((nth 7 state) ;; in // comment
|
|
2264 (verilog-re-search-backward "//" nil 'move)
|
|
2265 )))
|
|
2266 (narrow-to-region lim (point))
|
|
2267 (while (/= here (point))
|
|
2268 (setq here (point))
|
|
2269 (forward-comment (-(buffer-size)))
|
|
2270 (save-excursion
|
|
2271 (beginning-of-line)
|
|
2272 (if (looking-at "[ \t]*\\(`define\\)\\|\\(`ifdef\\)\\|\\(`else\\)\\|\\(`endif\\)\\|\\(`timescale\\)\\|\\(`include\\)")
|
|
2273 (setq jump t)
|
|
2274 (setq jump nil)))
|
|
2275 (if jump
|
|
2276 (beginning-of-line))
|
|
2277 )))
|
|
2278 )))
|
|
2279
|
|
2280 (defun verilog-forward-ws&directives (&optional lim)
|
|
2281 ;; forward skip over syntactic whitespace and compiler directives for Emacs 19.
|
|
2282 (save-restriction
|
|
2283 (let* ((lim (or lim (point-max)))
|
|
2284 (here lim)
|
|
2285 jump
|
|
2286 )
|
|
2287 (if (> lim (point))
|
|
2288 (progn
|
|
2289 (let ((state
|
|
2290 (save-excursion
|
|
2291 (parse-partial-sexp (point-min) (point)))))
|
|
2292 (cond
|
|
2293 ((nth 4 state) ;; in /* */ comment
|
|
2294 (verilog-re-search-forward "/\*" nil 'move)
|
|
2295 )
|
|
2296 ((nth 7 state) ;; in // comment
|
|
2297 (verilog-re-search-forward "//" nil 'move)
|
|
2298 )))
|
|
2299 (narrow-to-region (point) lim)
|
|
2300 (while (/= here (point))
|
|
2301 (setq here (point))
|
|
2302 (forward-comment (buffer-size))
|
|
2303 (save-excursion
|
|
2304 (beginning-of-line)
|
|
2305 (if (looking-at "[ \t]*\\(`define\\)\\|\\(`ifdef\\)\\|\\(`else\\)\\|\\(`endif\\)\\|\\(`timescale\\)")
|
|
2306 (setq jump t)))
|
|
2307 (if jump
|
|
2308 (beginning-of-line 2))
|
|
2309 )))
|
|
2310 )))
|
|
2311 (defun verilog-parenthesis-depth ()
|
|
2312 "Return non zero if in parenthetical-expression"
|
|
2313 (save-excursion
|
|
2314 (car (parse-partial-sexp (point-min) (point)))))
|
|
2315
|
|
2316 (defun verilog-in-comment-or-string-p ()
|
|
2317 "Return true if in a string or comment"
|
|
2318 (let ((state
|
|
2319 (save-excursion
|
|
2320 (parse-partial-sexp (point-min) (point)))))
|
|
2321 (or (nth 3 state) (nth 4 state) (nth 7 state))) ; Inside string or comment
|
|
2322 )
|
|
2323
|
|
2324 (defun verilog-in-star-comment-p ()
|
|
2325 "Return true if in a star comment"
|
|
2326 (let ((state
|
|
2327 (save-excursion
|
|
2328 (parse-partial-sexp (point-min) (point)))))
|
|
2329 (nth 4 state))
|
|
2330 )
|
|
2331
|
|
2332 (defun verilog-in-paren ()
|
|
2333 "Return true if in a parenthetical expression"
|
|
2334 (let ((state
|
|
2335 (save-excursion
|
|
2336 (parse-partial-sexp (point-min) (point)))))
|
|
2337 (/= 0 (nth 0 state)))
|
|
2338 )
|
|
2339
|
|
2340 (defun verilog-skip-forward-comment-or-string ()
|
|
2341 "Return true if in a string or comment"
|
|
2342 (let ((state
|
|
2343 (save-excursion
|
|
2344 (parse-partial-sexp (point-min) (point)))))
|
|
2345 (cond
|
|
2346 ((nth 3 state) ;Inside string
|
|
2347 (goto-char (nth 3 state))
|
|
2348 t)
|
|
2349 ((nth 7 state) ;Inside // comment
|
|
2350 (forward-line 1)
|
|
2351 t)
|
|
2352 ((nth 4 state) ;Inside any comment (hence /**/)
|
|
2353 (search-forward "*/"))
|
|
2354 (t
|
|
2355 nil)
|
|
2356 )
|
|
2357 )
|
|
2358 )
|
|
2359
|
|
2360 (defun verilog-skip-backward-comment-or-string ()
|
|
2361 "Return true if in a string or comment"
|
|
2362 (let ((state
|
|
2363 (save-excursion
|
|
2364 (parse-partial-sexp (point-min) (point)))))
|
|
2365 (cond
|
|
2366 ((nth 3 state) ;Inside string
|
|
2367 (search-backward "\"")
|
|
2368 t)
|
|
2369 ((nth 7 state) ;Inside // comment
|
|
2370 (search-backward "//")
|
|
2371 t)
|
|
2372 ((nth 4 state) ;Inside /* */ comment
|
|
2373 (search-backward "/*")
|
|
2374 t)
|
|
2375 (t
|
|
2376 nil)
|
|
2377 )
|
|
2378 )
|
|
2379 )
|
|
2380
|
|
2381 (defun verilog-skip-forward-comment-p ()
|
|
2382 "If in comment, move to end and return true"
|
|
2383 (let (state)
|
|
2384 (progn
|
|
2385 (setq state
|
|
2386 (save-excursion
|
|
2387 (parse-partial-sexp (point-min) (point))))
|
|
2388 (cond
|
|
2389 ((nth 3 state)
|
|
2390 t)
|
|
2391 ((nth 7 state) ;Inside // comment
|
|
2392 (end-of-line)
|
|
2393 (forward-char 1)
|
|
2394 t)
|
|
2395 ((nth 4 state) ;Inside any comment
|
|
2396 t)
|
|
2397 (t
|
|
2398 nil)
|
|
2399 )
|
|
2400 )
|
|
2401 )
|
|
2402 )
|
|
2403
|
|
2404 (defun verilog-indent-line-relative ()
|
|
2405 "Cheap version of indent line that only looks at
|
|
2406 a few lines to determine indent level"
|
|
2407 (interactive)
|
|
2408 (let ((indent-str))
|
|
2409 (save-excursion
|
|
2410 (beginning-of-line)
|
|
2411 (if (looking-at "^[ \t]*$")
|
|
2412 (cond ;- A blank line; No need to be too smart.
|
|
2413 ((bobp)
|
|
2414 (setq indent-str (list 'cpp 0)))
|
|
2415 ((verilog-continued-line)
|
|
2416 (let ((sp (point)))
|
|
2417 (if (verilog-continued-line)
|
|
2418 (progn (goto-char sp)
|
|
2419 (setq indent-str (list 'statement (verilog-indent-level))))
|
|
2420 (goto-char sp)
|
|
2421 (setq indent-str (list 'block (verilog-indent-level))))))
|
|
2422 (t
|
|
2423 (setq indent-str (verilog-calculate-indent))))
|
|
2424 (setq indent-str (verilog-calculate-indent))
|
|
2425 )
|
|
2426 )
|
|
2427 (verilog-do-indent indent-str)
|
|
2428 )
|
|
2429 )
|
|
2430 (defun verilog-indent-line ()
|
|
2431 "Indent for special part of code."
|
|
2432 (if (looking-at verilog-directive-re)
|
|
2433 ;; We could nicely nest `ifdef's, but...
|
|
2434 (progn
|
|
2435 (delete-horizontal-space)
|
|
2436 (indent-to 0)
|
|
2437 (list 'cpp 0)) ; Return verilog-calculate-indent data
|
|
2438 (verilog-do-indent (verilog-calculate-indent)))
|
|
2439 )
|
|
2440
|
|
2441 (defun verilog-do-indent (indent-str)
|
|
2442 ""
|
|
2443 (let ((type (car indent-str))
|
|
2444 (ind (car (cdr indent-str))))
|
|
2445 (delete-horizontal-space)
|
|
2446 (cond
|
134
|
2447 (; handle continued exp
|
98
|
2448 (eq type 'cexp)
|
|
2449 (let ((here (point)))
|
134
|
2450 (verilog-backward-syntactic-ws)
|
|
2451 (cond
|
|
2452 ((= (preceding-char) ?\,)
|
|
2453 (let* ( fst
|
|
2454 (column
|
|
2455 (save-excursion
|
|
2456 (backward-char 1)
|
|
2457 (verilog-beg-of-statement)
|
|
2458 (setq fst (point))
|
|
2459 (if (looking-at verilog-declaration-re)
|
|
2460 (progn ;; we have multiple words
|
|
2461 (goto-char (match-end 0))
|
|
2462 (skip-chars-forward " \t")
|
|
2463 (if (= (following-char) ?\[)
|
|
2464 (progn
|
|
2465 (forward-char 1)
|
|
2466 (backward-up-list -1)
|
|
2467 (skip-chars-forward " \t")
|
|
2468 )
|
98
|
2469 )
|
134
|
2470 )
|
|
2471 (;; we have a single word
|
|
2472 goto-char fst)
|
98
|
2473 )
|
134
|
2474 (current-column)
|
98
|
2475 )
|
134
|
2476 )
|
|
2477 )
|
98
|
2478 (goto-char here)
|
134
|
2479 (beginning-of-line)
|
|
2480 (delete-horizontal-space)
|
|
2481 (indent-to column))
|
|
2482 )
|
|
2483 ((= (preceding-char) ?\) )
|
|
2484 (goto-char here)
|
|
2485 (indent-to (eval (cdr (assoc type verilog-indent-alist))))
|
98
|
2486 )
|
134
|
2487 (t
|
|
2488 (goto-char here)
|
|
2489 (let ((val))
|
|
2490 (verilog-beg-of-statement)
|
|
2491 (if (verilog-re-search-forward "=[ \\t]*" here 'move)
|
|
2492 (setq val (current-column))
|
|
2493 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
|
|
2494 (goto-char here)
|
|
2495 (indent-to val)
|
|
2496 )
|
|
2497 )
|
|
2498 )
|
98
|
2499 )
|
|
2500 )
|
|
2501 (; handle inside parenthetical expressions
|
|
2502 (eq type 'cparenexp)
|
|
2503 (let ((column (save-excursion
|
|
2504 (backward-up-list 1)
|
|
2505 (forward-char 1)
|
|
2506 (skip-chars-forward " \t")
|
|
2507 (current-column))))
|
|
2508 (beginning-of-line)
|
|
2509 (delete-horizontal-space)
|
|
2510 (indent-to column)))
|
|
2511
|
134
|
2512 (;-- Handle the ends
|
|
2513 (looking-at verilog-end-block-re )
|
|
2514 (if (eq type 'statement)
|
|
2515 (indent-to (- ind verilog-indent-level))
|
|
2516 (indent-to ind)))
|
98
|
2517 (;-- Case -- maybe line 'em up
|
|
2518 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
|
|
2519 (progn
|
|
2520 (cond
|
|
2521 ((looking-at "\\<endcase\\>")
|
|
2522 (indent-to ind))
|
|
2523 (t
|
|
2524 (indent-to (eval (cdr (assoc type verilog-indent-alist))))
|
|
2525 ))))
|
|
2526
|
134
|
2527
|
98
|
2528 (;-- defun
|
|
2529 (and (eq type 'defun)
|
134
|
2530 (looking-at verilog-zero-indent-re))
|
98
|
2531 (indent-to 0))
|
|
2532
|
134
|
2533 (;-- declaration
|
|
2534 (and (or
|
|
2535 (eq type 'defun)
|
|
2536 (eq type 'block))
|
|
2537 (looking-at verilog-declaration-re))
|
|
2538 (verilog-indent-declaration ind))
|
|
2539
|
98
|
2540 (;-- Everything else
|
|
2541 t
|
|
2542 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
|
|
2543 (indent-to val)
|
|
2544 ))
|
|
2545 )
|
|
2546 (if (looking-at "[ \t]+$")
|
|
2547 (skip-chars-forward " \t"))
|
134
|
2548 indent-str ; Return indent data
|
98
|
2549 )
|
|
2550 )
|
|
2551
|
|
2552 (defun verilog-indent-level ()
|
|
2553 "Return the indent-level the current statement has."
|
|
2554 (save-excursion
|
|
2555 (beginning-of-line)
|
|
2556 (skip-chars-forward " \t")
|
|
2557 (current-column)))
|
|
2558
|
|
2559
|
|
2560 (defun verilog-case-indent-level ()
|
|
2561 "Return the indent-level the current statement has.
|
|
2562 Do not count named blocks or case-statements."
|
|
2563 (save-excursion
|
|
2564 (skip-chars-forward " \t")
|
|
2565 (cond
|
|
2566 ((looking-at verilog-named-block-re)
|
|
2567 (current-column))
|
|
2568 ((and (not (looking-at verilog-case-re))
|
134
|
2569 (looking-at "^[^:;]+[ \t]*:"))
|
98
|
2570 (search-forward ":" nil t)
|
|
2571 (skip-chars-forward " \t")
|
|
2572 (current-column))
|
|
2573 (t
|
|
2574 (current-column)))))
|
|
2575
|
|
2576 (defun verilog-indent-comment (&optional arg)
|
|
2577 "Indent current line as comment.
|
|
2578 If optional arg is non-nil, just return the
|
|
2579 column number the line should be indented to."
|
|
2580 (let* ((stcol
|
|
2581 (cond
|
|
2582 ((verilog-in-star-comment-p)
|
|
2583 (save-excursion
|
|
2584 (re-search-backward "/\\*" nil t)
|
|
2585 (1+(current-column))))
|
|
2586 ( comment-column
|
|
2587 comment-column )
|
|
2588 (t
|
|
2589 (save-excursion
|
|
2590 (re-search-backward "//" nil t)
|
|
2591 (current-column)))
|
|
2592 )
|
|
2593 ))
|
|
2594 (if arg
|
|
2595 (progn
|
|
2596 (delete-horizontal-space)
|
|
2597 (indent-to stcol))
|
|
2598 stcol
|
|
2599 )
|
|
2600 )
|
|
2601 )
|
|
2602
|
|
2603 ;;;
|
|
2604
|
134
|
2605 (defun verilog-pretty-declarations ()
|
|
2606 "Line up declarations arround point"
|
|
2607 (interactive)
|
|
2608 (save-excursion
|
|
2609 (if (progn
|
|
2610 (verilog-beg-of-statement-1)
|
|
2611 (looking-at verilog-declaration-re))
|
|
2612 (let* ((m1 (make-marker))
|
|
2613 (e) (r)
|
|
2614 (here (point))
|
|
2615 (start
|
|
2616 (progn
|
|
2617 (verilog-beg-of-statement-1)
|
|
2618 (while (looking-at verilog-declaration-re)
|
|
2619 (beginning-of-line)
|
|
2620 (setq e (point))
|
|
2621 (verilog-backward-syntactic-ws)
|
|
2622 (backward-char)
|
|
2623 (verilog-beg-of-statement-1))
|
|
2624 e))
|
|
2625 (end
|
|
2626 (progn
|
|
2627 (goto-char here)
|
|
2628 (verilog-end-of-statement)
|
|
2629 (setq e (point)) ;Might be on last line
|
|
2630 (verilog-forward-syntactic-ws)
|
|
2631 (while (looking-at verilog-declaration-re)
|
|
2632 (beginning-of-line)
|
|
2633 (verilog-end-of-statement)
|
|
2634 (setq e (point))
|
|
2635 (verilog-forward-syntactic-ws)
|
|
2636 )
|
|
2637 e))
|
|
2638 (edpos (set-marker (make-marker) end))
|
|
2639 (ind)
|
|
2640 (base-ind
|
|
2641 (progn
|
|
2642 (goto-char start)
|
|
2643 (verilog-do-indent (verilog-calculate-indent))
|
|
2644 (verilog-forward-ws&directives)
|
|
2645 (current-column)
|
|
2646 ))
|
|
2647 )
|
|
2648 (goto-char end)
|
|
2649 (goto-char start)
|
|
2650 (if (> (- end start) 100)
|
|
2651 (message "Lining up declarations..(please stand by)"))
|
|
2652 ;; Get the begining of line indent first
|
|
2653 (while (progn (setq e (marker-position edpos))
|
|
2654 (< (point) e))
|
|
2655 (delete-horizontal-space)
|
|
2656 (indent-to base-ind)
|
|
2657 (forward-line))
|
|
2658 ;; Now find biggest prefix
|
|
2659 (setq ind (verilog-get-lineup-indent start edpos))
|
|
2660 ;; Now indent each line.
|
|
2661 (goto-char start)
|
|
2662 (while (progn (setq e (marker-position edpos))
|
|
2663 (setq r (- e (point)))
|
|
2664 (> r 0))
|
|
2665 (setq e (point))
|
|
2666 (message "%d" r)
|
|
2667 (cond
|
|
2668 ((looking-at verilog-declaration-re-1)
|
|
2669 (let ((p (match-end 0)))
|
|
2670 (set-marker m1 p)
|
|
2671 (if (verilog-re-search-forward "\\[" p 'move)
|
|
2672 (progn
|
|
2673 (forward-char -1)
|
|
2674 (just-one-space)
|
|
2675 (goto-char (marker-position m1))
|
|
2676 (just-one-space)
|
|
2677 (indent-to ind)
|
|
2678 )
|
|
2679 (progn
|
|
2680 (just-one-space)
|
|
2681 (indent-to ind))
|
|
2682 )
|
|
2683 ))
|
|
2684 ((verilog-continued-line-1)
|
|
2685 (goto-char e)
|
|
2686 (delete-horizontal-space)
|
|
2687 (indent-to ind))
|
|
2688 (t ; Must be comment or white space
|
|
2689 (goto-char e)
|
|
2690 (verilog-forward-ws&directives)
|
|
2691 (forward-line -1)
|
|
2692 )
|
|
2693 )
|
|
2694 (forward-line 1)
|
|
2695 )
|
|
2696 (message "")
|
|
2697 )
|
|
2698 )
|
|
2699 )
|
|
2700 )
|
|
2701 (defun verilog-indent-declaration (baseind)
|
|
2702 "Indent current lines as declaration, lining up the variable names
|
|
2703 based on previous declaration's indentation."
|
98
|
2704 (interactive)
|
|
2705 (let ((pos (point-marker))
|
134
|
2706 (lim (save-excursion
|
|
2707 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil 'move)
|
|
2708 (point)))
|
|
2709 (ind)
|
|
2710 (m1 (make-marker))
|
98
|
2711 )
|
134
|
2712 ;; Use previous declaration (in this module) as template.
|
|
2713 (if (verilog-re-search-backward verilog-declaration-re-1 lim t)
|
|
2714 (progn
|
|
2715 (goto-char (match-end 0))
|
|
2716 (setq ind (current-column))
|
|
2717 (goto-char pos)
|
|
2718 (beginning-of-line)
|
|
2719 (indent-to (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
|
|
2720 (if (looking-at verilog-declaration-re-2)
|
|
2721 (let ((p (match-end 0)))
|
|
2722 (set-marker m1 p)
|
|
2723 (if (verilog-re-search-forward "\\[" p 'move)
|
|
2724 (progn
|
|
2725 (forward-char -1)
|
|
2726 (just-one-space)
|
|
2727 (goto-char (marker-position m1))
|
|
2728 (just-one-space)
|
|
2729 (indent-to ind)
|
98
|
2730 )
|
134
|
2731 (progn
|
|
2732 (just-one-space)
|
|
2733 (indent-to ind)
|
|
2734 )
|
|
2735 )
|
|
2736 )
|
|
2737 )
|
|
2738 )
|
|
2739 (indent-to (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
|
|
2740 )
|
|
2741 (goto-char pos)
|
|
2742 )
|
|
2743 )
|
98
|
2744
|
|
2745 ; "Return the indent level that will line up several lines within the region
|
|
2746 ;from b to e nicely. The lineup string is str."
|
|
2747 (defun verilog-get-lineup-indent (b edpos)
|
|
2748 (save-excursion
|
|
2749 (let ((ind 0) e)
|
|
2750 (goto-char b)
|
|
2751 ;; Get rightmost position
|
|
2752 (while (progn (setq e (marker-position edpos))
|
|
2753 (< (point) e))
|
|
2754 (if (verilog-re-search-forward verilog-declaration-re-1 e 'move)
|
|
2755 (progn
|
|
2756 (goto-char (match-end 0))
|
|
2757 (verilog-backward-syntactic-ws)
|
|
2758 (if (> (current-column) ind)
|
|
2759 (setq ind (current-column)))
|
|
2760 (goto-char (match-end 0)))))
|
|
2761 (if (> ind 0)
|
|
2762 (1+ ind)
|
|
2763 ;; No lineup-string found
|
|
2764 (goto-char b)
|
|
2765 (end-of-line)
|
|
2766 (skip-chars-backward " \t")
|
|
2767 (1+ (current-column))))))
|
|
2768
|
|
2769 ;; A useful mode debugging aide
|
|
2770 (defun verilog-comment-depth (type val)
|
|
2771 ""
|
|
2772 (save-excursion
|
|
2773 (let
|
|
2774 ((b (prog2
|
|
2775 (beginning-of-line)
|
|
2776 (point-marker)
|
|
2777 (end-of-line)))
|
|
2778 (e (point-marker)))
|
|
2779 (if (re-search-backward " /\\* \[#-\]# \[a-z\]+ \[0-9\]+ ## \\*/" b t)
|
|
2780 (progn
|
|
2781 (replace-match " /* -# ## */")
|
|
2782 (end-of-line))
|
|
2783 (progn
|
|
2784 (end-of-line)
|
|
2785 (insert " /* ## ## */"))))
|
|
2786 (backward-char 6)
|
|
2787 (insert
|
|
2788 (format "%s %d" type val))
|
|
2789 )
|
|
2790 )
|
|
2791 ;;;
|
|
2792 ;;;
|
|
2793 ;;; Completion
|
|
2794 ;;;
|
|
2795 (defvar verilog-str nil)
|
|
2796 (defvar verilog-all nil)
|
|
2797 (defvar verilog-pred nil)
|
|
2798 (defvar verilog-buffer-to-use nil)
|
|
2799 (defvar verilog-flag nil)
|
|
2800 (defvar verilog-toggle-completions nil
|
|
2801 "*Non-nil means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
|
|
2802 Repeated use of \\[verilog-complete-word] will show you all of them.
|
|
2803 Normally, when there is more than one possible completion,
|
|
2804 it displays a list of all possible completions.")
|
|
2805
|
|
2806
|
|
2807 (defvar verilog-type-keywords
|
|
2808 '("buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
|
|
2809 "integer" "nand" "nmos" "nor" "not" "notif0" "notif1" "or" "output" "parameter"
|
|
2810 "pmos" "pull0" "pull1" "pullup" "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
|
|
2811 "rtranif0" "rtranif1" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
|
|
2812 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor" )
|
|
2813 "*Keywords for types used when completing a word in a declaration or parmlist.
|
|
2814 \(eg. integer, real, char.) The types defined within the Verilog program
|
|
2815 will be completed runtime, and should not be added to this list.")
|
|
2816
|
|
2817 (defvar verilog-defun-keywords
|
|
2818 '("begin" "function" "task" "initial" "always" "assign" "posedge" "negedge" "endmodule")
|
|
2819 "*Keywords to complete when standing at first word of a line in declarative scope.
|
|
2820 \(eg. initial, always, begin, assign.)
|
|
2821 The procedures and variables defined within the Verilog program
|
|
2822 will be completed runtime and should not be added to this list.")
|
|
2823
|
|
2824 (defvar verilog-block-keywords
|
|
2825 '("begin" "fork" "join" "case" "end" "if" "else" "for" "while" "repeat")
|
|
2826 "*Keywords to complete when standing at first word of a line in behavorial scope.
|
|
2827 \(eg. begin, if, then, else, for, fork.)
|
|
2828 The procedures and variables defined within the Verilog program
|
|
2829 will be completed runtime and should not be added to this list.")
|
|
2830
|
|
2831 (defvar verilog-tf-keywords
|
|
2832 '("begin" "fork" "join" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
|
|
2833 "*Keywords to complete when standing at first word of a line in a task or function scope.
|
|
2834 \(eg. begin, if, then, else, for, fork.)
|
|
2835 The procedures and variables defined within the Verilog program
|
|
2836 will be completed runtime and should not be added to this list.")
|
|
2837
|
|
2838 (defvar verilog-case-keywords
|
|
2839 '("begin" "fork" "join" "case" "end" "endcase" "if" "else" "for" "repeat")
|
|
2840 "*Keywords to complete when standing at first word of a line in behavorial scope.
|
|
2841 \(eg. begin, if, then, else, for, fork.)
|
|
2842 The procedures and variables defined within the Verilog program
|
|
2843 will be completed runtime and should not be added to this list.")
|
|
2844
|
|
2845 (defvar verilog-separator-keywords
|
|
2846 '("else" "then" "begin")
|
|
2847 "*Keywords to complete when NOT standing at the first word of a statement.
|
|
2848 \(eg. else, then.)
|
|
2849 Variables and function names defined within the
|
|
2850 Verilog program are completed runtime and should not be added to this list.")
|
|
2851
|
|
2852 (defun verilog-string-diff (str1 str2)
|
|
2853 "Return index of first letter where STR1 and STR2 differs."
|
|
2854 (catch 'done
|
|
2855 (let ((diff 0))
|
|
2856 (while t
|
|
2857 (if (or (> (1+ diff) (length str1))
|
|
2858 (> (1+ diff) (length str2)))
|
|
2859 (throw 'done diff))
|
|
2860 (or (equal (aref str1 diff) (aref str2 diff))
|
|
2861 (throw 'done diff))
|
|
2862 (setq diff (1+ diff))))))
|
|
2863
|
|
2864 ;; Calculate all possible completions for functions if argument is `function',
|
|
2865 ;; completions for procedures if argument is `procedure' or both functions and
|
|
2866 ;; procedures otherwise.
|
|
2867
|
|
2868 (defun verilog-func-completion (type)
|
|
2869 ;; Build regular expression for module/task/function names
|
|
2870 (if (string= verilog-str "")
|
|
2871 (setq verilog-str "[a-zA-Z_]"))
|
|
2872 (let ((verilog-str (concat (cond
|
|
2873 ((eq type 'module) "\\<\\(module\\)\\s +")
|
|
2874 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
|
|
2875 (t "\\<\\(task\\|function\\|module\\)\\s +"))
|
|
2876 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
|
|
2877 match)
|
|
2878
|
|
2879 (if (not (looking-at verilog-defun-re))
|
|
2880 (verilog-re-search-backward verilog-defun-re nil t))
|
|
2881 (forward-char 1)
|
|
2882
|
|
2883 ;; Search through all reachable functions
|
|
2884 (goto-char (point-min))
|
|
2885 (while (verilog-re-search-forward verilog-str (point-max) t)
|
|
2886 (progn (setq match (buffer-substring (match-beginning 2)
|
|
2887 (match-end 2)))
|
|
2888 (if (or (null verilog-pred)
|
|
2889 (funcall verilog-pred match))
|
|
2890 (setq verilog-all (cons match verilog-all)))))
|
|
2891 (if (match-beginning 0)
|
|
2892 (goto-char (match-beginning 0)))))
|
|
2893
|
|
2894 (defun verilog-get-completion-decl ()
|
|
2895 ;; Macro for searching through current declaration (var, type or const)
|
|
2896 ;; for matches of `str' and adding the occurence tp `all'
|
|
2897 (let ((end (save-excursion (verilog-declaration-end)
|
|
2898 (point)))
|
|
2899 match)
|
|
2900 ;; Traverse lines
|
|
2901 (while (< (point) end)
|
|
2902 (if (verilog-re-search-forward verilog-declaration-re-1 (verilog-get-end-of-line) t)
|
|
2903 ;; Traverse current line
|
|
2904 (while (and (verilog-re-search-forward
|
|
2905 (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|"
|
|
2906 verilog-symbol-re)
|
|
2907 (verilog-get-beg-of-line) t)
|
|
2908 (not (match-end 1)))
|
|
2909 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
|
|
2910 (if (string-match (concat "\\<" verilog-str) match)
|
|
2911 (if (or (null verilog-pred)
|
|
2912 (funcall verilog-pred match))
|
|
2913 (setq verilog-all (cons match verilog-all))))))
|
|
2914 (if (verilog-re-search-forward "\\<record\\>" (verilog-get-end-of-line) t)
|
|
2915 (verilog-declaration-end)
|
|
2916 (forward-line 1)))))
|
|
2917
|
|
2918 (defun verilog-type-completion ()
|
|
2919 "Calculate all possible completions for types."
|
|
2920 (let ((start (point))
|
|
2921 goon)
|
|
2922 ;; Search for all reachable type declarations
|
|
2923 (while (or (verilog-beg-of-defun)
|
|
2924 (setq goon (not goon)))
|
|
2925 (save-excursion
|
|
2926 (if (and (< start (prog1 (save-excursion (verilog-end-of-defun)
|
|
2927 (point))
|
|
2928 (forward-char 1)))
|
|
2929 (verilog-re-search-forward
|
|
2930 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
|
|
2931 start t)
|
|
2932 (not (match-end 1)))
|
|
2933 ;; Check current type declaration
|
|
2934 (verilog-get-completion-decl))))))
|
|
2935
|
|
2936 (defun verilog-var-completion ()
|
|
2937 "Calculate all possible completions for variables (or constants)."
|
|
2938 nil)
|
|
2939 ; Not done yet; in 1.99 perhaps
|
|
2940 ; (let ((start (point))
|
|
2941 ; goon twice)
|
|
2942 ; ;; Search for all reachable var declarations
|
|
2943 ; (while (or (verilog-beg-of-defun)
|
|
2944 ; (setq goon (not goon)))
|
|
2945 ; (save-excursion
|
|
2946 ; (if (> start (prog1 (save-excursion (verilog-end-of-defun)
|
|
2947 ; (point))))
|
|
2948 ; () ; Declarations not reacable
|
|
2949 ; (cond ((and (verilog-re-search-forward verilog-declaration-re start t)
|
|
2950 ; ;; Check var/const declarations
|
|
2951 ; (verilog-get-completion-decl)))))))))
|
|
2952
|
|
2953
|
|
2954 (defun verilog-keyword-completion (keyword-list)
|
|
2955 "Give list of all possible completions of keywords in KEYWORD-LIST."
|
|
2956 (mapcar '(lambda (s)
|
|
2957 (if (string-match (concat "\\<" verilog-str) s)
|
|
2958 (if (or (null verilog-pred)
|
|
2959 (funcall verilog-pred s))
|
|
2960 (setq verilog-all (cons s verilog-all)))))
|
|
2961 keyword-list))
|
|
2962
|
|
2963 ;; Function passed to completing-read, try-completion or
|
|
2964 ;; all-completions to get completion on STR. If predicate is non-nil,
|
|
2965 ;; it must be a function to be called for every match to check if this
|
|
2966 ;; should really be a match. If flag is t, the function returns a list
|
|
2967 ;; of all possible completions. If it is nil it returns a string, the
|
|
2968 ;; longest possible completion, or t if STR is an exact match. If flag
|
|
2969 ;; is 'lambda, the function returns t if STR is an exact match, nil
|
|
2970 ;; otherwise.
|
|
2971
|
|
2972 (defun verilog-completion (verilog-str verilog-pred verilog-flag)
|
|
2973 (save-excursion
|
|
2974 (let ((verilog-all nil))
|
|
2975 ;; Set buffer to use for searching labels. This should be set
|
|
2976 ;; within functins which use verilog-completions
|
|
2977 (set-buffer verilog-buffer-to-use)
|
|
2978
|
|
2979 ;; Determine what should be completed
|
|
2980 (let ((state (car (verilog-calculate-indent))))
|
|
2981 (cond ((eq state 'defun)
|
|
2982 (save-excursion (verilog-var-completion))
|
|
2983 (verilog-func-completion 'module)
|
|
2984 (verilog-keyword-completion verilog-defun-keywords))
|
|
2985
|
|
2986 ((eq state 'block)
|
|
2987 (save-excursion (verilog-var-completion))
|
|
2988 (verilog-func-completion 'tf)
|
|
2989 (verilog-keyword-completion verilog-block-keywords))
|
|
2990
|
|
2991 ((eq state 'case)
|
|
2992 (save-excursion (verilog-var-completion))
|
|
2993 (verilog-func-completion 'tf)
|
|
2994 (verilog-keyword-completion verilog-case-keywords))
|
|
2995
|
|
2996 ((eq state 'tf)
|
|
2997 (save-excursion (verilog-var-completion))
|
|
2998 (verilog-func-completion 'tf)
|
|
2999 (verilog-keyword-completion verilog-tf-keywords))
|
|
3000
|
|
3001 (t;--Anywhere else
|
|
3002 (save-excursion (verilog-var-completion))
|
|
3003 (verilog-func-completion 'both)
|
|
3004 (verilog-keyword-completion verilog-separator-keywords))))
|
|
3005
|
|
3006 ;; Now we have built a list of all matches. Give response to caller
|
|
3007 (verilog-completion-response))))
|
|
3008
|
|
3009 (defun verilog-completion-response ()
|
|
3010 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
|
|
3011 ;; This was not called by all-completions
|
|
3012 (if (null verilog-all)
|
|
3013 ;; Return nil if there was no matching label
|
|
3014 nil
|
|
3015 ;; Get longest string common in the labels
|
|
3016 (let* ((elm (cdr verilog-all))
|
|
3017 (match (car verilog-all))
|
|
3018 (min (length match))
|
|
3019 tmp)
|
|
3020 (if (string= match verilog-str)
|
|
3021 ;; Return t if first match was an exact match
|
|
3022 (setq match t)
|
|
3023 (while (not (null elm))
|
|
3024 ;; Find longest common string
|
|
3025 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
|
|
3026 (progn
|
|
3027 (setq min tmp)
|
|
3028 (setq match (substring match 0 min))))
|
|
3029 ;; Terminate with match=t if this is an exact match
|
|
3030 (if (string= (car elm) verilog-str)
|
|
3031 (progn
|
|
3032 (setq match t)
|
|
3033 (setq elm nil))
|
|
3034 (setq elm (cdr elm)))))
|
|
3035 ;; If this is a test just for exact match, return nil ot t
|
|
3036 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
|
|
3037 nil
|
|
3038 match))))
|
|
3039 ;; If flag is t, this was called by all-completions. Return
|
|
3040 ;; list of all possible completions
|
|
3041 (verilog-flag
|
|
3042 verilog-all)))
|
|
3043
|
|
3044 (defvar verilog-last-word-numb 0)
|
|
3045 (defvar verilog-last-word-shown nil)
|
|
3046 (defvar verilog-last-completions nil)
|
|
3047
|
|
3048 (defun verilog-complete-word ()
|
|
3049 "Complete word at current point.
|
|
3050 \(See also `verilog-toggle-completions', `verilog-type-keywords',
|
|
3051 `verilog-start-keywords' and `verilog-separator-keywords'.)"
|
|
3052 (interactive)
|
|
3053 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
|
|
3054 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
|
|
3055 (verilog-str (buffer-substring b e))
|
|
3056 ;; The following variable is used in verilog-completion
|
|
3057 (verilog-buffer-to-use (current-buffer))
|
|
3058 (allcomp (if (and verilog-toggle-completions
|
|
3059 (string= verilog-last-word-shown verilog-str))
|
|
3060 verilog-last-completions
|
|
3061 (all-completions verilog-str 'verilog-completion)))
|
|
3062 (match (if verilog-toggle-completions
|
|
3063 "" (try-completion
|
|
3064 verilog-str (mapcar '(lambda (elm)
|
|
3065 (cons elm 0)) allcomp)))))
|
|
3066 ;; Delete old string
|
|
3067 (delete-region b e)
|
|
3068
|
|
3069 ;; Toggle-completions inserts whole labels
|
|
3070 (if verilog-toggle-completions
|
|
3071 (progn
|
|
3072 ;; Update entry number in list
|
|
3073 (setq verilog-last-completions allcomp
|
|
3074 verilog-last-word-numb
|
|
3075 (if (>= verilog-last-word-numb (1- (length allcomp)))
|
|
3076 0
|
|
3077 (1+ verilog-last-word-numb)))
|
|
3078 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
|
|
3079 ;; Display next match or same string if no match was found
|
|
3080 (if (not (null allcomp))
|
|
3081 (insert "" verilog-last-word-shown)
|
|
3082 (insert "" verilog-str)
|
|
3083 (message "(No match)")))
|
|
3084 ;; The other form of completion does not necessarly do that.
|
|
3085
|
|
3086 ;; Insert match if found, or the original string if no match
|
|
3087 (if (or (null match) (equal match 't))
|
|
3088 (progn (insert "" verilog-str)
|
|
3089 (message "(No match)"))
|
|
3090 (insert "" match))
|
|
3091 ;; Give message about current status of completion
|
|
3092 (cond ((equal match 't)
|
|
3093 (if (not (null (cdr allcomp)))
|
|
3094 (message "(Complete but not unique)")
|
|
3095 (message "(Sole completion)")))
|
|
3096 ;; Display buffer if the current completion didn't help
|
|
3097 ;; on completing the label.
|
|
3098 ((and (not (null (cdr allcomp))) (= (length verilog-str)
|
|
3099 (length match)))
|
|
3100 (with-output-to-temp-buffer "*Completions*"
|
|
3101 (display-completion-list allcomp))
|
|
3102 ;; Wait for a keypress. Then delete *Completion* window
|
|
3103 (momentary-string-display "" (point))
|
|
3104 (delete-window (get-buffer-window (get-buffer "*Completions*")))
|
|
3105 )))))
|
|
3106
|
|
3107 (defun verilog-show-completions ()
|
|
3108 "Show all possible completions at current point."
|
|
3109 (interactive)
|
|
3110 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
|
|
3111 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
|
|
3112 (verilog-str (buffer-substring b e))
|
|
3113 ;; The following variable is used in verilog-completion
|
|
3114 (verilog-buffer-to-use (current-buffer))
|
|
3115 (allcomp (if (and verilog-toggle-completions
|
|
3116 (string= verilog-last-word-shown verilog-str))
|
|
3117 verilog-last-completions
|
|
3118 (all-completions verilog-str 'verilog-completion))))
|
|
3119 ;; Show possible completions in a temporary buffer.
|
|
3120 (with-output-to-temp-buffer "*Completions*"
|
|
3121 (display-completion-list allcomp))
|
|
3122 ;; Wait for a keypress. Then delete *Completion* window
|
|
3123 (momentary-string-display "" (point))
|
|
3124 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
|
|
3125
|
|
3126
|
|
3127 (defun verilog-get-default-symbol ()
|
|
3128 "Return symbol around current point as a string."
|
|
3129 (save-excursion
|
|
3130 (buffer-substring (progn
|
|
3131 (skip-chars-backward " \t")
|
|
3132 (skip-chars-backward "a-zA-Z0-9_")
|
|
3133 (point))
|
|
3134 (progn
|
|
3135 (skip-chars-forward "a-zA-Z0-9_")
|
|
3136 (point)))))
|
|
3137
|
|
3138 (defun verilog-build-defun-re (str &optional arg)
|
|
3139 "Return function/task/module starting with STR as regular expression.
|
|
3140 With optional second arg non-nil, STR is the complete name of the instruction."
|
|
3141 (if arg
|
|
3142 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
|
|
3143 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
|
|
3144
|
|
3145 ;; Function passed to completing-read, try-completion or
|
|
3146 ;; all-completions to get completion on any function name. If
|
|
3147 ;; predicate is non-nil, it must be a function to be called for every
|
|
3148 ;; match to check if this should really be a match. If flag is t, the
|
|
3149 ;; function returns a list of all possible completions. If it is nil
|
|
3150 ;; it returns a string, the longest possible completion, or t if STR
|
|
3151 ;; is an exact match. If flag is 'lambda, the function returns t if
|
|
3152 ;; STR is an exact match, nil otherwise.
|
|
3153
|
|
3154 (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
|
|
3155 (save-excursion
|
|
3156 (let ((verilog-all nil)
|
|
3157 match)
|
|
3158
|
|
3159 ;; Set buffer to use for searching labels. This should be set
|
|
3160 ;; within functins which use verilog-completions
|
|
3161 (set-buffer verilog-buffer-to-use)
|
|
3162
|
|
3163 (let ((verilog-str verilog-str))
|
|
3164 ;; Build regular expression for functions
|
|
3165 (if (string= verilog-str "")
|
|
3166 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
|
|
3167 (setq verilog-str (verilog-build-defun-re verilog-str)))
|
|
3168 (goto-char (point-min))
|
|
3169
|
|
3170 ;; Build a list of all possible completions
|
|
3171 (while (verilog-re-search-forward verilog-str nil t)
|
|
3172 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
|
|
3173 (if (or (null verilog-pred)
|
|
3174 (funcall verilog-pred match))
|
|
3175 (setq verilog-all (cons match verilog-all)))))
|
|
3176
|
|
3177 ;; Now we have built a list of all matches. Give response to caller
|
|
3178 (verilog-completion-response))))
|
|
3179
|
|
3180 (defun verilog-goto-defun ()
|
|
3181 "Move to specified Verilog module/task/function.
|
|
3182 The default is a name found in the buffer around point."
|
|
3183 (interactive)
|
|
3184 (let* ((default (verilog-get-default-symbol))
|
|
3185 ;; The following variable is used in verilog-comp-function
|
|
3186 (verilog-buffer-to-use (current-buffer))
|
|
3187 (default (if (verilog-comp-defun default nil 'lambda)
|
|
3188 default ""))
|
|
3189 (label (if (not (string= default ""))
|
|
3190 ;; Do completion with default
|
|
3191 (completing-read (concat "Label: (default " default ") ")
|
|
3192 'verilog-comp-defun nil t "")
|
|
3193 ;; There is no default value. Complete without it
|
|
3194 (completing-read "Label: "
|
|
3195 'verilog-comp-defun nil t ""))))
|
|
3196 ;; If there was no response on prompt, use default value
|
|
3197 (if (string= label "")
|
|
3198 (setq label default))
|
|
3199 ;; Goto right place in buffer if label is not an empty string
|
|
3200 (or (string= label "")
|
|
3201 (progn
|
|
3202 (goto-char (point-min))
|
|
3203 (re-search-forward (verilog-build-defun-re label t))
|
|
3204 (beginning-of-line)))))
|
|
3205 (defun verilog-showscopes ()
|
|
3206 "list all scopes in this module"
|
|
3207 (interactive)
|
|
3208 (let (
|
|
3209 (buffer (current-buffer))
|
|
3210 (linenum 1)
|
|
3211 (nlines 0)
|
|
3212 (first 1)
|
|
3213 (prevpos (point-min))
|
|
3214 (final-context-start (make-marker))
|
|
3215 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)")
|
|
3216 )
|
|
3217 (with-output-to-temp-buffer "*Occur*"
|
|
3218 (save-excursion
|
|
3219 (message (format "Searching for %s ..." regexp))
|
|
3220 ;; Find next match, but give up if prev match was at end of buffer.
|
|
3221 (while (and (not (= prevpos (point-max)))
|
|
3222 (verilog-re-search-forward regexp nil t))
|
|
3223 (goto-char (match-beginning 0))
|
|
3224 (beginning-of-line)
|
|
3225 (save-match-data
|
|
3226 (setq linenum (+ linenum (count-lines prevpos (point)))))
|
|
3227 (setq prevpos (point))
|
|
3228 (goto-char (match-end 0))
|
|
3229 (let* ((start (save-excursion
|
|
3230 (goto-char (match-beginning 0))
|
|
3231 (forward-line (if (< nlines 0) nlines (- nlines)))
|
|
3232 (point)))
|
|
3233 (end (save-excursion
|
|
3234 (goto-char (match-end 0))
|
|
3235 (if (> nlines 0)
|
|
3236 (forward-line (1+ nlines))
|
|
3237 (forward-line 1))
|
|
3238 (point)))
|
|
3239 (tag (format "%3d" linenum))
|
|
3240 (empty (make-string (length tag) ?\ ))
|
|
3241 tem)
|
|
3242 (save-excursion
|
|
3243 (setq tem (make-marker))
|
|
3244 (set-marker tem (point))
|
|
3245 (set-buffer standard-output)
|
|
3246 (setq occur-pos-list (cons tem occur-pos-list))
|
|
3247 (or first (zerop nlines)
|
|
3248 (insert "--------\n"))
|
|
3249 (setq first nil)
|
|
3250 (insert-buffer-substring buffer start end)
|
|
3251 (backward-char (- end start))
|
|
3252 (setq tem (if (< nlines 0) (- nlines) nlines))
|
|
3253 (while (> tem 0)
|
|
3254 (insert empty ?:)
|
|
3255 (forward-line 1)
|
|
3256 (setq tem (1- tem)))
|
|
3257 (let ((this-linenum linenum))
|
|
3258 (set-marker final-context-start
|
|
3259 (+ (point) (- (match-end 0) (match-beginning 0))))
|
|
3260 (while (< (point) final-context-start)
|
|
3261 (if (null tag)
|
|
3262 (setq tag (format "%3d" this-linenum)))
|
|
3263 (insert tag ?:)))))))
|
|
3264 (set-buffer-modified-p nil))))
|
134
|
3265
|
|
3266 (defun verilog-submit-bug-report ()
|
|
3267 "Submit via mail a bug report on lazy-lock.el."
|
|
3268 (interactive)
|
|
3269 (let ((reporter-prompt-for-summary-p t))
|
|
3270 (reporter-submit-bug-report
|
|
3271 "verilog-mode-bugs@silicon-sorcery.com"
|
|
3272 (concat "verilog-mode v" (substring verilog-mode-version 12 -3))
|
|
3273 '(verilog-indent-level
|
|
3274 verilog-indent-level-module
|
|
3275 verilog-indent-level-declaration
|
|
3276 verilog-indent-level-behavorial
|
|
3277 verilog-case-indent
|
|
3278 verilog-auto-newline
|
|
3279 verilog-auto-indent-on-newline
|
|
3280 verilog-tab-always-indent
|
|
3281 verilog-auto-endcomments
|
|
3282 verilog-minimum-comment-distance
|
|
3283 verilog-indent-begin-after-if
|
|
3284 verilog-auto-lineup)
|
|
3285 nil nil
|
|
3286 (concat "Hi Mac,
|
|
3287
|
|
3288 I want to report a bug. I've read the `Bugs' section of `Info' on
|
|
3289 Emacs, so I know how to make a clear and unambiguous report. To get
|
|
3290 to that Info section, I typed
|
|
3291
|
|
3292 M-x info RET m " invocation-name " RET m bugs RET
|
|
3293
|
|
3294 Before I go further, I want to say that Verilog mode has changed my life.
|
|
3295 I save so much time, my files are colored nicely, my co workers respect
|
|
3296 my coding ability... until now. I'd really appreciate anything you
|
|
3297 could do to help me out with this minor deficiency in the product.
|
|
3298
|
|
3299 To reproduce the bug, start a fresh Emacs via " invocation-name "
|
|
3300 -no-init-file -no-site-file'. In a new buffer, in verilog mode, type
|
|
3301 the code included below.
|
|
3302
|
|
3303 Given those lines, I expected [[Fill in here]] to happen;
|
|
3304 but instead, [[Fill in here]] happens!.
|
|
3305
|
|
3306 == The code: =="))))
|
|
3307
|
98
|
3308 ;;; verilog.el ends here
|