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 ;; Revision: $Id: newcomment.el,v 1.13 2000/05/22 04:23:37 monnier Exp $
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; A replacement for simple.el's comment-related functions.
34 ;; - single-char nestable comment-start can only do the "\\s<+" stuff
35 ;; if the corresponding closing marker happens to be right.
36 ;; - comment-box in TeXinfo generate bogus comments @ccccc@
37 ;; - uncomment-region with a numeric argument can render multichar
38 ;; comment markers invalid.
39 ;; - comment-indent or comment-region when called inside a comment
40 ;; will happily break the surrounding comment.
41 ;; - comment-quote-nested will not (un)quote properly all nested comment
42 ;; markers if there are more than just comment-start and comment-end.
43 ;; For example, in Pascal where {...*) and (*...} are possible.
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
60 (defalias 'indent-for-comment
'comment-indent
)
61 (defalias 'set-comment-column
'comment-set-column
)
62 (defalias 'kill-comment
'comment-kill
)
63 (defalias 'indent-new-comment-line
'comment-indent-new-line
)
66 "Indenting and filling of comments."
70 (defvar comment-use-syntax
'undecided
71 "Non-nil if syntax-tables can be used instead of regexps.
72 Can also be `undecided' which means that a somewhat expensive test will
73 be used to try to determine whether syntax-tables should be trusted
74 to understand comments or not in the given buffer.
75 Major modes should set this variable.")
77 (defcustom comment-column
32
78 "*Column to indent right-margin comments to.
79 Setting this variable automatically makes it local to the current buffer.
80 Each mode establishes a different default value for this variable; you
81 can set the value for a particular mode using that mode's hook."
84 (make-variable-buffer-local 'comment-column
)
86 (defvar comment-start nil
87 "*String to insert to start a new comment, or nil if no comment syntax.")
89 (defvar comment-start-skip nil
90 "*Regexp to match the start of a comment plus everything up to its body.
91 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
92 at the place matched by the close of the first pair.")
94 (defvar comment-end-skip nil
95 "Regexp to match the end of a comment plus everything up to its body.")
97 (defvar comment-end
""
98 "*String to insert to end a new comment.
99 Should be an empty string if comments are terminated by end-of-line.")
101 (defvar comment-indent-hook nil
102 "Obsolete variable for function to compute desired indentation for a comment.
103 This function is called with no args with point at the beginning of
104 the comment's starting delimiter.")
106 (defvar comment-indent-function
107 (lambda () comment-column
)
108 "Function to compute desired indentation for a comment.
109 This function is called with no args with point at the beginning of
110 the comment's starting delimiter.")
112 (defvar block-comment-start nil
)
113 (defvar block-comment-end nil
)
115 (defvar comment-quote-nested t
116 "Non-nil if nested comments should be quoted.
117 This should be locally set by each major mode if needed.")
119 (defvar comment-continue nil
120 "Continuation string to insert for multiline comments.
121 This string will be added at the beginning of each line except the very
122 first one when commenting a region with a commenting style that allows
123 comments to span several lines.
124 It should generally have the same length as `comment-start' in order to
125 preserve indentation.
126 If it is nil a value will be automatically derived from `comment-start'
127 by replacing its first character with a space.")
129 (defvar comment-add
0
130 "How many more comment chars should be inserted by `comment-region'.
131 This determines the default value of the numeric argument of `comment-region'.
132 This should generally stay 0, except for a few modes like Lisp where
133 it can be convenient to set it to 1 so that regions are commented with
136 (defconst comment-styles
137 '((plain .
(nil nil nil nil
))
138 (indent .
(nil nil nil t
))
139 (aligned .
(nil t nil t
))
140 (multi-line .
(t nil nil t
))
141 (extra-line .
(t nil t t
))
143 (box-multi .
(t t t t
)))
144 "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
145 STYLE should be a mnemonic symbol.
146 MULTI specifies that comments are allowed to span multiple lines.
147 ALIGN specifies that the `comment-end' markers should be aligned.
148 EXTRA specifies that an extra line should be used before and after the
149 region to comment (to put the `comment-end' and `comment-start').
150 INDENT specifies that the `comment-start' markers should not be put at the
151 left margin but at the current indentation of the region to comment.")
153 (defcustom comment-style
'plain
154 "*Style to be used for `comment-region'.
155 See `comment-styles' for a list of available styles."
157 :type
`(choice ,@(mapcar (lambda (s) `(const ,(car s
))) comment-styles
)))
159 (defcustom comment-padding
1
160 "Padding string that `comment-region' puts between comment chars and text.
161 Can also be an integer which will be automatically turned into a string
162 of the corresponding number of spaces.
164 Extra spacing between the comment characters and the comment text
165 makes the comment easier to read. Default is 1. nil means 0.")
167 (defcustom comment-multi-line nil
168 "*Non-nil means \\[indent-new-comment-line] should continue same comment
169 on new line, with no new terminator or starter.
170 This is obsolete because you might as well use \\[newline-and-indent]."
178 (defun comment-string-strip (str beforep afterp
)
179 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
180 (string-match (concat "\\`" (if beforep
"\\s-*")
181 "\\(.*?\\)" (if afterp
"\\s-*\n?")
183 (match-string 1 str
))
185 (defun comment-string-reverse (s)
186 "Return the mirror image of string S, without any trailing space."
187 (comment-string-strip (concat (nreverse (string-to-list s
))) nil t
))
189 (defun comment-normalize-vars (&optional noerror
)
190 (if (not comment-start
) (or noerror
(error "No comment syntax is defined"))
191 ;; comment-use-syntax
192 (when (eq comment-use-syntax
'undecided
)
193 (set (make-local-variable 'comment-use-syntax
)
194 (let ((st (syntax-table))
196 (ce (if (string= "" comment-end
) "\n" comment-end
)))
197 ;; Try to skip over a comment using forward-comment
198 ;; to see if the syntax tables properly recognize it.
200 (set-syntax-table st
)
201 (insert cs
" hello " ce
)
202 (goto-char (point-min))
203 (and (forward-comment 1) (eobp))))))
205 (when (integerp comment-padding
)
206 (setq comment-padding
(make-string comment-padding ?
)))
208 ;;(setq comment-start (comment-string-strip comment-start t nil))
209 ;;(setq comment-end (comment-string-strip comment-end nil t))
211 (unless (or comment-continue
(string= comment-end
""))
212 (set (make-local-variable 'comment-continue
)
213 (concat (if (string-match "\\S-\\S-" comment-start
) " " "|")
214 (substring comment-start
1))))
215 ;; comment-skip regexps
216 (unless comment-start-skip
217 (set (make-local-variable 'comment-start-skip
)
218 (concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
219 (regexp-quote (comment-string-strip comment-start t t
))
221 (unless comment-end-skip
222 (let ((ce (if (string= "" comment-end
) "\n"
223 (comment-string-strip comment-end t t
))))
224 (set (make-local-variable 'comment-end-skip
)
225 (concat "\\s-*\\(\\s>" (if comment-quote-nested
"" "+")
226 "\\|" (regexp-quote (substring ce
0 1))
227 (if (and comment-quote-nested
(<= (length ce
) 1)) "" "+")
228 (regexp-quote (substring ce
1))
231 (defun comment-quote-re (str unp
)
232 (concat (regexp-quote (substring str
0 1))
233 "\\\\" (if unp
"+" "*")
234 (regexp-quote (substring str
1))))
236 (defun comment-quote-nested (cs ce unp
)
237 "Quote or unquote nested comments.
238 If UNP is non-nil, unquote nested comment markers."
239 (setq cs
(comment-string-strip cs t t
))
240 (setq ce
(comment-string-strip ce t t
))
241 (when (and comment-quote-nested
(> (length ce
) 0))
242 (let ((re (concat (comment-quote-re ce unp
)
243 "\\|" (comment-quote-re cs unp
))))
244 (goto-char (point-min))
245 (while (re-search-forward re nil t
)
246 (goto-char (match-beginning 0))
248 (if unp
(delete-char 1) (insert "\\"))
249 (when (= (length ce
) 1)
250 ;; If the comment-end is a single char, adding a \ after that
251 ;; "first" char won't deactivate it, so we turn such a CE
252 ;; into !CS. I.e. for pascal, we turn } into !{
254 (when (string= (match-string 0) ce
)
255 (replace-match (concat "!" cs
) t t
))
256 (when (and (< (point-min) (match-beginning 0))
257 (string= (buffer-substring (1- (match-beginning 0))
261 (delete-char (- (match-end 0) (match-beginning 0)))
268 (defun comment-search-forward (limit &optional noerror
)
269 "Find a comment start between point and LIMIT.
270 Moves point to inside the comment and returns the position of the
271 comment-starter. If no comment is found, moves point to LIMIT
272 and raises an error or returns nil of NOERROR is non-nil."
273 (if (not comment-use-syntax
)
274 (if (re-search-forward comment-start-skip limit noerror
)
275 (or (match-end 1) (match-beginning 0))
277 (unless noerror
(error "No comment")))
279 ;; Assume (at first) that pt is outside of any string.
280 (s (parse-partial-sexp pt
(or limit
(point-max)) nil nil nil t
)))
281 (when (and (nth 8 s
) (nth 3 s
))
282 ;; The search ended inside a string. Try to see if it
283 ;; works better when we assume that pt is inside a string.
284 (setq s
(parse-partial-sexp
285 pt
(or limit
(point-max)) nil nil
286 (list nil nil nil
(nth 3 s
) nil nil nil nil
)
288 (if (not (and (nth 8 s
) (not (nth 3 s
))))
289 (unless noerror
(error "No comment"))
290 ;; We found the comment.
293 (bol (line-beginning-position))
295 (while (and (null end
) (>= (point) bol
))
296 (if (looking-at comment-start-skip
)
297 (setq end
(min (or limit
(point-max)) (match-end 0)))
299 (goto-char (or end pos
))
302 (defun comment-search-backward (&optional limit noerror
)
303 "Find a comment start between LIMIT and point.
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 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
308 ;; stop there. This can be rather bad in general, but since
309 ;; comment-search-backward is only used to find the comment-column (in
310 ;; comment-set-column) and to find the comment-start string (via
311 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
312 (if (not (re-search-backward comment-start-skip limit t
))
313 (unless noerror
(error "No comment"))
315 (let* ((end (match-end 0))
316 (cs (comment-search-forward end t
))
319 (progn (beginning-of-line)
320 (comment-search-backward limit noerror
))
321 (while (progn (goto-char cs
)
324 (setq cs
(comment-search-forward end t
))))
329 (defun comment-beginning ()
330 "Find the beginning of the enclosing comment.
331 Returns nil if not inside a comment, else moves point and returns
332 the same as `comment-search-forward'."
334 (cs (comment-search-backward nil t
)))
338 (if (comment-forward 1) (> (point) pt
) (eobp)))
343 (defun comment-forward (&optional n
)
344 "Skip forward over N comments.
345 Just like `forward-comment' but only for positive N
346 and can use regexps instead of syntax."
348 (if (< n
0) (error "No comment-backward")
349 (if comment-use-syntax
(forward-comment n
)
351 (skip-syntax-forward " ")
353 (if (and (looking-at comment-start-skip
)
354 (re-search-forward comment-end-skip nil
'move
))
358 (defun comment-enter-backward ()
359 "Move from the end of a comment to the end of its content.
360 Point is assumed to be just at the end of a comment."
363 (progn (backward-char) (skip-syntax-backward " "))
367 (narrow-to-region (point) end
)
368 (if (re-search-forward (concat comment-end-skip
"\\'") nil t
)
369 (goto-char (match-beginning 0))
370 ;; comment-end-skip not found probably because it was not set right.
371 ;; Since \\s> should catch the single-char case, we'll blindly
372 ;; assume we're at the end of a two-char comment-end.
373 (goto-char (point-max))
375 (skip-chars-backward (string (char-after)))
376 (skip-syntax-backward " "))))))
382 (defun comment-indent (&optional continue
)
383 "Indent this line's comment to comment column, or insert an empty comment.
384 If CONTINUE is non-nil, use the `comment-continuation' markers if any."
386 (comment-normalize-vars)
387 (let* ((empty (save-excursion (beginning-of-line)
388 (looking-at "[ \t]*$")))
389 (starter (or (and continue comment-continue
)
390 (and empty block-comment-start
) comment-start
))
391 (ender (or (and continue comment-continue
"")
392 (and empty block-comment-end
) comment-end
)))
395 (error "No comment syntax defined"))
396 (t (let* ((eolpos (line-end-position))
399 (if (not (setq begpos
(comment-search-forward eolpos t
)))
400 (setq begpos
(point))
401 (setq cpos
(point-marker))
403 ;; Compute desired indent.
404 (if (= (current-column)
405 (setq indent
(if comment-indent-hook
406 (funcall comment-indent-hook
)
407 (funcall comment-indent-function
))))
409 ;; If that's different from current, change it.
410 (skip-chars-backward " \t")
411 (delete-region (point) begpos
)
413 ;; An existing comment?
415 (progn (goto-char cpos
) (set-marker cpos nil
))
419 (insert ender
))))))))
421 (defun comment-set-column (arg)
422 "Set the comment column based on point.
423 With no ARG, set the comment column to the current column.
424 With just minus as arg, kill any comment on this line.
425 With any other arg, set comment column to indentation of the previous comment
426 and then align or create a comment on this line at that column."
429 ((eq arg
'-
) (comment-kill nil
))
433 (comment-search-backward)
435 (goto-char (comment-search-forward (line-end-position)))
436 (setq comment-column
(current-column))
437 (message "Comment column set to %d" comment-column
))
439 (t (setq comment-column
(current-column))
440 (message "Comment column set to %d" comment-column
))))
442 (defun comment-kill (arg)
443 "Kill the comment on this line, if any.
444 With prefix ARG, kill comments on that many lines starting with this one."
446 (dotimes (_ (prefix-numeric-value arg
))
449 (let ((cs (comment-search-forward (line-end-position) t
)))
452 (skip-syntax-backward " ")
455 (kill-region cs
(if (bolp) (1- (point)) (point)))
456 (indent-according-to-mode))))
457 (if arg
(forward-line 1))))
459 (defun comment-padright (str &optional n
)
460 "Construct a string composed of STR plus `comment-padding'.
461 It also adds N copies of the last non-whitespace chars of STR.
462 If STR already contains padding, the corresponding amount is
463 ignored from `comment-padding'.
465 If N is `re', a regexp is returned instead, that would match
466 the string for any N."
468 (when (and (stringp str
) (not (string= "" str
)))
469 ;; Separate the actual string from any leading/trailing padding
470 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str
)
471 (let ((s (match-string 1 str
)) ;actual string
472 (lpad (substring str
0 (match-beginning 1))) ;left padding
473 (rpad (concat (substring str
(match-end 1)) ;original right padding
474 (substring comment-padding
;additional right padding
475 (min (- (match-end 0) (match-end 1))
476 (length comment-padding
)))))
477 ;; We can only duplicate C if the comment-end has multiple chars
478 ;; or if comments can be nested, else the comment-end `}' would
479 ;; be turned into `}}}' where only the first ends the comment
480 ;; and the rest becomes bogus junk.
481 (multi (not (and comment-quote-nested
482 ;; comment-end is a single char
483 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
484 (if (not (symbolp n
))
485 (concat lpad s
(when multi
(make-string n
(aref str
(1- (match-end 1))))) rpad
)
486 ;; construct a regexp that would match anything from just S
487 ;; to any possible output of this function for any N.
488 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
489 lpad
"") ;padding is not required
491 (when multi
"+") ;the last char of S might be repeated
492 (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
493 rpad
"")))))) ;padding is not required
495 (defun comment-padleft (str &optional n
)
496 "Construct a string composed of `comment-padding' plus STR.
497 It also adds N copies of the first non-whitespace chars of STR.
498 If STR already contains padding, the corresponding amount is
499 ignored from `comment-padding'.
501 If N is `re', a regexp is returned instead, that would match
502 the string for any N."
504 (when (and (stringp str
) (not (string= "" str
)))
505 ;; Only separate the left pad because we assume there is no right pad.
506 (string-match "\\`\\s-*" str
)
507 (let ((s (substring str
(match-end 0)))
508 (pad (concat (substring comment-padding
509 (min (- (match-end 0) (match-beginning 0))
510 (length comment-padding
)))
511 (match-string 0 str
)))
512 (c (aref str
(match-end 0))) ;the first non-space char of STR
513 ;; We can only duplicate C if the comment-end has multiple chars
514 ;; or if comments can be nested, else the comment-end `}' would
515 ;; be turned into `}}}' where only the first ends the comment
516 ;; and the rest becomes bogus junk.
517 (multi (not (and comment-quote-nested
518 ;; comment-end is a single char
519 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
520 (if (not (symbolp n
))
521 (concat pad
(when multi
(make-string n c
)) s
)
522 ;; Construct a regexp that would match anything from just S
523 ;; to any possible output of this function for any N.
524 ;; We match any number of leading spaces because this regexp will
525 ;; be used for uncommenting where we might want to remove
526 ;; uncomment markers with arbitrary leading space (because
527 ;; they were aligned).
529 (if multi
(concat (regexp-quote (string c
)) "*"))
530 (regexp-quote s
))))))
532 (defun uncomment-region (beg end
&optional arg
)
533 "Uncomment each line in the BEG..END region.
534 The numeric prefix ARG can specify a number of chars to remove from the
536 (interactive "*r\nP")
537 (comment-normalize-vars)
538 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
541 (setq end
(copy-marker end
))
542 (let ((numarg (prefix-numeric-value arg
))
544 (while (and (< (point) end
)
545 (setq spt
(comment-search-forward end t
)))
547 ;; Find the end of the comment.
550 (unless (comment-forward)
551 (error "Can't find the comment end"))
554 (ccs comment-continue
)
555 (srei (comment-padright ccs
're
))
556 (sre (and srei
(concat "^\\s-*?\\(" srei
"\\)"))))
558 (narrow-to-region spt ept
)
559 ;; Remove the comment-start.
561 (skip-syntax-backward " ")
562 ;; Check for special `=' used sometimes in comment-box.
563 (when (and (= (- (point) (point-min)) 1) (looking-at "=\\{7\\}"))
564 (skip-chars-forward "="))
565 ;; A box-comment starts with a looong comment-start marker.
566 (when (> (- (point) (point-min) (length comment-start
)) 7)
568 (when (looking-at (regexp-quote comment-padding
))
569 (goto-char (match-end 0)))
570 (when (and sre
(looking-at (concat "\\s-*\n\\s-*" srei
)))
571 (goto-char (match-end 0)))
572 (if (null arg
) (delete-region (point-min) (point))
573 (skip-syntax-backward " ")
574 (delete-char (- numarg
)))
576 ;; Remove the end-comment (and leading padding and such).
577 (goto-char (point-max)) (comment-enter-backward)
578 ;; Check for special `=' used sometimes in comment-box.
579 (when (and (= (- (point-max) (point)) 1) (> (point) 7)
580 (save-excursion (backward-char 7)
581 (looking-at "=\\{7\\}")))
582 (skip-chars-backward "="))
583 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
584 (when (and (bolp) (not (bobp))) (backward-char))
585 (if (null arg
) (delete-region (point) (point-max))
586 (skip-syntax-forward " ")
587 (delete-char numarg
)))
589 ;; Unquote any nested end-comment.
590 (comment-quote-nested comment-start comment-end t
)
592 ;; Eliminate continuation markers as well.
594 (let* ((cce (comment-string-reverse (or comment-continue
596 (erei (and box
(comment-padleft cce
're
)))
597 (ere (and erei
(concat "\\(" erei
"\\)\\s-*$"))))
598 (goto-char (point-min))
600 (if (and ere
(re-search-forward
601 ere
(line-end-position) t
))
602 (replace-match "" t t nil
(if (match-end 2) 2 1))
605 (re-search-forward sre
(line-end-position) t
))
606 (replace-match "" t t nil
(if (match-end 2) 2 1)))))
607 ;; Go the the end for the next comment.
608 (goto-char (point-max)))))
609 (set-marker end nil
))))
611 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent
&optional block
)
612 "Make the leading and trailing extra lines.
613 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
616 ;; Try to match CS and CE's content so they align aesthetically.
618 (setq ce
(comment-string-strip ce t t
))
619 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce
"\n" cs
))
621 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
624 (let* ((width (- max-indent min-indent
))
625 (s (concat cs
"a=m" cce
))
626 (e (concat ccs
"a=m" ce
))
627 (c (if (string-match ".*\\S-\\S-" cs
)
628 (aref cs
(1- (match-end 0))) ?
=))
629 (_ (string-match "\\s-*a=m\\s-*" s
))
631 (make-string (+ width
(- (match-end 0)
632 (match-beginning 0) (length cs
) 3)) c
)))
633 (setq cs
(replace-match fill t t s
))
634 (string-match "\\s-*a=m\\s-*" e
)
635 (setq ce
(replace-match fill t t e
))))
636 (cons (concat cs
"\n" (make-string min-indent ?
) ccs
)
637 (concat cce
"\n" (make-string (+ min-indent eindent
) ?
) ce
))))
639 (def-edebug-spec comment-with-narrowing t
)
640 (put 'comment-with-narrowing
'lisp-indent-function
2)
641 (defmacro comment-with-narrowing
(beg end
&rest body
)
642 "Execute BODY with BEG..END narrowing.
643 Space is added (and then removed) at the beginning for the text's
644 indentation to be kept as it was before narrowing."
645 (let ((bindent (make-symbol "bindent")))
646 `(let ((,bindent
(save-excursion (goto-char beg
) (current-column))))
648 (narrow-to-region beg end
)
649 (goto-char (point-min))
650 (insert (make-string ,bindent ?
))
653 ;; remove the bindent
655 (goto-char (point-min))
656 (when (looking-at " *")
657 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent
)))
659 (setq ,bindent
(- ,bindent n
))))
663 (while (and (> ,bindent
0) (re-search-forward " *" e t
))
664 (let ((n (min ,bindent
(- (match-end 0) (match-beginning 0) 1))))
665 (goto-char (match-beginning 0))
667 (setq ,bindent
(- ,bindent n
)))))))))))
669 (defun comment-region-internal (beg end cs ce
670 &optional ccs cce block lines indent
)
671 "Comment region BEG..END.
672 CS and CE are the comment start resp end string.
673 CCS and CCE are the comment continuation strings for the start resp end
674 of lines (default to CS and CE).
675 BLOCK indicates that end of lines should be marked with either CCE, CE or CS
676 \(if CE is empty) and that those markers should be aligned.
677 LINES indicates that an extra lines will be used at the beginning and end
678 of the region for CE and CS.
679 INDENT indicates to put CS and CCS at the current indentation of the region
680 rather than at left margin."
681 ;;(assert (< beg end))
683 ;; Sanitize CE and CCE.
684 (if (and (stringp ce
) (string= "" ce
)) (setq ce nil
))
685 (if (and (stringp cce
) (string= "" cce
)) (setq cce nil
))
686 ;; If CE is empty, multiline cannot be used.
687 (unless ce
(setq ccs nil cce nil
))
688 ;; Should we mark empty lines as well ?
689 (if (or ccs block lines
) (setq no-empty nil
))
690 ;; Make sure we have end-markers for BLOCK mode.
691 (when block
(unless ce
(setq ce
(comment-string-reverse cs
))))
692 ;; If BLOCK is not requested, we don't need CCE.
693 (unless block
(setq cce nil
))
694 ;; Continuation defaults to the same as CS and CE.
695 (unless ccs
(setq ccs cs cce ce
))
699 ;; If the end is not at the end of a line and the comment-end
700 ;; is implicit (i.e. a newline), explicitly insert a newline.
701 (unless (or ce
(eolp)) (insert "\n") (indent-according-to-mode))
702 (comment-with-narrowing beg end
703 (let ((min-indent (point-max))
705 (goto-char (point-min))
706 ;; Quote any nested comment marker
707 (comment-quote-nested comment-start comment-end nil
)
709 ;; Loop over all lines to find the needed indentations.
710 (goto-char (point-min))
713 (unless (looking-at "[ \t]*$")
714 (setq min-indent
(min min-indent
(current-indentation))))
716 (setq max-indent
(max max-indent
(current-column)))
717 (not (or (eobp) (progn (forward-line) nil
)))))
719 ;; Inserting ccs can change max-indent by (1- tab-width).
721 (+ max-indent
(max (length cs
) (length ccs
)) tab-width -
1))
722 (unless indent
(setq min-indent
0))
724 ;; make the leading and trailing lines if requested
727 (comment-make-extra-lines
728 cs ce ccs cce min-indent max-indent block
)))
730 (setq ce
(cdr csce
))))
732 (goto-char (point-min))
733 ;; Loop over all lines from BEG to END.
736 (unless (and no-empty
(looking-at "[ \t]*$"))
737 (move-to-column min-indent t
)
738 (insert cs
) (setq cs ccs
) ;switch to CCS after the first line
740 (if (eobp) (setq cce ce
))
742 (when block
(move-to-column max-indent t
))
745 (not (or (eobp) (progn (forward-line) nil
))))))))))
747 (defun comment-region (beg end
&optional arg
)
748 "Comment or uncomment each line in the region.
749 With just \\[universal-prefix] prefix arg, uncomment each line in region BEG..END.
750 Numeric prefix arg ARG means use ARG comment characters.
751 If ARG is negative, delete that many comment characters instead.
752 Comments are terminated on each line, even for syntax in which newline does
753 not end the comment. Blank lines do not get comments.
755 The strings used as comment starts are built from
756 `comment-start' without trailing spaces and `comment-padding'."
757 (interactive "*r\nP")
758 (comment-normalize-vars)
759 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
760 (let* ((numarg (prefix-numeric-value arg
))
762 (style (cdr (assoc comment-style comment-styles
)))
763 (lines (nth 2 style
))
764 (block (nth 1 style
))
765 (multi (nth 0 style
)))
767 ;; we use `chars' instead of `syntax' because `\n' might be
768 ;; of end-comment syntax rather than of whitespace syntax.
769 ;; sanitize BEG and END
770 (goto-char beg
) (skip-chars-forward " \t\n\r") (beginning-of-line)
771 (setq beg
(max beg
(point)))
772 (goto-char end
) (skip-chars-backward " \t\n\r") (end-of-line)
773 (setq end
(min end
(point)))
774 (if (>= beg end
) (error "Nothing to comment"))
780 (progn (goto-char beg
) (beginning-of-line)
781 (skip-syntax-forward " ")
783 (progn (goto-char end
) (end-of-line) (skip-syntax-backward " ")
785 (or (not (string= "" comment-end
)) block
)
786 (progn (goto-char beg
) (search-forward "\n" end t
)))))
788 ;; don't add end-markers just because the user asked for `block'
789 (unless (or lines
(string= "" comment-end
)) (setq block nil
))
792 ((consp arg
) (uncomment-region beg end
))
793 ((< numarg
0) (uncomment-region beg end
(- numarg
)))
795 (setq numarg
(if (and (null arg
) (= (length comment-start
) 1))
797 (comment-region-internal
799 (let ((s (comment-padright comment-start numarg
)))
800 (if (string-match comment-start-skip s
) s
801 (comment-padright comment-start
)))
802 (let ((s (comment-padleft comment-end numarg
)))
803 (and s
(if (string-match comment-end-skip s
) s
804 (comment-padright comment-end
))))
805 (if multi
(comment-padright comment-continue numarg
))
806 (if multi
(comment-padleft (comment-string-reverse comment-continue
) numarg
))
811 (defun comment-box (beg end
&optional arg
)
812 "Comment out the BEG..END region, putting it inside a box.
813 The numeric prefix ARG specifies how many characters to add to begin- and
814 end- comment markers additionally to what `comment-add' already specifies."
815 (interactive "*r\np")
816 (let ((comment-style (if (cadr (assoc comment-style comment-styles
))
818 (comment-region beg end
(+ comment-add arg
))))
820 (defun comment-dwim (arg)
821 "Call the comment command you want (Do What I Mean).
822 If the region is active and `transient-mark-mode' is on, call
823 `comment-region' (unless it only consists in comments, in which
824 case it calls `uncomment-region').
825 Else, if the current line is empty, insert a comment and indent it.
826 Else if a prefix ARG is specified, call `comment-kill'.
827 Else, call `comment-indent'."
829 (comment-normalize-vars)
830 (if (and mark-active transient-mark-mode
)
831 (let ((beg (min (point) (mark)))
832 (end (max (point) (mark))))
833 (if (save-excursion ;; check for already commented region
835 (comment-forward (point-max))
837 (uncomment-region beg end arg
)
838 (comment-region beg end arg
)))
839 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
840 ;; FIXME: If there's no comment to kill on this line and ARG is
841 ;; specified, calling comment-kill is not very clever.
842 (if arg
(comment-kill (and (integerp arg
) arg
)) (comment-indent))
843 (let ((add (if arg
(prefix-numeric-value arg
)
844 (if (= (length comment-start
) 1) comment-add
0))))
845 (insert (comment-padright comment-start add
))
847 (unless (string= "" comment-end
)
848 (insert (comment-padleft comment-end add
)))
849 (indent-according-to-mode))))))
851 (defcustom comment-auto-fill-only-comments nil
852 "Non-nil means to only auto-fill inside comments.
853 This has no effect in modes that do not define a comment syntax."
857 (defun comment-indent-new-line (&optional soft
)
858 "Break line at point and indent, continuing comment if within one.
859 This indents the body of the continued comment
860 under the previous comment line.
862 This command is intended for styles where you write a comment per line,
863 starting a new comment (and terminating it if necessary) on each line.
864 If you want to continue one comment across several lines, use \\[newline-and-indent].
866 If a fill column is specified, it overrides the use of the comment column
867 or comment indentation.
869 The inserted newline is marked hard if variable `use-hard-newlines' is true,
870 unless optional argument SOFT is non-nil."
872 (comment-normalize-vars t
)
874 ;; If we are not inside a comment and we only auto-fill comments,
875 ;; don't do anything (unless no comment syntax is defined).
876 (unless (and comment-start
877 comment-auto-fill-only-comments
879 (prog1 (setq compos
(comment-beginning))
880 (setq comin
(point))))))
882 ;; Now we know we should auto-fill.
883 (delete-region (progn (skip-chars-backward " \t") (point))
884 (progn (skip-chars-forward " \t") (point)))
885 (if soft
(insert-and-inherit ?
\n) (newline 1))
888 (indent-to-left-margin)
889 (insert-and-inherit fill-prefix
))
891 ;; If necessary check whether we're inside a comment.
892 (unless (or comment-multi-line compos
(null comment-start
))
895 (setq compos
(comment-beginning))
896 (setq comin
(point))))
898 ;; If we're not inside a comment, just try to indent.
899 (if (not compos
) (indent-according-to-mode)
900 (let* ((comment-column
901 ;; The continuation indentation should be somewhere between
902 ;; the current line's indentation (plus 2 for good measure)
903 ;; and the current comment's indentation, with a preference
904 ;; for comment-column.
907 (min (current-column) (max comment-column
908 (+ 2 (current-indentation))))))
909 (comstart (buffer-substring compos comin
))
911 (string-match (regexp-quote (comment-string-strip
915 (if normalp comment-end
916 ;; The comment starter is not the normal comment-start
917 ;; so we can't just use comment-end.
920 (if (not (comment-forward)) comment-end
921 (comment-string-strip
923 (save-excursion (comment-enter-backward) (point))
926 (comment-start comstart
)
927 ;; Force comment-continue to be recreated from comment-start.
928 (comment-continue nil
))
929 (insert-and-inherit ?
\n)
931 (comment-indent (cadr (assoc comment-style comment-styles
)))
935 (let ((comend (buffer-substring pt
(point))))
936 ;; The 1+ is to make sure we delete the \n inserted above.
937 (delete-region pt
(1+ (point)))
941 (forward-char))))))))))
943 (provide 'newcomment
)
946 ;; $Log: newcomment.el,v $
947 ;; Revision 1.13 2000/05/22 04:23:37 monnier
948 ;; (comment-region-internal): Go back to BEG after quoting
949 ;; the nested comment markers.
951 ;; Revision 1.12 2000/05/21 00:27:31 monnier
952 ;; (comment-styles): New `box-multi'.
953 ;; (comment-normalize-vars): Better default for comment-continue to
954 ;; avoid whitespace-only continuations.
955 ;; (comment-search-forward): Always move even in the no-syntax case.
956 ;; (comment-padright): Only obey N if it's only obeyed for padleft.
957 ;; (comment-make-extra-lines): Better handling of empty continuations.
958 ;; Use `=' for the filler if comment-start has only one character.
959 ;; (uncomment-region): Try handling the special `=' filler.
960 ;; (comment-region): Allow LINES even if MULTI is nil.
961 ;; (comment-box): Choose box style based on comment-style.
963 ;; Revision 1.11 2000/05/19 15:37:41 monnier
964 ;; Fix license text and author.
965 ;; Move aliases (indent-for-comment, set-comment-column, kill-comment
966 ;; and indent-new-comment-line) to the beginning of the file.
967 ;; Get rid of the last few CLisms.
968 ;; (comment-forward): Avoid decf.
969 ;; (comment-make-extra-lines): Comment-out asserts.
970 ;; (comment-with-narrowing): Properly create uninterned symbol.
971 ;; (comment-region-internal): Comment-out asserts. Avoid incf and decf.
972 ;; (comment-indent-new-line): Fix bug where compt could be set but
973 ;; not comstart. Set comment-column more carefully.
975 ;; Revision 1.10 2000/05/17 19:32:32 monnier
976 ;; (comment-beginning): Handle unclosed comment.
977 ;; (comment-auto-fill-only-comments): New var.
978 ;; (comment-indent-new-line): Obey comment-auto-fill-only-comments.
979 ;; Align with comment-column rather than previous comment if previous
980 ;; comment's indentation is greater than comment-column.
982 ;; Revision 1.9 2000/05/16 22:02:37 monnier
983 ;; (comment-string-strip): Strip terminating newlines.
984 ;; (comment-search-forward): Make LIMIT compulsory.
985 ;; If an unterminated string (rather than a comment) is found, try again,
986 ;; assuming that the region starts inside a string.
987 ;; (comment-beginning): Make sure we don't move if we find a comment but
988 ;; it's not the one we're in.
989 ;; (comment-enter-backward): Handle the case where comment-end-skip fails.
990 ;; (comment-indent): Normalize variables and use line-end-position.
991 ;; (comment-kill): Use line-end-position.
992 ;; (comment-spill): Remove.
993 ;; (comment-indent-new-line): Renamed from indent-new-comment-line.
994 ;; Cleaned up old commented-out code.
995 ;; Reset comment-continue and comment-end before calling comment-indent.
997 ;; Revision 1.8 2000/05/14 00:56:10 monnier
998 ;; (comment-start, comment-start-skip, comment-end): Made `defvar'.
999 ;; (comment-style): Extract the choice out of comment-styles.
1000 ;; (comment-continue): Just a simple string now.
1001 ;; (comment-normalize-vars): Update for the new comment-continue.
1002 ;; (until, comment-end-quote-re): Removed.
1003 ;; (comment-quote-re, comment-quote-nested): New functions for quoting.
1004 ;; These quote both the end and the start and also work for single-chars.
1005 ;; (comment-padright): Added lots of comments.
1006 ;; (comment-padleft): Added more comments. Check comment-end rather than
1007 ;; STR to determine whether N can be applied or not.
1008 ;; (uncomment-region): Rename BLOCK to BOX.
1009 ;; Use the new comment-quote-nested.
1010 ;; Use only one marker and properly set it back to nil.
1011 ;; Be more picky when eliminating continuation markers.
1013 ;; Revision 1.7 2000/05/13 19:41:08 monnier
1014 ;; (comment-use-syntax): Change `maybe' to `undecided'.
1015 ;; (comment-quote-nested): New. Replaces comment-nested.
1016 ;; (comment-add): Turn into a mere defvar or a integer.
1017 ;; (comment-style): Change default to `plain'.
1018 ;; (comment-styles): Rename `plain' to `indent' and create a new plainer `plain'
1019 ;; (comment-string-reverse): Use nreverse.
1020 ;; (comment-normalize-vars): Change `maybe' to `undecided', add comments.
1021 ;; Don't infer the setting of comment-nested anymore (the default for
1022 ;; comment-quote-nested is safe). Use comment-quote-nested.
1023 ;; (comment-end-quote-re): Use comment-quote-nested.
1024 ;; (comment-search-forward): Obey LIMIT.
1025 ;; (comment-indent): Don't skip forward further past comment-search-forward.
1026 ;; (comment-padleft): Use comment-quote-nested.
1027 ;; (comment-make-extra-lines): Use `cons' rather than `values'.
1028 ;; (comment-region-internal): New arg INDENT. Use line-end-position.
1029 ;; Avoid multiple-value-setq.
1030 ;; (comment-region): Follow the new comment-add semantics.
1031 ;; Don't do box comments any more.
1032 ;; (comment-box): New function.
1033 ;; (comment-dwim): Only do the region stuff is transient-mark-active.
1035 ;; Revision 1.6 1999/12/08 00:19:51 monnier
1036 ;; various fixes and gratuitous movements.
1038 ;; Revision 1.5 1999/11/30 16:20:55 monnier
1039 ;; (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
1040 ;; (comment-use-syntax): Whether to use the syntax-table or just the regexps.
1041 ;; (comment-end-skip): To find the end of the text.
1044 ;; Revision 1.4 1999/11/29 01:31:47 monnier
1045 ;; (comment-find): New function.
1046 ;; (indent-for-comment, set-comment-column, kill-comment): use it.
1048 ;; Revision 1.3 1999/11/29 00:49:18 monnier
1049 ;; (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
1050 ;; (comment-normalize-vars): Set default (cdr comment-continue)
1051 ;; (comment-end-quote-re): new function taken out of `comment-region-internal'
1052 ;; (uncomment-region): Rewritten using syntax-tables. Also unquotes
1053 ;; nested comment-ends and eliminates continuation markers.
1054 ;; (comment-region-internal): Don't create a default for cce.
1055 ;; Use `comment-end-quote-re'.
1057 ;; Revision 1.2 1999/11/28 21:33:55 monnier
1058 ;; (comment-make-extra-lines): Moved out of comment-region-internal.
1059 ;; (comment-with-narrowing): New macro. Provides a way to preserve
1060 ;; indentation inside narrowing.
1061 ;; (comment-region-internal): Add "\n" to close the comment if necessary.
1062 ;; Correctly handle commenting-out when BEG is not bolp.
1064 ;; Revision 1.1 1999/11/28 18:51:06 monnier
1065 ;; First "working" version:
1066 ;; - uncomment-region doesn't work for some unknown reason
1067 ;; - comment-multi-line allows the use of multi line comments
1068 ;; - comment-extra-lines allows yet another style choice
1069 ;; - comment-add allows to default to `;;'
1070 ;; - comment-region on a comment calls uncomment-region
1071 ;; - C-u C-u comment-region aligns comment end markers
1072 ;; - C-u C-u C-u comment-region puts the comment inside a rectangle
1073 ;; - comment-region will try to quote comment-end markers inside the region
1074 ;; - comment-start markers are placed at the indentation level
1077 ;;; newcomment.el ends here