1 ;;; newcomment.el --- (un)comment regions of buffers
3 ;; Copyright (C) 1999, 2000 Free Software Foundation Inc.
5 ;; Author: code extracted from Emacs-20's simple.el
6 ;; Maintainer: Stefan Monnier <monnier@cs.yale.edu>
7 ;; Keywords: comment uncomment
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; A replacement for simple.el's comment-related functions.
32 ;; - nested comments in sgml-mode are not properly quoted.
33 ;; - single-char nestable comment-start can only do the "\\s<+" stuff
34 ;; if the corresponding closing marker happens to be right.
35 ;; - comment-box in TeXinfo generate bogus comments @ccccc@
36 ;; - uncomment-region with a numeric argument can render multichar
37 ;; comment markers invalid.
38 ;; - comment-indent or comment-region when called inside a comment
39 ;; will happily break the surrounding comment.
40 ;; - comment-quote-nested will not (un)quote properly all nested comment
41 ;; markers if there are more than just comment-start and comment-end.
42 ;; For example, in Pascal where {...*) and (*...} are possible.
46 ;; - quantized steps in comment-alignment
47 ;; - try to align tail comments
48 ;; - check what c-comment-line-break-function has to say
49 ;; - spill auto-fill of comments onto the end of the next line
50 ;; - uncomment-region with a consp (for blocks) or somehow make the
51 ;; deletion of continuation markers less dangerous
52 ;; - drop block-comment-<foo> unless it's really used
53 ;; - uncomment-region on a subpart of a comment
54 ;; - support gnu-style "multi-line with space in continue"
55 ;; - somehow allow comment-dwim to use the region even if transient-mark-mode
58 ;; - when auto-filling a comment, try to move the comment to the left
59 ;; rather than break it (if possible).
60 ;; - sometimes default the comment-column to the same
61 ;; one used on the preceding line(s).
66 (defalias 'indent-for-comment
'comment-indent
)
68 (defalias 'set-comment-column
'comment-set-column
)
70 (defalias 'kill-comment
'comment-kill
)
72 (defalias 'indent-new-comment-line
'comment-indent-new-line
)
76 "Indenting and filling of comments."
81 (defvar comment-use-syntax
'undecided
82 "Non-nil if syntax-tables can be used instead of regexps.
83 Can also be `undecided' which means that a somewhat expensive test will
84 be used to try to determine whether syntax-tables should be trusted
85 to understand comments or not in the given buffer.
86 Major modes should set this variable.")
88 (defcustom comment-fill-column nil
89 "Column to use for `comment-indent'. If nil, use `fill-column' instead."
90 :type
'(choice (const nil
) integer
))
93 (defcustom comment-column
32
94 "*Column to indent right-margin comments to.
95 Each mode establishes a different default value for this variable; you
96 can set the value for a particular mode using that mode's hook.
97 Comments might be indented to a value smaller than this in order
98 not to go beyond `comment-fill-column'."
100 (make-variable-buffer-local 'comment-column
)
103 (defvar comment-start nil
104 "*String to insert to start a new comment, or nil if no comment syntax.")
107 (defvar comment-start-skip nil
108 "*Regexp to match the start of a comment plus everything up to its body.
109 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
110 at the place matched by the close of the first pair.")
113 (defvar comment-end-skip nil
114 "Regexp to match the end of a comment plus everything up to its body.")
117 (defvar comment-end
""
118 "*String to insert to end a new comment.
119 Should be an empty string if comments are terminated by end-of-line.")
122 (defvar comment-indent-function
'comment-indent-default
123 "Function to compute desired indentation for a comment.
124 This function is called with no args with point at the beginning of
125 the comment's starting delimiter and should return either the desired
126 column indentation or nil.
127 If nil is returned, indentation is delegated to `indent-according-to-mode'.")
129 (defvar block-comment-start nil
)
130 (defvar block-comment-end nil
)
132 (defvar comment-quote-nested t
133 "Non-nil if nested comments should be quoted.
134 This should be locally set by each major mode if needed.")
136 (defvar comment-continue nil
137 "Continuation string to insert for multiline comments.
138 This string will be added at the beginning of each line except the very
139 first one when commenting a region with a commenting style that allows
140 comments to span several lines.
141 It should generally have the same length as `comment-start' in order to
142 preserve indentation.
143 If it is nil a value will be automatically derived from `comment-start'
144 by replacing its first character with a space.")
146 (defvar comment-add
0
147 "How many more comment chars should be inserted by `comment-region'.
148 This determines the default value of the numeric argument of `comment-region'.
149 This should generally stay 0, except for a few modes like Lisp where
150 it can be convenient to set it to 1 so that regions are commented with
153 (defconst comment-styles
154 '((plain .
(nil nil nil nil
))
155 (indent .
(nil nil nil t
))
156 (aligned .
(nil t nil t
))
157 (multi-line .
(t nil nil t
))
158 (extra-line .
(t nil t t
))
160 (box-multi .
(t t t t
)))
161 "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
162 STYLE should be a mnemonic symbol.
163 MULTI specifies that comments are allowed to span multiple lines.
164 ALIGN specifies that the `comment-end' markers should be aligned.
165 EXTRA specifies that an extra line should be used before and after the
166 region to comment (to put the `comment-end' and `comment-start').
167 INDENT specifies that the `comment-start' markers should not be put at the
168 left margin but at the current indentation of the region to comment.")
171 (defcustom comment-style
'plain
172 "*Style to be used for `comment-region'.
173 See `comment-styles' for a list of available styles."
174 :type
(if (boundp 'comment-styles
)
175 `(choice ,@(mapcar (lambda (s) `(const ,(car s
))) comment-styles
))
179 (defcustom comment-padding
" "
180 "Padding string that `comment-region' puts between comment chars and text.
181 Can also be an integer which will be automatically turned into a string
182 of the corresponding number of spaces.
184 Extra spacing between the comment characters and the comment text
185 makes the comment easier to read. Default is 1. nil means 0."
186 :type
'(choice string integer
(const nil
)))
189 (defcustom comment-multi-line nil
190 "*Non-nil means \\[comment-indent-new-line] continues comments, with no new terminator or starter.
191 This is obsolete because you might as well use \\[newline-and-indent]."
198 (defun comment-string-strip (str beforep afterp
)
199 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
200 (string-match (concat "\\`" (if beforep
"\\s-*")
201 "\\(.*?\\)" (if afterp
"\\s-*\n?")
203 (match-string 1 str
))
205 (defun comment-string-reverse (s)
206 "Return the mirror image of string S, without any trailing space."
207 (comment-string-strip (concat (nreverse (string-to-list s
))) nil t
))
210 (defun comment-normalize-vars (&optional noerror
)
211 (if (not comment-start
) (or noerror
(error "No comment syntax is defined"))
212 ;; comment-use-syntax
213 (when (eq comment-use-syntax
'undecided
)
214 (set (make-local-variable 'comment-use-syntax
)
215 (let ((st (syntax-table))
217 (ce (if (string= "" comment-end
) "\n" comment-end
)))
218 ;; Try to skip over a comment using forward-comment
219 ;; to see if the syntax tables properly recognize it.
221 (set-syntax-table st
)
222 (insert cs
" hello " ce
)
223 (goto-char (point-min))
224 (and (forward-comment 1) (eobp))))))
226 (unless comment-padding
(setq comment-padding
0))
227 (when (integerp comment-padding
)
228 (setq comment-padding
(make-string comment-padding ?
)))
230 ;;(setq comment-start (comment-string-strip comment-start t nil))
231 ;;(setq comment-end (comment-string-strip comment-end nil t))
233 (unless (or comment-continue
(string= comment-end
""))
234 (set (make-local-variable 'comment-continue
)
235 (concat (if (string-match "\\S-\\S-" comment-start
) " " "|")
236 (substring comment-start
1)))
237 ;; Hasn't been necessary yet.
238 ;; (unless (string-match comment-start-skip comment-continue)
239 ;; (kill-local-variable 'comment-continue))
241 ;; comment-skip regexps
242 (unless (and comment-start-skip
243 ;; In case comment-start has changed since last time.
244 (string-match comment-start-skip comment-start
))
245 (set (make-local-variable 'comment-start-skip
)
246 (concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
247 (regexp-quote (comment-string-strip comment-start t t
))
248 ;; Let's not allow any \s- but only [ \t] since \n
249 ;; might be both a comment-end marker and \s-.
251 (unless (and comment-end-skip
252 ;; In case comment-end has changed since last time.
253 (string-match comment-end-skip comment-end
))
254 (let ((ce (if (string= "" comment-end
) "\n"
255 (comment-string-strip comment-end t t
))))
256 (set (make-local-variable 'comment-end-skip
)
257 ;; We use [ \t] rather than \s- because we don't want to
258 ;; remove ^L in C mode when uncommenting.
259 (concat "[ \t]*\\(\\s>" (if comment-quote-nested
"" "+")
260 "\\|" (regexp-quote (substring ce
0 1))
261 (if (and comment-quote-nested
(<= (length ce
) 1)) "" "+")
262 (regexp-quote (substring ce
1))
265 (defun comment-quote-re (str unp
)
266 (concat (regexp-quote (substring str
0 1))
267 "\\\\" (if unp
"+" "*")
268 (regexp-quote (substring str
1))))
270 (defun comment-quote-nested (cs ce unp
)
271 "Quote or unquote nested comments.
272 If UNP is non-nil, unquote nested comment markers."
273 (setq cs
(comment-string-strip cs t t
))
274 (setq ce
(comment-string-strip ce t t
))
275 (when (and comment-quote-nested
(> (length ce
) 0))
276 (let ((re (concat (comment-quote-re ce unp
)
277 "\\|" (comment-quote-re cs unp
))))
278 (goto-char (point-min))
279 (while (re-search-forward re nil t
)
280 (goto-char (match-beginning 0))
282 (if unp
(delete-char 1) (insert "\\"))
283 (when (= (length ce
) 1)
284 ;; If the comment-end is a single char, adding a \ after that
285 ;; "first" char won't deactivate it, so we turn such a CE
286 ;; into !CS. I.e. for pascal, we turn } into !{
288 (when (string= (match-string 0) ce
)
289 (replace-match (concat "!" cs
) t t
))
290 (when (and (< (point-min) (match-beginning 0))
291 (string= (buffer-substring (1- (match-beginning 0))
295 (delete-char (- (match-end 0) (match-beginning 0)))
302 (defun comment-search-forward (limit &optional noerror
)
303 "Find a comment start between point and LIMIT.
304 Moves point to inside the comment and returns the position of the
305 comment-starter. If no comment is found, moves point to LIMIT
306 and raises an error or returns nil of NOERROR is non-nil."
307 (if (not comment-use-syntax
)
308 (if (re-search-forward comment-start-skip limit noerror
)
309 (or (match-end 1) (match-beginning 0))
311 (unless noerror
(error "No comment")))
313 ;; Assume (at first) that pt is outside of any string.
314 (s (parse-partial-sexp pt
(or limit
(point-max)) nil nil nil t
)))
315 (when (and (nth 8 s
) (nth 3 s
))
316 ;; The search ended inside a string. Try to see if it
317 ;; works better when we assume that pt is inside a string.
318 (setq s
(parse-partial-sexp
319 pt
(or limit
(point-max)) nil nil
320 (list nil nil nil
(nth 3 s
) nil nil nil nil
)
322 (if (not (and (nth 8 s
) (not (nth 3 s
))))
323 (unless noerror
(error "No comment"))
324 ;; We found the comment.
327 (bol (line-beginning-position))
329 (while (and (null end
) (>= (point) bol
))
330 (if (looking-at comment-start-skip
)
331 (setq end
(min (or limit
(point-max)) (match-end 0)))
333 (goto-char (or end pos
))
336 (defun comment-search-backward (&optional limit noerror
)
337 "Find a comment start between LIMIT and point.
338 Moves point to inside the comment and returns the position of the
339 comment-starter. If no comment is found, moves point to LIMIT
340 and raises an error or returns nil of NOERROR is non-nil."
341 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
342 ;; stop there. This can be rather bad in general, but since
343 ;; comment-search-backward is only used to find the comment-column (in
344 ;; comment-set-column) and to find the comment-start string (via
345 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
346 (if (not (re-search-backward comment-start-skip limit t
))
347 (unless noerror
(error "No comment"))
349 (let* ((end (match-end 0))
350 (cs (comment-search-forward end t
))
353 (progn (beginning-of-line)
354 (comment-search-backward limit noerror
))
355 (while (progn (goto-char cs
)
358 (setq cs
(comment-search-forward end t
))))
363 (defun comment-beginning ()
364 "Find the beginning of the enclosing comment.
365 Returns nil if not inside a comment, else moves point and returns
366 the same as `comment-search-forward'."
368 ;; We should really test `in-string-p' but that can be expensive.
369 (unless (eq (get-text-property (point) 'face
) 'font-lock-string-face
)
371 (cs (comment-search-backward nil t
)))
376 ;; For modes where comment-start and comment-end are the same,
377 ;; the search above may have found a `ce' rather than a `cs'.
378 (or (not (looking-at comment-end-skip
))
379 ;; Maybe font-lock knows that it's a `cs'?
380 (eq (get-text-property (match-end 0) 'face
)
381 'font-lock-comment-face
)
382 (unless (eq (get-text-property (point) 'face
)
383 'font-lock-comment-face
)
384 ;; Let's assume it's a `cs' if we're on the same line.
385 (>= (line-end-position) pt
)))
386 ;; Make sure that PT is not past the end of the comment.
387 (if (comment-forward 1) (> (point) pt
) (eobp))))
392 (defun comment-forward (&optional n
)
393 "Skip forward over N comments.
394 Just like `forward-comment' but only for positive N
395 and can use regexps instead of syntax."
397 (if (< n
0) (error "No comment-backward")
398 (if comment-use-syntax
(forward-comment n
)
401 (if (or (forward-comment 1)
402 (and (looking-at comment-start-skip
)
403 (goto-char (match-end 0))
404 (re-search-forward comment-end-skip nil
'move
)))
408 (defun comment-enter-backward ()
409 "Move from the end of a comment to the end of its content.
410 Point is assumed to be just at the end of a comment."
413 (progn (backward-char) (skip-syntax-backward " "))
417 (narrow-to-region (point) end
)
418 (if (re-search-forward (concat comment-end-skip
"\\'") nil t
)
419 (goto-char (match-beginning 0))
420 ;; comment-end-skip not found probably because it was not set right.
421 ;; Since \\s> should catch the single-char case, we'll blindly
422 ;; assume we're at the end of a two-char comment-end.
423 (goto-char (point-max))
425 (skip-chars-backward (string (char-after)))
426 (skip-syntax-backward " "))))))
433 (defun comment-indent-default ()
434 "Default for `comment-indent-function'."
435 (if (and (looking-at "\\s<\\s<\\(\\s<\\)?")
436 (or (match-end 1) (/= (current-column) (current-indentation))))
438 (when (or (/= (current-column) (current-indentation))
439 (and (> comment-add
0) (looking-at "\\s<\\S<")))
443 (defun comment-indent (&optional continue
)
444 "Indent this line's comment to comment column, or insert an empty comment.
445 If CONTINUE is non-nil, use the `comment-continue' markers if any."
447 (comment-normalize-vars)
448 (let* ((empty (save-excursion (beginning-of-line)
449 (looking-at "[ \t]*$")))
450 (starter (or (and continue comment-continue
)
451 (and empty block-comment-start
) comment-start
))
452 (ender (or (and continue comment-continue
"")
453 (and empty block-comment-end
) comment-end
)))
454 (unless starter
(error "No comment syntax defined"))
456 (let* ((eolpos (line-end-position))
457 (begpos (comment-search-forward eolpos t
))
459 ;; An existing comment?
462 (if (and (not (looking-at "[\t\n ]"))
463 (looking-at comment-end-skip
))
464 ;; The comment is empty and we have skipped all its space
465 ;; and landed right before the comment-ender:
466 ;; Go back to the middle of the space.
467 (forward-char (/ (skip-chars-backward " \t") -
2)))
468 (setq cpos
(point-marker)))
469 ;; If none, insert one.
471 ;; Some comment-indent-function insist on not moving comments that
472 ;; are in column 0, so we first go to the likely target column.
473 (indent-to comment-column
)
474 (setq begpos
(point))
476 (setq cpos
(point-marker))
479 ;; Compute desired indent.
480 (setq indent
(save-excursion (funcall comment-indent-function
)))
482 ;; comment-indent-function refuses: delegate to indent.
483 (indent-according-to-mode)
484 ;; Avoid moving comments past the fill-column.
485 (unless (save-excursion (skip-chars-backward " \t") (bolp))
489 (- (or comment-fill-column fill-column
)
490 (save-excursion (end-of-line) (current-column)))))))
491 (unless (= (current-column) indent
)
492 ;; If that's different from current, change it.
493 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
494 (indent-to (if (bolp) indent
495 (max indent
(1+ (current-column)))))))
497 (set-marker cpos nil
))))
500 (defun comment-set-column (arg)
501 "Set the comment column based on point.
502 With no ARG, set the comment column to the current column.
503 With just minus as arg, kill any comment on this line.
504 With any other arg, set comment column to indentation of the previous comment
505 and then align or create a comment on this line at that column."
508 ((eq arg
'-
) (comment-kill nil
))
512 (comment-search-backward)
514 (goto-char (comment-search-forward (line-end-position)))
515 (setq comment-column
(current-column))
516 (message "Comment column set to %d" comment-column
))
518 (t (setq comment-column
(current-column))
519 (message "Comment column set to %d" comment-column
))))
522 (defun comment-kill (arg)
523 "Kill the comment on this line, if any.
524 With prefix ARG, kill comments on that many lines starting with this one."
526 (dotimes (_ (prefix-numeric-value arg
))
529 (let ((cs (comment-search-forward (line-end-position) t
)))
532 (skip-syntax-backward " ")
535 (kill-region cs
(if (bolp) (1- (point)) (point)))
536 (indent-according-to-mode))))
537 (if arg
(forward-line 1))))
539 (defun comment-padright (str &optional n
)
540 "Construct a string composed of STR plus `comment-padding'.
541 It also adds N copies of the last non-whitespace chars of STR.
542 If STR already contains padding, the corresponding amount is
543 ignored from `comment-padding'.
545 If N is `re', a regexp is returned instead, that would match
546 the string for any N."
548 (when (and (stringp str
) (not (string= "" str
)))
549 ;; Separate the actual string from any leading/trailing padding
550 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str
)
551 (let ((s (match-string 1 str
)) ;actual string
552 (lpad (substring str
0 (match-beginning 1))) ;left padding
553 (rpad (concat (substring str
(match-end 1)) ;original right padding
554 (substring comment-padding
;additional right padding
555 (min (- (match-end 0) (match-end 1))
556 (length comment-padding
)))))
557 ;; We can only duplicate C if the comment-end has multiple chars
558 ;; or if comments can be nested, else the comment-end `}' would
559 ;; be turned into `}}}' where only the first ends the comment
560 ;; and the rest becomes bogus junk.
561 (multi (not (and comment-quote-nested
562 ;; comment-end is a single char
563 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
564 (if (not (symbolp n
))
565 (concat lpad s
(when multi
(make-string n
(aref str
(1- (match-end 1))))) rpad
)
566 ;; construct a regexp that would match anything from just S
567 ;; to any possible output of this function for any N.
568 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
569 lpad
"") ;padding is not required
571 (when multi
"+") ;the last char of S might be repeated
572 (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
573 rpad
"")))))) ;padding is not required
575 (defun comment-padleft (str &optional n
)
576 "Construct a string composed of `comment-padding' plus STR.
577 It also adds N copies of the first non-whitespace chars of STR.
578 If STR already contains padding, the corresponding amount is
579 ignored from `comment-padding'.
581 If N is `re', a regexp is returned instead, that would match
582 the string for any N."
584 (when (and (stringp str
) (not (string= "" str
)))
585 ;; Only separate the left pad because we assume there is no right pad.
586 (string-match "\\`\\s-*" str
)
587 (let ((s (substring str
(match-end 0)))
588 (pad (concat (substring comment-padding
589 (min (- (match-end 0) (match-beginning 0))
590 (length comment-padding
)))
591 (match-string 0 str
)))
592 (c (aref str
(match-end 0))) ;the first non-space char of STR
593 ;; We can only duplicate C if the comment-end has multiple chars
594 ;; or if comments can be nested, else the comment-end `}' would
595 ;; be turned into `}}}' where only the first ends the comment
596 ;; and the rest becomes bogus junk.
597 (multi (not (and comment-quote-nested
598 ;; comment-end is a single char
599 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
600 (if (not (symbolp n
))
601 (concat pad
(when multi
(make-string n c
)) s
)
602 ;; Construct a regexp that would match anything from just S
603 ;; to any possible output of this function for any N.
604 ;; We match any number of leading spaces because this regexp will
605 ;; be used for uncommenting where we might want to remove
606 ;; uncomment markers with arbitrary leading space (because
607 ;; they were aligned).
609 (if multi
(concat (regexp-quote (string c
)) "*"))
610 (regexp-quote s
))))))
613 (defun uncomment-region (beg end
&optional arg
)
614 "Uncomment each line in the BEG..END region.
615 The numeric prefix ARG can specify a number of chars to remove from the
617 (interactive "*r\nP")
618 (comment-normalize-vars)
619 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
622 (setq end
(copy-marker end
))
623 (let ((numarg (prefix-numeric-value arg
))
625 (while (and (< (point) end
)
626 (setq spt
(comment-search-forward end t
)))
628 ;; Find the end of the comment.
631 (unless (comment-forward)
632 (error "Can't find the comment end"))
635 (ccs comment-continue
)
636 (srei (comment-padright ccs
're
))
637 (sre (and srei
(concat "^\\s-*?\\(" srei
"\\)"))))
639 (narrow-to-region spt ept
)
640 ;; Remove the comment-start.
642 (skip-syntax-backward " ")
643 ;; Check for special `=' used sometimes in comment-box.
644 (when (and (= (- (point) (point-min)) 1) (looking-at "=\\{7\\}"))
645 (skip-chars-forward "="))
646 ;; A box-comment starts with a looong comment-start marker.
647 (when (> (- (point) (point-min) (length comment-start
)) 7)
649 (when (looking-at (regexp-quote comment-padding
))
650 (goto-char (match-end 0)))
651 (when (and sre
(looking-at (concat "\\s-*\n\\s-*" srei
)))
652 (goto-char (match-end 0)))
653 (if (null arg
) (delete-region (point-min) (point))
654 (skip-syntax-backward " ")
655 (delete-char (- numarg
)))
657 ;; Remove the end-comment (and leading padding and such).
658 (goto-char (point-max)) (comment-enter-backward)
659 ;; Check for special `=' used sometimes in comment-box.
660 (when (= (- (point-max) (point)) 1)
662 ;; skip `=' but only if there are at least 7.
663 (when (> (skip-chars-backward "=") -
7) (goto-char pos
))))
664 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
665 (when (and (bolp) (not (bobp))) (backward-char))
666 (if (null arg
) (delete-region (point) (point-max))
667 (skip-syntax-forward " ")
668 (delete-char numarg
)))
670 ;; Unquote any nested end-comment.
671 (comment-quote-nested comment-start comment-end t
)
673 ;; Eliminate continuation markers as well.
675 (let* ((cce (comment-string-reverse (or comment-continue
677 (erei (and box
(comment-padleft cce
're
)))
678 (ere (and erei
(concat "\\(" erei
"\\)\\s-*$"))))
679 (goto-char (point-min))
681 (if (and ere
(re-search-forward
682 ere
(line-end-position) t
))
683 (replace-match "" t t nil
(if (match-end 2) 2 1))
686 (re-search-forward sre
(line-end-position) t
))
687 (replace-match "" t t nil
(if (match-end 2) 2 1)))))
688 ;; Go to the end for the next comment.
689 (goto-char (point-max)))))
690 (set-marker end nil
))))
692 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent
&optional block
)
693 "Make the leading and trailing extra lines.
694 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
697 ;; Try to match CS and CE's content so they align aesthetically.
699 (setq ce
(comment-string-strip ce t t
))
700 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce
"\n" cs
))
702 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
705 (let* ((width (- max-indent min-indent
))
706 (s (concat cs
"a=m" cce
))
707 (e (concat ccs
"a=m" ce
))
708 (c (if (string-match ".*\\S-\\S-" cs
)
709 (aref cs
(1- (match-end 0))) ?
=))
710 (_ (string-match "\\s-*a=m\\s-*" s
))
712 (make-string (+ width
(- (match-end 0)
713 (match-beginning 0) (length cs
) 3)) c
)))
714 (setq cs
(replace-match fill t t s
))
715 (string-match "\\s-*a=m\\s-*" e
)
716 (setq ce
(replace-match fill t t e
))))
717 (cons (concat cs
"\n" (make-string min-indent ?
) ccs
)
718 (concat cce
"\n" (make-string (+ min-indent eindent
) ?
) ce
))))
720 (def-edebug-spec comment-with-narrowing t
)
721 (put 'comment-with-narrowing
'lisp-indent-function
2)
722 (defmacro comment-with-narrowing
(beg end
&rest body
)
723 "Execute BODY with BEG..END narrowing.
724 Space is added (and then removed) at the beginning for the text's
725 indentation to be kept as it was before narrowing."
726 (let ((bindent (make-symbol "bindent")))
727 `(let ((,bindent
(save-excursion (goto-char beg
) (current-column))))
729 (narrow-to-region beg end
)
730 (goto-char (point-min))
731 (insert (make-string ,bindent ?
))
734 ;; remove the bindent
736 (goto-char (point-min))
737 (when (looking-at " *")
738 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent
)))
740 (setq ,bindent
(- ,bindent n
))))
744 (while (and (> ,bindent
0) (re-search-forward " *" e t
))
745 (let ((n (min ,bindent
(- (match-end 0) (match-beginning 0) 1))))
746 (goto-char (match-beginning 0))
748 (setq ,bindent
(- ,bindent n
)))))))))))
750 (defun comment-region-internal (beg end cs ce
751 &optional ccs cce block lines indent
)
752 "Comment region BEG..END.
753 CS and CE are the comment start resp end string.
754 CCS and CCE are the comment continuation strings for the start resp end
755 of lines (default to CS and CE).
756 BLOCK indicates that end of lines should be marked with either CCE, CE or CS
757 \(if CE is empty) and that those markers should be aligned.
758 LINES indicates that an extra lines will be used at the beginning and end
759 of the region for CE and CS.
760 INDENT indicates to put CS and CCS at the current indentation of the region
761 rather than at left margin."
762 ;;(assert (< beg end))
764 ;; Sanitize CE and CCE.
765 (if (and (stringp ce
) (string= "" ce
)) (setq ce nil
))
766 (if (and (stringp cce
) (string= "" cce
)) (setq cce nil
))
767 ;; If CE is empty, multiline cannot be used.
768 (unless ce
(setq ccs nil cce nil
))
769 ;; Should we mark empty lines as well ?
770 (if (or ccs block lines
) (setq no-empty nil
))
771 ;; Make sure we have end-markers for BLOCK mode.
772 (when block
(unless ce
(setq ce
(comment-string-reverse cs
))))
773 ;; If BLOCK is not requested, we don't need CCE.
774 (unless block
(setq cce nil
))
775 ;; Continuation defaults to the same as CS and CE.
776 (unless ccs
(setq ccs cs cce ce
))
780 ;; If the end is not at the end of a line and the comment-end
781 ;; is implicit (i.e. a newline), explicitly insert a newline.
782 (unless (or ce
(eolp)) (insert "\n") (indent-according-to-mode))
783 (comment-with-narrowing beg end
784 (let ((min-indent (point-max))
786 (goto-char (point-min))
787 ;; Quote any nested comment marker
788 (comment-quote-nested comment-start comment-end nil
)
790 ;; Loop over all lines to find the needed indentations.
791 (goto-char (point-min))
794 (unless (looking-at "[ \t]*$")
795 (setq min-indent
(min min-indent
(current-indentation))))
797 (setq max-indent
(max max-indent
(current-column)))
798 (not (or (eobp) (progn (forward-line) nil
)))))
800 ;; Inserting ccs can change max-indent by (1- tab-width).
802 (+ max-indent
(max (length cs
) (length ccs
)) tab-width -
1))
803 (unless indent
(setq min-indent
0))
805 ;; make the leading and trailing lines if requested
808 (comment-make-extra-lines
809 cs ce ccs cce min-indent max-indent block
)))
811 (setq ce
(cdr csce
))))
813 (goto-char (point-min))
814 ;; Loop over all lines from BEG to END.
817 (unless (and no-empty
(looking-at "[ \t]*$"))
818 (move-to-column min-indent t
)
819 (insert cs
) (setq cs ccs
) ;switch to CCS after the first line
821 (if (eobp) (setq cce ce
))
823 (when block
(move-to-column max-indent t
))
826 (not (or (eobp) (progn (forward-line) nil
))))))))))
829 (defun comment-region (beg end
&optional arg
)
830 "Comment or uncomment each line in the region.
831 With just \\[universal-argument] prefix arg, uncomment each line in region BEG..END.
832 Numeric prefix arg ARG means use ARG comment characters.
833 If ARG is negative, delete that many comment characters instead.
834 By default, comments start at the left margin, are terminated on each line,
835 even for syntax in which newline does not end the comment and blank lines
836 do not get comments. This can be changed with `comment-style'.
838 The strings used as comment starts are built from
839 `comment-start' without trailing spaces and `comment-padding'."
840 (interactive "*r\nP")
841 (comment-normalize-vars)
842 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
843 (let* ((numarg (prefix-numeric-value arg
))
845 (style (cdr (assoc comment-style comment-styles
)))
846 (lines (nth 2 style
))
847 (block (nth 1 style
))
848 (multi (nth 0 style
)))
850 ;; we use `chars' instead of `syntax' because `\n' might be
851 ;; of end-comment syntax rather than of whitespace syntax.
852 ;; sanitize BEG and END
853 (goto-char beg
) (skip-chars-forward " \t\n\r") (beginning-of-line)
854 (setq beg
(max beg
(point)))
855 (goto-char end
) (skip-chars-backward " \t\n\r") (end-of-line)
856 (setq end
(min end
(point)))
857 (if (>= beg end
) (error "Nothing to comment"))
863 (progn (goto-char beg
) (beginning-of-line)
864 (skip-syntax-forward " ")
866 (progn (goto-char end
) (end-of-line) (skip-syntax-backward " ")
868 (or (not (string= "" comment-end
)) block
)
869 (progn (goto-char beg
) (search-forward "\n" end t
)))))
871 ;; don't add end-markers just because the user asked for `block'
872 (unless (or lines
(string= "" comment-end
)) (setq block nil
))
875 ((consp arg
) (uncomment-region beg end
))
876 ((< numarg
0) (uncomment-region beg end
(- numarg
)))
878 (setq numarg
(if (and (null arg
) (= (length comment-start
) 1))
880 (comment-region-internal
882 (let ((s (comment-padright comment-start numarg
)))
883 (if (string-match comment-start-skip s
) s
884 (comment-padright comment-start
)))
885 (let ((s (comment-padleft comment-end numarg
)))
886 (and s
(if (string-match comment-end-skip s
) s
887 (comment-padright comment-end
))))
888 (if multi
(comment-padright comment-continue numarg
))
889 (if multi
(comment-padleft (comment-string-reverse comment-continue
) numarg
))
894 (defun comment-box (beg end
&optional arg
)
895 "Comment out the BEG..END region, putting it inside a box.
896 The numeric prefix ARG specifies how many characters to add to begin- and
897 end- comment markers additionally to what `comment-add' already specifies."
898 (interactive "*r\np")
899 (let ((comment-style (if (cadr (assoc comment-style comment-styles
))
901 (comment-region beg end
(+ comment-add arg
))))
905 (defun comment-or-uncomment-region (beg end
&optional arg
)
906 "Call `comment-region', unless the region only consists of comments,
907 in which case call `uncomment-region'. If a prefix arg is given, it
908 is passed on to the respective function."
909 (interactive "*r\nP")
910 (funcall (if (save-excursion ;; check for already commented region
912 (comment-forward (point-max))
914 'uncomment-region
'comment-region
)
918 (defun comment-dwim (arg)
919 "Call the comment command you want (Do What I Mean).
920 If the region is active and `transient-mark-mode' is on, call
921 `comment-region' (unless it only consists of comments, in which
922 case it calls `uncomment-region').
923 Else, if the current line is empty, insert a comment and indent it.
924 Else if a prefix ARG is specified, call `comment-kill'.
925 Else, call `comment-indent'."
927 (comment-normalize-vars)
928 (if (and mark-active transient-mark-mode
)
929 (comment-or-uncomment-region (region-beginning) (region-end) arg
)
930 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
931 ;; FIXME: If there's no comment to kill on this line and ARG is
932 ;; specified, calling comment-kill is not very clever.
933 (if arg
(comment-kill (and (integerp arg
) arg
)) (comment-indent))
934 (let ((add (if arg
(prefix-numeric-value arg
)
935 (if (= (length comment-start
) 1) comment-add
0))))
936 ;; Some modes insist on keeping column 0 comment in column 0
937 ;; so we need to move away from it before inserting the comment.
938 (indent-according-to-mode)
939 (insert (comment-padright comment-start add
))
941 (unless (string= "" comment-end
)
942 (insert (comment-padleft comment-end add
)))
943 (indent-according-to-mode))))))
945 (defcustom comment-auto-fill-only-comments nil
946 "Non-nil means to only auto-fill inside comments.
947 This has no effect in modes that do not define a comment syntax."
950 (defun comment-valid-prefix (prefix compos
)
952 ;; Accept any prefix if the current comment is not EOL-terminated.
953 (save-excursion (goto-char compos
) (comment-forward) (not (bolp)))
954 ;; Accept any prefix that starts with a comment-start marker.
955 (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip
"\\)")
959 (defun comment-indent-new-line (&optional soft
)
960 "Break line at point and indent, continuing comment if within one.
961 This indents the body of the continued comment
962 under the previous comment line.
964 This command is intended for styles where you write a comment per line,
965 starting a new comment (and terminating it if necessary) on each line.
966 If you want to continue one comment across several lines, use \\[newline-and-indent].
968 If a fill column is specified, it overrides the use of the comment column
969 or comment indentation.
971 The inserted newline is marked hard if variable `use-hard-newlines' is true,
972 unless optional argument SOFT is non-nil."
974 (comment-normalize-vars t
)
976 ;; If we are not inside a comment and we only auto-fill comments,
977 ;; don't do anything (unless no comment syntax is defined).
978 (unless (and comment-start
979 comment-auto-fill-only-comments
980 (not (interactive-p))
982 (prog1 (setq compos
(comment-beginning))
983 (setq comin
(point))))))
985 ;; Now we know we should auto-fill.
986 ;; Insert the newline before removing empty space so that markers
987 ;; get preserved better.
988 (if soft
(insert-and-inherit ?
\n) (newline 1))
989 (save-excursion (forward-char -
1) (delete-horizontal-space))
990 (delete-horizontal-space)
992 (if (and fill-prefix
(not adaptive-fill-mode
))
993 ;; Blindly trust a non-adaptive fill-prefix.
995 (indent-to-left-margin)
996 (insert-before-markers-and-inherit fill-prefix
))
998 ;; If necessary check whether we're inside a comment.
999 (unless (or compos
(null comment-start
))
1002 (setq compos
(comment-beginning))
1003 (setq comin
(point))))
1006 ;; If there's an adaptive prefix, use it unless we're inside
1007 ;; a comment and the prefix is not a comment starter.
1010 (comment-valid-prefix fill-prefix compos
)))
1011 (indent-to-left-margin)
1012 (insert-and-inherit fill-prefix
))
1013 ;; If we're not inside a comment, just try to indent.
1014 ((not compos
) (indent-according-to-mode))
1016 (let* ((comment-column
1017 ;; The continuation indentation should be somewhere between
1018 ;; the current line's indentation (plus 2 for good measure)
1019 ;; and the current comment's indentation, with a preference
1020 ;; for comment-column.
1022 ;; FIXME: use prev line's info rather than first line's.
1024 (min (current-column) (max comment-column
1025 (+ 2 (current-indentation))))))
1026 (comstart (buffer-substring compos comin
))
1028 (string-match (regexp-quote (comment-string-strip
1032 (if normalp comment-end
1033 ;; The comment starter is not the normal comment-start
1034 ;; so we can't just use comment-end.
1037 (if (not (comment-forward)) comment-end
1038 (comment-string-strip
1040 (save-excursion (comment-enter-backward) (point))
1043 (comment-start comstart
)
1044 (continuep (or comment-multi-line
1045 (cadr (assoc comment-style comment-styles
))))
1046 ;; Force comment-continue to be recreated from comment-start.
1047 ;; FIXME: wrong if comment-continue was set explicitly!
1048 ;; FIXME: use prev line's continuation if available.
1049 (comment-continue nil
))
1050 (if (and comment-multi-line
(> (length comment-end
) 0))
1051 (indent-according-to-mode)
1052 (insert-and-inherit ?
\n)
1054 (comment-indent continuep
)
1058 (let ((comend (buffer-substring pt
(point))))
1059 ;; The 1+ is to make sure we delete the \n inserted above.
1060 (delete-region pt
(1+ (point)))
1062 (insert comend
))))))))))))
1064 (provide 'newcomment
)
1066 ;;; newcomment.el ends here