Emacs 中类似 Eclipse 的行注释

标签 eclipse emacs comments elisp

在 Eclipse 中,突出显示多行并按 Ctrl+/注释选择的每一行。

Emacs 有个功能 comment-or-uncomment-region这与我想要的很接近,但如果该区域仅部分覆盖我要评论的行,则行为会有所不同。

有什么办法可以让我创建一个类似于 comment-or-uncomment-region 的函数吗? ,但是不管区域是如何选择的,它是否注释了区域的每一行?

换句话说,只要该区域包含该行,我希望该函数就好像该区域占据整行一样,因此它的行为与 Eclipse 的选择注释一样。

编辑:我实际上正在使用 comment-or-uncomment-region-or-line作为答案提到的函数而不是函数 comment-or-uncomment-region Emacs 自带的。

我觉得这似乎值得一提,因为前者似乎更多地反射(reflect)了行注释在 Eclipse 中的工作方式。也就是说,如果不存在区域,则对该点所在的行进行注释。

最佳答案

我最终组合了来自 的部分juanleon 的和 艾文斯 的答案以获得更像 Eclipse 评论的东西。

这是最终产品:

(defun comment-eclipse ()
  (interactive)
  (let ((start (line-beginning-position))
        (end (line-end-position)))
    (when (or (not transient-mark-mode) (region-active-p))
      (setq start (save-excursion
                    (goto-char (region-beginning))
                    (beginning-of-line)
                    (point))
            end (save-excursion
                  (goto-char (region-end))
                  (end-of-line)
                  (point))))
    (comment-or-uncomment-region start end)))

如果有什么问题,请告诉我。

关于Emacs 中类似 Eclipse 的行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20041904/

相关文章:

emacs - 如何在 Emacs 中转到特定数字字符?

java - 为什么 tomcat 服务器在 eclipse 中无法正常启动?

java - 如何更改 Java 评论模板?

java - 停止 eclipse 项目中的服务器

documentation - 忽略Doxygen注释 block 中的行

javascript - 为什么 Javascript 会忽略单行 HTML 注释?

javascript - Facebook 评论插件如何工作?

java - 用于生成 Java 源代码的 Eclipse UML 插件

linux - emacs23 到 emacs24 : imap shell "Opening nnimap server...failed: Pseudo-terminal will not be allocated ..."

emacs - nrepl 每 1024 个字符插入 <newline>