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

Pulimon
  • 1,776
  • 4
  • 31
  • 45

2 Answers2

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.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
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"))))
scottfrazer
  • 17,079
  • 4
  • 51
  • 49
  • Thanks a million bro... All the 'else's have been commented with the corresponding CONDITIONs so its ok even if it stops at 'else. :) – Pulimon Jan 04 '12 at 04:45
  • 1
    I'd think you'd want that, otherwise how would you know if it was inside the 'ifdef part or the 'else part :) – scottfrazer Jan 04 '12 at 12:55
  • 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. – scottfrazer Jan 04 '12 at 13:38