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