使用 emacs 折叠/识别 verilog 代码中当前的 `ifdef 块

发布于 2024-12-24 02:26:39 字数 670 浏览 1 评论 0原文

我的工作涉及浏览包含“ifdef 块”的长 verilog 代码。我的主要编辑器是 emacs。以下是一个典型示例:

module Foo(
...
...
);

// Inputs, Outputs
...

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

如您所见,foo <= 1'b1; 位于 ifdef CONDITION_1else 块中 假设我的点位于 foo <= 1'b1; 行上,有什么方法可以直接移动到 ifdef CONDITION_1 行或折叠代码,以便我可以看条件1?我尝试使用向后增量搜索,但在这种情况下,我最终得到 ifdef CONDITION_2 我尝试使用 hide-ifdef-mode,但它识别的是 #ifdef 而不是 `ifdef。这些块不使用括号。所以使用 CMu 没有帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写下不归期 2024-12-31 02:26:39

这将做到这一点,尽管对于上面的示例,它将停止在

`else // CONDITION_1

语句处,因为这是包含 foo 赋值的内容:

(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"))))

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"))))
感情洁癖 2024-12-31 02:26:39

elisp 常量 hif-cpp-prefix 控制 hide-ifdef-mode 使用的基本语法。我猜您可以将其定义为如下所示。 (警告,这未经测试,因为我自己不使用 verilog。)

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

请注意,这是使用 defconst 而不是 defvar 定义的,因此 的编译版本hide-ifdef-mode 可能仍使用原始值。将未编译的文件加载到 Emacs 中可能会解决这个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文