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

问题描述

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

In Eclipse, highlighting multiple rows and pressing 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-区域或线函数作为回答,而不是Emacs附带的函数 comment-or-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.

推荐答案

我结束了来自 juanleon Ehvince

以下是最终产品:

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

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

08-19 13:33