C 模式下的 Emacs 注释区

发布于 2024-08-07 04:41:01 字数 503 浏览 9 评论 0原文

在GNU Emacs中,有没有一个好的方法可以将C模式下的comment-region命令从更改

/* This is a comment which extends  */
/* over more than one line in C. */

/* This is a comment which extends
   over more than one line in C. */

?我已经尝试过

(setq comment-multi-line t)

,但这没有帮助。 Emacs 手册中有一个关于多行注释的 部分,但它没有提及任何内容。

In GNU Emacs, is there a good way to change the comment-region command in C mode from

/* This is a comment which extends  */
/* over more than one line in C. */

to

/* This is a comment which extends
   over more than one line in C. */

? I have tried

(setq comment-multi-line t)

but this does not help. There is a section on multi-line comments in the Emacs manual, but it does not mention anything.

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

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

发布评论

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

评论(2

风尘浪孓 2024-08-14 04:41:01

从 Emacs 21 开始,出现了一个名为 的模块'newcomment,它具有不同的注释样式(请参阅变量'comment-styles。此设置接近您想要的:(

(setq comment-style 'multi-line)

注意:您可能应该在 'c-mode-hook 中进行该设置)

但是,没有任何设置使注释看起来像您想要的那样。

我看到获得您想要的内容的最简单方法是添加此技巧:

(defadvice comment-region-internal (before comment-region-internal-hack-ccs activate)
  "override 4th argument to be just spaces"
  (when (eq major-mode 'c-mode)  ; some condition here
    (let ((arg (ad-get-arg 4)))
      (when arg
        (ad-set-arg 4 (make-string (length arg) ?\ ))))))

comment-style 的当前设置总是在注释行前加上“ * ”(如果不是整个“ /* ”),

如果您没有 Emacs 21,我想您可以简单地下载 < a href="http://cvs.savannah.gnu.org/viewvc/*checkout*/emacs/lisp/newcomment.el?root=emacs" rel="noreferrer">newcomment.el 我不知道它在早期版本的 Emacs 中是否能正常工作,但可能值得一试,尽管升级 Emacs 会是一个更好的解决方案。

我的黑客破坏了'uncomment-region。正确的修复方法是更改​​ 'comment-padright。这需要更多的研究,以免破坏其他东西。上述 hack 仅更改 'c-mode 中的行为(根据您的喜好调整条件)。

Since Emacs 21, there's been a module named 'newcomment, which has different comment styles (see the variable 'comment-styles. This setting gets close to what you want:

(setq comment-style 'multi-line)

(Note: you should probably make that setting in 'c-mode-hook).

However, none of the settings make the comments look like what you want.

The easiest way I saw to get what you want is to add this hack:

(defadvice comment-region-internal (before comment-region-internal-hack-ccs activate)
  "override 4th argument to be just spaces"
  (when (eq major-mode 'c-mode)  ; some condition here
    (let ((arg (ad-get-arg 4)))
      (when arg
        (ad-set-arg 4 (make-string (length arg) ?\ ))))))

The current settings for comment-style always prefix the comment lines with " * " (if not the whole " /* ").

If you don't have Emacs 21, I suppose you could simply download newcomment.el from the repository. I don't know if it works as-is in earlier versions of Emacs, but it might be worth a shot, though upgrading Emacs would be a better solution.

My hack breaks the 'uncomment-region. A proper fix would be to change 'comment-padright. That would take a little more research so as not to break other things. The above hack only changes behavior in 'c-mode (adjust the condition to your liking).

各自安好 2024-08-14 04:41:01

我能找到的最接近内置注释支持的方法是将 comment-style 设置为 multi-line,这将产生以下结果:

/* This is a comment which extends
 * over more than one line in C. */

如果这还不够接近,查看 newcomment.el 并根据需要定义您自己的注释函数。

Closest I could find with the built-in commenting support is if you set comment-style to multi-line, which will produce this:

/* This is a comment which extends
 * over more than one line in C. */

If that isn't close enough, take a look at newcomment.el and define your own commenting functions as appropriate.

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