【问题标题】:Show enclosing #ifdef blocks in Emacs在 Emacs 中显示封闭的 #ifdef 块
【发布时间】:2015-08-08 11:56:32
【问题描述】:

是否可以在 Emacs 中显示当前的#ifdef 块(例如在模式行中)?

例如:

#ifdef A
... | #cursor position num. 1
#ifdef !B & C
...
#else /* !B & C */
foo(); | #cursor position num. 2
#endif /* !B & C */
#endif /* A */

如果我将光标放在位置 1,它应该在模式行中显示 A 并在位置 2 显示 A & !(!B & C)。 我已经在使用 hide-if-def 模式。但有时我需要处理所有#ifdef 块。

【问题讨论】:

  • 不是答案,但使用C-c C-u 跳到最近的封闭#ifdef 非常有用。然后,您可以使用C-u C-<SPC> 回到原来的位置。

标签: emacs macros elisp


【解决方案1】:

WhichFunction 并不总是成功的,但它应该可以很好地与 C/C++ 代码一起使用。此自定义将为识别方案添加一个新功能,当您在 #ifdef 块内时​​会告诉您。

(require 'which-func)
(which-function-mode 1)
(defun name-of-current-conditional ()
  "rather inelegant coding, but it works"
  (interactive)
  (let (outer)
    (condition-case nil
        (dotimes (myv 10)
          (save-excursion
            (c-up-conditional (1+ myv))
            (setq outer (buffer-substring-no-properties 
                              (line-beginning-position)
                              (line-end-position)))))
      (error nil))
    outer))
(setq which-func-functions '(name-of-current-conditional))

【讨论】:

  • 感谢您的工作,但我认为它仅在使用简单 ifdef 的情况下才有用。当我在“else”下时,它显示“ifdef A”。在这种情况下,我希望看到“ifdef !A”或“ifndef A”。它也不处理复杂的“ifdefs”。我的项目中有很多嵌套的 ifdef。也许拥有这样的功能几乎是不可能的。
  • @folq - 是的,您希望代码在您离开时对子句执行实际的布尔逻辑。您可以使用我的代码开始将它们组合在一起,但这比我想要承担的任务更大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多