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
8 ;; Revision: $Id: newcomment.el,v 1.30 2001/02/22 01:47:40 monnier Exp $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; A replacement for simple.el's comment-related functions.
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 ;; - try to align tail comments
47 ;; - check what c-comment-line-break-function has to say
48 ;; - spill auto-fill of comments onto the end of the next line
49 ;; - uncomment-region with a consp (for blocks) or somehow make the
50 ;; deletion of continuation markers less dangerous
51 ;; - drop block-comment-<foo> unless it's really used
52 ;; - uncomment-region on a subpart of a comment
53 ;; - support gnu-style "multi-line with space in continue"
54 ;; - somehow allow comment-dwim to use the region even if transient-mark-mode
57 ;; - when auto-filling a comment, try to move the comment to the left
58 ;; rather than break it (if possible).
59 ;; - sometimes default the comment-column to the same
60 ;; one used on the preceding line(s).
65 (defalias 'indent-for-comment
'comment-indent
)
67 (defalias 'set-comment-column
'comment-set-column
)
69 (defalias 'kill-comment
'comment-kill
)
71 (defalias 'indent-new-comment-line
'comment-indent-new-line
)
75 "Indenting and filling of comments."
80 (defvar comment-use-syntax
'undecided
81 "Non-nil if syntax-tables can be used instead of regexps.
82 Can also be `undecided' which means that a somewhat expensive test will
83 be used to try to determine whether syntax-tables should be trusted
84 to understand comments or not in the given buffer.
85 Major modes should set this variable.")
88 (defcustom comment-column
32
89 "*Column to indent right-margin comments to.
90 Setting this variable automatically makes it local to the current buffer.
91 Each mode establishes a different default value for this variable; you
92 can set the value for a particular mode using that mode's hook."
95 (make-variable-buffer-local 'comment-column
)
98 (defvar comment-start nil
99 "*String to insert to start a new comment, or nil if no comment syntax.")
102 (defvar comment-start-skip nil
103 "*Regexp to match the start of a comment plus everything up to its body.
104 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
105 at the place matched by the close of the first pair.")
108 (defvar comment-end-skip nil
109 "Regexp to match the end of a comment plus everything up to its body.")
112 (defvar comment-end
""
113 "*String to insert to end a new comment.
114 Should be an empty string if comments are terminated by end-of-line.")
117 (defvar comment-indent-function
'comment-indent-default
118 "Function to compute desired indentation for a comment.
119 This function is called with no args with point at the beginning of
120 the comment's starting delimiter and should return either the desired
121 column indentation or nil.
122 If nil is returned, indentation is delegated to `indent-according-to-mode'.")
124 (defvar block-comment-start nil
)
125 (defvar block-comment-end nil
)
127 (defvar comment-quote-nested t
128 "Non-nil if nested comments should be quoted.
129 This should be locally set by each major mode if needed.")
131 (defvar comment-continue nil
132 "Continuation string to insert for multiline comments.
133 This string will be added at the beginning of each line except the very
134 first one when commenting a region with a commenting style that allows
135 comments to span several lines.
136 It should generally have the same length as `comment-start' in order to
137 preserve indentation.
138 If it is nil a value will be automatically derived from `comment-start'
139 by replacing its first character with a space.")
141 (defvar comment-add
0
142 "How many more comment chars should be inserted by `comment-region'.
143 This determines the default value of the numeric argument of `comment-region'.
144 This should generally stay 0, except for a few modes like Lisp where
145 it can be convenient to set it to 1 so that regions are commented with
148 (defconst comment-styles
149 '((plain .
(nil nil nil nil
))
150 (indent .
(nil nil nil t
))
151 (aligned .
(nil t nil t
))
152 (multi-line .
(t nil nil t
))
153 (extra-line .
(t nil t t
))
155 (box-multi .
(t t t t
)))
156 "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
157 STYLE should be a mnemonic symbol.
158 MULTI specifies that comments are allowed to span multiple lines.
159 ALIGN specifies that the `comment-end' markers should be aligned.
160 EXTRA specifies that an extra line should be used before and after the
161 region to comment (to put the `comment-end' and `comment-start').
162 INDENT specifies that the `comment-start' markers should not be put at the
163 left margin but at the current indentation of the region to comment.")
166 (defcustom comment-style
'plain
167 "*Style to be used for `comment-region'.
168 See `comment-styles' for a list of available styles."
170 :type
(if (boundp 'comment-styles
)
171 `(choice ,@(mapcar (lambda (s) `(const ,(car s
))) comment-styles
))
175 (defcustom comment-padding
" "
176 "Padding string that `comment-region' puts between comment chars and text.
177 Can also be an integer which will be automatically turned into a string
178 of the corresponding number of spaces.
180 Extra spacing between the comment characters and the comment text
181 makes the comment easier to read. Default is 1. nil means 0.")
184 (defcustom comment-multi-line nil
185 "*Non-nil means \\[comment-indent-new-line] continues comments, with no new terminator or starter.
186 This is obsolete because you might as well use \\[newline-and-indent]."
194 (defun comment-string-strip (str beforep afterp
)
195 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
196 (string-match (concat "\\`" (if beforep
"\\s-*")
197 "\\(.*?\\)" (if afterp
"\\s-*\n?")
199 (match-string 1 str
))
201 (defun comment-string-reverse (s)
202 "Return the mirror image of string S, without any trailing space."
203 (comment-string-strip (concat (nreverse (string-to-list s
))) nil t
))
205 (defun comment-normalize-vars (&optional noerror
)
206 (if (not comment-start
) (or noerror
(error "No comment syntax is defined"))
207 ;; comment-use-syntax
208 (when (eq comment-use-syntax
'undecided
)
209 (set (make-local-variable 'comment-use-syntax
)
210 (let ((st (syntax-table))
212 (ce (if (string= "" comment-end
) "\n" comment-end
)))
213 ;; Try to skip over a comment using forward-comment
214 ;; to see if the syntax tables properly recognize it.
216 (set-syntax-table st
)
217 (insert cs
" hello " ce
)
218 (goto-char (point-min))
219 (and (forward-comment 1) (eobp))))))
221 (when (integerp comment-padding
)
222 (setq comment-padding
(make-string comment-padding ?
)))
224 ;;(setq comment-start (comment-string-strip comment-start t nil))
225 ;;(setq comment-end (comment-string-strip comment-end nil t))
227 (unless (or comment-continue
(string= comment-end
""))
228 (set (make-local-variable 'comment-continue
)
229 (concat (if (string-match "\\S-\\S-" comment-start
) " " "|")
230 (substring comment-start
1))))
231 ;; comment-skip regexps
232 (unless comment-start-skip
233 (set (make-local-variable 'comment-start-skip
)
234 (concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
235 (regexp-quote (comment-string-strip comment-start t t
))
236 ;; Let's not allow any \s- but only [ \t] since \n
237 ;; might be both a comment-end marker and \s-.
239 (unless comment-end-skip
240 (let ((ce (if (string= "" comment-end
) "\n"
241 (comment-string-strip comment-end t t
))))
242 (set (make-local-variable 'comment-end-skip
)
243 (concat "\\s-*\\(\\s>" (if comment-quote-nested
"" "+")
244 "\\|" (regexp-quote (substring ce
0 1))
245 (if (and comment-quote-nested
(<= (length ce
) 1)) "" "+")
246 (regexp-quote (substring ce
1))
249 (defun comment-quote-re (str unp
)
250 (concat (regexp-quote (substring str
0 1))
251 "\\\\" (if unp
"+" "*")
252 (regexp-quote (substring str
1))))
254 (defun comment-quote-nested (cs ce unp
)
255 "Quote or unquote nested comments.
256 If UNP is non-nil, unquote nested comment markers."
257 (setq cs
(comment-string-strip cs t t
))
258 (setq ce
(comment-string-strip ce t t
))
259 (when (and comment-quote-nested
(> (length ce
) 0))
260 (let ((re (concat (comment-quote-re ce unp
)
261 "\\|" (comment-quote-re cs unp
))))
262 (goto-char (point-min))
263 (while (re-search-forward re nil t
)
264 (goto-char (match-beginning 0))
266 (if unp
(delete-char 1) (insert "\\"))
267 (when (= (length ce
) 1)
268 ;; If the comment-end is a single char, adding a \ after that
269 ;; "first" char won't deactivate it, so we turn such a CE
270 ;; into !CS. I.e. for pascal, we turn } into !{
272 (when (string= (match-string 0) ce
)
273 (replace-match (concat "!" cs
) t t
))
274 (when (and (< (point-min) (match-beginning 0))
275 (string= (buffer-substring (1- (match-beginning 0))
279 (delete-char (- (match-end 0) (match-beginning 0)))
286 (defun comment-search-forward (limit &optional noerror
)
287 "Find a comment start between point and LIMIT.
288 Moves point to inside the comment and returns the position of the
289 comment-starter. If no comment is found, moves point to LIMIT
290 and raises an error or returns nil of NOERROR is non-nil."
291 (if (not comment-use-syntax
)
292 (if (re-search-forward comment-start-skip limit noerror
)
293 (or (match-end 1) (match-beginning 0))
295 (unless noerror
(error "No comment")))
297 ;; Assume (at first) that pt is outside of any string.
298 (s (parse-partial-sexp pt
(or limit
(point-max)) nil nil nil t
)))
299 (when (and (nth 8 s
) (nth 3 s
))
300 ;; The search ended inside a string. Try to see if it
301 ;; works better when we assume that pt is inside a string.
302 (setq s
(parse-partial-sexp
303 pt
(or limit
(point-max)) nil nil
304 (list nil nil nil
(nth 3 s
) nil nil nil nil
)
306 (if (not (and (nth 8 s
) (not (nth 3 s
))))
307 (unless noerror
(error "No comment"))
308 ;; We found the comment.
311 (bol (line-beginning-position))
313 (while (and (null end
) (>= (point) bol
))
314 (if (looking-at comment-start-skip
)
315 (setq end
(min (or limit
(point-max)) (match-end 0)))
317 (goto-char (or end pos
))
320 (defun comment-search-backward (&optional limit noerror
)
321 "Find a comment start between LIMIT and point.
322 Moves point to inside the comment and returns the position of the
323 comment-starter. If no comment is found, moves point to LIMIT
324 and raises an error or returns nil of NOERROR is non-nil."
325 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
326 ;; stop there. This can be rather bad in general, but since
327 ;; comment-search-backward is only used to find the comment-column (in
328 ;; comment-set-column) and to find the comment-start string (via
329 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
330 (if (not (re-search-backward comment-start-skip limit t
))
331 (unless noerror
(error "No comment"))
333 (let* ((end (match-end 0))
334 (cs (comment-search-forward end t
))
337 (progn (beginning-of-line)
338 (comment-search-backward limit noerror
))
339 (while (progn (goto-char cs
)
342 (setq cs
(comment-search-forward end t
))))
347 (defun comment-beginning ()
348 "Find the beginning of the enclosing comment.
349 Returns nil if not inside a comment, else moves point and returns
350 the same as `comment-search-forward'."
352 ;; We should really test `in-string-p' but that can be expensive.
353 (unless (eq (get-text-property (point) 'face
) 'font-lock-string-face
)
355 (cs (comment-search-backward nil t
)))
359 (if (comment-forward 1) (> (point) pt
) (eobp)))
364 (defun comment-forward (&optional n
)
365 "Skip forward over N comments.
366 Just like `forward-comment' but only for positive N
367 and can use regexps instead of syntax."
369 (if (< n
0) (error "No comment-backward")
370 (if comment-use-syntax
(forward-comment n
)
372 (skip-syntax-forward " ")
374 (if (and (looking-at comment-start-skip
)
375 (goto-char (match-end 0))
376 (re-search-forward comment-end-skip nil
'move
))
380 (defun comment-enter-backward ()
381 "Move from the end of a comment to the end of its content.
382 Point is assumed to be just at the end of a comment."
385 (progn (backward-char) (skip-syntax-backward " "))
389 (narrow-to-region (point) end
)
390 (if (re-search-forward (concat comment-end-skip
"\\'") nil t
)
391 (goto-char (match-beginning 0))
392 ;; comment-end-skip not found probably because it was not set right.
393 ;; Since \\s> should catch the single-char case, we'll blindly
394 ;; assume we're at the end of a two-char comment-end.
395 (goto-char (point-max))
397 (skip-chars-backward (string (char-after)))
398 (skip-syntax-backward " "))))))
405 (defun comment-indent-default ()
406 "Default for `comment-indent-function'."
407 (if (and (looking-at "\\s<\\s<\\(\\s<\\)?")
408 (or (match-end 1) (/= (current-column) (current-indentation))))
410 (when (or (/= (current-column) (current-indentation))
411 (and (> comment-add
0) (looking-at "\\s<\\S<")))
415 (defun comment-indent (&optional continue
)
416 "Indent this line's comment to comment column, or insert an empty comment.
417 If CONTINUE is non-nil, use the `comment-continuation' markers if any."
419 (comment-normalize-vars)
420 (let* ((empty (save-excursion (beginning-of-line)
421 (looking-at "[ \t]*$")))
422 (starter (or (and continue comment-continue
)
423 (and empty block-comment-start
) comment-start
))
424 (ender (or (and continue comment-continue
"")
425 (and empty block-comment-end
) comment-end
)))
426 (unless starter
(error "No comment syntax defined"))
428 (let* ((eolpos (line-end-position))
429 (begpos (comment-search-forward eolpos t
))
431 ;; An existing comment?
432 (if begpos
(setq cpos
(point-marker))
433 ;; If none, insert one.
435 ;; Some comment-indent-function insist on not moving comments that
436 ;; are in column 0, so we insert a space to avoid this special case
438 (setq begpos
(point))
440 (setq cpos
(point-marker))
443 ;; Compute desired indent.
444 (setq indent
(save-excursion (funcall comment-indent-function
)))
446 ;; comment-indent-function refuses delegates to indent.
447 (indent-according-to-mode)
448 ;; Avoid moving comments past the fill-column.
453 (save-excursion (end-of-line) (current-column))))))
454 (if (= (current-column) indent
)
456 ;; If that's different from current, change it.
457 (skip-chars-backward " \t")
458 (delete-region (point) begpos
)
459 (indent-to (if (bolp) indent
460 (max indent
(1+ (current-column)))))))
462 (set-marker cpos nil
))))
465 (defun comment-set-column (arg)
466 "Set the comment column based on point.
467 With no ARG, set the comment column to the current column.
468 With just minus as arg, kill any comment on this line.
469 With any other arg, set comment column to indentation of the previous comment
470 and then align or create a comment on this line at that column."
473 ((eq arg
'-
) (comment-kill nil
))
477 (comment-search-backward)
479 (goto-char (comment-search-forward (line-end-position)))
480 (setq comment-column
(current-column))
481 (message "Comment column set to %d" comment-column
))
483 (t (setq comment-column
(current-column))
484 (message "Comment column set to %d" comment-column
))))
487 (defun comment-kill (arg)
488 "Kill the comment on this line, if any.
489 With prefix ARG, kill comments on that many lines starting with this one."
491 (dotimes (_ (prefix-numeric-value arg
))
494 (let ((cs (comment-search-forward (line-end-position) t
)))
497 (skip-syntax-backward " ")
500 (kill-region cs
(if (bolp) (1- (point)) (point)))
501 (indent-according-to-mode))))
502 (if arg
(forward-line 1))))
504 (defun comment-padright (str &optional n
)
505 "Construct a string composed of STR plus `comment-padding'.
506 It also adds N copies of the last non-whitespace chars of STR.
507 If STR already contains padding, the corresponding amount is
508 ignored from `comment-padding'.
510 If N is `re', a regexp is returned instead, that would match
511 the string for any N."
513 (when (and (stringp str
) (not (string= "" str
)))
514 ;; Separate the actual string from any leading/trailing padding
515 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str
)
516 (let ((s (match-string 1 str
)) ;actual string
517 (lpad (substring str
0 (match-beginning 1))) ;left padding
518 (rpad (concat (substring str
(match-end 1)) ;original right padding
519 (substring comment-padding
;additional right padding
520 (min (- (match-end 0) (match-end 1))
521 (length comment-padding
)))))
522 ;; We can only duplicate C if the comment-end has multiple chars
523 ;; or if comments can be nested, else the comment-end `}' would
524 ;; be turned into `}}}' where only the first ends the comment
525 ;; and the rest becomes bogus junk.
526 (multi (not (and comment-quote-nested
527 ;; comment-end is a single char
528 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
529 (if (not (symbolp n
))
530 (concat lpad s
(when multi
(make-string n
(aref str
(1- (match-end 1))))) rpad
)
531 ;; construct a regexp that would match anything from just S
532 ;; to any possible output of this function for any N.
533 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
534 lpad
"") ;padding is not required
536 (when multi
"+") ;the last char of S might be repeated
537 (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
538 rpad
"")))))) ;padding is not required
540 (defun comment-padleft (str &optional n
)
541 "Construct a string composed of `comment-padding' plus STR.
542 It also adds N copies of the first non-whitespace chars of STR.
543 If STR already contains padding, the corresponding amount is
544 ignored from `comment-padding'.
546 If N is `re', a regexp is returned instead, that would match
547 the string for any N."
549 (when (and (stringp str
) (not (string= "" str
)))
550 ;; Only separate the left pad because we assume there is no right pad.
551 (string-match "\\`\\s-*" str
)
552 (let ((s (substring str
(match-end 0)))
553 (pad (concat (substring comment-padding
554 (min (- (match-end 0) (match-beginning 0))
555 (length comment-padding
)))
556 (match-string 0 str
)))
557 (c (aref str
(match-end 0))) ;the first non-space char of STR
558 ;; We can only duplicate C if the comment-end has multiple chars
559 ;; or if comments can be nested, else the comment-end `}' would
560 ;; be turned into `}}}' where only the first ends the comment
561 ;; and the rest becomes bogus junk.
562 (multi (not (and comment-quote-nested
563 ;; comment-end is a single char
564 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
565 (if (not (symbolp n
))
566 (concat pad
(when multi
(make-string n c
)) s
)
567 ;; Construct a regexp that would match anything from just S
568 ;; to any possible output of this function for any N.
569 ;; We match any number of leading spaces because this regexp will
570 ;; be used for uncommenting where we might want to remove
571 ;; uncomment markers with arbitrary leading space (because
572 ;; they were aligned).
574 (if multi
(concat (regexp-quote (string c
)) "*"))
575 (regexp-quote s
))))))
578 (defun uncomment-region (beg end
&optional arg
)
579 "Uncomment each line in the BEG..END region.
580 The numeric prefix ARG can specify a number of chars to remove from the
582 (interactive "*r\nP")
583 (comment-normalize-vars)
584 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
587 (setq end
(copy-marker end
))
588 (let ((numarg (prefix-numeric-value arg
))
590 (while (and (< (point) end
)
591 (setq spt
(comment-search-forward end t
)))
593 ;; Find the end of the comment.
596 (unless (comment-forward)
597 (error "Can't find the comment end"))
600 (ccs comment-continue
)
601 (srei (comment-padright ccs
're
))
602 (sre (and srei
(concat "^\\s-*?\\(" srei
"\\)"))))
604 (narrow-to-region spt ept
)
605 ;; Remove the comment-start.
607 (skip-syntax-backward " ")
608 ;; Check for special `=' used sometimes in comment-box.
609 (when (and (= (- (point) (point-min)) 1) (looking-at "=\\{7\\}"))
610 (skip-chars-forward "="))
611 ;; A box-comment starts with a looong comment-start marker.
612 (when (> (- (point) (point-min) (length comment-start
)) 7)
614 (when (looking-at (regexp-quote comment-padding
))
615 (goto-char (match-end 0)))
616 (when (and sre
(looking-at (concat "\\s-*\n\\s-*" srei
)))
617 (goto-char (match-end 0)))
618 (if (null arg
) (delete-region (point-min) (point))
619 (skip-syntax-backward " ")
620 (delete-char (- numarg
)))
622 ;; Remove the end-comment (and leading padding and such).
623 (goto-char (point-max)) (comment-enter-backward)
624 ;; Check for special `=' used sometimes in comment-box.
625 (when (= (- (point-max) (point)) 1)
627 ;; skip `=' but only if there are at least 7.
628 (when (> (skip-chars-backward "=") -
7) (goto-char pos
))))
629 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
630 (when (and (bolp) (not (bobp))) (backward-char))
631 (if (null arg
) (delete-region (point) (point-max))
632 (skip-syntax-forward " ")
633 (delete-char numarg
)))
635 ;; Unquote any nested end-comment.
636 (comment-quote-nested comment-start comment-end t
)
638 ;; Eliminate continuation markers as well.
640 (let* ((cce (comment-string-reverse (or comment-continue
642 (erei (and box
(comment-padleft cce
're
)))
643 (ere (and erei
(concat "\\(" erei
"\\)\\s-*$"))))
644 (goto-char (point-min))
646 (if (and ere
(re-search-forward
647 ere
(line-end-position) t
))
648 (replace-match "" t t nil
(if (match-end 2) 2 1))
651 (re-search-forward sre
(line-end-position) t
))
652 (replace-match "" t t nil
(if (match-end 2) 2 1)))))
653 ;; Go the the end for the next comment.
654 (goto-char (point-max)))))
655 (set-marker end nil
))))
657 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent
&optional block
)
658 "Make the leading and trailing extra lines.
659 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
662 ;; Try to match CS and CE's content so they align aesthetically.
664 (setq ce
(comment-string-strip ce t t
))
665 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce
"\n" cs
))
667 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
670 (let* ((width (- max-indent min-indent
))
671 (s (concat cs
"a=m" cce
))
672 (e (concat ccs
"a=m" ce
))
673 (c (if (string-match ".*\\S-\\S-" cs
)
674 (aref cs
(1- (match-end 0))) ?
=))
675 (_ (string-match "\\s-*a=m\\s-*" s
))
677 (make-string (+ width
(- (match-end 0)
678 (match-beginning 0) (length cs
) 3)) c
)))
679 (setq cs
(replace-match fill t t s
))
680 (string-match "\\s-*a=m\\s-*" e
)
681 (setq ce
(replace-match fill t t e
))))
682 (cons (concat cs
"\n" (make-string min-indent ?
) ccs
)
683 (concat cce
"\n" (make-string (+ min-indent eindent
) ?
) ce
))))
685 (def-edebug-spec comment-with-narrowing t
)
686 (put 'comment-with-narrowing
'lisp-indent-function
2)
687 (defmacro comment-with-narrowing
(beg end
&rest body
)
688 "Execute BODY with BEG..END narrowing.
689 Space is added (and then removed) at the beginning for the text's
690 indentation to be kept as it was before narrowing."
691 (let ((bindent (make-symbol "bindent")))
692 `(let ((,bindent
(save-excursion (goto-char beg
) (current-column))))
694 (narrow-to-region beg end
)
695 (goto-char (point-min))
696 (insert (make-string ,bindent ?
))
699 ;; remove the bindent
701 (goto-char (point-min))
702 (when (looking-at " *")
703 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent
)))
705 (setq ,bindent
(- ,bindent n
))))
709 (while (and (> ,bindent
0) (re-search-forward " *" e t
))
710 (let ((n (min ,bindent
(- (match-end 0) (match-beginning 0) 1))))
711 (goto-char (match-beginning 0))
713 (setq ,bindent
(- ,bindent n
)))))))))))
715 (defun comment-region-internal (beg end cs ce
716 &optional ccs cce block lines indent
)
717 "Comment region BEG..END.
718 CS and CE are the comment start resp end string.
719 CCS and CCE are the comment continuation strings for the start resp end
720 of lines (default to CS and CE).
721 BLOCK indicates that end of lines should be marked with either CCE, CE or CS
722 \(if CE is empty) and that those markers should be aligned.
723 LINES indicates that an extra lines will be used at the beginning and end
724 of the region for CE and CS.
725 INDENT indicates to put CS and CCS at the current indentation of the region
726 rather than at left margin."
727 ;;(assert (< beg end))
729 ;; Sanitize CE and CCE.
730 (if (and (stringp ce
) (string= "" ce
)) (setq ce nil
))
731 (if (and (stringp cce
) (string= "" cce
)) (setq cce nil
))
732 ;; If CE is empty, multiline cannot be used.
733 (unless ce
(setq ccs nil cce nil
))
734 ;; Should we mark empty lines as well ?
735 (if (or ccs block lines
) (setq no-empty nil
))
736 ;; Make sure we have end-markers for BLOCK mode.
737 (when block
(unless ce
(setq ce
(comment-string-reverse cs
))))
738 ;; If BLOCK is not requested, we don't need CCE.
739 (unless block
(setq cce nil
))
740 ;; Continuation defaults to the same as CS and CE.
741 (unless ccs
(setq ccs cs cce ce
))
745 ;; If the end is not at the end of a line and the comment-end
746 ;; is implicit (i.e. a newline), explicitly insert a newline.
747 (unless (or ce
(eolp)) (insert "\n") (indent-according-to-mode))
748 (comment-with-narrowing beg end
749 (let ((min-indent (point-max))
751 (goto-char (point-min))
752 ;; Quote any nested comment marker
753 (comment-quote-nested comment-start comment-end nil
)
755 ;; Loop over all lines to find the needed indentations.
756 (goto-char (point-min))
759 (unless (looking-at "[ \t]*$")
760 (setq min-indent
(min min-indent
(current-indentation))))
762 (setq max-indent
(max max-indent
(current-column)))
763 (not (or (eobp) (progn (forward-line) nil
)))))
765 ;; Inserting ccs can change max-indent by (1- tab-width).
767 (+ max-indent
(max (length cs
) (length ccs
)) tab-width -
1))
768 (unless indent
(setq min-indent
0))
770 ;; make the leading and trailing lines if requested
773 (comment-make-extra-lines
774 cs ce ccs cce min-indent max-indent block
)))
776 (setq ce
(cdr csce
))))
778 (goto-char (point-min))
779 ;; Loop over all lines from BEG to END.
782 (unless (and no-empty
(looking-at "[ \t]*$"))
783 (move-to-column min-indent t
)
784 (insert cs
) (setq cs ccs
) ;switch to CCS after the first line
786 (if (eobp) (setq cce ce
))
788 (when block
(move-to-column max-indent t
))
791 (not (or (eobp) (progn (forward-line) nil
))))))))))
794 (defun comment-region (beg end
&optional arg
)
795 "Comment or uncomment each line in the region.
796 With just \\[universal-argument] prefix arg, uncomment each line in region BEG..END.
797 Numeric prefix arg ARG means use ARG comment characters.
798 If ARG is negative, delete that many comment characters instead.
799 By default, comments start at the left margin, are terminated on each line,
800 even for syntax in which newline does not end the comment and blank lines
801 do not get comments. This can be changed with `comment-style'.
803 The strings used as comment starts are built from
804 `comment-start' without trailing spaces and `comment-padding'."
805 (interactive "*r\nP")
806 (comment-normalize-vars)
807 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
808 (let* ((numarg (prefix-numeric-value arg
))
810 (style (cdr (assoc comment-style comment-styles
)))
811 (lines (nth 2 style
))
812 (block (nth 1 style
))
813 (multi (nth 0 style
)))
815 ;; we use `chars' instead of `syntax' because `\n' might be
816 ;; of end-comment syntax rather than of whitespace syntax.
817 ;; sanitize BEG and END
818 (goto-char beg
) (skip-chars-forward " \t\n\r") (beginning-of-line)
819 (setq beg
(max beg
(point)))
820 (goto-char end
) (skip-chars-backward " \t\n\r") (end-of-line)
821 (setq end
(min end
(point)))
822 (if (>= beg end
) (error "Nothing to comment"))
828 (progn (goto-char beg
) (beginning-of-line)
829 (skip-syntax-forward " ")
831 (progn (goto-char end
) (end-of-line) (skip-syntax-backward " ")
833 (or (not (string= "" comment-end
)) block
)
834 (progn (goto-char beg
) (search-forward "\n" end t
)))))
836 ;; don't add end-markers just because the user asked for `block'
837 (unless (or lines
(string= "" comment-end
)) (setq block nil
))
840 ((consp arg
) (uncomment-region beg end
))
841 ((< numarg
0) (uncomment-region beg end
(- numarg
)))
843 (setq numarg
(if (and (null arg
) (= (length comment-start
) 1))
845 (comment-region-internal
847 (let ((s (comment-padright comment-start numarg
)))
848 (if (string-match comment-start-skip s
) s
849 (comment-padright comment-start
)))
850 (let ((s (comment-padleft comment-end numarg
)))
851 (and s
(if (string-match comment-end-skip s
) s
852 (comment-padright comment-end
))))
853 (if multi
(comment-padright comment-continue numarg
))
854 (if multi
(comment-padleft (comment-string-reverse comment-continue
) numarg
))
859 (defun comment-box (beg end
&optional arg
)
860 "Comment out the BEG..END region, putting it inside a box.
861 The numeric prefix ARG specifies how many characters to add to begin- and
862 end- comment markers additionally to what `comment-add' already specifies."
863 (interactive "*r\np")
864 (let ((comment-style (if (cadr (assoc comment-style comment-styles
))
866 (comment-region beg end
(+ comment-add arg
))))
869 (defun comment-dwim (arg)
870 "Call the comment command you want (Do What I Mean).
871 If the region is active and `transient-mark-mode' is on, call
872 `comment-region' (unless it only consists of comments, in which
873 case it calls `uncomment-region').
874 Else, if the current line is empty, insert a comment and indent it.
875 Else if a prefix ARG is specified, call `comment-kill'.
876 Else, call `comment-indent'."
878 (comment-normalize-vars)
879 (if (and mark-active transient-mark-mode
)
880 (let ((beg (min (point) (mark)))
881 (end (max (point) (mark))))
882 (if (save-excursion ;; check for already commented region
884 (comment-forward (point-max))
886 (uncomment-region beg end arg
)
887 (comment-region beg end arg
)))
888 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
889 ;; FIXME: If there's no comment to kill on this line and ARG is
890 ;; specified, calling comment-kill is not very clever.
891 (if arg
(comment-kill (and (integerp arg
) arg
)) (comment-indent))
892 (let ((add (if arg
(prefix-numeric-value arg
)
893 (if (= (length comment-start
) 1) comment-add
0))))
894 ;; Some modes insist on keeping column 0 comment in column 0
895 ;; so we need to move away from it before inserting the comment.
896 (indent-according-to-mode)
897 (insert (comment-padright comment-start add
))
899 (unless (string= "" comment-end
)
900 (insert (comment-padleft comment-end add
)))
901 (indent-according-to-mode))))))
903 (defcustom comment-auto-fill-only-comments nil
904 "Non-nil means to only auto-fill inside comments.
905 This has no effect in modes that do not define a comment syntax."
910 (defun comment-indent-new-line (&optional soft
)
911 "Break line at point and indent, continuing comment if within one.
912 This indents the body of the continued comment
913 under the previous comment line.
915 This command is intended for styles where you write a comment per line,
916 starting a new comment (and terminating it if necessary) on each line.
917 If you want to continue one comment across several lines, use \\[newline-and-indent].
919 If a fill column is specified, it overrides the use of the comment column
920 or comment indentation.
922 The inserted newline is marked hard if variable `use-hard-newlines' is true,
923 unless optional argument SOFT is non-nil."
925 (comment-normalize-vars t
)
927 ;; If we are not inside a comment and we only auto-fill comments,
928 ;; don't do anything (unless no comment syntax is defined).
929 (unless (and comment-start
930 comment-auto-fill-only-comments
932 (prog1 (setq compos
(comment-beginning))
933 (setq comin
(point))))))
935 ;; Now we know we should auto-fill.
936 (delete-horizontal-space)
937 (if soft
(insert-and-inherit ?
\n) (newline 1))
940 (indent-to-left-margin)
941 (insert-and-inherit fill-prefix
))
943 ;; If necessary check whether we're inside a comment.
944 (unless (or comment-multi-line compos
(null comment-start
))
947 (setq compos
(comment-beginning))
948 (setq comin
(point))))
950 ;; If we're not inside a comment, just try to indent.
951 (if (not compos
) (indent-according-to-mode)
952 (let* ((comment-column
953 ;; The continuation indentation should be somewhere between
954 ;; the current line's indentation (plus 2 for good measure)
955 ;; and the current comment's indentation, with a preference
956 ;; for comment-column.
959 (min (current-column) (max comment-column
960 (+ 2 (current-indentation))))))
961 (comstart (buffer-substring compos comin
))
963 (string-match (regexp-quote (comment-string-strip
967 (if normalp comment-end
968 ;; The comment starter is not the normal comment-start
969 ;; so we can't just use comment-end.
972 (if (not (comment-forward)) comment-end
973 (comment-string-strip
975 (save-excursion (comment-enter-backward) (point))
978 (comment-start comstart
)
979 ;; Force comment-continue to be recreated from comment-start.
980 (comment-continue nil
))
981 (insert-and-inherit ?
\n)
983 (comment-indent (cadr (assoc comment-style comment-styles
)))
987 (let ((comend (buffer-substring pt
(point))))
988 ;; The 1+ is to make sure we delete the \n inserted above.
989 (delete-region pt
(1+ (point)))
993 (forward-char))))))))))
995 (provide 'newcomment
)
997 ;;; newcomment.el ends here