1

My work invovles browsing through long verilog codes which incorporates `ifdef blocks. My primary editor is emacs. Following is a typical example:

module Foo(
...
...
);

// Inputs, Outputs
...

`ifdef CONDITION_1
...
...    
`else // CONDITION_1
...
...    
`ifdef CONDITION_2
...
...
`endif //CONDITION_2
...
...
     foo <= 1'b1;
...
...
`endif // CONDITION_1

As you can see foo <= 1'b1; is in the else block of ifdef CONDITION_1 Assuming that my point is on the line foo <= 1'b1; is there any way by which I could directly move to the line ifdef CONDITION_1 or fold the code so that I can see CONDITION1? I tried using backward incremental search but in that case I end up at ifdef CONDITION_2 i tried using the hide-ifdef-mode but it identifies #ifdef instead of `ifdef. These blocks do not use parenthesis. And so using C-M-u doesn't help

2 Answers 2

2

This will do it, although for your example above it will stop at the

`else // CONDITION_1

statement since that is what is enclosing the foo assignment:

(defun my-verilog-up-ifdef ()
  "Go up `ifdef/`ifndef/`else/`endif macros until an enclosing one is found."
  (interactive)
  (let ((pos (point)) (depth 0) done)
    (while (and (not done)
           (re-search-backward "^\\s-*`\\(ifdef\\|ifndef\\|else\\|endif\\)" nil t))
      (if (looking-at "\\s-*`endif")
          (setq depth (1+ depth))
        (if (= depth 0)
            (setq done t)
          (when (looking-at "\\s-*`if")
            (setq depth (1- depth))))))
    (unless done
      (goto-char pos)
      (error "Not inside an `ifdef construct"))))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a million bro... All the 'else's have been commented with the corresponding CONDITIONs so its ok even if it stops at 'else. :)
I'd think you'd want that, otherwise how would you know if it was inside the 'ifdef part or the 'else part :)
Also in case it wasn't obvious: If a 'else isn't commented, just execute the command again from there and it will jump to the 'ifdef.
2

The elisp constant hif-cpp-prefix controls the basic syntax that hide-ifdef-mode use. I guess that you could define it to something like the following. (Warning, this is untested, as I don't use verilog myself.)

(setq hif-cpp-prefix "\\(^\\|\r\\)[ \t]*\\(#\\|`\\)[ \t]*")

Note that this is defined using defconst rather than defvar, so the compiled version of hide-ifdef-mode might still use the original value. Loading the uncompiled file into Emacs would probably solve this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.