emacs: Cc % 在 auctex 中做什么以及如何让它表现得更好?

发布于 2024-10-04 09:39:33 字数 234 浏览 5 评论 0原文

Cc % 应该是用于注释内容的 emacs auctex 模式快捷方式。 (还有 Cc ; 可以注释掉标记的区域,但是这个可以工作)。现在有时它会注释掉一行,有时它会注释掉一行及其上面的行。它似乎没有非常一致的行为。

我真正希望它做的是注释掉光标所在的行,除非它位于开始或结束标记上,在这种情况下注释掉整个环境。 (实际上,我只想理解注释宏稍微奇怪的行为......)

C-c % is supposed to be the emacs auctex mode shortcut for commenting out stuff. (There's also C-c ; which comments out the marked region, but that one works). Now sometimes it comments out a single line, sometimes it comments out a line and the ones above it. It doesn't seem to have very consistent behaviour.

What I'd really like it to do is comment out the line the cursor is on unless it's on a begin or end tag, in which case comment out the whole environment. (Actually, I'd settle for just understanding the slightly odd behaviour of the comment macro...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2024-10-11 09:39:33

Cc % 运行 TeX-comment-or-uncomment-paragraph。要了解此处的段落到底是什么,请参阅手册

命令:TeX-comment-or-uncomment-paragraph
(Cc %) 在当前段落中每行的开头添加或删除 %。删除 % 字符时,该段落被视为由所有以 % 开头的前后行组成,直到第一个非注释行。


这是一个评论功能,或多或少可以满足您的需求。仅当 LaTeX-syntropic-commentst (即使如此,也并不总是很好)。

(defun LaTeX-comment-environment-or-line (arg)
  "Comment or uncomment the current line.
If the current line is the \\begin or \\end line of an environment, comment
or uncomment the whole environment."
  (interactive "*P")
  (save-match-data
    (save-excursion
      (beginning-of-line)
      (cond
       ((looking-at (concat "\\s-*\\(" TeX-comment-start-regexp "\\)?\\s-*"
                            (regexp-quote TeX-esc) "begin"))
        (let ((begin (point)))
          (goto-char (match-end 0))
          (LaTeX-find-matching-end)
          (TeX-comment-or-uncomment-region begin (point) arg)))
       ((looking-at (concat "\\s-*\\(" TeX-comment-start-regexp "\\)?\\s-*"
                            (regexp-quote TeX-esc) "end"))
        (let ((end (save-excursion (end-of-line) (point))))
          (LaTeX-find-matching-begin)
          (beginning-of-line)
          (TeX-comment-or-uncomment-region (point) end arg)))
       (t
        (TeX-comment-or-uncomment-region
         (point) (save-excursion (end-of-line) (point)) arg))))))

C-c % runs TeX-comment-or-uncomment-paragraph. For what exactly is considered a paragraph here, see the manual:

Command: TeX-comment-or-uncomment-paragraph
(C-c %) Add or remove % from the beginning of each line in the current paragraph. When removing % characters the paragraph is considered to consist of all preceding and succeeding lines starting with a %, until the first non-comment line.


Here's a commenting function that does more or less what you want. Uncommenting an environment only works if LaTeX-syntactic-comments is t (and not always very well even then).

(defun LaTeX-comment-environment-or-line (arg)
  "Comment or uncomment the current line.
If the current line is the \\begin or \\end line of an environment, comment
or uncomment the whole environment."
  (interactive "*P")
  (save-match-data
    (save-excursion
      (beginning-of-line)
      (cond
       ((looking-at (concat "\\s-*\\(" TeX-comment-start-regexp "\\)?\\s-*"
                            (regexp-quote TeX-esc) "begin"))
        (let ((begin (point)))
          (goto-char (match-end 0))
          (LaTeX-find-matching-end)
          (TeX-comment-or-uncomment-region begin (point) arg)))
       ((looking-at (concat "\\s-*\\(" TeX-comment-start-regexp "\\)?\\s-*"
                            (regexp-quote TeX-esc) "end"))
        (let ((end (save-excursion (end-of-line) (point))))
          (LaTeX-find-matching-begin)
          (beginning-of-line)
          (TeX-comment-or-uncomment-region (point) end arg)))
       (t
        (TeX-comment-or-uncomment-region
         (point) (save-excursion (end-of-line) (point)) arg))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文