1 ;;; newcomment.el --- (un)comment regions of buffers -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
5 ;; Author: code extracted from Emacs-20's simple.el
6 ;; Maintainer: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: comment uncomment
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 3 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 ;; A replacement for simple.el's comment-related functions.
31 ;; - boxed comments in Perl are not properly uncommented because they are
32 ;; uncommented one-line at a time.
33 ;; - nested comments in sgml-mode are not properly quoted.
34 ;; - single-char nestable comment-start can only do the "\\s<+" stuff
35 ;; if the corresponding closing marker happens to be right.
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 ;; - rebox.el-style refill.
47 ;; - quantized steps in comment-alignment.
48 ;; - try to align tail comments.
49 ;; - check what c-comment-line-break-function has to say.
50 ;; - spill auto-fill of comments onto the end of the next line.
51 ;; - uncomment-region with a consp (for blocks) or somehow make the
52 ;; deletion of continuation markers less dangerous.
53 ;; - drop block-comment-<foo> unless it's really used.
54 ;; - uncomment-region on a subpart of a comment.
55 ;; - support gnu-style "multi-line with space in continue".
56 ;; - somehow allow comment-dwim to use the region even if transient-mark-mode
59 ;; - when auto-filling a comment, try to move the comment to the left
60 ;; rather than break it (if possible).
61 ;; - sometimes default the comment-column to the same
62 ;; one used on the preceding line(s).
67 (defalias 'indent-for-comment
'comment-indent
)
69 (defalias 'set-comment-column
'comment-set-column
)
71 (defalias 'kill-comment
'comment-kill
)
73 (defalias 'indent-new-comment-line
'comment-indent-new-line
)
76 "Indenting and filling of comments."
81 ;; Autoload this to avoid warnings, since some major modes define it.
83 (defvar comment-use-syntax
'undecided
84 "Non-nil if syntax-tables can be used instead of regexps.
85 Can also be `undecided' which means that a somewhat expensive test will
86 be used to try to determine whether syntax-tables should be trusted
87 to understand comments or not in the given buffer.
88 Major modes should set this variable.")
90 (defcustom comment-fill-column nil
91 "Column to use for `comment-indent'. If nil, use `fill-column' instead."
92 :type
'(choice (const nil
) integer
)
96 (defcustom comment-column
32
97 "Column to indent right-margin comments to.
98 Each mode may establish a different default value for this variable; you
99 can set the value for a particular mode using that mode's hook.
100 Comments might be indented to a different value in order not to go beyond
101 `comment-fill-column' or in order to align them with surrounding comments."
104 (make-variable-buffer-local 'comment-column
)
105 ;;;###autoload(put 'comment-column 'safe-local-variable 'integerp)
108 (defvar comment-start nil
109 "*String to insert to start a new comment, or nil if no comment syntax.")
110 ;;;###autoload(put 'comment-start 'safe-local-variable 'string-or-null-p)
113 (defvar comment-start-skip nil
114 "*Regexp to match the start of a comment plus everything up to its body.
115 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
116 at the place matched by the close of the first pair.")
117 ;;;###autoload(put 'comment-start-skip 'safe-local-variable 'string-or-null-p)
120 (defvar comment-end-skip nil
121 "Regexp to match the end of a comment plus everything back to its body.")
122 ;;;###autoload(put 'comment-end-skip 'safe-local-variable 'string-or-null-p)
125 (defvar comment-end
(purecopy "")
126 "*String to insert to end a new comment.
127 Should be an empty string if comments are terminated by end-of-line.")
128 ;;;###autoload(put 'comment-end 'safe-local-variable 'string-or-null-p)
131 (defvar comment-indent-function
'comment-indent-default
132 "Function to compute desired indentation for a comment.
133 This function is called with no args with point at the beginning of
134 the comment's starting delimiter and should return either the desired
135 column indentation or nil.
136 If nil is returned, indentation is delegated to `indent-according-to-mode'.")
139 (defvar comment-insert-comment-function nil
140 "Function to insert a comment when a line doesn't contain one.
141 The function has no args.
143 Applicable at least in modes for languages like fixed-format Fortran where
144 comments always start in column zero.")
146 (defvar comment-region-function
'comment-region-default
147 "Function to comment a region.
148 Its args are the same as those of `comment-region', but BEG and END are
149 guaranteed to be correctly ordered. It is called within `save-excursion'.
151 Applicable at least in modes for languages like fixed-format Fortran where
152 comments always start in column zero.")
154 (defvar uncomment-region-function
'uncomment-region-default
155 "Function to uncomment a region.
156 Its args are the same as those of `uncomment-region', but BEG and END are
157 guaranteed to be correctly ordered. It is called within `save-excursion'.
159 Applicable at least in modes for languages like fixed-format Fortran where
160 comments always start in column zero.")
163 (defvar block-comment-start nil
)
164 (defvar block-comment-end nil
)
166 (defvar comment-quote-nested t
167 "Non-nil if nested comments should be quoted.
168 This should be locally set by each major mode if needed.")
170 (defvar comment-continue nil
171 "Continuation string to insert for multiline comments.
172 This string will be added at the beginning of each line except the very
173 first one when commenting a region with a commenting style that allows
174 comments to span several lines.
175 It should generally have the same length as `comment-start' in order to
176 preserve indentation.
177 If it is nil a value will be automatically derived from `comment-start'
178 by replacing its first character with a space.")
180 (defvar comment-add
0
181 "How many more comment chars should be inserted by `comment-region'.
182 This determines the default value of the numeric argument of `comment-region'.
183 The `plain' comment style doubles this value.
185 This should generally stay 0, except for a few modes like Lisp where
186 it is 1 so that regions are commented with two or three semi-colons.")
189 (defconst comment-styles
190 '((plain nil nil nil nil
191 "Start in column 0 (do not indent), as in Emacs-20")
192 (indent-or-triple nil nil nil multi-char
193 "Start in column 0, but only for single-char starters")
194 (indent nil nil nil t
195 "Full comment per line, ends not aligned")
197 "Full comment per line, ends aligned")
199 "Full comment per line, ends aligned, + top and bottom")
200 (extra-line t nil t t
201 "One comment for all lines, end on a line by itself")
202 (multi-line t nil nil t
203 "One comment for all lines, end on last commented line")
205 "One comment for all lines, + top and bottom"))
206 "Comment region style definitions.
207 Each style is defined with a form (STYLE . (MULTI ALIGN EXTRA INDENT DOC)).
208 DOC should succinctly describe the style.
209 STYLE should be a mnemonic symbol.
210 MULTI specifies that comments are allowed to span multiple lines.
211 e.g. in C it comments regions as
217 if `comment-end' is empty, this has no effect.
219 ALIGN specifies that the `comment-end' markers should be aligned.
220 e.g. in C it comments regions as
226 if `comment-end' is empty, this has no effect, unless EXTRA is also set,
227 in which case the comment gets wrapped in a box.
229 EXTRA specifies that an extra line should be used before and after the
230 region to comment (to put the `comment-end' and `comment-start').
231 e.g. in C it comments regions as
239 if the comment style is not multi line, this has no effect, unless ALIGN
240 is also set, in which case the comment gets wrapped in a box.
242 INDENT specifies that the `comment-start' markers should not be put at the
243 left margin but at the current indentation of the region to comment.
244 If INDENT is `multi-char', that means indent multi-character
245 comment starters, but not one-character comment starters.")
248 (defcustom comment-style
'indent
249 "Style to be used for `comment-region'.
250 See `comment-styles' for a list of available styles."
251 :type
(if (boundp 'comment-styles
)
253 ,@(mapcar (lambda (s)
254 `(const :tag
,(format "%s: %s" (car s
) (nth 5 s
))
262 (defcustom comment-padding
(purecopy " ")
263 "Padding string that `comment-region' puts between comment chars and text.
264 Can also be an integer which will be automatically turned into a string
265 of the corresponding number of spaces.
267 Extra spacing between the comment characters and the comment text
268 makes the comment easier to read. Default is 1. nil means 0."
269 :type
'(choice string integer
(const nil
))
273 (defcustom comment-multi-line nil
274 "Non-nil means `comment-indent-new-line' continues comments.
275 That is, it inserts no new terminator or starter.
276 This affects `auto-fill-mode', which is the main reason to
277 customize this variable.
279 It also affects \\[indent-new-comment-line]. However, if you want this
280 behavior for explicit filling, you might as well use \\[newline-and-indent]."
284 (defcustom comment-empty-lines nil
285 "If nil, `comment-region' does not comment out empty lines.
286 If t, it always comments out empty lines.
287 If `eol' it only comments out empty lines if comments are
288 terminated by the end of line (i.e. `comment-end' is empty)."
289 :type
'(choice (const :tag
"Never" nil
)
290 (const :tag
"Always" t
)
291 (const :tag
"EOl-terminated" 'eol
))
298 (defun comment-string-strip (str beforep afterp
)
299 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
300 (string-match (concat "\\`" (if beforep
"\\s-*")
301 "\\(.*?\\)" (if afterp
"\\s-*\n?")
303 (match-string 1 str
))
305 (defun comment-string-reverse (s)
306 "Return the mirror image of string S, without any trailing space."
307 (comment-string-strip (concat (nreverse (string-to-list s
))) nil t
))
310 (defun comment-normalize-vars (&optional noerror
)
311 "Check and setup the variables needed by other commenting functions.
312 Functions autoloaded from newcomment.el, being entry points, should call
313 this function before any other, so the rest of the code can assume that
314 the variables are properly set."
315 (unless (and (not comment-start
) noerror
)
316 (unless comment-start
317 (let ((cs (read-string "No comment syntax is defined. Use: ")))
318 (if (zerop (length cs
))
319 (error "No comment syntax defined")
320 (set (make-local-variable 'comment-start
) cs
)
321 (set (make-local-variable 'comment-start-skip
) cs
))))
322 ;; comment-use-syntax
323 (when (eq comment-use-syntax
'undecided
)
324 (set (make-local-variable 'comment-use-syntax
)
325 (let ((st (syntax-table))
327 (ce (if (string= "" comment-end
) "\n" comment-end
)))
328 ;; Try to skip over a comment using forward-comment
329 ;; to see if the syntax tables properly recognize it.
331 (set-syntax-table st
)
332 (insert cs
" hello " ce
)
333 (goto-char (point-min))
334 (and (forward-comment 1) (eobp))))))
336 (unless comment-padding
(setq comment-padding
0))
337 (when (integerp comment-padding
)
338 (setq comment-padding
(make-string comment-padding ?
)))
340 ;;(setq comment-start (comment-string-strip comment-start t nil))
341 ;;(setq comment-end (comment-string-strip comment-end nil t))
343 (unless (or comment-continue
(string= comment-end
""))
344 (set (make-local-variable 'comment-continue
)
345 (concat (if (string-match "\\S-\\S-" comment-start
) " " "|")
346 (substring comment-start
1)))
347 ;; Hasn't been necessary yet.
348 ;; (unless (string-match comment-start-skip comment-continue)
349 ;; (kill-local-variable 'comment-continue))
351 ;; comment-skip regexps
352 (unless (and comment-start-skip
353 ;; In case comment-start has changed since last time.
354 (string-match comment-start-skip comment-start
))
355 (set (make-local-variable 'comment-start-skip
)
356 (concat "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
357 (regexp-quote (comment-string-strip comment-start t t
))
358 ;; Let's not allow any \s- but only [ \t] since \n
359 ;; might be both a comment-end marker and \s-.
361 (unless (and comment-end-skip
362 ;; In case comment-end has changed since last time.
363 (string-match comment-end-skip
364 (if (string= "" comment-end
) "\n" comment-end
)))
365 (let ((ce (if (string= "" comment-end
) "\n"
366 (comment-string-strip comment-end t t
))))
367 (set (make-local-variable 'comment-end-skip
)
368 ;; We use [ \t] rather than \s- because we don't want to
369 ;; remove ^L in C mode when uncommenting.
370 (concat "[ \t]*\\(\\s>" (if comment-quote-nested
"" "+")
371 "\\|" (regexp-quote (substring ce
0 1))
372 (if (and comment-quote-nested
(<= (length ce
) 1)) "" "+")
373 (regexp-quote (substring ce
1))
376 (defun comment-quote-re (str unp
)
377 (concat (regexp-quote (substring str
0 1))
378 "\\\\" (if unp
"+" "*")
379 (regexp-quote (substring str
1))))
381 (defun comment-quote-nested (cs ce unp
)
382 "Quote or unquote nested comments.
383 If UNP is non-nil, unquote nested comment markers."
384 (setq cs
(comment-string-strip cs t t
))
385 (setq ce
(comment-string-strip ce t t
))
386 (when (and comment-quote-nested
(> (length ce
) 0))
387 (let ((re (concat (comment-quote-re ce unp
)
388 "\\|" (comment-quote-re cs unp
))))
389 (goto-char (point-min))
390 (while (re-search-forward re nil t
)
391 (goto-char (match-beginning 0))
393 (if unp
(delete-char 1) (insert "\\"))
394 (when (= (length ce
) 1)
395 ;; If the comment-end is a single char, adding a \ after that
396 ;; "first" char won't deactivate it, so we turn such a CE
397 ;; into !CS. I.e. for pascal, we turn } into !{
399 (when (string= (match-string 0) ce
)
400 (replace-match (concat "!" cs
) t t
))
401 (when (and (< (point-min) (match-beginning 0))
402 (string= (buffer-substring (1- (match-beginning 0))
406 (delete-char (- (match-end 0) (match-beginning 0)))
413 (defvar comment-use-global-state nil
414 "Non-nil means that the global syntactic context is used.
415 More specifically, it means that `syntax-ppss' is used to find out whether
416 point is within a string or not. Major modes whose syntax is faithfully
417 described by the syntax-tables can set this to non-nil so comment markers
418 in strings will not confuse Emacs.")
420 (defun comment-search-forward (limit &optional noerror
)
421 "Find a comment start between point and LIMIT.
422 Moves point to inside the comment and returns the position of the
423 comment-starter. If no comment is found, moves point to LIMIT
424 and raises an error or returns nil if NOERROR is non-nil."
425 (if (not comment-use-syntax
)
426 (if (re-search-forward comment-start-skip limit noerror
)
427 (or (match-end 1) (match-beginning 0))
429 (unless noerror
(error "No comment")))
431 ;; Assume (at first) that pt is outside of any string.
432 (s (parse-partial-sexp pt
(or limit
(point-max)) nil nil
433 (if comment-use-global-state
(syntax-ppss pt
))
435 (when (and (nth 8 s
) (nth 3 s
) (not comment-use-global-state
))
436 ;; The search ended at eol inside a string. Try to see if it
437 ;; works better when we assume that pt is inside a string.
438 (setq s
(parse-partial-sexp
439 pt
(or limit
(point-max)) nil nil
440 (list nil nil nil
(nth 3 s
) nil nil nil nil
)
442 (if (or (not (and (nth 8 s
) (not (nth 3 s
))))
443 ;; Make sure the comment starts after PT.
445 (unless noerror
(error "No comment"))
446 ;; We found the comment.
449 (bol (line-beginning-position))
451 (while (and (null end
) (>= (point) bol
))
452 (if (looking-at comment-start-skip
)
453 (setq end
(min (or limit
(point-max)) (match-end 0)))
455 (goto-char (or end pos
))
458 (defun comment-search-backward (&optional limit noerror
)
459 "Find a comment start between LIMIT and point.
460 Moves point to inside the comment and returns the position of the
461 comment-starter. If no comment is found, moves point to LIMIT
462 and raises an error or returns nil if NOERROR is non-nil."
463 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
464 ;; stop there. This can be rather bad in general, but since
465 ;; comment-search-backward is only used to find the comment-column (in
466 ;; comment-set-column) and to find the comment-start string (via
467 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
468 (if (not (re-search-backward comment-start-skip limit t
))
469 (unless noerror
(error "No comment"))
471 (let* ((end (match-end 0))
472 (cs (comment-search-forward end t
))
475 (progn (beginning-of-line)
476 (comment-search-backward limit noerror
))
477 (while (progn (goto-char cs
)
480 (setq cs
(comment-search-forward end t
))))
485 (defun comment-beginning ()
486 "Find the beginning of the enclosing comment.
487 Returns nil if not inside a comment, else moves point and returns
488 the same as `comment-search-backward'."
490 ;; We should really test `in-string-p' but that can be expensive.
491 (unless (eq (get-text-property (point) 'face
) 'font-lock-string-face
)
493 (cs (comment-search-backward nil t
)))
498 ;; For modes where comment-start and comment-end are the same,
499 ;; the search above may have found a `ce' rather than a `cs'.
500 (or (if comment-end-skip
(not (looking-at comment-end-skip
)))
501 ;; Maybe font-lock knows that it's a `cs'?
502 (eq (get-text-property (match-end 0) 'face
)
503 'font-lock-comment-face
)
504 (unless (eq (get-text-property (point) 'face
)
505 'font-lock-comment-face
)
506 ;; Let's assume it's a `cs' if we're on the same line.
507 (>= (line-end-position) pt
)))
508 ;; Make sure that PT is not past the end of the comment.
509 (if (comment-forward 1) (> (point) pt
) (eobp))))
514 (defun comment-forward (&optional n
)
515 "Skip forward over N comments.
516 Just like `forward-comment' but only for positive N
517 and can use regexps instead of syntax."
519 (if (< n
0) (error "No comment-backward")
520 (if comment-use-syntax
(forward-comment n
)
523 (if (or (forward-comment 1)
524 (and (looking-at comment-start-skip
)
525 (goto-char (match-end 0))
526 (re-search-forward comment-end-skip nil
'move
)))
530 (defun comment-enter-backward ()
531 "Move from the end of a comment to the end of its content.
532 Point is assumed to be just at the end of a comment."
535 (progn (backward-char) (skip-syntax-backward " "))
539 (narrow-to-region (line-beginning-position) (point))
540 (goto-char (point-min))
541 (re-search-forward (concat comment-end-skip
"\\'") nil t
)))
542 (goto-char (match-beginning 0)))
543 ;; comment-end-skip not found probably because it was not set
544 ;; right. Since \\s> should catch the single-char case, let's
545 ;; check that we're looking at a two-char comment ender.
546 ((not (or (<= (- (point-max) (line-beginning-position)) 1)
547 (zerop (logand (car (syntax-after (- (point) 1)))
548 ;; Here we take advantage of the fact that
549 ;; the syntax class " " is encoded to 0,
550 ;; so " 4" gives us just the 4 bit.
551 (car (string-to-syntax " 4"))))
552 (zerop (logand (car (syntax-after (- (point) 2)))
553 (car (string-to-syntax " 3"))))))
555 (skip-chars-backward (string (char-after)))
556 (skip-syntax-backward " "))
557 ;; No clue what's going on: maybe we're really not right after the
558 ;; end of a comment. Maybe we're at the "end" because of EOB rather
559 ;; than because of a marker.
560 (t (skip-syntax-backward " ")))))
567 (defun comment-indent-default ()
568 "Default for `comment-indent-function'."
569 (if (and (looking-at "\\s<\\s<\\(\\s<\\)?")
570 (or (match-end 1) (/= (current-column) (current-indentation))))
572 (when (or (/= (current-column) (current-indentation))
573 (and (> comment-add
0) (looking-at "\\s<\\(\\S<\\|\\'\\)")))
576 (defun comment-choose-indent (&optional indent
)
577 "Choose the indentation to use for a right-hand-side comment.
578 The criteria are (in this order):
579 - try to keep the comment's text within `comment-fill-column'.
580 - try to align with surrounding comments.
581 - prefer INDENT (or `comment-column' if nil).
582 Point is expected to be at the start of the comment."
583 (unless indent
(setq indent comment-column
))
584 ;; Avoid moving comments past the fill-column.
585 (let ((max (+ (current-column)
586 (- (or comment-fill-column fill-column
)
587 (save-excursion (end-of-line) (current-column)))))
589 (min (save-excursion (skip-chars-backward " \t")
590 (if (bolp) 0 (1+ (current-column))))))
592 (if (< max min
) (setq max min
))
593 ;; Don't move past the fill column.
594 (if (<= max indent
) (setq indent max
))
595 ;; We can choose anywhere between min..max.
596 ;; Let's try to align to a comment on the previous line.
598 (when (and (zerop (forward-line -
1))
599 (setq other
(comment-search-forward
600 (line-end-position) t
)))
601 (goto-char other
) (setq other
(current-column))))
602 (if (and other
(<= other max
) (>= other min
))
603 ;; There is a comment and it's in the range: bingo!
605 ;; Can't align to a previous comment: let's try to align to comments
606 ;; on the following lines, then. These have not been re-indented yet,
607 ;; so we can't directly align ourselves with them. All we do is to try
608 ;; and choose an indentation point with which they will be able to
611 (while (and (zerop (forward-line 1))
612 (setq other
(comment-search-forward
613 (line-end-position) t
)))
615 (let ((omax (+ (current-column)
616 (- (or comment-fill-column fill-column
)
617 (save-excursion (end-of-line) (current-column)))))
618 (omin (save-excursion (skip-chars-backward " \t")
619 (1+ (current-column)))))
620 (if (and (>= omax min
) (<= omin max
))
621 (progn (setq min
(max omin min
))
622 (setq max
(min omax max
)))
623 ;; Can't align with this anyway, so exit the loop.
624 (goto-char (point-max))))))
625 ;; Return the closest point to indent within min..max.
626 (max min
(min max indent
)))))
629 (defun comment-indent (&optional continue
)
630 "Indent this line's comment to `comment-column', or insert an empty comment.
631 If CONTINUE is non-nil, use the `comment-continue' markers if any."
633 (comment-normalize-vars)
634 (let* ((empty (save-excursion (beginning-of-line)
635 (looking-at "[ \t]*$")))
636 (starter (or (and continue comment-continue
)
637 (and empty block-comment-start
) comment-start
))
638 (ender (or (and continue comment-continue
"")
639 (and empty block-comment-end
) comment-end
)))
640 (unless starter
(error "No comment syntax defined"))
642 (let* ((eolpos (line-end-position))
643 (begpos (comment-search-forward eolpos t
))
645 (if (and comment-insert-comment-function
(not begpos
))
646 ;; If no comment and c-i-c-f is set, let it do everything.
647 (funcall comment-insert-comment-function
)
648 ;; An existing comment?
651 (if (and (not (looking-at "[\t\n ]"))
652 (looking-at comment-end-skip
))
653 ;; The comment is empty and we have skipped all its space
654 ;; and landed right before the comment-ender:
655 ;; Go back to the middle of the space.
656 (forward-char (/ (skip-chars-backward " \t") -
2)))
657 (setq cpos
(point-marker)))
658 ;; If none, insert one.
660 ;; Some `comment-indent-function's insist on not moving
661 ;; comments that are in column 0, so we first go to the
662 ;; likely target column.
663 (indent-to comment-column
)
664 ;; Ensure there's a space before the comment for things
665 ;; like sh where it matters (as well as being neater).
666 (unless (memq (char-before) '(nil ?
\n ?
\t ?\s
))
668 (setq begpos
(point))
670 (setq cpos
(point-marker))
673 ;; Compute desired indent.
674 (setq indent
(save-excursion (funcall comment-indent-function
)))
675 ;; If `indent' is nil and there's code before the comment, we can't
676 ;; use `indent-according-to-mode', so we default to comment-column.
677 (unless (or indent
(save-excursion (skip-chars-backward " \t") (bolp)))
678 (setq indent comment-column
))
680 ;; comment-indent-function refuses: delegate to line-indent.
681 (indent-according-to-mode)
682 ;; If the comment is at the right of code, adjust the indentation.
683 (unless (save-excursion (skip-chars-backward " \t") (bolp))
684 (setq indent
(comment-choose-indent indent
)))
685 ;; Update INDENT to leave at least one space
686 ;; after other nonwhite text on the line.
688 (skip-chars-backward " \t")
690 (setq indent
(max indent
(1+ (current-column))))))
691 ;; If that's different from comment's current position, change it.
692 (unless (= (current-column) indent
)
693 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
696 (set-marker cpos nil
)))))
699 (defun comment-set-column (arg)
700 "Set the comment column based on point.
701 With no ARG, set the comment column to the current column.
702 With just minus as arg, kill any comment on this line.
703 With any other arg, set comment column to indentation of the previous comment
704 and then align or create a comment on this line at that column."
707 ((eq arg
'-
) (comment-kill nil
))
709 (comment-normalize-vars)
712 (comment-search-backward)
714 (goto-char (comment-search-forward (line-end-position)))
715 (setq comment-column
(current-column))
716 (message "Comment column set to %d" comment-column
))
718 (t (setq comment-column
(current-column))
719 (message "Comment column set to %d" comment-column
))))
722 (defun comment-kill (arg)
723 "Kill the first comment on this line, if any.
724 With prefix ARG, kill comments on that many lines starting with this one."
726 (comment-normalize-vars)
727 (dotimes (_i (prefix-numeric-value arg
))
730 (let ((cs (comment-search-forward (line-end-position) t
)))
733 (skip-syntax-backward " ")
736 (kill-region cs
(if (bolp) (1- (point)) (point)))
737 (indent-according-to-mode))))
738 (if arg
(forward-line 1))))
740 (defun comment-padright (str &optional n
)
741 "Construct a string composed of STR plus `comment-padding'.
742 It also adds N copies of the last non-whitespace chars of STR.
743 If STR already contains padding, the corresponding amount is
744 ignored from `comment-padding'.
746 If N is `re', a regexp is returned instead, that would match
747 the string for any N."
749 (when (and (stringp str
) (not (string= "" str
)))
750 ;; Separate the actual string from any leading/trailing padding
751 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str
)
752 (let ((s (match-string 1 str
)) ;actual string
753 (lpad (substring str
0 (match-beginning 1))) ;left padding
754 (rpad (concat (substring str
(match-end 1)) ;original right padding
755 (substring comment-padding
;additional right padding
756 (min (- (match-end 0) (match-end 1))
757 (length comment-padding
)))))
758 ;; We can only duplicate C if the comment-end has multiple chars
759 ;; or if comments can be nested, else the comment-end `}' would
760 ;; be turned into `}}}' where only the first ends the comment
761 ;; and the rest becomes bogus junk.
762 (multi (not (and comment-quote-nested
763 ;; comment-end is a single char
764 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
765 (if (not (symbolp n
))
766 (concat lpad s
(when multi
(make-string n
(aref str
(1- (match-end 1))))) rpad
)
767 ;; construct a regexp that would match anything from just S
768 ;; to any possible output of this function for any N.
769 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
770 lpad
"") ;padding is not required
772 (when multi
"+") ;the last char of S might be repeated
773 (mapconcat (lambda (c) (concat (regexp-quote (string c
)) "?"))
774 rpad
"")))))) ;padding is not required
776 (defun comment-padleft (str &optional n
)
777 "Construct a string composed of `comment-padding' plus STR.
778 It also adds N copies of the first non-whitespace chars of STR.
779 If STR already contains padding, the corresponding amount is
780 ignored from `comment-padding'.
782 If N is `re', a regexp is returned instead, that would match
783 the string for any N."
785 (when (and (stringp str
) (not (string= "" str
)))
786 ;; Only separate the left pad because we assume there is no right pad.
787 (string-match "\\`\\s-*" str
)
788 (let ((s (substring str
(match-end 0)))
789 (pad (concat (substring comment-padding
790 (min (- (match-end 0) (match-beginning 0))
791 (length comment-padding
)))
792 (match-string 0 str
)))
793 (c (aref str
(match-end 0))) ;the first non-space char of STR
794 ;; We can only duplicate C if the comment-end has multiple chars
795 ;; or if comments can be nested, else the comment-end `}' would
796 ;; be turned into `}}}' where only the first ends the comment
797 ;; and the rest becomes bogus junk.
798 (multi (not (and comment-quote-nested
799 ;; comment-end is a single char
800 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end
)))))
801 (if (not (symbolp n
))
802 (concat pad
(when multi
(make-string n c
)) s
)
803 ;; Construct a regexp that would match anything from just S
804 ;; to any possible output of this function for any N.
805 ;; We match any number of leading spaces because this regexp will
806 ;; be used for uncommenting where we might want to remove
807 ;; uncomment markers with arbitrary leading space (because
808 ;; they were aligned).
810 (if multi
(concat (regexp-quote (string c
)) "*"))
811 (regexp-quote s
))))))
814 (defun uncomment-region (beg end
&optional arg
)
815 "Uncomment each line in the BEG .. END region.
816 The numeric prefix ARG can specify a number of chars to remove from the
818 (interactive "*r\nP")
819 (comment-normalize-vars)
820 (when (> beg end
) (setq beg
(prog1 end
(setq end beg
))))
821 ;; Bind `comment-use-global-state' to nil. While uncommenting a region
822 ;; (which works a line at a time), a comment can appear to be
823 ;; included in a mult-line string, but it is actually not.
824 (let ((comment-use-global-state nil
))
826 (funcall uncomment-region-function beg end arg
))))
828 (defun uncomment-region-default (beg end
&optional arg
)
829 "Uncomment each line in the BEG .. END region.
830 The numeric prefix ARG can specify a number of chars to remove from the
833 (setq end
(copy-marker end
))
834 (let* ((numarg (prefix-numeric-value arg
))
835 (ccs comment-continue
)
836 (srei (comment-padright ccs
're
))
837 (csre (comment-padright comment-start
're
))
838 (sre (and srei
(concat "^\\s-*?\\(" srei
"\\)")))
840 (while (and (< (point) end
)
841 (setq spt
(comment-search-forward end t
)))
843 ;; Find the end of the comment.
846 (unless (or (comment-forward)
847 ;; Allow non-terminated comments.
849 (error "Can't find the comment end"))
852 (box-equal nil
)) ;Whether we might be using `=' for boxes.
854 (narrow-to-region spt ept
)
856 ;; Remove the comment-start.
858 (skip-syntax-backward " ")
859 ;; A box-comment starts with a looong comment-start marker.
860 (when (and (or (and (= (- (point) (point-min)) 1)
862 (looking-at "=\\{7\\}")
863 (not (eq (char-before (point-max)) ?
\n))
864 (skip-chars-forward "="))
865 (> (- (point) (point-min) (length comment-start
)) 7))
866 (> (count-lines (point-min) (point-max)) 2))
868 ;; Skip the padding. Padding can come from comment-padding and/or
869 ;; from comment-start, so we first check comment-start.
870 (if (or (save-excursion (goto-char (point-min)) (looking-at csre
))
871 (looking-at (regexp-quote comment-padding
)))
872 (goto-char (match-end 0)))
873 (when (and sre
(looking-at (concat "\\s-*\n\\s-*" srei
)))
874 (goto-char (match-end 0)))
875 (if (null arg
) (delete-region (point-min) (point))
876 (let ((opoint (point-marker)))
877 (skip-syntax-backward " ")
878 (delete-char (- numarg
))
879 (unless (and (not (bobp))
880 (save-excursion (goto-char (point-min))
881 (looking-at comment-start-skip
)))
882 ;; If there's something left but it doesn't look like
883 ;; a comment-start any more, just remove it.
884 (delete-region (point-min) opoint
))))
886 ;; Remove the end-comment (and leading padding and such).
887 (goto-char (point-max)) (comment-enter-backward)
888 ;; Check for special `=' used sometimes in comment-box.
889 (when (and box-equal
(not (eq (char-before (point-max)) ?
\n)))
891 ;; skip `=' but only if there are at least 7.
892 (when (> (skip-chars-backward "=") -
7) (goto-char pos
))))
893 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
894 (when (and (bolp) (not (bobp))) (backward-char))
895 (if (null arg
) (delete-region (point) (point-max))
896 (skip-syntax-forward " ")
898 (unless (or (eobp) (looking-at comment-end-skip
))
899 ;; If there's something left but it doesn't look like
900 ;; a comment-end any more, just remove it.
901 (delete-region (point) (point-max)))))
903 ;; Unquote any nested end-comment.
904 (comment-quote-nested comment-start comment-end t
)
906 ;; Eliminate continuation markers as well.
908 (let* ((cce (comment-string-reverse (or comment-continue
910 (erei (and box
(comment-padleft cce
're
)))
911 (ere (and erei
(concat "\\(" erei
"\\)\\s-*$"))))
912 (goto-char (point-min))
914 (if (and ere
(re-search-forward
915 ere
(line-end-position) t
))
916 (replace-match "" t t nil
(if (match-end 2) 2 1))
919 (re-search-forward sre
(line-end-position) t
))
920 (replace-match "" t t nil
(if (match-end 2) 2 1)))))
921 ;; Go to the end for the next comment.
922 (goto-char (point-max))))))
923 (set-marker end nil
))
925 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent
&optional block
)
926 "Make the leading and trailing extra lines.
927 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
930 ;; Try to match CS and CE's content so they align aesthetically.
932 (setq ce
(comment-string-strip ce t t
))
933 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce
"\n" cs
))
935 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
938 (let* ((width (- max-indent min-indent
))
939 (s (concat cs
"a=m" cce
))
940 (e (concat ccs
"a=m" ce
))
941 (c (if (string-match ".*\\S-\\S-" cs
)
942 (aref cs
(1- (match-end 0)))
943 (if (and (equal comment-end
"") (string-match ".*\\S-" cs
))
944 (aref cs
(1- (match-end 0))) ?
=)))
946 (_ (string-match re s
))
949 (make-string (+ width
(- (match-end 0)
950 (match-beginning 0) lcs
3)) c
)))
951 (setq cs
(replace-match fill t t s
))
952 (when (and (not (string-match comment-start-skip cs
))
953 (string-match "a=m" s
))
954 ;; The whitespace around CS cannot be ignored: put it back.
956 (setq fill
(make-string (- width lcs
) c
))
957 (setq cs
(replace-match fill t t s
)))
959 (setq ce
(replace-match fill t t e
))))
960 (cons (concat cs
"\n" (make-string min-indent ?
) ccs
)
961 (concat cce
"\n" (make-string (+ min-indent eindent
) ?
) ce
))))
963 (defmacro comment-with-narrowing
(beg end
&rest body
)
964 "Execute BODY with BEG..END narrowing.
965 Space is added (and then removed) at the beginning for the text's
966 indentation to be kept as it was before narrowing."
967 (declare (debug t
) (indent 2))
968 (let ((bindent (make-symbol "bindent")))
969 `(let ((,bindent
(save-excursion (goto-char ,beg
) (current-column))))
971 (narrow-to-region ,beg
,end
)
972 (goto-char (point-min))
973 (insert (make-string ,bindent ?
))
976 ;; remove the bindent
978 (goto-char (point-min))
979 (when (looking-at " *")
980 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent
)))
982 (setq ,bindent
(- ,bindent n
))))
986 (while (and (> ,bindent
0) (re-search-forward " *" e t
))
987 (let ((n (min ,bindent
(- (match-end 0) (match-beginning 0) 1))))
988 (goto-char (match-beginning 0))
990 (setq ,bindent
(- ,bindent n
)))))))))))
992 (defun comment-add (arg)
993 "Compute the number of extra comment starter characters.
994 \(Extra semicolons in Lisp mode, extra stars in C mode, etc.)
995 If ARG is non-nil, just follow ARG.
996 If the comment starter is multi-char, just follow ARG.
997 Otherwise obey `comment-add'."
998 (if (and (null arg
) (= (string-match "[ \t]*\\'" comment-start
) 1))
1000 (1- (prefix-numeric-value arg
))))
1002 (defun comment-region-internal (beg end cs ce
1003 &optional ccs cce block lines indent
)
1004 "Comment region BEG .. END.
1005 CS and CE are the comment start string and comment end string,
1006 respectively. CCS and CCE are the comment continuation strings
1007 for the start and end of lines, respectively (default to CS and CE).
1008 BLOCK indicates that end of lines should be marked with either CCE,
1009 CE or CS \(if CE is empty) and that those markers should be aligned.
1010 LINES indicates that an extra lines will be used at the beginning
1011 and end of the region for CE and CS.
1012 INDENT indicates to put CS and CCS at the current indentation of
1013 the region rather than at left margin."
1014 ;;(assert (< beg end))
1015 (let ((no-empty (not (or (eq comment-empty-lines t
)
1016 (and comment-empty-lines
(zerop (length ce
))))))
1018 ;; Sanitize CE and CCE.
1019 (if (and (stringp ce
) (string= "" ce
)) (setq ce nil
))
1020 (setq ce-sanitized ce
)
1021 (if (and (stringp cce
) (string= "" cce
)) (setq cce nil
))
1022 ;; If CE is empty, multiline cannot be used.
1023 (unless ce
(setq ccs nil cce nil
))
1024 ;; Should we mark empty lines as well ?
1025 (if (or ccs block lines
) (setq no-empty nil
))
1026 ;; Make sure we have end-markers for BLOCK mode.
1027 (when block
(unless ce
(setq ce
(comment-string-reverse cs
))))
1028 ;; If BLOCK is not requested, we don't need CCE.
1029 (unless block
(setq cce nil
))
1030 ;; Continuation defaults to the same as CS and CE.
1031 (unless ccs
(setq ccs cs cce ce
))
1035 ;; If the end is not at the end of a line and the comment-end
1036 ;; is implicit (i.e. a newline), explicitly insert a newline.
1037 (unless (or ce-sanitized
(eolp)) (insert "\n") (indent-according-to-mode))
1038 (comment-with-narrowing beg end
1039 (let ((min-indent (point-max))
1041 (goto-char (point-min))
1042 ;; Quote any nested comment marker
1043 (comment-quote-nested comment-start comment-end nil
)
1045 ;; Loop over all lines to find the needed indentations.
1046 (goto-char (point-min))
1049 (unless (looking-at "[ \t]*$")
1050 (setq min-indent
(min min-indent
(current-indentation))))
1052 (setq max-indent
(max max-indent
(current-column)))
1053 (not (or (eobp) (progn (forward-line) nil
)))))
1056 (+ max-indent
(max (length cs
) (length ccs
))
1057 ;; Inserting ccs can change max-indent by (1- tab-width)
1058 ;; but only if there are TABs in the boxed text, of course.
1059 (if (save-excursion (goto-char beg
)
1060 (search-forward "\t" end t
))
1062 (unless indent
(setq min-indent
0))
1064 ;; make the leading and trailing lines if requested
1067 (comment-make-extra-lines
1068 cs ce ccs cce min-indent max-indent block
)))
1069 (setq cs
(car csce
))
1070 (setq ce
(cdr csce
))))
1072 (goto-char (point-min))
1073 ;; Loop over all lines from BEG to END.
1076 (unless (and no-empty
(looking-at "[ \t]*$"))
1077 (move-to-column min-indent t
)
1078 (insert cs
) (setq cs ccs
) ;switch to CCS after the first line
1080 (if (eobp) (setq cce ce
))
1082 (when block
(move-to-column max-indent t
))
1085 (not (or (eobp) (progn (forward-line) nil
))))))))))
1088 (defun comment-region (beg end
&optional arg
)
1089 "Comment or uncomment each line in the region.
1090 With just \\[universal-argument] prefix arg, uncomment each line in region BEG .. END.
1091 Numeric prefix ARG means use ARG comment characters.
1092 If ARG is negative, delete that many comment characters instead.
1094 The strings used as comment starts are built from `comment-start'
1095 and `comment-padding'; the strings used as comment ends are built
1096 from `comment-end' and `comment-padding'.
1098 By default, the `comment-start' markers are inserted at the
1099 current indentation of the region, and comments are terminated on
1100 each line (even for syntaxes in which newline does not end the
1101 comment and blank lines do not get comments). This can be
1102 changed with `comment-style'."
1103 (interactive "*r\nP")
1104 (comment-normalize-vars)
1105 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
1107 ;; FIXME: maybe we should call uncomment depending on ARG.
1108 (funcall comment-region-function beg end arg
)))
1110 (defun comment-region-default (beg end
&optional arg
)
1111 (let* ((numarg (prefix-numeric-value arg
))
1112 (style (cdr (assoc comment-style comment-styles
)))
1113 (lines (nth 2 style
))
1114 (block (nth 1 style
))
1115 (multi (nth 0 style
)))
1117 ;; We use `chars' instead of `syntax' because `\n' might be
1118 ;; of end-comment syntax rather than of whitespace syntax.
1119 ;; sanitize BEG and END
1120 (goto-char beg
) (skip-chars-forward " \t\n\r") (beginning-of-line)
1121 (setq beg
(max beg
(point)))
1122 (goto-char end
) (skip-chars-backward " \t\n\r") (end-of-line)
1123 (setq end
(min end
(point)))
1124 (if (>= beg end
) (error "Nothing to comment"))
1130 (progn (goto-char beg
) (beginning-of-line)
1131 (skip-syntax-forward " ")
1133 (progn (goto-char end
) (end-of-line) (skip-syntax-backward " ")
1135 (or block
(not (string= "" comment-end
)))
1136 (or block
(progn (goto-char beg
) (search-forward "\n" end t
)))))
1138 ;; don't add end-markers just because the user asked for `block'
1139 (unless (or lines
(string= "" comment-end
)) (setq block nil
))
1142 ((consp arg
) (uncomment-region beg end
))
1143 ((< numarg
0) (uncomment-region beg end
(- numarg
)))
1145 (let ((multi-char (/= (string-match "[ \t]*\\'" comment-start
) 1))
1147 (if (eq (nth 3 style
) 'multi-char
)
1150 (setq indent multi-char
1151 ;; Triple if we will put the comment starter at the margin
1152 ;; and the first line of the region isn't indented
1153 ;; at least two spaces.
1154 triple
(and (not multi-char
) (looking-at "\t\\| "))))
1155 (setq indent
(nth 3 style
)))
1157 ;; In Lisp and similar modes with one-character comment starters,
1158 ;; double it by default if `comment-add' says so.
1159 ;; If it isn't indented, triple it.
1160 (if (and (null arg
) (not multi-char
))
1161 (setq numarg
(* comment-add
(if triple
2 1)))
1162 (setq numarg
(1- (prefix-numeric-value arg
))))
1164 (comment-region-internal
1166 (let ((s (comment-padright comment-start numarg
)))
1167 (if (string-match comment-start-skip s
) s
1168 (comment-padright comment-start
)))
1169 (let ((s (comment-padleft comment-end numarg
)))
1170 (and s
(if (string-match comment-end-skip s
) s
1171 (comment-padright comment-end
))))
1172 (if multi
(comment-padright comment-continue numarg
))
1174 (comment-padleft (comment-string-reverse comment-continue
) numarg
))
1180 (defun comment-box (beg end
&optional arg
)
1181 "Comment out the BEG .. END region, putting it inside a box.
1182 The numeric prefix ARG specifies how many characters to add to begin- and
1183 end- comment markers additionally to what `comment-add' already specifies."
1184 (interactive "*r\np")
1185 (comment-normalize-vars)
1186 (let ((comment-style (if (cadr (assoc comment-style comment-styles
))
1188 (comment-region beg end
(+ comment-add arg
))))
1190 (defun comment-only-p (beg end
)
1191 "Return non-nil if the text between BEG and END is all comments."
1194 (comment-forward (point-max))
1198 (defun comment-or-uncomment-region (beg end
&optional arg
)
1199 "Call `comment-region', unless the region only consists of comments,
1200 in which case call `uncomment-region'. If a prefix arg is given, it
1201 is passed on to the respective function."
1202 (interactive "*r\nP")
1203 (comment-normalize-vars)
1204 (funcall (if (comment-only-p beg end
)
1205 'uncomment-region
'comment-region
)
1209 (defun comment-dwim (arg)
1210 "Call the comment command you want (Do What I Mean).
1211 If the region is active and `transient-mark-mode' is on, call
1212 `comment-region' (unless it only consists of comments, in which
1213 case it calls `uncomment-region').
1214 Else, if the current line is empty, call `comment-insert-comment-function'
1215 if it is defined, otherwise insert a comment and indent it.
1216 Else if a prefix ARG is specified, call `comment-kill'.
1217 Else, call `comment-indent'.
1218 You can configure `comment-style' to change the way regions are commented."
1220 (comment-normalize-vars)
1221 (if (and mark-active transient-mark-mode
)
1222 (comment-or-uncomment-region (region-beginning) (region-end) arg
)
1223 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
1224 ;; FIXME: If there's no comment to kill on this line and ARG is
1225 ;; specified, calling comment-kill is not very clever.
1226 (if arg
(comment-kill (and (integerp arg
) arg
)) (comment-indent))
1227 ;; Inserting a comment on a blank line. comment-indent calls
1228 ;; c-i-c-f if needed in the non-blank case.
1229 (if comment-insert-comment-function
1230 (funcall comment-insert-comment-function
)
1231 (let ((add (comment-add arg
)))
1232 ;; Some modes insist on keeping column 0 comment in column 0
1233 ;; so we need to move away from it before inserting the comment.
1234 (indent-according-to-mode)
1235 (insert (comment-padright comment-start add
))
1237 (unless (string= "" comment-end
)
1238 (insert (comment-padleft comment-end add
)))
1239 (indent-according-to-mode)))))))
1242 (defcustom comment-auto-fill-only-comments nil
1243 "Non-nil means to only auto-fill inside comments.
1244 This has no effect in modes that do not define a comment syntax."
1248 (defun comment-valid-prefix-p (prefix compos
)
1249 "Check that the adaptive fill prefix is consistent with the context.
1250 PREFIX is the prefix (presumably guessed by `adaptive-fill-mode').
1251 COMPOS is the position of the beginning of the comment we're in, or nil
1252 if we're not inside a comment."
1253 ;; This consistency checking is mostly needed to workaround the limitation
1254 ;; of auto-fill-mode whose paragraph-determination doesn't pay attention
1255 ;; to comment boundaries.
1257 ;; We're not inside a comment: the prefix shouldn't match
1258 ;; a comment-starter.
1259 (not (and comment-start comment-start-skip
1260 (string-match comment-start-skip prefix
)))
1262 ;; Accept any prefix if the current comment is not EOL-terminated.
1263 (save-excursion (goto-char compos
) (comment-forward) (not (bolp)))
1264 ;; Accept any prefix that starts with the same comment-start marker
1265 ;; as the current one.
1266 (when (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip
"\\)")
1268 (let ((prefix-com (comment-string-strip (match-string 0 prefix
) nil t
)))
1269 (string-match "\\`[ \t]*" prefix-com
)
1270 (let* ((prefix-space (match-string 0 prefix-com
))
1271 (prefix-indent (string-width prefix-space
))
1272 (prefix-comstart (substring prefix-com
(match-end 0))))
1275 ;; The comstart marker is the same.
1276 (and (looking-at (regexp-quote prefix-comstart
))
1277 ;; The indentation as well.
1278 (or (= prefix-indent
1279 (- (current-column) (current-left-margin)))
1280 ;; Check the indentation in two different ways, just
1281 ;; to try and avoid most of the potential funny cases.
1283 (buffer-substring (point)
1284 (progn (move-to-left-margin)
1289 (defun comment-indent-new-line (&optional soft
)
1290 "Break line at point and indent, continuing comment if within one.
1291 This indents the body of the continued comment
1292 under the previous comment line.
1294 This command is intended for styles where you write a comment per line,
1295 starting a new comment (and terminating it if necessary) on each line.
1296 If you want to continue one comment across several lines, use \\[newline-and-indent].
1298 If a fill column is specified, it overrides the use of the comment column
1299 or comment indentation.
1301 The inserted newline is marked hard if variable `use-hard-newlines' is true,
1302 unless optional argument SOFT is non-nil."
1304 (comment-normalize-vars t
)
1306 ;; If we are not inside a comment and we only auto-fill comments,
1307 ;; don't do anything (unless no comment syntax is defined).
1308 (unless (and comment-start
1309 comment-auto-fill-only-comments
1310 (not (called-interactively-p 'interactive
))
1311 (not (save-excursion
1312 (prog1 (setq compos
(comment-beginning))
1313 (setq comin
(point))))))
1315 ;; Now we know we should auto-fill.
1316 ;; Insert the newline before removing empty space so that markers
1317 ;; get preserved better.
1318 (if soft
(insert-and-inherit ?
\n) (newline 1))
1319 (save-excursion (forward-char -
1) (delete-horizontal-space))
1320 (delete-horizontal-space)
1322 (if (and fill-prefix
(not adaptive-fill-mode
))
1323 ;; Blindly trust a non-adaptive fill-prefix.
1325 (indent-to-left-margin)
1326 (insert-before-markers-and-inherit fill-prefix
))
1328 ;; If necessary check whether we're inside a comment.
1329 (unless (or compos
(null comment-start
))
1332 (setq compos
(comment-beginning))
1333 (setq comin
(point))))
1336 ;; If there's an adaptive prefix, use it unless we're inside
1337 ;; a comment and the prefix is not a comment starter.
1339 (comment-valid-prefix-p fill-prefix compos
))
1340 (indent-to-left-margin)
1341 (insert-and-inherit fill-prefix
))
1342 ;; If we're not inside a comment, just try to indent.
1343 ((not compos
) (indent-according-to-mode))
1345 (let* ((comment-column
1346 ;; The continuation indentation should be somewhere between
1347 ;; the current line's indentation (plus 2 for good measure)
1348 ;; and the current comment's indentation, with a preference
1349 ;; for comment-column.
1351 ;; FIXME: use prev line's info rather than first line's.
1353 (min (current-column) (max comment-column
1354 (+ 2 (current-indentation))))))
1355 (comstart (buffer-substring compos comin
))
1357 (string-match (regexp-quote (comment-string-strip
1361 (if normalp comment-end
1362 ;; The comment starter is not the normal comment-start
1363 ;; so we can't just use comment-end.
1366 (if (not (comment-forward)) comment-end
1367 (comment-string-strip
1369 (save-excursion (comment-enter-backward) (point))
1372 (comment-start comstart
)
1373 (continuep (or comment-multi-line
1374 (cadr (assoc comment-style comment-styles
))))
1375 ;; Force comment-continue to be recreated from comment-start.
1376 ;; FIXME: wrong if comment-continue was set explicitly!
1377 ;; FIXME: use prev line's continuation if available.
1378 (comment-continue nil
))
1379 (if (and comment-multi-line
(> (length comment-end
) 0))
1380 (indent-according-to-mode)
1381 (insert-and-inherit ?
\n)
1383 (comment-indent continuep
)
1387 (let ((comend (buffer-substring pt
(point))))
1388 ;; The 1+ is to make sure we delete the \n inserted above.
1389 (delete-region pt
(1+ (point)))
1391 (insert comend
))))))))))))
1393 (provide 'newcomment
)
1395 ;;; newcomment.el ends here