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

时间:2013-11-18 06:59:06

标签: eclipse emacs comments elisp

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

Emacs的函数comment-or-uncomment-region与我想要的一样接近,但如果该区域仅部分覆盖我试图评论的行,则表现不同。

有没有办法让我的函数类似于comment-or-uncomment-region,但是不管区域是如何被选中的,它都会评论该区域的每一行?

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

编辑:我实际上使用了comment-or-uncomment-region-or-line函数作为答案,而不是Emacs附带的函数comment-or-uncomment-region

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

7 个答案:

答案 0 :(得分: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)))

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

答案 1 :(得分:5)

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

答案 2 :(得分:3)

这里有一个功能可以完成您所描述的内容:

(defun comment-or-uncomment-region-eclipse-style (beg end &optional arg)
  (interactive "*r\nP")
  (comment-or-uncomment-region
   (save-excursion
     (goto-char beg)
     (beginning-of-line)
     (point))
   (save-excursion
     (goto-char end)
     (end-of-line)
     (point)) arg))

答案 3 :(得分:3)

FWIW,我不使用comment-or-uncomment-region。我改用 comment-region 。它类似,但它让决定是否取消注释或评论。它允许您嵌套注释,而不是在已经注释掉的情况下自动取消注释该区域。使用数字前缀arg,它在Lisp中使用了许多注释开始字符(例如;;;;;;,...。使用简单的C-u前缀arg,它会取消注释。我将它绑定到C-x C-;

无论如何,我认为这可以做到你想要的,使用comment-region(参见一般行为):

(defun comment-region-lines (beg end &optional arg)
  "Like `comment-region', but comment/uncomment whole lines."
  (interactive "*r\nP")
  (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
  (let ((bol  (save-excursion (goto-char beg) (line-beginning-position)))
        (eol  (save-excursion (goto-char end) (line-end-position))))
    (comment-region bol end arg)))

;; Suggested binding
(define-key ctl-x-map [(control ?\;)] 'comment-region-lines)

保存并恢复该区域。如果只选择了一行的一部分,它就可以工作。我甚至可以自己使用它(这说的很多,因为我对这种事情有很好的习惯)。

答案 4 :(得分:1)

与Juanleon的解决方案相比,我添加了一个事实,即如果你没有选择一个区域,它将(un)评论当前行并转到下一行(而不是基于你没有看到的标记做某事):

  (defun comment-or-uncomment-region-or-line ()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
    (if (region-active-p)
        (setq beg (region-beginning) end (region-end))
        (setq beg (line-beginning-position) end (line-end-position)))
    (comment-or-uncomment-region beg end)
    (next-line)))
;; bind it to F7:
(global-set-key (kbd "<f7>")'comment-or-uncomment-region-or-line)

取自:Emacs comment/uncomment current line

答案 5 :(得分:1)

有一个文件提供以下

(defun ar-comment-or-uncomment-lor (&optional copy beg end)
  "Comment line or region, unless it's already commented:
uncomment then.

...&#34;     ...

之后光标位于下一行,允许重复执行。

使用 C-u 复制当前行并将其作为上述注释插入 - 从而在编辑时提醒先前的状态。

在此处获取:

https://github.com/andreas-roehler/werkstatt/blob/master/ar-comment-lor.el

答案 6 :(得分:1)

这里对Ehvince的功能略有改动,如果文字被注释掉,它只会前进到下一行。即,如果取消注释文本,通常需要保留光标。

(defun comment-or-uncomment-region-or-line ()
  "Comments or uncomments the region or the current line if there's no active region."
  (interactive)
  (let (beg end)
    (if (region-active-p)
        (setq beg (region-beginning) end (region-end))
      (setq beg (line-beginning-position) end (line-end-position)))
    (comment-or-uncomment-region beg end)
    (when (comment-only-p beg end)
        (next-logical-line))))
相关问题