parathesis matching in emacs-verilog mode 
Author Message
 parathesis matching in emacs-verilog mode


(begin--end, task--endtask ...) in emacs one week ago. I did some
enhancement to ease of use and fixed some bugs. Please get following
code and try it. If you do cut and paste, please note some lines are
very long. You need correct before use it

Have Fun

Yanming

[ .verilog.opt 9K ]
;;  SYNOPSIS: emacs'verilog mode enhancements, bounce on begin/end/etc.
;;  GPL(C) Mohsin_Ahmed //www.cs.albany.edu/~mosh      v0.9 991007

;;  FILE:     moshveri.el
;;  USAGE:    put this in your .emacs:
;; (load-library "verilog-mode.el")
;; (load-file "PATH_TO_THE_FILE/ve-mod.el")
;; (setq auto-mode-alist (append auto-mode-alist '(
;;       ("\\.v$"  . verilog-mode))))
;;
;; Change history:

;;

;;                                                 10-22-1999
;;      o Change the matching behaviour to find the closest
;;        parathesis, so the cousor do not need to be placed on key words
;;      o fix bugs to move cross the /* */ block
;;      o Added function to move the parathesis backward
;;      o Added function to move the parathesis forward
;;
;;    Install:
;;      put following line in .emacs.
;;      (load-options-file  "moshveri.el")
;;      (add-hook 'verilog-mode-hook 'moshveri-mode)
;;    Use:
;;      Hit F9, F10, F11, F12 key
;;
;; bug found:
;;      skip // comment not working properly sometimes.

