类似于Eclipse的线条在Emacs中的评论

bit*_*ips 10 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-regionEmacs附带的函数.

我觉得这值得一提,因为前者似乎更多地反映了Eclipse中的注释行如何工作.也就是说,如果不存在区域,则评论该点所在的线.

bit*_*ips 8

我最后将juanleon和Ehvince的答案中的部分组合在一起,以获得更像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)))
Run Code Online (Sandbox Code Playgroud)

如果有任何问题,请告诉我.


Clé*_*ent 5

请注意,emacs 25具有comment-line绑定的新功能C-x C-;.