(comment-add): New function.
[emacs.git] / lisp / newcomment.el
blob877d5c9f399c1cdb38ae0fbb2569fc65588d7f67
1 ;;; newcomment.el --- (un)comment regions of buffers
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: code extracted from Emacs-20's simple.el
7 ;; Maintainer: Stefan Monnier <monnier@iro.umontreal.ca>
8 ;; 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 2, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; A replacement for simple.el's comment-related functions.
31 ;;; Bugs:
33 ;; - boxed comments in Perl are not properly uncommented because they are
34 ;; uncommented one-line at a time.
35 ;; - nested comments in sgml-mode are not properly quoted.
36 ;; - single-char nestable comment-start can only do the "\\s<+" stuff
37 ;; if the corresponding closing marker happens to be right.
38 ;; - uncomment-region with a numeric argument can render multichar
39 ;; comment markers invalid.
40 ;; - comment-indent or comment-region when called inside a comment
41 ;; will happily break the surrounding comment.
42 ;; - comment-quote-nested will not (un)quote properly all nested comment
43 ;; markers if there are more than just comment-start and comment-end.
44 ;; For example, in Pascal where {...*) and (*...} are possible.
46 ;;; Todo:
48 ;; - rebox.el-style refill.
49 ;; - quantized steps in comment-alignment.
50 ;; - try to align tail comments.
51 ;; - check what c-comment-line-break-function has to say.
52 ;; - spill auto-fill of comments onto the end of the next line.
53 ;; - uncomment-region with a consp (for blocks) or somehow make the
54 ;; deletion of continuation markers less dangerous.
55 ;; - drop block-comment-<foo> unless it's really used.
56 ;; - uncomment-region on a subpart of a comment.
57 ;; - support gnu-style "multi-line with space in continue".
58 ;; - somehow allow comment-dwim to use the region even if transient-mark-mode
59 ;; is not turned on.
61 ;; - when auto-filling a comment, try to move the comment to the left
62 ;; rather than break it (if possible).
63 ;; - sometimes default the comment-column to the same
64 ;; one used on the preceding line(s).
66 ;;; Code:
68 ;;;###autoload
69 (defalias 'indent-for-comment 'comment-indent)
70 ;;;###autoload
71 (defalias 'set-comment-column 'comment-set-column)
72 ;;;###autoload
73 (defalias 'kill-comment 'comment-kill)
74 ;;;###autoload
75 (defalias 'indent-new-comment-line 'comment-indent-new-line)
77 (defgroup comment nil
78 "Indenting and filling of comments."
79 :prefix "comment-"
80 :version "21.1"
81 :group 'fill)
83 ;; Autoload this to avoid warnings, since some major modes define it.
84 ;;;###autoload
85 (defvar comment-use-syntax 'undecided
86 "Non-nil if syntax-tables can be used instead of regexps.
87 Can also be `undecided' which means that a somewhat expensive test will
88 be used to try to determine whether syntax-tables should be trusted
89 to understand comments or not in the given buffer.
90 Major modes should set this variable.")
92 (defcustom comment-fill-column nil
93 "Column to use for `comment-indent'. If nil, use `fill-column' instead."
94 :type '(choice (const nil) integer)
95 :group 'comment)
97 ;;;###autoload
98 (defcustom comment-column 32
99 "Column to indent right-margin comments to.
100 Each mode establishes a different default value for this variable; you
101 can set the value for a particular mode using that mode's hook.
102 Comments might be indented to a value smaller than this in order
103 not to go beyond `comment-fill-column'."
104 :type 'integer
105 :group 'comment)
106 (make-variable-buffer-local 'comment-column)
108 ;;;###autoload
109 (defvar comment-start nil
110 "*String to insert to start a new comment, or nil if no comment syntax.")
112 ;;;###autoload
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.")
118 ;;;###autoload
119 (defvar comment-end-skip nil
120 "Regexp to match the end of a comment plus everything up to its body.")
122 ;;;###autoload
123 (defvar comment-end ""
124 "*String to insert to end a new comment.
125 Should be an empty string if comments are terminated by end-of-line.")
127 ;;;###autoload
128 (defvar comment-indent-function 'comment-indent-default
129 "Function to compute desired indentation for a comment.
130 This function is called with no args with point at the beginning of
131 the comment's starting delimiter and should return either the desired
132 column indentation or nil.
133 If nil is returned, indentation is delegated to `indent-according-to-mode'.")
135 ;;;###autoload
136 (defvar comment-insert-comment-function nil
137 "Function to insert a comment when a line doesn't contain one.
138 The function has no args.
140 Applicable at least in modes for languages like fixed-format Fortran where
141 comments always start in column zero.")
143 (defvar comment-region-function 'comment-region-default
144 "Function to comment a region.
145 Its args are the same as those of `comment-region', but BEG and END are
146 guaranteed to be correctly ordered. It is called within `save-excursion'.
148 Applicable at least in modes for languages like fixed-format Fortran where
149 comments always start in column zero.")
151 (defvar uncomment-region-function 'uncomment-region-default
152 "Function to uncomment a region.
153 Its args are the same as those of `uncomment-region', but BEG and END are
154 guaranteed to be correctly ordered. It is called within `save-excursion'.
156 Applicable at least in modes for languages like fixed-format Fortran where
157 comments always start in column zero.")
159 ;; ?? never set
160 (defvar block-comment-start nil)
161 (defvar block-comment-end nil)
163 (defvar comment-quote-nested t
164 "Non-nil if nested comments should be quoted.
165 This should be locally set by each major mode if needed.")
167 (defvar comment-continue nil
168 "Continuation string to insert for multiline comments.
169 This string will be added at the beginning of each line except the very
170 first one when commenting a region with a commenting style that allows
171 comments to span several lines.
172 It should generally have the same length as `comment-start' in order to
173 preserve indentation.
174 If it is nil a value will be automatically derived from `comment-start'
175 by replacing its first character with a space.")
177 (defvar comment-add 0
178 "How many more comment chars should be inserted by `comment-region'.
179 This determines the default value of the numeric argument of `comment-region'.
180 This should generally stay 0, except for a few modes like Lisp where
181 it can be convenient to set it to 1 so that regions are commented with
182 two semi-colons.")
184 (defconst comment-styles
185 '((plain . (nil nil nil nil))
186 (indent . (nil nil nil t))
187 (aligned . (nil t nil t))
188 (multi-line . (t nil nil t))
189 (extra-line . (t nil t t))
190 (box . (nil t t t))
191 (box-multi . (t t t t)))
192 "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
193 STYLE should be a mnemonic symbol.
194 MULTI specifies that comments are allowed to span multiple lines.
195 ALIGN specifies that the `comment-end' markers should be aligned.
196 EXTRA specifies that an extra line should be used before and after the
197 region to comment (to put the `comment-end' and `comment-start').
198 INDENT specifies that the `comment-start' markers should not be put at the
199 left margin but at the current indentation of the region to comment.")
201 ;;;###autoload
202 (defcustom comment-style 'plain
203 "Style to be used for `comment-region'.
204 See `comment-styles' for a list of available styles."
205 :type (if (boundp 'comment-styles)
206 `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles))
207 'symbol)
208 :group 'comment)
210 ;;;###autoload
211 (defcustom comment-padding " "
212 "Padding string that `comment-region' puts between comment chars and text.
213 Can also be an integer which will be automatically turned into a string
214 of the corresponding number of spaces.
216 Extra spacing between the comment characters and the comment text
217 makes the comment easier to read. Default is 1. nil means 0."
218 :type '(choice string integer (const nil))
219 :group 'comment)
221 ;;;###autoload
222 (defcustom comment-multi-line nil
223 "Non-nil means `comment-indent-new-line' continues comments.
224 That is, it inserts no new terminator or starter.
225 This affects `auto-fill-mode', which is the main reason to
226 customize this variable.
228 It also affects \\[indent-new-comment-line]. However, if you want this
229 behavior for explicit filling, you might as well use \\[newline-and-indent]."
230 :type 'boolean
231 :group 'comment)
233 (defcustom comment-empty-lines nil
234 "If nil, `comment-region' does not comment out empty lines.
235 If t, it always comments out empty lines.
236 if `eol' it only comments out empty lines if comments are
237 terminated by the end of line (i.e. `comment-end' is empty)."
238 :type '(choice (const :tag "Never" nil)
239 (const :tag "Always" t)
240 (const :tag "EOl-terminated" 'eol))
241 :group 'comment)
243 ;;;;
244 ;;;; Helpers
245 ;;;;
247 (defun comment-string-strip (str beforep afterp)
248 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
249 (string-match (concat "\\`" (if beforep "\\s-*")
250 "\\(.*?\\)" (if afterp "\\s-*\n?")
251 "\\'") str)
252 (match-string 1 str))
254 (defun comment-string-reverse (s)
255 "Return the mirror image of string S, without any trailing space."
256 (comment-string-strip (concat (nreverse (string-to-list s))) nil t))
258 ;;;###autoload
259 (defun comment-normalize-vars (&optional noerror)
260 "Check and setup the variables needed by other commenting functions.
261 Functions autoloaded from newcomment.el, being entry points, should call
262 this function before any other, so the rest of the code can assume that
263 the variables are properly set."
264 (unless (and (not comment-start) noerror)
265 (unless comment-start
266 (let ((cs (read-string "No comment syntax is defined. Use: ")))
267 (if (zerop (length cs))
268 (error "No comment syntax defined")
269 (set (make-local-variable 'comment-start) cs))))
270 ;; comment-use-syntax
271 (when (eq comment-use-syntax 'undecided)
272 (set (make-local-variable 'comment-use-syntax)
273 (let ((st (syntax-table))
274 (cs comment-start)
275 (ce (if (string= "" comment-end) "\n" comment-end)))
276 ;; Try to skip over a comment using forward-comment
277 ;; to see if the syntax tables properly recognize it.
278 (with-temp-buffer
279 (set-syntax-table st)
280 (insert cs " hello " ce)
281 (goto-char (point-min))
282 (and (forward-comment 1) (eobp))))))
283 ;; comment-padding
284 (unless comment-padding (setq comment-padding 0))
285 (when (integerp comment-padding)
286 (setq comment-padding (make-string comment-padding ? )))
287 ;; comment markers
288 ;;(setq comment-start (comment-string-strip comment-start t nil))
289 ;;(setq comment-end (comment-string-strip comment-end nil t))
290 ;; comment-continue
291 (unless (or comment-continue (string= comment-end ""))
292 (set (make-local-variable 'comment-continue)
293 (concat (if (string-match "\\S-\\S-" comment-start) " " "|")
294 (substring comment-start 1)))
295 ;; Hasn't been necessary yet.
296 ;; (unless (string-match comment-start-skip comment-continue)
297 ;; (kill-local-variable 'comment-continue))
299 ;; comment-skip regexps
300 (unless (and comment-start-skip
301 ;; In case comment-start has changed since last time.
302 (string-match comment-start-skip comment-start))
303 (set (make-local-variable 'comment-start-skip)
304 (concat "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
305 (regexp-quote (comment-string-strip comment-start t t))
306 ;; Let's not allow any \s- but only [ \t] since \n
307 ;; might be both a comment-end marker and \s-.
308 "+\\)[ \t]*")))
309 (unless (and comment-end-skip
310 ;; In case comment-end has changed since last time.
311 (string-match comment-end-skip comment-end))
312 (let ((ce (if (string= "" comment-end) "\n"
313 (comment-string-strip comment-end t t))))
314 (set (make-local-variable 'comment-end-skip)
315 ;; We use [ \t] rather than \s- because we don't want to
316 ;; remove ^L in C mode when uncommenting.
317 (concat "[ \t]*\\(\\s>" (if comment-quote-nested "" "+")
318 "\\|" (regexp-quote (substring ce 0 1))
319 (if (and comment-quote-nested (<= (length ce) 1)) "" "+")
320 (regexp-quote (substring ce 1))
321 "\\)"))))))
323 (defun comment-quote-re (str unp)
324 (concat (regexp-quote (substring str 0 1))
325 "\\\\" (if unp "+" "*")
326 (regexp-quote (substring str 1))))
328 (defun comment-quote-nested (cs ce unp)
329 "Quote or unquote nested comments.
330 If UNP is non-nil, unquote nested comment markers."
331 (setq cs (comment-string-strip cs t t))
332 (setq ce (comment-string-strip ce t t))
333 (when (and comment-quote-nested (> (length ce) 0))
334 (let ((re (concat (comment-quote-re ce unp)
335 "\\|" (comment-quote-re cs unp))))
336 (goto-char (point-min))
337 (while (re-search-forward re nil t)
338 (goto-char (match-beginning 0))
339 (forward-char 1)
340 (if unp (delete-char 1) (insert "\\"))
341 (when (= (length ce) 1)
342 ;; If the comment-end is a single char, adding a \ after that
343 ;; "first" char won't deactivate it, so we turn such a CE
344 ;; into !CS. I.e. for pascal, we turn } into !{
345 (if (not unp)
346 (when (string= (match-string 0) ce)
347 (replace-match (concat "!" cs) t t))
348 (when (and (< (point-min) (match-beginning 0))
349 (string= (buffer-substring (1- (match-beginning 0))
350 (1- (match-end 0)))
351 (concat "!" cs)))
352 (backward-char 2)
353 (delete-char (- (match-end 0) (match-beginning 0)))
354 (insert ce))))))))
356 ;;;;
357 ;;;; Navigation
358 ;;;;
360 (defvar comment-use-global-state nil
361 "Non-nil means that the global syntactic context is used.
362 More specifically, it means that `syntax-ppss' is used to find out whether
363 point is within a string or not. Major modes whose syntax is faithfully
364 described by the syntax-tables can set this to non-nil so comment markers
365 in strings will not confuse Emacs.")
367 (defun comment-search-forward (limit &optional noerror)
368 "Find a comment start between point and LIMIT.
369 Moves point to inside the comment and returns the position of the
370 comment-starter. If no comment is found, moves point to LIMIT
371 and raises an error or returns nil if NOERROR is non-nil."
372 (if (not comment-use-syntax)
373 (if (re-search-forward comment-start-skip limit noerror)
374 (or (match-end 1) (match-beginning 0))
375 (goto-char limit)
376 (unless noerror (error "No comment")))
377 (let* ((pt (point))
378 ;; Assume (at first) that pt is outside of any string.
379 (s (parse-partial-sexp pt (or limit (point-max)) nil nil
380 (if comment-use-global-state (syntax-ppss pt))
381 t)))
382 (when (and (nth 8 s) (nth 3 s) (not comment-use-global-state))
383 ;; The search ended at eol inside a string. Try to see if it
384 ;; works better when we assume that pt is inside a string.
385 (setq s (parse-partial-sexp
386 pt (or limit (point-max)) nil nil
387 (list nil nil nil (nth 3 s) nil nil nil nil)
388 t)))
389 (if (not (and (nth 8 s) (not (nth 3 s))))
390 (unless noerror (error "No comment"))
391 ;; We found the comment.
392 (let ((pos (point))
393 (start (nth 8 s))
394 (bol (line-beginning-position))
395 (end nil))
396 (while (and (null end) (>= (point) bol))
397 (if (looking-at comment-start-skip)
398 (setq end (min (or limit (point-max)) (match-end 0)))
399 (backward-char)))
400 (goto-char (or end pos))
401 start)))))
403 (defun comment-search-backward (&optional limit noerror)
404 "Find a comment start between LIMIT and point.
405 Moves point to inside the comment and returns the position of the
406 comment-starter. If no comment is found, moves point to LIMIT
407 and raises an error or returns nil if NOERROR is non-nil."
408 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
409 ;; stop there. This can be rather bad in general, but since
410 ;; comment-search-backward is only used to find the comment-column (in
411 ;; comment-set-column) and to find the comment-start string (via
412 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
413 (if (not (re-search-backward comment-start-skip limit t))
414 (unless noerror (error "No comment"))
415 (beginning-of-line)
416 (let* ((end (match-end 0))
417 (cs (comment-search-forward end t))
418 (pt (point)))
419 (if (not cs)
420 (progn (beginning-of-line)
421 (comment-search-backward limit noerror))
422 (while (progn (goto-char cs)
423 (comment-forward)
424 (and (< (point) end)
425 (setq cs (comment-search-forward end t))))
426 (setq pt (point)))
427 (goto-char pt)
428 cs))))
430 (defun comment-beginning ()
431 "Find the beginning of the enclosing comment.
432 Returns nil if not inside a comment, else moves point and returns
433 the same as `comment-search-backward'."
434 ;; HACK ATTACK!
435 ;; We should really test `in-string-p' but that can be expensive.
436 (unless (eq (get-text-property (point) 'face) 'font-lock-string-face)
437 (let ((pt (point))
438 (cs (comment-search-backward nil t)))
439 (when cs
440 (if (save-excursion
441 (goto-char cs)
442 (and
443 ;; For modes where comment-start and comment-end are the same,
444 ;; the search above may have found a `ce' rather than a `cs'.
445 (or (if comment-end-skip (not (looking-at comment-end-skip)))
446 ;; Maybe font-lock knows that it's a `cs'?
447 (eq (get-text-property (match-end 0) 'face)
448 'font-lock-comment-face)
449 (unless (eq (get-text-property (point) 'face)
450 'font-lock-comment-face)
451 ;; Let's assume it's a `cs' if we're on the same line.
452 (>= (line-end-position) pt)))
453 ;; Make sure that PT is not past the end of the comment.
454 (if (comment-forward 1) (> (point) pt) (eobp))))
456 (goto-char pt)
457 nil)))))
459 (defun comment-forward (&optional n)
460 "Skip forward over N comments.
461 Just like `forward-comment' but only for positive N
462 and can use regexps instead of syntax."
463 (setq n (or n 1))
464 (if (< n 0) (error "No comment-backward")
465 (if comment-use-syntax (forward-comment n)
466 (while (> n 0)
467 (setq n
468 (if (or (forward-comment 1)
469 (and (looking-at comment-start-skip)
470 (goto-char (match-end 0))
471 (re-search-forward comment-end-skip nil 'move)))
472 (1- n) -1)))
473 (= n 0))))
475 (defun comment-enter-backward ()
476 "Move from the end of a comment to the end of its content.
477 Point is assumed to be just at the end of a comment."
478 (if (bolp)
479 ;; comment-end = ""
480 (progn (backward-char) (skip-syntax-backward " "))
481 (cond
482 ((save-restriction
483 (narrow-to-region (line-beginning-position) (point))
484 (goto-char (point-min))
485 (re-search-forward (concat comment-end-skip "\\'") nil t))
486 (goto-char (match-beginning 0)))
487 ;; comment-end-skip not found. Maybe we're at EOB which implicitly
488 ;; closes the comment.
489 ((eobp) (skip-syntax-backward " "))
491 ;; else comment-end-skip was not found probably because it was not
492 ;; set right. Since \\s> should catch the single-char case, we'll
493 ;; blindly assume we're at the end of a two-char comment-end.
494 (backward-char 2)
495 (skip-chars-backward (string (char-after)))
496 (skip-syntax-backward " ")))))
498 ;;;;
499 ;;;; Commands
500 ;;;;
502 ;;;###autoload
503 (defun comment-indent-default ()
504 "Default for `comment-indent-function'."
505 (if (and (looking-at "\\s<\\s<\\(\\s<\\)?")
506 (or (match-end 1) (/= (current-column) (current-indentation))))
508 (when (or (/= (current-column) (current-indentation))
509 (and (> comment-add 0) (looking-at "\\s<\\(\\S<\\|\\'\\)")))
510 comment-column)))
512 ;;;###autoload
513 (defun comment-indent (&optional continue)
514 "Indent this line's comment to `comment-column', or insert an empty comment.
515 If CONTINUE is non-nil, use the `comment-continue' markers if any."
516 (interactive "*")
517 (comment-normalize-vars)
518 (let* ((empty (save-excursion (beginning-of-line)
519 (looking-at "[ \t]*$")))
520 (starter (or (and continue comment-continue)
521 (and empty block-comment-start) comment-start))
522 (ender (or (and continue comment-continue "")
523 (and empty block-comment-end) comment-end)))
524 (unless starter (error "No comment syntax defined"))
525 (beginning-of-line)
526 (let* ((eolpos (line-end-position))
527 (begpos (comment-search-forward eolpos t))
528 cpos indent)
529 ;; An existing comment?
530 (if begpos
531 (progn
532 (if (and (not (looking-at "[\t\n ]"))
533 (looking-at comment-end-skip))
534 ;; The comment is empty and we have skipped all its space
535 ;; and landed right before the comment-ender:
536 ;; Go back to the middle of the space.
537 (forward-char (/ (skip-chars-backward " \t") -2)))
538 (setq cpos (point-marker)))
539 ;; If none, insert one.
540 (if comment-insert-comment-function
541 (funcall comment-insert-comment-function)
542 (save-excursion
543 ;; Some `comment-indent-function's insist on not moving
544 ;; comments that are in column 0, so we first go to the
545 ;; likely target column.
546 (indent-to comment-column)
547 ;; Ensure there's a space before the comment for things
548 ;; like sh where it matters (as well as being neater).
549 (unless (memq (char-before) '(nil ?\n ?\t ?\ ))
550 (insert ?\ ))
551 (setq begpos (point))
552 (insert starter)
553 (setq cpos (point-marker))
554 (insert ender))))
555 (goto-char begpos)
556 ;; Compute desired indent.
557 (setq indent (save-excursion (funcall comment-indent-function)))
558 ;; If `indent' is nil and there's code before the comment, we can't
559 ;; use `indent-according-to-mode', so we default to comment-column.
560 (unless (or indent (save-excursion (skip-chars-backward " \t") (bolp)))
561 (setq indent comment-column))
562 (if (not indent)
563 ;; comment-indent-function refuses: delegate to line-indent.
564 (indent-according-to-mode)
565 ;; If the comment is at the left of code, adjust the indentation.
566 (unless (save-excursion (skip-chars-backward " \t") (bolp))
567 ;; Avoid moving comments past the fill-column.
568 (let ((max (+ (current-column)
569 (- (or comment-fill-column fill-column)
570 (save-excursion (end-of-line) (current-column))))))
571 (if (<= max indent)
572 (setq indent max) ;Don't move past the fill column.
573 ;; We can choose anywhere between indent..max.
574 ;; Let's try to align to a comment on the previous line.
575 (let ((other nil)
576 (min (max indent
577 (save-excursion (skip-chars-backward " \t")
578 (1+ (current-column))))))
579 (save-excursion
580 (when (and (zerop (forward-line -1))
581 (setq other (comment-search-forward
582 (line-end-position) t)))
583 (goto-char other) (setq other (current-column))))
584 (if (and other (<= other max) (>= other min))
585 ;; There is a comment and it's in the range: bingo.
586 (setq indent other)
587 ;; Let's try to align to a comment on the next line, then.
588 (let ((other nil))
589 (save-excursion
590 (when (and (zerop (forward-line 1))
591 (setq other (comment-search-forward
592 (line-end-position) t)))
593 (goto-char other) (setq other (current-column))))
594 (if (and other (<= other max) (> other min))
595 ;; There is a comment and it's in the range: bingo.
596 (setq indent other))))))))
597 (unless (= (current-column) indent)
598 ;; If that's different from current, change it.
599 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
600 (indent-to (if (bolp) indent
601 (max indent (1+ (current-column)))))))
602 (goto-char cpos)
603 (set-marker cpos nil))))
605 ;;;###autoload
606 (defun comment-set-column (arg)
607 "Set the comment column based on point.
608 With no ARG, set the comment column to the current column.
609 With just minus as arg, kill any comment on this line.
610 With any other arg, set comment column to indentation of the previous comment
611 and then align or create a comment on this line at that column."
612 (interactive "P")
613 (cond
614 ((eq arg '-) (comment-kill nil))
615 (arg
616 (comment-normalize-vars)
617 (save-excursion
618 (beginning-of-line)
619 (comment-search-backward)
620 (beginning-of-line)
621 (goto-char (comment-search-forward (line-end-position)))
622 (setq comment-column (current-column))
623 (message "Comment column set to %d" comment-column))
624 (comment-indent))
625 (t (setq comment-column (current-column))
626 (message "Comment column set to %d" comment-column))))
628 ;;;###autoload
629 (defun comment-kill (arg)
630 "Kill the comment on this line, if any.
631 With prefix ARG, kill comments on that many lines starting with this one."
632 (interactive "P")
633 (comment-normalize-vars)
634 (dotimes (_ (prefix-numeric-value arg))
635 (save-excursion
636 (beginning-of-line)
637 (let ((cs (comment-search-forward (line-end-position) t)))
638 (when cs
639 (goto-char cs)
640 (skip-syntax-backward " ")
641 (setq cs (point))
642 (comment-forward)
643 (kill-region cs (if (bolp) (1- (point)) (point)))
644 (indent-according-to-mode))))
645 (if arg (forward-line 1))))
647 (defun comment-padright (str &optional n)
648 "Construct a string composed of STR plus `comment-padding'.
649 It also adds N copies of the last non-whitespace chars of STR.
650 If STR already contains padding, the corresponding amount is
651 ignored from `comment-padding'.
652 N defaults to 0.
653 If N is `re', a regexp is returned instead, that would match
654 the string for any N."
655 (setq n (or n 0))
656 (when (and (stringp str) (not (string= "" str)))
657 ;; Separate the actual string from any leading/trailing padding
658 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
659 (let ((s (match-string 1 str)) ;actual string
660 (lpad (substring str 0 (match-beginning 1))) ;left padding
661 (rpad (concat (substring str (match-end 1)) ;original right padding
662 (substring comment-padding ;additional right padding
663 (min (- (match-end 0) (match-end 1))
664 (length comment-padding)))))
665 ;; We can only duplicate C if the comment-end has multiple chars
666 ;; or if comments can be nested, else the comment-end `}' would
667 ;; be turned into `}}}' where only the first ends the comment
668 ;; and the rest becomes bogus junk.
669 (multi (not (and comment-quote-nested
670 ;; comment-end is a single char
671 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
672 (if (not (symbolp n))
673 (concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
674 ;; construct a regexp that would match anything from just S
675 ;; to any possible output of this function for any N.
676 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
677 lpad "") ;padding is not required
678 (regexp-quote s)
679 (when multi "+") ;the last char of S might be repeated
680 (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
681 rpad "")))))) ;padding is not required
683 (defun comment-padleft (str &optional n)
684 "Construct a string composed of `comment-padding' plus STR.
685 It also adds N copies of the first non-whitespace chars of STR.
686 If STR already contains padding, the corresponding amount is
687 ignored from `comment-padding'.
688 N defaults to 0.
689 If N is `re', a regexp is returned instead, that would match
690 the string for any N."
691 (setq n (or n 0))
692 (when (and (stringp str) (not (string= "" str)))
693 ;; Only separate the left pad because we assume there is no right pad.
694 (string-match "\\`\\s-*" str)
695 (let ((s (substring str (match-end 0)))
696 (pad (concat (substring comment-padding
697 (min (- (match-end 0) (match-beginning 0))
698 (length comment-padding)))
699 (match-string 0 str)))
700 (c (aref str (match-end 0))) ;the first non-space char of STR
701 ;; We can only duplicate C if the comment-end has multiple chars
702 ;; or if comments can be nested, else the comment-end `}' would
703 ;; be turned into `}}}' where only the first ends the comment
704 ;; and the rest becomes bogus junk.
705 (multi (not (and comment-quote-nested
706 ;; comment-end is a single char
707 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
708 (if (not (symbolp n))
709 (concat pad (when multi (make-string n c)) s)
710 ;; Construct a regexp that would match anything from just S
711 ;; to any possible output of this function for any N.
712 ;; We match any number of leading spaces because this regexp will
713 ;; be used for uncommenting where we might want to remove
714 ;; uncomment markers with arbitrary leading space (because
715 ;; they were aligned).
716 (concat "\\s-*"
717 (if multi (concat (regexp-quote (string c)) "*"))
718 (regexp-quote s))))))
720 ;;;###autoload
721 (defun uncomment-region (beg end &optional arg)
722 "Uncomment each line in the BEG .. END region.
723 The numeric prefix ARG can specify a number of chars to remove from the
724 comment markers."
725 (interactive "*r\nP")
726 (comment-normalize-vars)
727 (when (> beg end) (setq beg (prog1 end (setq end beg))))
728 ;; Bind `comment-use-global-state' to nil. While uncommenting a region
729 ;; (which works a line at a time), a comment can appear to be
730 ;; included in a mult-line string, but it is actually not.
731 (let ((comment-use-global-state nil))
732 (save-excursion
733 (funcall uncomment-region-function beg end arg))))
735 (defun uncomment-region-default (beg end &optional arg)
736 "Uncomment each line in the BEG .. END region.
737 The numeric prefix ARG can specify a number of chars to remove from the
738 comment markers."
739 (goto-char beg)
740 (setq end (copy-marker end))
741 (let* ((numarg (prefix-numeric-value arg))
742 (ccs comment-continue)
743 (srei (comment-padright ccs 're))
744 (csre (comment-padright comment-start 're))
745 (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
746 spt)
747 (while (and (< (point) end)
748 (setq spt (comment-search-forward end t)))
749 (let ((ipt (point))
750 ;; Find the end of the comment.
751 (ept (progn
752 (goto-char spt)
753 (unless (or (comment-forward)
754 ;; Allow non-terminated comments.
755 (eobp))
756 (error "Can't find the comment end"))
757 (point)))
758 (box nil)
759 (box-equal nil)) ;Whether we might be using `=' for boxes.
760 (save-restriction
761 (narrow-to-region spt ept)
763 ;; Remove the comment-start.
764 (goto-char ipt)
765 (skip-syntax-backward " ")
766 ;; A box-comment starts with a looong comment-start marker.
767 (when (and (or (and (= (- (point) (point-min)) 1)
768 (setq box-equal t)
769 (looking-at "=\\{7\\}")
770 (not (eq (char-before (point-max)) ?\n))
771 (skip-chars-forward "="))
772 (> (- (point) (point-min) (length comment-start)) 7))
773 (> (count-lines (point-min) (point-max)) 2))
774 (setq box t))
775 ;; Skip the padding. Padding can come from comment-padding and/or
776 ;; from comment-start, so we first check comment-start.
777 (if (or (save-excursion (goto-char (point-min)) (looking-at csre))
778 (looking-at (regexp-quote comment-padding)))
779 (goto-char (match-end 0)))
780 (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
781 (goto-char (match-end 0)))
782 (if (null arg) (delete-region (point-min) (point))
783 (skip-syntax-backward " ")
784 (delete-char (- numarg))
785 (unless (or (bobp)
786 (save-excursion (goto-char (point-min))
787 (looking-at comment-start-skip)))
788 ;; If there's something left but it doesn't look like
789 ;; a comment-start any more, just remove it.
790 (delete-region (point-min) (point))))
792 ;; Remove the end-comment (and leading padding and such).
793 (goto-char (point-max)) (comment-enter-backward)
794 ;; Check for special `=' used sometimes in comment-box.
795 (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
796 (let ((pos (point)))
797 ;; skip `=' but only if there are at least 7.
798 (when (> (skip-chars-backward "=") -7) (goto-char pos))))
799 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
800 (when (and (bolp) (not (bobp))) (backward-char))
801 (if (null arg) (delete-region (point) (point-max))
802 (skip-syntax-forward " ")
803 (delete-char numarg)
804 (unless (or (eobp) (looking-at comment-end-skip))
805 ;; If there's something left but it doesn't look like
806 ;; a comment-end any more, just remove it.
807 (delete-region (point) (point-max)))))
809 ;; Unquote any nested end-comment.
810 (comment-quote-nested comment-start comment-end t)
812 ;; Eliminate continuation markers as well.
813 (when sre
814 (let* ((cce (comment-string-reverse (or comment-continue
815 comment-start)))
816 (erei (and box (comment-padleft cce 're)))
817 (ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
818 (goto-char (point-min))
819 (while (progn
820 (if (and ere (re-search-forward
821 ere (line-end-position) t))
822 (replace-match "" t t nil (if (match-end 2) 2 1))
823 (setq ere nil))
824 (forward-line 1)
825 (re-search-forward sre (line-end-position) t))
826 (replace-match "" t t nil (if (match-end 2) 2 1)))))
827 ;; Go to the end for the next comment.
828 (goto-char (point-max))))))
829 (set-marker end nil))
831 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
832 "Make the leading and trailing extra lines.
833 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
834 (let ((eindent 0))
835 (if (not block)
836 ;; Try to match CS and CE's content so they align aesthetically.
837 (progn
838 (setq ce (comment-string-strip ce t t))
839 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce "\n" cs))
840 (setq eindent
841 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
842 0))))
843 ;; box comment
844 (let* ((width (- max-indent min-indent))
845 (s (concat cs "a=m" cce))
846 (e (concat ccs "a=m" ce))
847 (c (if (string-match ".*\\S-\\S-" cs)
848 (aref cs (1- (match-end 0)))
849 (if (and (equal comment-end "") (string-match ".*\\S-" cs))
850 (aref cs (1- (match-end 0))) ?=)))
851 (re "\\s-*a=m\\s-*")
852 (_ (string-match re s))
853 (lcs (length cs))
854 (fill
855 (make-string (+ width (- (match-end 0)
856 (match-beginning 0) lcs 3)) c)))
857 (setq cs (replace-match fill t t s))
858 (when (and (not (string-match comment-start-skip cs))
859 (string-match "a=m" s))
860 ;; The whitespace around CS cannot be ignored: put it back.
861 (setq re "a=m")
862 (setq fill (make-string (- width lcs) c))
863 (setq cs (replace-match fill t t s)))
864 (string-match re e)
865 (setq ce (replace-match fill t t e))))
866 (cons (concat cs "\n" (make-string min-indent ? ) ccs)
867 (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce))))
869 (defmacro comment-with-narrowing (beg end &rest body)
870 "Execute BODY with BEG..END narrowing.
871 Space is added (and then removed) at the beginning for the text's
872 indentation to be kept as it was before narrowing."
873 (declare (debug t) (indent 2))
874 (let ((bindent (make-symbol "bindent")))
875 `(let ((,bindent (save-excursion (goto-char ,beg) (current-column))))
876 (save-restriction
877 (narrow-to-region ,beg ,end)
878 (goto-char (point-min))
879 (insert (make-string ,bindent ? ))
880 (prog1
881 (progn ,@body)
882 ;; remove the bindent
883 (save-excursion
884 (goto-char (point-min))
885 (when (looking-at " *")
886 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent)))
887 (delete-char n)
888 (setq ,bindent (- ,bindent n))))
889 (end-of-line)
890 (let ((e (point)))
891 (beginning-of-line)
892 (while (and (> ,bindent 0) (re-search-forward " *" e t))
893 (let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1))))
894 (goto-char (match-beginning 0))
895 (delete-char n)
896 (setq ,bindent (- ,bindent n)))))))))))
898 (defun comment-add (arg)
899 (if (and (null arg) (= (string-match "[ \t]*\\'" comment-start) 1))
900 comment-add
901 (1- (prefix-numeric-value arg))))
903 (defun comment-region-internal (beg end cs ce
904 &optional ccs cce block lines indent)
905 "Comment region BEG .. END.
906 CS and CE are the comment start string and comment end string,
907 respectively. CCS and CCE are the comment continuation strings
908 for the start and end of lines, respectively (default to CS and CE).
909 BLOCK indicates that end of lines should be marked with either CCE,
910 CE or CS \(if CE is empty) and that those markers should be aligned.
911 LINES indicates that an extra lines will be used at the beginning
912 and end of the region for CE and CS.
913 INDENT indicates to put CS and CCS at the current indentation of
914 the region rather than at left margin."
915 ;;(assert (< beg end))
916 (let ((no-empty (not (or (eq comment-empty-lines t)
917 (and comment-empty-lines (zerop (length ce)))))))
918 ;; Sanitize CE and CCE.
919 (if (and (stringp ce) (string= "" ce)) (setq ce nil))
920 (if (and (stringp cce) (string= "" cce)) (setq cce nil))
921 ;; If CE is empty, multiline cannot be used.
922 (unless ce (setq ccs nil cce nil))
923 ;; Should we mark empty lines as well ?
924 (if (or ccs block lines) (setq no-empty nil))
925 ;; Make sure we have end-markers for BLOCK mode.
926 (when block (unless ce (setq ce (comment-string-reverse cs))))
927 ;; If BLOCK is not requested, we don't need CCE.
928 (unless block (setq cce nil))
929 ;; Continuation defaults to the same as CS and CE.
930 (unless ccs (setq ccs cs cce ce))
932 (save-excursion
933 (goto-char end)
934 ;; If the end is not at the end of a line and the comment-end
935 ;; is implicit (i.e. a newline), explicitly insert a newline.
936 (unless (or ce (eolp)) (insert "\n") (indent-according-to-mode))
937 (comment-with-narrowing beg end
938 (let ((min-indent (point-max))
939 (max-indent 0))
940 (goto-char (point-min))
941 ;; Quote any nested comment marker
942 (comment-quote-nested comment-start comment-end nil)
944 ;; Loop over all lines to find the needed indentations.
945 (goto-char (point-min))
946 (while
947 (progn
948 (unless (looking-at "[ \t]*$")
949 (setq min-indent (min min-indent (current-indentation))))
950 (end-of-line)
951 (setq max-indent (max max-indent (current-column)))
952 (not (or (eobp) (progn (forward-line) nil)))))
954 (setq max-indent
955 (+ max-indent (max (length cs) (length ccs))
956 ;; Inserting ccs can change max-indent by (1- tab-width)
957 ;; but only if there are TABs in the boxed text, of course.
958 (if (save-excursion (goto-char beg)
959 (search-forward "\t" end t))
960 (1- tab-width) 0)))
961 (unless indent (setq min-indent 0))
963 ;; make the leading and trailing lines if requested
964 (when lines
965 (let ((csce
966 (comment-make-extra-lines
967 cs ce ccs cce min-indent max-indent block)))
968 (setq cs (car csce))
969 (setq ce (cdr csce))))
971 (goto-char (point-min))
972 ;; Loop over all lines from BEG to END.
973 (while
974 (progn
975 (unless (and no-empty (looking-at "[ \t]*$"))
976 (move-to-column min-indent t)
977 (insert cs) (setq cs ccs) ;switch to CCS after the first line
978 (end-of-line)
979 (if (eobp) (setq cce ce))
980 (when cce
981 (when block (move-to-column max-indent t))
982 (insert cce)))
983 (end-of-line)
984 (not (or (eobp) (progn (forward-line) nil))))))))))
986 ;;;###autoload
987 (defun comment-region (beg end &optional arg)
988 "Comment or uncomment each line in the region.
989 With just \\[universal-argument] prefix arg, uncomment each line in region BEG .. END.
990 Numeric prefix ARG means use ARG comment characters.
991 If ARG is negative, delete that many comment characters instead.
992 By default, comments start at the left margin, are terminated on each line,
993 even for syntax in which newline does not end the comment and blank lines
994 do not get comments. This can be changed with `comment-style'.
996 The strings used as comment starts are built from
997 `comment-start' without trailing spaces and `comment-padding'."
998 (interactive "*r\nP")
999 (comment-normalize-vars)
1000 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
1001 (save-excursion
1002 ;; FIXME: maybe we should call uncomment depending on ARG.
1003 (funcall comment-region-function beg end arg)))
1005 (defun comment-region-default (beg end &optional arg)
1006 (let* ((numarg (prefix-numeric-value arg))
1007 (style (cdr (assoc comment-style comment-styles)))
1008 (lines (nth 2 style))
1009 (block (nth 1 style))
1010 (multi (nth 0 style)))
1011 ;; we use `chars' instead of `syntax' because `\n' might be
1012 ;; of end-comment syntax rather than of whitespace syntax.
1013 ;; sanitize BEG and END
1014 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
1015 (setq beg (max beg (point)))
1016 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
1017 (setq end (min end (point)))
1018 (if (>= beg end) (error "Nothing to comment"))
1020 ;; sanitize LINES
1021 (setq lines
1022 (and
1023 lines ;; multi
1024 (progn (goto-char beg) (beginning-of-line)
1025 (skip-syntax-forward " ")
1026 (>= (point) beg))
1027 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
1028 (<= (point) end))
1029 (or block (not (string= "" comment-end)))
1030 (or block (progn (goto-char beg) (search-forward "\n" end t)))))
1032 ;; don't add end-markers just because the user asked for `block'
1033 (unless (or lines (string= "" comment-end)) (setq block nil))
1035 (cond
1036 ((consp arg) (uncomment-region beg end))
1037 ((< numarg 0) (uncomment-region beg end (- numarg)))
1039 (setq numarg (comment-add arg))
1040 (comment-region-internal
1041 beg end
1042 (let ((s (comment-padright comment-start numarg)))
1043 (if (string-match comment-start-skip s) s
1044 (comment-padright comment-start)))
1045 (let ((s (comment-padleft comment-end numarg)))
1046 (and s (if (string-match comment-end-skip s) s
1047 (comment-padright comment-end))))
1048 (if multi (comment-padright comment-continue numarg))
1049 (if multi
1050 (comment-padleft (comment-string-reverse comment-continue) numarg))
1051 block
1052 lines
1053 (nth 3 style))))))
1055 (defun comment-box (beg end &optional arg)
1056 "Comment out the BEG .. END region, putting it inside a box.
1057 The numeric prefix ARG specifies how many characters to add to begin- and
1058 end- comment markers additionally to what `comment-add' already specifies."
1059 (interactive "*r\np")
1060 (let ((comment-style (if (cadr (assoc comment-style comment-styles))
1061 'box-multi 'box)))
1062 (comment-region beg end (+ comment-add arg))))
1065 ;;;###autoload
1066 (defun comment-or-uncomment-region (beg end &optional arg)
1067 "Call `comment-region', unless the region only consists of comments,
1068 in which case call `uncomment-region'. If a prefix arg is given, it
1069 is passed on to the respective function."
1070 (interactive "*r\nP")
1071 (comment-normalize-vars)
1072 (funcall (if (save-excursion ;; check for already commented region
1073 (goto-char beg)
1074 (comment-forward (point-max))
1075 (<= end (point)))
1076 'uncomment-region 'comment-region)
1077 beg end arg))
1079 ;;;###autoload
1080 (defun comment-dwim (arg)
1081 "Call the comment command you want (Do What I Mean).
1082 If the region is active and `transient-mark-mode' is on, call
1083 `comment-region' (unless it only consists of comments, in which
1084 case it calls `uncomment-region').
1085 Else, if the current line is empty, insert a comment and indent it.
1086 Else if a prefix ARG is specified, call `comment-kill'.
1087 Else, call `comment-indent'.
1088 You can configure `comment-style' to change the way regions are commented."
1089 (interactive "*P")
1090 (comment-normalize-vars)
1091 (if (and mark-active transient-mark-mode)
1092 (comment-or-uncomment-region (region-beginning) (region-end) arg)
1093 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
1094 ;; FIXME: If there's no comment to kill on this line and ARG is
1095 ;; specified, calling comment-kill is not very clever.
1096 (if arg (comment-kill (and (integerp arg) arg)) (comment-indent))
1097 (let ((add (comment-add arg)))
1098 ;; Some modes insist on keeping column 0 comment in column 0
1099 ;; so we need to move away from it before inserting the comment.
1100 (indent-according-to-mode)
1101 (insert (comment-padright comment-start add))
1102 (save-excursion
1103 (unless (string= "" comment-end)
1104 (insert (comment-padleft comment-end add)))
1105 (indent-according-to-mode))))))
1107 ;;;###autoload
1108 (defcustom comment-auto-fill-only-comments nil
1109 "Non-nil means to only auto-fill inside comments.
1110 This has no effect in modes that do not define a comment syntax."
1111 :type 'boolean
1112 :group 'comment)
1114 (defun comment-valid-prefix-p (prefix compos)
1116 ;; Accept any prefix if the current comment is not EOL-terminated.
1117 (save-excursion (goto-char compos) (comment-forward) (not (bolp)))
1118 ;; Accept any prefix that starts with a comment-start marker.
1119 (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)")
1120 prefix)))
1122 ;;;###autoload
1123 (defun comment-indent-new-line (&optional soft)
1124 "Break line at point and indent, continuing comment if within one.
1125 This indents the body of the continued comment
1126 under the previous comment line.
1128 This command is intended for styles where you write a comment per line,
1129 starting a new comment (and terminating it if necessary) on each line.
1130 If you want to continue one comment across several lines, use \\[newline-and-indent].
1132 If a fill column is specified, it overrides the use of the comment column
1133 or comment indentation.
1135 The inserted newline is marked hard if variable `use-hard-newlines' is true,
1136 unless optional argument SOFT is non-nil."
1137 (interactive)
1138 (comment-normalize-vars t)
1139 (let (compos comin)
1140 ;; If we are not inside a comment and we only auto-fill comments,
1141 ;; don't do anything (unless no comment syntax is defined).
1142 (unless (and comment-start
1143 comment-auto-fill-only-comments
1144 (not (interactive-p))
1145 (not (save-excursion
1146 (prog1 (setq compos (comment-beginning))
1147 (setq comin (point))))))
1149 ;; Now we know we should auto-fill.
1150 ;; Insert the newline before removing empty space so that markers
1151 ;; get preserved better.
1152 (if soft (insert-and-inherit ?\n) (newline 1))
1153 (save-excursion (forward-char -1) (delete-horizontal-space))
1154 (delete-horizontal-space)
1156 (if (and fill-prefix (not adaptive-fill-mode))
1157 ;; Blindly trust a non-adaptive fill-prefix.
1158 (progn
1159 (indent-to-left-margin)
1160 (insert-before-markers-and-inherit fill-prefix))
1162 ;; If necessary check whether we're inside a comment.
1163 (unless (or compos (null comment-start))
1164 (save-excursion
1165 (backward-char)
1166 (setq compos (comment-beginning))
1167 (setq comin (point))))
1169 (cond
1170 ;; If there's an adaptive prefix, use it unless we're inside
1171 ;; a comment and the prefix is not a comment starter.
1172 ((and fill-prefix
1173 (or (not compos)
1174 (comment-valid-prefix-p fill-prefix compos)))
1175 (indent-to-left-margin)
1176 (insert-and-inherit fill-prefix))
1177 ;; If we're not inside a comment, just try to indent.
1178 ((not compos) (indent-according-to-mode))
1180 (let* ((comment-column
1181 ;; The continuation indentation should be somewhere between
1182 ;; the current line's indentation (plus 2 for good measure)
1183 ;; and the current comment's indentation, with a preference
1184 ;; for comment-column.
1185 (save-excursion
1186 ;; FIXME: use prev line's info rather than first line's.
1187 (goto-char compos)
1188 (min (current-column) (max comment-column
1189 (+ 2 (current-indentation))))))
1190 (comstart (buffer-substring compos comin))
1191 (normalp
1192 (string-match (regexp-quote (comment-string-strip
1193 comment-start t t))
1194 comstart))
1195 (comment-end
1196 (if normalp comment-end
1197 ;; The comment starter is not the normal comment-start
1198 ;; so we can't just use comment-end.
1199 (save-excursion
1200 (goto-char compos)
1201 (if (not (comment-forward)) comment-end
1202 (comment-string-strip
1203 (buffer-substring
1204 (save-excursion (comment-enter-backward) (point))
1205 (point))
1206 nil t)))))
1207 (comment-start comstart)
1208 (continuep (or comment-multi-line
1209 (cadr (assoc comment-style comment-styles))))
1210 ;; Force comment-continue to be recreated from comment-start.
1211 ;; FIXME: wrong if comment-continue was set explicitly!
1212 ;; FIXME: use prev line's continuation if available.
1213 (comment-continue nil))
1214 (if (and comment-multi-line (> (length comment-end) 0))
1215 (indent-according-to-mode)
1216 (insert-and-inherit ?\n)
1217 (forward-char -1)
1218 (comment-indent continuep)
1219 (save-excursion
1220 (let ((pt (point)))
1221 (end-of-line)
1222 (let ((comend (buffer-substring pt (point))))
1223 ;; The 1+ is to make sure we delete the \n inserted above.
1224 (delete-region pt (1+ (point)))
1225 (end-of-line 0)
1226 (insert comend))))))))))))
1228 (provide 'newcomment)
1230 ;; arch-tag: 01e3320a-00c8-44ea-a696-8f8e7354c858
1231 ;;; newcomment.el ends here