(defun moshveri-mode ()
  "verilog mode enhancements"
  (interactive)
  (modify-syntax-entry ?_ "w"   )    ; Underscore is a word.
  (local-set-key [(control down)]    'moshveri-next-func)
  (local-set-key [(control up  )]    'moshveri-prev-func)
;;  (local-set-key [?\C-x ?w]          'moshveri-which-func)
;;  (local-set-key [?\C-=]             'moshveri-match-paren)
;;  (local-set-key [kp-space]          'moshveri-match-paren)
  (local-set-key [f9]               'moshveri-which-func)
  (local-set-key [f10]              'moshveri-match-paren)
  (local-set-key [f11]              'moshveri-match-backward)
  (local-set-key [f12]              'moshveri-match-forward)
)

(defvar moshveri-module-re (concat
   "^\\(module\\|primitive\\)[ \t]+\\([A-Za-z_0-9]+\\)?"
  )
  "Regexp matching verilog modules used by moshveri-which-func also."
  )

(defun moshveri-prev-func ()
  "goto last verilog function"
  (interactive)
  (beginning-of-line)
  (if (re-search-backward moshveri-module-re 0 t)
      nil
    (goto-char 0))
  (moshveri-which-func)
  )

(defun moshveri-next-func ()
  "goto next verilog function"
  (interactive)
  (end-of-line)
  (if (re-search-forward moshveri-module-re (point-max) t)
      nil
    (goto-char (point-max))
  )
  (moshveri-which-func)
)

(defvar moshveri-parens (concat
  "\\<\\("
  "begin"         "\\|"
  "end"           "\\|"
  "module"        "\\|"
  "endmodule"     "\\|"
  "table"         "\\|"
  "endtable"      "\\|"
  "task"          "\\|"
  "endtask"       "\\|"
  "fork"          "\\|"
  "join"          "\\|"
  "case"          "\\|"
  "endcase"       "\\|"
  "primitive"     "\\|"
  "endprimitive"  "\\|"
  "specify"       "\\|"
  "endspecify"  
  "\\)\\>"        "\\|"
  "/[\\*]+"        "\\|"
  "[\\*]+/"
))

(defun moshveri-match-paren (&optional findtok count token)
  "Goto matching verilog begin/end etc.
   Bound to \\[moshveri-match-paren]."
  (interactive)
  (cond

   ((equal findtok "begin")
    (while (> count 0)
      (re-search-backward moshveri-parens (point-min))
      (setq token (match-string 1))
      (cond
       ((comment-shadow nil) nil)
       ;; use looking at since may match ..****/
        ;          ((equal token  "*/")           (search-backward "/*"))
       ((looking-at "\\*/") (search-backward "/*"))
       ((equal token  "end")          (setq count (+ count 1)))
       ((equal token  "endtable")     (setq count (+ count 1)))
       ((equal token  "endmodule")    (setq count (+ count 1)))
       ((equal token  "endprimitive") (setq count (+ count 1)))
       ((equal token  "endspecify")   (setq count (+ count 1)))
       ((equal token  "endcase")      (setq count (+ count 1)))
       ((equal token  "join")         (setq count (+ count 1)))
       ((equal token  "endtask")      (setq count (+ count 1)))
       ((equal token  "begin")        (setq count (- count 1)))
       ((equal token  "module")       (setq count (- count 1)))
       ((equal token  "primitive")    (setq count (- count 1)))
       ((equal token  "table")        (setq count (- count 1)))
       ((equal token  "specify")      (setq count (- count 1)))
       ((equal token  "case")         (setq count (- count 1)))
       ((equal token  "fork")         (setq count (- count 1)))
       ((equal token  "task")         (setq count (- count 1)))
       )
      ))

   ((equal findtok "end")
    (while (> count 0)
      (re-search-forward moshveri-parens (point-max))
      (setq token (match-string 1))
      (backward-char 1)
      (cond
 ;      ((comment-shadow t) nil)
       ;; use look since /* matches /*****...
       ;;((equal token  "/*")           (search-forward "*/"))
       ;; this is very trick part to match /*, Actually it matches /********...
       ((looking-at "\\*")  (search-forward  "*/"))
       ((equal token  "begin")        (setq count (+ count 1)))
       ((equal token  "module")       (setq count (+ count 1)))
       ((equal token  "primitive")    (setq count (+ count 1)))
       ((equal token  "table")        (setq count (+ count 1)))
       ((equal token  "specify")      (setq count (+ count 1)))
       ((equal token  "case")         (setq count (+ count 1)))
       ((equal token  "fork")         (setq count (+ count 1)))
       ((equal token  "task")         (setq count (+ count 1)))
       ((equal token  "end")          (setq count (- count 1)))
       ((equal token  "endmodule")    (setq count (- count 1)))
       ((equal token  "endprimitive") (setq count (- count 1)))
       ((equal token  "endtable")     (setq count (- count 1)))
       ((equal token  "endspecify")   (setq count (- count 1)))
       ((equal token  "endcase")      (setq count (- count 1)))
       ((equal token  "join")         (setq count (- count 1)))
       ((equal token  "endtask")      (setq count (- count 1)))
       )
      )
    (backward-word 1)
    )

   ((looking-at "\\<\\(begin\\|module\\|primitive\\|case\\|specify\\|table\\|task\\|fork\\)\\>")
    (end-of-line)
    (moshveri-match-paren "end" 1)
   )
   ((looking-at "\\<\\(end\\|join\\)" )
    (backward-char)
    (moshveri-match-paren "begin" 1)
   )

   ;; Match /* comments */
   ;;((looking-at "/\\*") (search-forward  "*/") (backward-char 2))
   ;;((looking-at "\\*/") (search-backward "/*"))

   ;; match parathesis
   ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
   ((looking-at "\\s\)") (forward-char 1) (backward-list 1))

   ((moshveri-match-paren "end" 1))

;; I hate to change window

   ;; If nothing switch windows.
   ;; Single window?
   ((eq (count-windows) 1) (exchange-point-and-mark))
   ;; go to other window.

   ;; it is illegal code here, however, it give me a bell, that is what I want
   (not_matched)

;   (t (other-window '1))

  )
)

(defun comment-shadow (forward)
  (interactive)
  "If we are in // comment shadow move out of it, return // comment position"
  (let (eol comment-at)
    (save-excursion
      (end-of-line)
      (setq eol (point))
      (beginning-of-line)
      (setq comment-at (search-forward "//" eol 'noerror))
    )
    (if comment-at
     (progn
       (setq comment-at (- comment-at 2))
       (if (> (point) comment-at) ;; inside comment?
           (if forward (forward-line 1) (goto-char comment-at))
         (setq comment-at nil)    ;; outside comment
       )
    ))
    comment-at     ;; return where we were inside a comment.
))

;; =========================================================
(defvar moshveri-func-name "none" "Name of function for cut/paste.")

(defun moshveri-which-func (&optional moveit)
  "show and color the current function name in which the point is located.
   move point if moveit."
  (interactive)
  (let (mark1 mark2)
    (save-excursion
      (cond      
       ((eq major-mode 'verilog-mode)
        (end-of-line)
        (re-search-backward moshveri-module-re)
        (setq mark1 (match-beginning 2))
        (setq mark2 (match-end       2))
        )
       )
      )
    ;; Now return the function name if mark1 and mark2 are set.
    ;; else return an empty string.
    (if (and mark1 mark2)
        (progn
          (setq moshveri-func-name (buffer-substring mark1 mark2)) ; Save "foo"
          (message "Currently in: %s()" moshveri-func-name) ; Print foo()
          (if moveit (goto-char mark1)) ; goto to start.
          moshveri-func-name            ; Return name.
          )
      ""
      )
    )
)

(defun moshveri-match-backward ()
  "match begining of verilog parathesis"
  (interactive)
  (re-search-backward moshveri-parens (point-min))
  (cond
   ((comment-shadow nil) nil)
   ((looking-at "\\*/") (search-backward "/*"))
   )
)

(defun moshveri-match-forward ()
  "match ends of verilog parathesis"
  (interactive)
  (looking-at "\\<\\(begin\\|module\\|primitive\\|case\\|specify\\|table\\|task\\|fork\\)\\>")
  (forward-word 1)  
  (re-search-forward moshveri-parens (point-max))
  (backward-char 1)
  (cond
   ((comment-shadow t) nil)
   ;; this is very trick part to match /*, Actually it matches /********...
   ( (looking-at "\\*")  (search-forward  "*/"))
   ;; if not /* then back one word
   ((not(looking-at "\\*")) (backward-word 1))
   )
)

(provide 'moshveri)

;;(add-hook 'verilog-mode-hook 'moshveri-mode)
;; EOF



Tue, 09 Apr 2002 03:00:00 GMT  
 parathesis matching in emacs-verilog mode

Quote:

> This is a multi-part message in MIME format.
> --------------620F3E2BCEC021068B77F7B3
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit


> (begin--end, task--endtask ...) in emacs one week ago. I did some
> enhancement to ease of use and fixed some bugs. Please get following
> code and try it. If you do cut and paste, please note some lines are
> very long. You need correct before use it

> Have Fun

Per the emacs standard, C-M-f is how you move forward across a
balanced expression; and C-M-b is how you move back across such a
thing.  

These two key strokes (should) work in every major emacs editing mode.

In particular, they also work in the verilog-mode.  
(ref <http://www.surefirev.com/verilog-mode.html> )

These are reasonable key strokes to use, as C-f and C-b move forward
and backward a character; and M-f and M-b move forward and backward a
word.

Hence, this addition is really already part of the emacs verilog-mode.
Or am I missing something?

Ref:
C-M-f runs `forward-sexp'

`forward-sexp' is an interactive compiled Lisp function
  -- loaded from "/home/sandbox/mac/h/xemacs-21.1.4/lisp/lisp.elc"
(forward-sexp &optional ARG)

Documentation:
Move forward across one balanced expression (sexp).
With argument, do it that many times.  Negative arg -N means
move backward across N balanced expressions.

--

  _    //     Verisity Design              408-374-4100 x 100
   \  //      <http://www.verisity.com>    408-374-4174 FAX
    \//erisity                 Formerly SureFire Verification
--------------------------------------------------------------
Get my verilog emacs mode from
                  <http://www.surefirev.com/verilog-mode.html>



Sun, 19 May 2002 03:00:00 GMT  
 parathesis matching in emacs-verilog mode
You are right,  the my function almost does the same thing as standard with
one exception in which I defined a key to move back and forth at the same
set of parenthesis.

regards,

Yanming

Quote:

> Per the emacs standard, C-M-f is how you move forward across a
> balanced expression; and C-M-b is how you move back across such a
> thing.

> These two key strokes (should) work in every major emacs editing mode.

> In particular, they also work in the verilog-mode.
> (ref <http://www.surefirev.com/verilog-mode.html> )

> These are reasonable key strokes to use, as C-f and C-b move forward
> and backward a character; and M-f and M-b move forward and backward a
> word.

> Hence, this addition is really already part of the emacs verilog-mode.
> Or am I missing something?



Mon, 20 May 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Emacs Verilog mode & 2001

2. emacs verilog mode?

3. Extension to Emacs Verilog-Mode ?

4. Emacs verilog mode

5. verilog-mode for emacs, `define and /*autosense*/

6. emacs & verilog-mode

7. Emacs verilog mode

8. Emacs verilog mode ands Silos?

9. Emacs Verilog-mode problems

10. Verilog mode for (x)emacs???

11. Emacs verilog mode problem

12. emacs verilog mode (or updated LSE files)

 

 
Powered by phpBB® Forum Software