【问题标题】:Eclipse-like Line Commenting in EmacsEmacs 中类似 Eclipse 的行注释
【发布时间】:2013-12-01 06:08:38
【问题描述】:

在 Eclipse 中,高亮显示多行并按下 Ctrl+/ 会匹配所选内容的每一行。

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

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

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

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

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

【问题讨论】:

    标签: eclipse emacs comments elisp


    【解决方案1】:

    我最终将 juanleonEhvince 的答案的部分结合起来,得到了更像 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)))
    

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

    【讨论】:

    • 此功能适用于瞬态标记模式的用户。它不适用于“旧式区域”的用户。如果您通过(or (not transient-mark-mode) (region-active-p)) 更改条件,则两种用户都可以使用。
    • 感谢您的提醒!而且我不知道瞬态标记模式是一种更现代的东西。
    【解决方案2】:

    请注意,emacs 25 有一个新函数 comment-line 绑定到 C-x C-;

    【讨论】:

    • @RickyRobinson Emacs 25 将在接下来的几周内到期,如果不是几天的话:)
    【解决方案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))
    

    【讨论】:

    • 这行得通,但我遇到了意想不到的副作用。当我再次执行该命令时,来自前一个区域的行都未注释。此外,移动该点并再次执行命令将从该区域开始的行注释/取消注释到该点。如果没有选择区域,是否有办法改变行为,例如只评论当前行?同时,我将尝试查看其他地方提到的comment-or-uncomment-region-or-line 以获得指导。
    【解决方案4】:

    FWIW,我不使用comment-or-uncomment-region。我改用 comment-region。这很相似,但它让 决定是取消评论还是评论。它允许您嵌套 cmets,而不是在区域已被注释掉时自动取消注释。使用数字前缀 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)
    

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

    【讨论】:

      【解决方案5】:

      与 Juanleon 的解决方案相比,我补充说,如果您不选择区域,它将(取消)注释当前行并转到下一行(而不是根据您看不到的标记做某事):

        (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

      【讨论】:

      • 我没有提到我实际上使用的是这个函数,而不是 Emacs 自带的comment-or-uncomment-region。我想避免的行为仍然会发生这种情况,因为如果我不选择整行,它不会像我想要的那样评论。
      • 我正在使用邪恶模式,所以我没有你的 pb:要逐行选择一个区域,我按“V”(而不是简单的“v”逐个字符)。跨度>
      【解决方案6】:

      有一个文件提供以下内容

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

      ..." ...

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

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

      在这里获取:

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

      【讨论】:

        【解决方案7】:

        这里对 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))))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-29
          • 1970-01-01
          • 2013-05-07
          • 1970-01-01
          相关资源
          最近更新 更多