Emacs 中类似 Eclipse 的行注释 [英] Eclipse-like Line Commenting in Emacs

查看:25
本文介绍了Emacs 中类似 Eclipse 的行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Eclipse, highlighting multiple rows and pressing Ctrl+/ comments each of the lines of the selection.

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

Emacs has a function comment-or-uncomment-region that is close what I want, but behaves differently if the region only partially covers the lines I'm trying to comment.

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

Is there any way I make a function similar to comment-or-uncomment-region, but have it comment each of the lines of the region regardless of how the region is selected?

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

In other words, I want the function to act as though the region occupies the whole line as long as the region includes that line, so it behaves as Eclipse's selection commenting does.

我实际上使用的是作为答案提到的 comment-or-uncomment-region-or-line 函数而不是 comment-or 函数Emacs 自带的 -uncomment-region.

I am actually using the comment-or-uncomment-region-or-line function mentioned as an answer instead of the function comment-or-uncomment-region that comes with Emacs.

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

I feel as though this is worth mentioning because the former seems to reflect how the line commenting works in Eclipse more. That is, the line the point is on is commented if no region exists.

推荐答案

我最终将 juanleonEhvince 的答案的部分结合起来,以获得一些只是有点像 Eclipse 的评论.

I ended up combining parts from juanleon's and Ehvince's answers to get something just a little more like Eclipse's commenting.

这是最终产品:

(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)))

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

Please let me know if anything is wrong with it.

这篇关于Emacs 中类似 Eclipse 的行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