【问题标题】:Command to uncomment multiple lines without selecting them命令取消注释多行而不选择它们
【发布时间】:2013-08-17 21:04:33
【问题描述】:

emacs 中是否有一个命令可以取消注释整个注释块而不必先标记它?

例如,假设点位于以下代码中的注释内:

  (setq doing-this t)
  ;; (progn |<--This is the point
  ;;   (er/expand-region 1)
  ;;   (uncomment-region (region-beginning) (region-end)))

我想要一个能把它变成这样的命令:

  (setq doing-this t)
  (progn
    (er/expand-region 1)
    (uncomment-region (region-beginning) (region-end)))

编写一个(un)cmets 一行的命令相当容易,但是我还没有找到一个尽可能多的uncmets 的命令。有没有可用的?

【问题讨论】:

标签: emacs comments elisp


【解决方案1】:

快速回复 --- 代码可以改进并变得更有用。例如,除了;;;,您可能希望将其扩展到其他类型的 cmets。

(defun uncomment-these-lines ()
  (interactive)
  (let ((opoint  (point))
        beg end)
    (save-excursion
      (forward-line 0)
      (while (looking-at "^;;; ") (forward-line -1))
      (unless (= opoint (point))
        (forward-line 1)
        (setq beg  (point)))
      (goto-char opoint)
      (forward-line 0)
      (while (looking-at "^;;; ") (forward-line 1))
      (unless (= opoint (point))
        (setq end  (point)))
      (when (and beg  end)
        (comment-region beg end '(4))))))

密钥是comment-region。 FWIW,我将comment-region 绑定到C-x C-;。只需将其与 C-u 一起使用即可取消注释。

【讨论】:

  • @Drew 如果您愿意,请查看变量comment-startcomment-end 以使代码更通用。
  • 是的,它可以通过多种方式进行改进,包括可选地处理除;;; at bol 之外的其他类型的 cmets。我自己对这样的命令没有太多用处,但也许@BruceConnor 会想要扩展它。
【解决方案2】:

您可以使用 Emacs 的注释处理功能来制作 Drew 命令的通用版本。

(defun uncomment-current ()
  (interactive)
  (save-excursion
    (goto-char (point-at-eol))
    (goto-char (nth 8 (syntax-ppss)))
    (uncomment-region
     (progn
       (forward-comment -10000)
       (point))
     (progn
       (forward-comment 10000)
       (point)))))

【讨论】:

  • forward-comment“放弃”太容易了;如果点不是直接在评论后面,它似乎什么也抓不到。假设注释是该行的第一个非空格,这可以通过在偏移的开头添加(beginning-of-line) 来解决。
  • 我会使用uncomment-region。我会首先使用(goto-char (nth 8 (syntax-ppss))) 之类的内容移出当前评论。
猜你喜欢
  • 2018-05-31
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 2017-03-14
相关资源
最近更新 更多