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.10 2000/05/17 19:32:32 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.
42 ;; - try to align tail comments
43 ;; - check what c-comment-line-break-function has to say
44 ;; - spill auto-fill of comments onto the end of the next line
45 ;; - uncomment-region with a consp (for blocks) or somehow make the
46 ;; deletion of continuation markers less dangerous
47 ;; - drop block-comment-<foo> unless it's really used
48 ;; - uncomment-region on a subpart of a comment
49 ;; - support gnu-style "multi-line with space in continue"
50 ;; - somehow allow comment-dwim to use the region even if transient-mark-mode
55 (defalias 'indent-for-comment
'comment-indent
)
56 (defalias 'set-comment-column
'comment-set-column
)
57 (defalias 'kill-comment
'comment-kill
)
58 (defalias 'indent-new-comment-line
'comment-indent-new-line
)
61 "Indenting and filling of comments."
65 (defvar comment-use-syntax
'undecided
66 "Non-nil if syntax-tables can be used instead of regexps.
67 Can also be `undecided' which means that a somewhat expensive test will
68 be used to try to determine whether syntax-tables should be trusted
69 to understand comments or not in the given buffer.
70 Major modes should set this variable.")
72 (defcustom comment-column
32
73 "*Column to indent right-margin comments to.
74 Setting this variable automatically makes it local to the current buffer.
75 Each mode establishes a different default value for this variable; you
76 can set the value for a particular mode using that mode's hook."
79 (make-variable-buffer-local 'comment-column
)
81 (defvar comment-start nil
82 "*String to insert to start a new comment, or nil if no comment syntax.")
84 (defvar comment-start-skip nil
85 "*Regexp to match the start of a comment plus everything up to its body.
86 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
87 at the place matched by the close of the first pair.")
89 (defvar comment-end-skip nil
90 "Regexp to match the end of a comment plus everything up to its body.")
92 (defvar comment-end
""
93 "*String to insert to end a new comment.
94 Should be an empty string if comments are terminated by end-of-line.")
96 (defvar comment-indent-hook nil
97 "Obsolete variable for function to compute desired indentation for a comment.
98 This function is called with no args with point at the beginning of
99 the comment's starting delimiter.")
101 (defvar comment-indent-function
102 (lambda () comment-column
)
103 "Function to compute desired indentation for a comment.
104 This function is called with no args with point at the beginning of
105 the comment's starting delimiter.")
107 (defvar block-comment-start nil
)
108 (defvar block-comment-end nil
)
110 (defvar comment-quote-nested t
111 "Non-nil if nested comments should be quoted.
112 This should be locally set by each major mode if needed.")
114 (defvar comment-continue nil
115 "Continuation string to insert for multiline comments.
116 This string will be added at the beginning of each line except the very
117 first one when commenting a region with a commenting style that allows
118 comments to span several lines.
119 It should generally have the same length as `comment-start' in order to
120 preserve indentation.
121 If it is nil a value will be automatically derived from `comment-start'
122 by replacing its first character with a space.")
124 (defvar comment-add
0
125 "How many more comment chars should be inserted by `comment-region'.
126 This determines the default value of the numeric argument of `comment-region'.
127 This should generally stay 0, except for a few modes like Lisp where
128 it can be convenient to set it to 1 so that regions are commented with
131 (defconst comment-styles
132 '((plain .
(nil nil nil nil
))
133 (indent .
(nil nil nil t
))
134 (aligned .
(nil t nil t
))
135 (multi-line .
(t nil nil t
))
136 (extra-line .
(t nil t t
))
138 "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
139 STYLE should be a mnemonic symbol.
140 MULTI specifies that comments are allowed to span multiple lines.
141 ALIGN specifies that the `comment-end' markers should be aligned.
142 EXTRA specifies that an extra line should be used before and after the
143 region to comment (to put the `comment-end' and `comment-start').
144 INDENT specifies that the `comment-start' markers should not be put at the
145 left margin but at the current indentation of the region to comment.")
147 (defcustom comment-style
'plain
148 "*Style to be used for `comment-region'.
149 See `comment-styles' for a list of available styles."
151 :type
`(choice ,@(mapcar (lambda (s) `(const ,(car s
))) comment-styles
)))
153 (defcustom comment-padding
1
154 "Padding string that `comment-region' puts between comment chars and text.
155 Can also be an integer which will be automatically turned into a string
156 of the corresponding number of spaces.
158 Extra spacing between the comment characters and the comment text
159 makes the comment easier to read. Default is 1. nil means 0.")
161 (defcustom comment-multi-line nil
162 "*Non-nil means \\[indent-new-comment-line] should continue same comment
163 on new line, with no new terminator or starter.
164 This is obsolete because you might as well use \\[newline-and-indent]."
172 (defun comment-string-strip (str beforep afterp
)
173 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
174 (string-match (concat "\\`" (if beforep
"\\s-*")
175 "\\(.*?\\)" (if afterp
"\\s-*\n?")
177 (match-string 1 str
))
179 (defun comment-string-reverse (s)
180 "Return the mirror image of string S, without any trailing space."
181 (comment-string-strip (concat (nreverse (string-to-list s
))) nil t
))
183 (defun comment-normalize-vars (&optional noerror
)
184 (if (not comment-start
) (or noerror
(error "No comment syntax is defined"))
185 ;; comment-use-syntax
186 (when (eq comment-use-syntax
'undecided
)
187 (set (make-local-variable 'comment-use-syntax
)
188 (let ((st (syntax-table))
190 (ce (if (string= "" comment-end
) "\n" comment-end
)))
191 ;; Try to skip over a comment using forward-comment
192 ;; to see if the syntax tables properly recognize it.
194 (set-syntax-table st
)
195 (insert cs
" hello " ce
)
196 (goto-char (point-min))
197 (and (forward-comment 1) (eobp))))))
199 (when (integerp comment-padding
)
200 (setq comment-padding
(make-string comment-padding ?
)))
202 ;;(setq comment-start (comment-string-strip comment-start t nil))
203 ;;(setq comment-end (comment-string-strip comment-end nil t))
205 (unless (or comment-continue
(string= comment-end
""))
206 (set (make-local-variable 'comment-continue
)
207 (concat " " (substring comment-start
1))))
208 ;; comment-skip regexps
209 (unless comment-start-skip
210 (set (make-local-variable 'comment-start-skip
)
211 (concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
212 (regexp-quote (comment-string-strip comment-start t t
))
214 (unless comment-end-skip
215 (let ((ce (if (string= "" comment-end
) "\n"
216 (comment-string-strip comment-end t t
))))
217 (set (make-local-variable 'comment-end-skip
)
218 (concat "\\s-*\\(\\s>" (if comment-quote-nested
"" "+")
219 "\\|" (regexp-quote (substring ce
0 1))
220 (if (and comment-quote-nested
(<= (length ce
) 1)) "" "+")
221 (regexp-quote (substring ce
1))
224 (defun comment-quote-re (str unp
)
225 (concat (regexp-quote (substring str
0 1))
226 "\\\\" (if unp
"+" "*")
227 (regexp-quote (substring str
1))))
229 (defun comment-quote-nested (cs ce unp
)
230 "Quote or unquote nested comments.
231 If UNP is non-nil, unquote nested comment markers."
232 (setq cs
(comment-string-strip cs t t
))
233 (setq ce
(comment-string-strip ce t t
))
234 (when (and comment-quote-nested
(> (length ce
) 0))
235 (let ((re (concat (comment-quote-re ce unp
)
236 "\\|" (comment-quote-re cs unp
))))
237 (goto-char (point-min))
238 (while (re-search-forward re nil t
)
239 (goto-char (match-beginning 0))
241 (if unp
(delete-char 1) (insert "\\"))
242 (when (= (length ce
) 1)
243 ;; If the comment-end is a single char, adding a \ after that
244 ;; "first" char won't deactivate it, so we turn such a CE
245 ;; into !CS. I.e. for pascal, we turn } into !{
247 (when (string= (match-string 0) ce
)
248 (replace-match (concat "!" cs
) t t
))
249 (when (and (< (point-min) (match-beginning 0))
250 (string= (buffer-substring (1- (match-beginning 0))
254 (delete-char (- (match-end 0) (match-beginning 0)))
261 (defun comment-search-forward (limit &optional noerror
)
262 "Find a comment start between point and LIMIT.
263 Moves point to inside the comment and returns the position of the
264 comment-starter. If no comment is found, moves point to LIMIT
265 and raises an error or returns nil of NOERROR is non-nil."
266 (if (not comment-use-syntax
)
267 (when (re-search-forward comment-start-skip limit noerror
)
268 (or (match-end 1) (match-beginning 0)))
270 ;; Assume (at first) that pt is outside of any string.
271 (s (parse-partial-sexp pt
(or limit
(point-max)) nil nil nil t
)))
272 (when (and (nth 8 s
) (nth 3 s
))
273 ;; The search ended inside a string. Try to see if it
274 ;; works better when we assume that pt is inside a string.
275 (setq s
(parse-partial-sexp
276 pt
(or limit
(point-max)) nil nil
277 (list nil nil nil
(nth 3 s
) nil nil nil nil
)
279 (if (not (and (nth 8 s
) (not (nth 3 s
))))
280 (unless noerror
(error "No comment"))
281 ;; We found the comment.
284 (bol (save-excursion (beginning-of-line) (point)))
286 (while (and (null end
) (>= (point) bol
))
287 (if (looking-at comment-start-skip
)
288 (setq end
(min (or limit
(point-max)) (match-end 0)))
293 (defun comment-search-backward (&optional limit noerror
)
294 "Find a comment start between LIMIT and point.
295 Moves point to inside the comment and returns the position of the
296 comment-starter. If no comment is found, moves point to LIMIT
297 and raises an error or returns nil of NOERROR is non-nil."
298 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
299 ;; stop there. This can be rather bad in general, but since
300 ;; comment-search-backward is only used to find the comment-column (in
301 ;; comment-set-column) and to find the comment-start string (via
302 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
303 (if (not (re-search-backward comment-start-skip limit t
))
304 (unless noerror
(error "No comment"))
306 (let* ((end (match-end 0))
307 (cs (comment-search-forward end t
))
310 (progn (beginning-of-line)
311 (comment-search-backward limit noerror
))
312 (while (progn (goto-char cs
)
315 (setq cs
(comment-search-forward end t
))))
320 (defun comment-beginning ()
321 "Find the beginning of the enclosing comment.
322 Returns nil if not inside a comment, else moves point and returns
323 the same as `comment-search-forward'."
325 (cs (comment-search-backward nil t
)))
329 (if (comment-forward 1) (> (point) pt
) (eobp)))
334 (defun comment-forward (&optional n
)
335 "Skip forward over N comments.
336 Just like `forward-comment' but only for positive N
337 and can use regexps instead of syntax."
339 (if (< n
0) (error "No comment-backward")
340 (if comment-use-syntax
(forward-comment n
)
342 (skip-syntax-forward " ")
344 (if (and (looking-at comment-start-skip
)
345 (re-search-forward comment-end-skip nil
'move
))
349 (defun comment-enter-backward ()
350 "Move from the end of a comment to the end of its content.
351 Point is assumed to be just at the end of a comment."
354 (progn (backward-char) (skip-syntax-backward " "))
358 (narrow-to-region (point) end
)
359 (if (re-search-forward (concat comment-end-skip
"\\'") nil t
)
360 (goto-char (match-beginning 0))
361 ;; comment-end-skip not found probably because it was not set right.
362 ;; Since \\s> should catch the single-char case, we'll blindly
363 ;; assume we're at the end of a two-char comment-end.
364 (goto-char (point-max))
366 (skip-chars-backward (string (char-after)))
367 (skip-syntax-backward " "))))))
373 (defun comment-indent (&optional continue
)
374 "Indent this line's comment to comment column, or insert an empty comment.
375 If CONTINUE is non-nil, use the `comment-continuation' markers if any."
377 (comment-normalize-vars)
378 (let* ((empty (save-excursion (beginning-of-line)
379 (looking-at "[ \t]*$")))
380 (starter (or (and continue comment-continue
)
381 (and empty block-comment-start
) comment-start
))
382 (ender (or (and continue comment-continue
"")
383 (and empty block-comment-end
) comment-end
)))
386 (error "No comment syntax defined"))
387 (t (let* ((eolpos (line-end-position))
390 (when (setq begpos
(comment-search-forward eolpos t
))
391 (setq cpos
(point-marker))
393 (setq begpos
(point))
394 ;; Compute desired indent.
395 (if (= (current-column)
396 (setq indent
(if comment-indent-hook
397 (funcall comment-indent-hook
)
398 (funcall comment-indent-function
))))
400 ;; If that's different from current, change it.
401 (skip-chars-backward " \t")
402 (delete-region (point) begpos
)
404 ;; An existing comment?
406 (progn (goto-char cpos
)
407 (set-marker cpos nil
))
411 (insert ender
))))))))
413 (defun comment-set-column (arg)
414 "Set the comment column based on point.
415 With no ARG, set the comment column to the current column.
416 With just minus as arg, kill any comment on this line.
417 With any other arg, set comment column to indentation of the previous comment
418 and then align or create a comment on this line at that column."
421 ((eq arg
'-
) (comment-kill nil
))
425 (comment-search-backward)
427 (goto-char (comment-search-forward (line-end-position)))
428 (setq comment-column
(current-column))
429 (message "Comment column set to %d" comment-column
))
431 (t (setq comment-column
(current-column))
432 (message "Comment column set to %d" comment-column
))))
434 (defun comment-kill (arg)
435 "Kill the comment on this line, if any.
436 With prefix ARG, kill comments on that many lines starting with this one."
438 (dotimes (_ (prefix-numeric-value arg
))
441 (let ((cs (comment-search-forward (line-end-position) t
)))
444 (skip-syntax-backward " ")
447 (kill-region cs
(if (bolp) (1- (point)) (point)))
448 (indent-according-to-mode))))
449 (if arg
(forward-line 1))))
451 (defun comment-padright (str &optional n
)
452 "Construct a string composed of STR plus `comment-padding'.
453 It also adds N copies of the last non-whitespace chars of STR.
454 If STR already contains padding, the corresponding amount is
455 ignored from `comment-padding'.
457 If N is `re', a regexp is returned instead, that would match
458 the string for any N."
460 (when (and (stringp str
) (not (string= "" str
)))
461 ;; Separate the actual string from any leading/trailing padding
462 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str
)
463 (let ((s (match-string 1 str
)) ;actual string
464 (lpad (substring str
0 (match-beginning 1))) ;left padding
465 (rpad (concat (substring str
(match-end 1)) ;original right padding
466 (substring comment-padding
;additional right padding
467 (min (- (match-end 0) (match-end 1))
468 (length comment-padding
))))))
469 (if (not (symbolp n
))
470 (concat lpad s
(make-string n
(aref str
(1- (match-end 1)))) rpad
)
471 ;; construct a regexp that would match anything from just S
472 ;; to any possible output of this function for any N.
473 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
474 lpad
"") ;padding is not required
475 (regexp-quote s
) "+" ;the last char of S might be repeated
476 (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
477 rpad
"")))))) ;padding is not required
479 (defun comment-padleft (str &optional n
)
480 "Construct a string composed of `comment-padding' plus STR.
481 It also adds N copies of the first non-whitespace chars of STR.
482 If STR already contains padding, the corresponding amount is
483 ignored from `comment-padding'.
485 If N is `re', a regexp is returned instead, that would match
486 the string for any N."
488 (when (and (stringp str
) (not (string= "" str
)))
489 ;; Only separate the left pad because we assume there is no right pad.
490 (string-match "\\`\\s-*" str
)
491 (let ((s (substring str
(match-end 0)))
492 (pad (concat (substring comment-padding
493 (min (- (match-end 0) (match-beginning 0))
494 (length comment-padding
)))
495 (match-string 0 str
)))
496 (c (aref str
(match-end 0))) ;the first non-space char of STR
497 ;; We can only duplicate C if the comment-end has multiple chars
498 ;; or if comments can be nested, else the comment-end `}' would
499 ;; be turned into `}}}' where only the first ends the comment
500 ;; and the rest becomes bogus junk.
501 (multi (not (and comment-quote-nested
502 ;; comment-end is a single char
503 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
504 (if (not (symbolp n
))
505 (concat pad
(when multi
(make-string n c
)) s
)
506 ;; Construct a regexp that would match anything from just S
507 ;; to any possible output of this function for any N.
508 ;; We match any number of leading spaces because this regexp will
509 ;; be used for uncommenting where we might want to remove
510 ;; uncomment markers with arbitrary leading space (because
511 ;; they were aligned).
513 (if multi
(concat (regexp-quote (string c
)) "*"))
514 (regexp-quote s
))))))
516 (defun uncomment-region (beg end
&optional arg
)
517 "Uncomment each line in the BEG..END region.
518 The numeric prefix ARG can specify a number of chars to remove from the
520 (interactive "*r\nP")
521 (comment-normalize-vars)
522 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
525 (setq end
(copy-marker end
))
526 (let ((numarg (prefix-numeric-value arg
))
528 (while (and (< (point) end
)
529 (setq spt
(comment-search-forward end t
)))
531 ;; Find the end of the comment.
534 (unless (comment-forward)
535 (error "Can't find the comment end"))
538 (ccs comment-continue
)
539 (srei (comment-padright ccs
're
))
540 (sre (and srei
(concat "^\\s-*?\\(" srei
"\\)"))))
542 (narrow-to-region spt ept
)
543 ;; Remove the comment-start.
545 (skip-syntax-backward " ")
546 ;; A box-comment starts with a looong comment-start marker.
547 (when (> (- (point) (point-min) (length comment-start
)) 7)
549 (when (looking-at (regexp-quote comment-padding
))
550 (goto-char (match-end 0)))
551 (when (and sre
(looking-at (concat "\\s-*\n\\s-*" srei
)))
552 (goto-char (match-end 0)))
553 (if (null arg
) (delete-region (point-min) (point))
554 (skip-syntax-backward " ")
555 (delete-char (- numarg
)))
557 ;; Remove the end-comment (and leading padding and such).
558 (goto-char (point-max)) (comment-enter-backward)
559 (unless (string-match "\\`\\(\n\\|\\s-\\)*\\'"
560 (buffer-substring (point) (point-max)))
561 (when (and (bolp) (not (bobp))) (backward-char))
562 (if (null arg
) (delete-region (point) (point-max))
563 (skip-syntax-forward " ")
564 (delete-char numarg
)))
566 ;; Unquote any nested end-comment.
567 (comment-quote-nested comment-start comment-end t
)
569 ;; Eliminate continuation markers as well.
571 (let* ((cce (comment-string-reverse (or comment-continue
573 (erei (and box
(comment-padleft cce
're
)))
574 (ere (and erei
(concat "\\(" erei
"\\)\\s-*$"))))
575 (goto-char (point-min))
577 (if (and ere
(re-search-forward
578 ere
(line-end-position) t
))
579 (replace-match "" t t nil
(if (match-end 2) 2 1))
582 (re-search-forward sre
(line-end-position) t
))
583 (replace-match "" t t nil
(if (match-end 2) 2 1)))))
584 ;; Go the the end for the next comment.
585 (goto-char (point-max)))))
586 (set-marker end nil
))))
588 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent
&optional block
)
589 "Make the leading and trailing extra lines.
590 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
592 (let* ((s (concat cs
"a=m" cce
"\n"
593 (make-string min-indent ?
) ccs
))
594 (e (concat cce
"\n" (make-string min-indent ?
)
596 ;;(_ (assert (string-match "\\s-*\\(a=m\\)\\s-*" s)))
597 (fill (make-string (+ (- max-indent
602 (aref s
(match-end 0)))))
603 (setq cs
(replace-match fill t t s
))
604 ;;(assert (string-match "\\s-*\\(a=m\\)\\s-*" e))
605 (setq ce
(replace-match fill t t e
)))
606 (when (and ce
(string-match "\\`\\s-*\\(.*\\S-\\)\\s-*\\'" ce
))
607 (setq ce
(match-string 1 ce
)))
608 (let* ((c (concat ce
"a=m" cs
))
609 (indent (if (string-match "\\(.+\\).*a=m\\(.*\\)\\1" c
)
611 (- (match-end 2) (match-beginning 2))
612 (- (match-beginning 0)))
615 (setq ce
(concat cce
"\n" (make-string indent ?
) (or ce cs
)))
616 (setq cs
(concat cs
"\n" (make-string min-indent ?
) ccs
))))
619 (def-edebug-spec comment-with-narrowing t
)
620 (put 'comment-with-narrowing
'lisp-indent-function
2)
621 (defmacro comment-with-narrowing
(beg end
&rest body
)
622 "Execute BODY with BEG..END narrowing.
623 Space is added (and then removed) at the beginning for the text's
624 indentation to be kept as it was before narrowing."
625 (let ((bindent (make-symbol "bindent")))
626 `(let ((,bindent
(save-excursion (goto-char beg
) (current-column))))
628 (narrow-to-region beg end
)
629 (goto-char (point-min))
630 (insert (make-string ,bindent ?
))
633 ;; remove the bindent
635 (goto-char (point-min))
636 (when (looking-at " *")
637 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent
)))
639 (setq ,bindent
(- ,bindent n
))))
643 (while (and (> ,bindent
0) (re-search-forward " *" e t
))
644 (let ((n (min ,bindent
(- (match-end 0) (match-beginning 0) 1))))
645 (goto-char (match-beginning 0))
647 (setq ,bindent
(- ,bindent n
)))))))))))
649 (defun comment-region-internal (beg end cs ce
650 &optional ccs cce block lines indent
)
651 "Comment region BEG..END.
652 CS and CE are the comment start resp. end string.
653 CCS and CCE are the comment continuation strings for the start resp. end
654 of lines (default to CS and CE).
655 BLOCK indicates that end of lines should be marked with either CCE, CE or CS
656 \(if CE is empty) and that those markers should be aligned.
657 LINES indicates that an extra lines will be used at the beginning and end
658 of the region for CE and CS.
659 INDENT indicates to put CS and CCS at the current indentation of the region
660 rather than at left margin."
661 ;;(assert (< beg end))
663 ;; Sanitize CE and CCE.
664 (if (and (stringp ce
) (string= "" ce
)) (setq ce nil
))
665 (if (and (stringp cce
) (string= "" cce
)) (setq cce nil
))
666 ;; If CE is empty, multiline cannot be used.
667 (unless ce
(setq ccs nil cce nil
))
668 ;; Should we mark empty lines as well ?
669 (if (or ccs block lines
) (setq no-empty nil
))
670 ;; Make sure we have end-markers for BLOCK mode.
671 (when block
(unless ce
(setq ce
(comment-string-reverse cs
))))
672 ;; If BLOCK is not requested, we don't need CCE.
673 (unless block
(setq cce nil
))
674 ;; Continuation defaults to the same as CS and CE.
675 (unless ccs
(setq ccs cs cce ce
))
679 ;; If the end is not at the end of a line and the comment-end
680 ;; is implicit (i.e. a newline), explicitly insert a newline.
681 (unless (or ce
(eolp)) (insert "\n") (indent-according-to-mode))
682 (comment-with-narrowing beg end
683 (let ((min-indent (point-max))
685 (goto-char (point-min))
686 ;; Quote any nested comment marker
687 (comment-quote-nested comment-start comment-end nil
)
689 ;; Loop over all lines to find the needed indentations.
692 (unless (looking-at "[ \t]*$")
693 (setq min-indent
(min min-indent
(current-indentation))))
695 (setq max-indent
(max max-indent
(current-column)))
696 (not (or (eobp) (progn (forward-line) nil
)))))
698 ;; Inserting ccs can change max-indent by (1- tab-width).
700 (+ max-indent
(max (length cs
) (length ccs
)) tab-width -
1))
701 (unless indent
(setq min-indent
0))
703 ;; make the leading and trailing lines if requested
706 (comment-make-extra-lines
707 cs ce ccs cce min-indent max-indent block
)))
709 (setq ce
(cdr csce
))))
711 (goto-char (point-min))
712 ;; Loop over all lines from BEG to END.
715 (unless (and no-empty
(looking-at "[ \t]*$"))
716 (move-to-column min-indent t
)
717 (insert cs
) (setq cs ccs
) ;switch to CCS after the first line
719 (if (eobp) (setq cce ce
))
721 (when block
(move-to-column max-indent t
))
724 (not (or (eobp) (progn (forward-line) nil
))))))))))
726 (defun comment-region (beg end
&optional arg
)
727 "Comment or uncomment each line in the region.
728 With just \\[universal-prefix] prefix arg, uncomment each line in region BEG..END.
729 Numeric prefix arg ARG means use ARG comment characters.
730 If ARG is negative, delete that many comment characters instead.
731 Comments are terminated on each line, even for syntax in which newline does
732 not end the comment. Blank lines do not get comments.
734 The strings used as comment starts are built from
735 `comment-start' without trailing spaces and `comment-padding'."
736 (interactive "*r\nP")
737 (comment-normalize-vars)
738 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
739 (let* ((numarg (prefix-numeric-value arg
))
741 (style (cdr (assoc comment-style comment-styles
)))
742 (lines (nth 2 style
))
743 (block (nth 1 style
))
744 (multi (nth 0 style
)))
746 ;; we use `chars' instead of `syntax' because `\n' might be
747 ;; of end-comment syntax rather than of whitespace syntax.
748 ;; sanitize BEG and END
749 (goto-char beg
) (skip-chars-forward " \t\n\r") (beginning-of-line)
750 (setq beg
(max beg
(point)))
751 (goto-char end
) (skip-chars-backward " \t\n\r") (end-of-line)
752 (setq end
(min end
(point)))
753 (if (>= beg end
) (error "Nothing to comment"))
759 (progn (goto-char beg
) (beginning-of-line)
760 (skip-syntax-forward " ")
762 (progn (goto-char end
) (end-of-line) (skip-syntax-backward " ")
764 (or (not (string= "" comment-end
)) block
)
765 (progn (goto-char beg
) (search-forward "\n" end t
)))))
767 ;; don't add end-markers just because the user asked for `block'
768 (unless (or lines
(string= "" comment-end
)) (setq block nil
))
771 ((consp arg
) (uncomment-region beg end
))
772 ((< numarg
0) (uncomment-region beg end
(- numarg
)))
774 (setq numarg
(if (and (null arg
) (= (length comment-start
) 1))
776 (comment-region-internal
778 (let ((s (comment-padright comment-start numarg
)))
779 (if (string-match comment-start-skip s
) s
780 (comment-padright comment-start
)))
781 (let ((s (comment-padleft comment-end numarg
)))
782 (and s
(if (string-match comment-end-skip s
) s
783 (comment-padright comment-end
))))
784 (if multi
(comment-padright comment-continue numarg
))
785 (if multi
(comment-padleft (comment-string-reverse comment-continue
) numarg
))
790 (defun comment-box (beg end
&optional arg
)
791 "Comment out the BEG..END region, putting it inside a box.
792 The numeric prefix ARG specifies how many characters to add to begin- and
793 end- comment markers additionally to what `comment-add' already specifies."
794 (interactive "*r\np")
795 (let ((comment-style 'box
))
796 (comment-region beg end
(+ comment-add arg
))))
798 (defun comment-dwim (arg)
799 "Call the comment command you want (Do What I Mean).
800 If the region is active and `transient-mark-mode' is on, call
801 `comment-region' (unless it only consists in comments, in which
802 case it calls `uncomment-region').
803 Else, if the current line is empty, insert a comment and indent it.
804 Else if a prefix ARG is specified, call `comment-kill'.
805 Else, call `comment-indent'."
807 (comment-normalize-vars)
808 (if (and mark-active transient-mark-mode
)
809 (let ((beg (min (point) (mark)))
810 (end (max (point) (mark))))
811 (if (save-excursion ;; check for already commented region
813 (comment-forward (point-max))
815 (uncomment-region beg end arg
)
816 (comment-region beg end arg
)))
817 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
818 ;; FIXME: If there's no comment to kill on this line and ARG is
819 ;; specified, calling comment-kill is not very clever.
820 (if arg
(comment-kill (and (integerp arg
) arg
)) (comment-indent))
821 (let ((add (if arg
(prefix-numeric-value arg
)
822 (if (= (length comment-start
) 1) comment-add
0))))
823 (insert (comment-padright comment-start add
))
825 (unless (string= "" comment-end
)
826 (insert (comment-padleft comment-end add
)))
827 (indent-according-to-mode))))))
829 (defcustom comment-auto-fill-only-comments nil
830 "Non-nil means to only auto-fill inside comments.
831 This has no effect in modes that do not define a comment syntax."
835 (defun comment-indent-new-line (&optional soft
)
836 "Break line at point and indent, continuing comment if within one.
837 This indents the body of the continued comment
838 under the previous comment line.
840 This command is intended for styles where you write a comment per line,
841 starting a new comment (and terminating it if necessary) on each line.
842 If you want to continue one comment across several lines, use \\[newline-and-indent].
844 If a fill column is specified, it overrides the use of the comment column
845 or comment indentation.
847 The inserted newline is marked hard if variable `use-hard-newlines' is true,
848 unless optional argument SOFT is non-nil."
850 (comment-normalize-vars t
)
852 ;; If we are not inside a comment and we only auto-fill comments,
853 ;; don't do anything (unless no comment syntax is defined).
854 (unless (and comment-start
855 comment-auto-fill-only-comments
857 (prog1 (setq compos
(comment-beginning))
858 (setq comin
(point))))))
860 ;; Now we know we should auto-fill.
861 (delete-region (progn (skip-chars-backward " \t") (point))
862 (progn (skip-chars-forward " \t") (point)))
863 (if soft
(insert-and-inherit ?
\n) (newline 1))
866 (indent-to-left-margin)
867 (insert-and-inherit fill-prefix
))
869 ;; If necessary check whether we're inside a comment.
870 (unless (or comment-multi-line compos
(null comment-start
))
873 (setq compos
(comment-beginning))
874 (setq comin
(point))))
876 ;; If we're not inside a comment, just try to indent.
877 (if (not compos
) (indent-according-to-mode)
878 (let* ((comment-column
879 ;; The continuation indentation should be somewhere between
880 ;; the current line's indentation (plus 2 for good measure)
881 ;; and the current comment's indentation, with a preference
882 ;; for comment-column.
885 (min (current-column) (max comment-column
886 (+ 2 (current-indentation))))))
887 (comstart (buffer-substring compos comin
))
889 (string-match (regexp-quote (comment-string-strip
893 (if normalp comment-end
894 ;; The comment starter is not the normal comment-start
895 ;; so we can't just use comment-end.
898 (if (not (comment-forward)) comment-end
899 (comment-string-strip
901 (save-excursion (comment-enter-backward) (point))
904 (comment-start comstart
)
905 ;; Force comment-continue to be recreated from comment-start.
906 (comment-continue nil
))
907 (insert-and-inherit ?
\n)
909 (comment-indent (cadr (assoc comment-style comment-styles
)))
913 (let ((comend (buffer-substring pt
(point))))
914 ;; The 1+ is to make sure we delete the \n inserted above.
915 (delete-region pt
(1+ (point)))
919 (forward-char))))))))))
921 (provide 'newcomment
)
924 ;; $Log: newcomment.el,v $
925 ;; Revision 1.10 2000/05/17 19:32:32 monnier
926 ;; (comment-beginning): Handle unclosed comment.
927 ;; (comment-auto-fill-only-comments): New var.
928 ;; (comment-indent-new-line): Obey comment-auto-fill-only-comments.
929 ;; Align with comment-column rather than previous comment if previous
930 ;; comment's indentation is greater than comment-column.
932 ;; Revision 1.9 2000/05/16 22:02:37 monnier
933 ;; (comment-string-strip): Strip terminating newlines.
934 ;; (comment-search-forward): Make LIMIT compulsory.
935 ;; If an unterminated string (rather than a comment) is found, try again,
936 ;; assuming that the region starts inside a string.
937 ;; (comment-beginning): Make sure we don't move if we find a comment but
938 ;; it's not the one we're in.
939 ;; (comment-enter-backward): Handle the case where comment-end-skip fails.
940 ;; (comment-indent): Normalize variables and use line-end-position.
941 ;; (comment-kill): Use line-end-position.
942 ;; (comment-spill): Remove.
943 ;; (comment-indent-new-line): Renamed from indent-new-comment-line.
944 ;; Cleaned up old commented-out code.
945 ;; Reset comment-continue and comment-end before calling comment-indent.
947 ;; Revision 1.8 2000/05/14 00:56:10 monnier
948 ;; (comment-start, comment-start-skip, comment-end): Made `defvar'.
949 ;; (comment-style): Extract the choice out of comment-styles.
950 ;; (comment-continue): Just a simple string now.
951 ;; (comment-normalize-vars): Update for the new comment-continue.
952 ;; (until, comment-end-quote-re): Removed.
953 ;; (comment-quote-re, comment-quote-nested): New functions for quoting.
954 ;; These quote both the end and the start and also work for single-chars.
955 ;; (comment-padright): Added lots of comments.
956 ;; (comment-padleft): Added more comments. Check comment-end rather than
957 ;; STR to determine whether N can be applied or not.
958 ;; (uncomment-region): Rename BLOCK to BOX.
959 ;; Use the new comment-quote-nested.
960 ;; Use only one marker and properly set it back to nil.
961 ;; Be more picky when eliminating continuation markers.
963 ;; Revision 1.7 2000/05/13 19:41:08 monnier
964 ;; (comment-use-syntax): Change `maybe' to `undecided'.
965 ;; (comment-quote-nested): New. Replaces comment-nested.
966 ;; (comment-add): Turn into a mere defvar or a integer.
967 ;; (comment-style): Change default to `plain'.
968 ;; (comment-styles): Rename `plain' to `indent' and create a new plainer `plain'
969 ;; (comment-string-reverse): Use nreverse.
970 ;; (comment-normalize-vars): Change `maybe' to `undecided', add comments.
971 ;; Don't infer the setting of comment-nested anymore (the default for
972 ;; comment-quote-nested is safe). Use comment-quote-nested.
973 ;; (comment-end-quote-re): Use comment-quote-nested.
974 ;; (comment-search-forward): Obey LIMIT.
975 ;; (comment-indent): Don't skip forward further past comment-search-forward.
976 ;; (comment-padleft): Use comment-quote-nested.
977 ;; (comment-make-extra-lines): Use `cons' rather than `values'.
978 ;; (comment-region-internal): New arg INDENT. Use line-end-position.
979 ;; Avoid multiple-value-setq.
980 ;; (comment-region): Follow the new comment-add semantics.
981 ;; Don't do box comments any more.
982 ;; (comment-box): New function.
983 ;; (comment-dwim): Only do the region stuff is transient-mark-active.
985 ;; Revision 1.6 1999/12/08 00:19:51 monnier
986 ;; various fixes and gratuitous movements.
988 ;; Revision 1.5 1999/11/30 16:20:55 monnier
989 ;; (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
990 ;; (comment-use-syntax): Whether to use the syntax-table or just the regexps.
991 ;; (comment-end-skip): To find the end of the text.
994 ;; Revision 1.4 1999/11/29 01:31:47 monnier
995 ;; (comment-find): New function.
996 ;; (indent-for-comment, set-comment-column, kill-comment): use it.
998 ;; Revision 1.3 1999/11/29 00:49:18 monnier
999 ;; (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
1000 ;; (comment-normalize-vars): Set default (cdr comment-continue)
1001 ;; (comment-end-quote-re): new function taken out of `comment-region-internal'
1002 ;; (uncomment-region): Rewritten using syntax-tables. Also unquotes
1003 ;; nested comment-ends and eliminates continuation markers.
1004 ;; (comment-region-internal): Don't create a default for cce.
1005 ;; Use `comment-end-quote-re'.
1007 ;; Revision 1.2 1999/11/28 21:33:55 monnier
1008 ;; (comment-make-extra-lines): Moved out of comment-region-internal.
1009 ;; (comment-with-narrowing): New macro. Provides a way to preserve
1010 ;; indentation inside narrowing.
1011 ;; (comment-region-internal): Add "\n" to close the comment if necessary.
1012 ;; Correctly handle commenting-out when BEG is not bolp.
1014 ;; Revision 1.1 1999/11/28 18:51:06 monnier
1015 ;; First "working" version:
1016 ;; - uncomment-region doesn't work for some unknown reason
1017 ;; - comment-multi-line allows the use of multi line comments
1018 ;; - comment-extra-lines allows yet another style choice
1019 ;; - comment-add allows to default to `;;'
1020 ;; - comment-region on a comment calls uncomment-region
1021 ;; - C-u C-u comment-region aligns comment end markers
1022 ;; - C-u C-u C-u comment-region puts the comment inside a rectangle
1023 ;; - comment-region will try to quote comment-end markers inside the region
1024 ;; - comment-start markers are placed at the indentation level
1027 ;;; newcomment.el ends here