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. 开发者_C百科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...)
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))))))
精彩评论