*** empty log message ***
[emacs.git] / lisp / newcomment.el
blob91943503f5e46a54650789d598c67ce659195404
1 ;;; newcomment.el --- (un)comment regions of buffers
3 ;; Copyright (C) 1999,2000,2003 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 (set (make-local-variable 'comment-start)
255 (read-string "No comment syntax is defined. Use: ")))
256 ;; comment-use-syntax
257 (when (eq comment-use-syntax 'undecided)
258 (set (make-local-variable 'comment-use-syntax)
259 (let ((st (syntax-table))
260 (cs comment-start)
261 (ce (if (string= "" comment-end) "\n" comment-end)))
262 ;; Try to skip over a comment using forward-comment
263 ;; to see if the syntax tables properly recognize it.
264 (with-temp-buffer
265 (set-syntax-table st)
266 (insert cs " hello " ce)
267 (goto-char (point-min))
268 (and (forward-comment 1) (eobp))))))
269 ;; comment-padding
270 (unless comment-padding (setq comment-padding 0))
271 (when (integerp comment-padding)
272 (setq comment-padding (make-string comment-padding ? )))
273 ;; comment markers
274 ;;(setq comment-start (comment-string-strip comment-start t nil))
275 ;;(setq comment-end (comment-string-strip comment-end nil t))
276 ;; comment-continue
277 (unless (or comment-continue (string= comment-end ""))
278 (set (make-local-variable 'comment-continue)
279 (concat (if (string-match "\\S-\\S-" comment-start) " " "|")
280 (substring comment-start 1)))
281 ;; Hasn't been necessary yet.
282 ;; (unless (string-match comment-start-skip comment-continue)
283 ;; (kill-local-variable 'comment-continue))
285 ;; comment-skip regexps
286 (unless (and comment-start-skip
287 ;; In case comment-start has changed since last time.
288 (string-match comment-start-skip comment-start))
289 (set (make-local-variable 'comment-start-skip)
290 (concat "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
291 (regexp-quote (comment-string-strip comment-start t t))
292 ;; Let's not allow any \s- but only [ \t] since \n
293 ;; might be both a comment-end marker and \s-.
294 "+\\)[ \t]*")))
295 (unless (and comment-end-skip
296 ;; In case comment-end has changed since last time.
297 (string-match comment-end-skip comment-end))
298 (let ((ce (if (string= "" comment-end) "\n"
299 (comment-string-strip comment-end t t))))
300 (set (make-local-variable 'comment-end-skip)
301 ;; We use [ \t] rather than \s- because we don't want to
302 ;; remove ^L in C mode when uncommenting.
303 (concat "[ \t]*\\(\\s>" (if comment-quote-nested "" "+")
304 "\\|" (regexp-quote (substring ce 0 1))
305 (if (and comment-quote-nested (<= (length ce) 1)) "" "+")
306 (regexp-quote (substring ce 1))
307 "\\)"))))))
309 (defun comment-quote-re (str unp)
310 (concat (regexp-quote (substring str 0 1))
311 "\\\\" (if unp "+" "*")
312 (regexp-quote (substring str 1))))
314 (defun comment-quote-nested (cs ce unp)
315 "Quote or unquote nested comments.
316 If UNP is non-nil, unquote nested comment markers."
317 (setq cs (comment-string-strip cs t t))
318 (setq ce (comment-string-strip ce t t))
319 (when (and comment-quote-nested (> (length ce) 0))
320 (let ((re (concat (comment-quote-re ce unp)
321 "\\|" (comment-quote-re cs unp))))
322 (goto-char (point-min))
323 (while (re-search-forward re nil t)
324 (goto-char (match-beginning 0))
325 (forward-char 1)
326 (if unp (delete-char 1) (insert "\\"))
327 (when (= (length ce) 1)
328 ;; If the comment-end is a single char, adding a \ after that
329 ;; "first" char won't deactivate it, so we turn such a CE
330 ;; into !CS. I.e. for pascal, we turn } into !{
331 (if (not unp)
332 (when (string= (match-string 0) ce)
333 (replace-match (concat "!" cs) t t))
334 (when (and (< (point-min) (match-beginning 0))
335 (string= (buffer-substring (1- (match-beginning 0))
336 (1- (match-end 0)))
337 (concat "!" cs)))
338 (backward-char 2)
339 (delete-char (- (match-end 0) (match-beginning 0)))
340 (insert ce))))))))
342 ;;;;
343 ;;;; Navigation
344 ;;;;
346 (defun comment-search-forward (limit &optional noerror)
347 "Find a comment start between point and LIMIT.
348 Moves point to inside the comment and returns the position of the
349 comment-starter. If no comment is found, moves point to LIMIT
350 and raises an error or returns nil of NOERROR is non-nil."
351 (if (not comment-use-syntax)
352 (if (re-search-forward comment-start-skip limit noerror)
353 (or (match-end 1) (match-beginning 0))
354 (goto-char limit)
355 (unless noerror (error "No comment")))
356 (let* ((pt (point))
357 ;; Assume (at first) that pt is outside of any string.
358 (s (parse-partial-sexp pt (or limit (point-max)) nil nil nil t)))
359 (when (and (nth 8 s) (nth 3 s))
360 ;; The search ended inside a string. Try to see if it
361 ;; works better when we assume that pt is inside a string.
362 (setq s (parse-partial-sexp
363 pt (or limit (point-max)) nil nil
364 (list nil nil nil (nth 3 s) nil nil nil nil)
365 t)))
366 (if (not (and (nth 8 s) (not (nth 3 s))))
367 (unless noerror (error "No comment"))
368 ;; We found the comment.
369 (let ((pos (point))
370 (start (nth 8 s))
371 (bol (line-beginning-position))
372 (end nil))
373 (while (and (null end) (>= (point) bol))
374 (if (looking-at comment-start-skip)
375 (setq end (min (or limit (point-max)) (match-end 0)))
376 (backward-char)))
377 (goto-char (or end pos))
378 start)))))
380 (defun comment-search-backward (&optional limit noerror)
381 "Find a comment start between LIMIT and point.
382 Moves point to inside the comment and returns the position of the
383 comment-starter. If no comment is found, moves point to LIMIT
384 and raises an error or returns nil of NOERROR is non-nil."
385 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
386 ;; stop there. This can be rather bad in general, but since
387 ;; comment-search-backward is only used to find the comment-column (in
388 ;; comment-set-column) and to find the comment-start string (via
389 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
390 (if (not (re-search-backward comment-start-skip limit t))
391 (unless noerror (error "No comment"))
392 (beginning-of-line)
393 (let* ((end (match-end 0))
394 (cs (comment-search-forward end t))
395 (pt (point)))
396 (if (not cs)
397 (progn (beginning-of-line)
398 (comment-search-backward limit noerror))
399 (while (progn (goto-char cs)
400 (comment-forward)
401 (and (< (point) end)
402 (setq cs (comment-search-forward end t))))
403 (setq pt (point)))
404 (goto-char pt)
405 cs))))
407 (defun comment-beginning ()
408 "Find the beginning of the enclosing comment.
409 Returns nil if not inside a comment, else moves point and returns
410 the same as `comment-search-forward'."
411 ;; HACK ATTACK!
412 ;; We should really test `in-string-p' but that can be expensive.
413 (unless (eq (get-text-property (point) 'face) 'font-lock-string-face)
414 (let ((pt (point))
415 (cs (comment-search-backward nil t)))
416 (when cs
417 (if (save-excursion
418 (goto-char cs)
419 (and
420 ;; For modes where comment-start and comment-end are the same,
421 ;; the search above may have found a `ce' rather than a `cs'.
422 (or (not (looking-at comment-end-skip))
423 ;; Maybe font-lock knows that it's a `cs'?
424 (eq (get-text-property (match-end 0) 'face)
425 'font-lock-comment-face)
426 (unless (eq (get-text-property (point) 'face)
427 'font-lock-comment-face)
428 ;; Let's assume it's a `cs' if we're on the same line.
429 (>= (line-end-position) pt)))
430 ;; Make sure that PT is not past the end of the comment.
431 (if (comment-forward 1) (> (point) pt) (eobp))))
433 (goto-char pt)
434 nil)))))
436 (defun comment-forward (&optional n)
437 "Skip forward over N comments.
438 Just like `forward-comment' but only for positive N
439 and can use regexps instead of syntax."
440 (setq n (or n 1))
441 (if (< n 0) (error "No comment-backward")
442 (if comment-use-syntax (forward-comment n)
443 (while (> n 0)
444 (setq n
445 (if (or (forward-comment 1)
446 (and (looking-at comment-start-skip)
447 (goto-char (match-end 0))
448 (re-search-forward comment-end-skip nil 'move)))
449 (1- n) -1)))
450 (= n 0))))
452 (defun comment-enter-backward ()
453 "Move from the end of a comment to the end of its content.
454 Point is assumed to be just at the end of a comment."
455 (if (bolp)
456 ;; comment-end = ""
457 (progn (backward-char) (skip-syntax-backward " "))
458 (let ((end (point)))
459 (beginning-of-line)
460 (save-restriction
461 (narrow-to-region (point) end)
462 (if (re-search-forward (concat comment-end-skip "\\'") nil t)
463 (goto-char (match-beginning 0))
464 ;; comment-end-skip not found probably because it was not set right.
465 ;; Since \\s> should catch the single-char case, we'll blindly
466 ;; assume we're at the end of a two-char comment-end.
467 (goto-char (point-max))
468 (backward-char 2)
469 (skip-chars-backward (string (char-after)))
470 (skip-syntax-backward " "))))))
472 ;;;;
473 ;;;; Commands
474 ;;;;
476 ;;;###autoload
477 (defun comment-indent-default ()
478 "Default for `comment-indent-function'."
479 (if (and (looking-at "\\s<\\s<\\(\\s<\\)?")
480 (or (match-end 1) (/= (current-column) (current-indentation))))
482 (when (or (/= (current-column) (current-indentation))
483 (and (> comment-add 0) (looking-at "\\s<\\S<")))
484 comment-column)))
486 ;;;###autoload
487 (defun comment-indent (&optional continue)
488 "Indent this line's comment to `comment-column', or insert an empty comment.
489 If CONTINUE is non-nil, use the `comment-continue' markers if any."
490 (interactive "*")
491 (comment-normalize-vars)
492 (let* ((empty (save-excursion (beginning-of-line)
493 (looking-at "[ \t]*$")))
494 (starter (or (and continue comment-continue)
495 (and empty block-comment-start) comment-start))
496 (ender (or (and continue comment-continue "")
497 (and empty block-comment-end) comment-end)))
498 (unless starter (error "No comment syntax defined"))
499 (beginning-of-line)
500 (let* ((eolpos (line-end-position))
501 (begpos (comment-search-forward eolpos t))
502 cpos indent)
503 ;; An existing comment?
504 (if begpos
505 (progn
506 (if (and (not (looking-at "[\t\n ]"))
507 (looking-at comment-end-skip))
508 ;; The comment is empty and we have skipped all its space
509 ;; and landed right before the comment-ender:
510 ;; Go back to the middle of the space.
511 (forward-char (/ (skip-chars-backward " \t") -2)))
512 (setq cpos (point-marker)))
513 ;; If none, insert one.
514 (if comment-insert-comment-function
515 (funcall comment-insert-comment-function)
516 (save-excursion
517 ;; Some `comment-indent-function's insist on not moving
518 ;; comments that are in column 0, so we first go to the
519 ;; likely target column.
520 (indent-to comment-column)
521 ;; Ensure there's a space before the comment for things
522 ;; like sh where it matters (as well as being neater).
523 (unless (memq (char-before) '(nil ?\n ?\t ?\ ))
524 (insert ?\ ))
525 (setq begpos (point))
526 (insert starter)
527 (setq cpos (point-marker))
528 (insert ender))))
529 (goto-char begpos)
530 ;; Compute desired indent.
531 (setq indent (save-excursion (funcall comment-indent-function)))
532 ;; If `indent' is nil and there's code before the comment, we can't
533 ;; use `indent-according-to-mode', so we default to comment-column.
534 (unless (or indent (save-excursion (skip-chars-backward " \t") (bolp)))
535 (setq indent comment-column))
536 (if (not indent)
537 ;; comment-indent-function refuses: delegate to line-indent.
538 (indent-according-to-mode)
539 ;; If the comment is at the left of code, adjust the indentation.
540 (unless (save-excursion (skip-chars-backward " \t") (bolp))
541 ;; Avoid moving comments past the fill-column.
542 (let ((max (+ (current-column)
543 (- (or comment-fill-column fill-column)
544 (save-excursion (end-of-line) (current-column))))))
545 (if (<= max indent)
546 (setq indent max) ;Don't move past the fill column.
547 ;; We can choose anywhere between indent..max.
548 ;; Let's try to align to a comment on the previous line.
549 (let ((other nil)
550 (min (max indent
551 (save-excursion (skip-chars-backward " \t")
552 (1+ (current-column))))))
553 (save-excursion
554 (when (and (zerop (forward-line -1))
555 (setq other (comment-search-forward
556 (line-end-position) t)))
557 (goto-char other) (setq other (current-column))))
558 (if (and other (<= other max) (>= other min))
559 ;; There is a comment and it's in the range: bingo.
560 (setq indent other)
561 ;; Let's try to align to a comment on the next line, then.
562 (let ((other nil))
563 (save-excursion
564 (when (and (zerop (forward-line 1))
565 (setq other (comment-search-forward
566 (line-end-position) t)))
567 (goto-char other) (setq other (current-column))))
568 (if (and other (<= other max) (> other min))
569 ;; There is a comment and it's in the range: bingo.
570 (setq indent other))))))))
571 (unless (= (current-column) indent)
572 ;; If that's different from current, change it.
573 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
574 (indent-to (if (bolp) indent
575 (max indent (1+ (current-column)))))))
576 (goto-char cpos)
577 (set-marker cpos nil))))
579 ;;;###autoload
580 (defun comment-set-column (arg)
581 "Set the comment column based on point.
582 With no ARG, set the comment column to the current column.
583 With just minus as arg, kill any comment on this line.
584 With any other arg, set comment column to indentation of the previous comment
585 and then align or create a comment on this line at that column."
586 (interactive "P")
587 (cond
588 ((eq arg '-) (comment-kill nil))
589 (arg
590 (comment-normalize-vars)
591 (save-excursion
592 (beginning-of-line)
593 (comment-search-backward)
594 (beginning-of-line)
595 (goto-char (comment-search-forward (line-end-position)))
596 (setq comment-column (current-column))
597 (message "Comment column set to %d" comment-column))
598 (comment-indent))
599 (t (setq comment-column (current-column))
600 (message "Comment column set to %d" comment-column))))
602 ;;;###autoload
603 (defun comment-kill (arg)
604 "Kill the comment on this line, if any.
605 With prefix ARG, kill comments on that many lines starting with this one."
606 (interactive "P")
607 (comment-normalize-vars)
608 (dotimes (_ (prefix-numeric-value arg))
609 (save-excursion
610 (beginning-of-line)
611 (let ((cs (comment-search-forward (line-end-position) t)))
612 (when cs
613 (goto-char cs)
614 (skip-syntax-backward " ")
615 (setq cs (point))
616 (comment-forward)
617 (kill-region cs (if (bolp) (1- (point)) (point)))
618 (indent-according-to-mode))))
619 (if arg (forward-line 1))))
621 (defun comment-padright (str &optional n)
622 "Construct a string composed of STR plus `comment-padding'.
623 It also adds N copies of the last non-whitespace chars of STR.
624 If STR already contains padding, the corresponding amount is
625 ignored from `comment-padding'.
626 N defaults to 0.
627 If N is `re', a regexp is returned instead, that would match
628 the string for any N."
629 (setq n (or n 0))
630 (when (and (stringp str) (not (string= "" str)))
631 ;; Separate the actual string from any leading/trailing padding
632 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
633 (let ((s (match-string 1 str)) ;actual string
634 (lpad (substring str 0 (match-beginning 1))) ;left padding
635 (rpad (concat (substring str (match-end 1)) ;original right padding
636 (substring comment-padding ;additional right padding
637 (min (- (match-end 0) (match-end 1))
638 (length comment-padding)))))
639 ;; We can only duplicate C if the comment-end has multiple chars
640 ;; or if comments can be nested, else the comment-end `}' would
641 ;; be turned into `}}}' where only the first ends the comment
642 ;; and the rest becomes bogus junk.
643 (multi (not (and comment-quote-nested
644 ;; comment-end is a single char
645 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
646 (if (not (symbolp n))
647 (concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
648 ;; construct a regexp that would match anything from just S
649 ;; to any possible output of this function for any N.
650 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
651 lpad "") ;padding is not required
652 (regexp-quote s)
653 (when multi "+") ;the last char of S might be repeated
654 (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
655 rpad "")))))) ;padding is not required
657 (defun comment-padleft (str &optional n)
658 "Construct a string composed of `comment-padding' plus STR.
659 It also adds N copies of the first non-whitespace chars of STR.
660 If STR already contains padding, the corresponding amount is
661 ignored from `comment-padding'.
662 N defaults to 0.
663 If N is `re', a regexp is returned instead, that would match
664 the string for any N."
665 (setq n (or n 0))
666 (when (and (stringp str) (not (string= "" str)))
667 ;; Only separate the left pad because we assume there is no right pad.
668 (string-match "\\`\\s-*" str)
669 (let ((s (substring str (match-end 0)))
670 (pad (concat (substring comment-padding
671 (min (- (match-end 0) (match-beginning 0))
672 (length comment-padding)))
673 (match-string 0 str)))
674 (c (aref str (match-end 0))) ;the first non-space char of STR
675 ;; We can only duplicate C if the comment-end has multiple chars
676 ;; or if comments can be nested, else the comment-end `}' would
677 ;; be turned into `}}}' where only the first ends the comment
678 ;; and the rest becomes bogus junk.
679 (multi (not (and comment-quote-nested
680 ;; comment-end is a single char
681 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
682 (if (not (symbolp n))
683 (concat pad (when multi (make-string n c)) s)
684 ;; Construct a regexp that would match anything from just S
685 ;; to any possible output of this function for any N.
686 ;; We match any number of leading spaces because this regexp will
687 ;; be used for uncommenting where we might want to remove
688 ;; uncomment markers with arbitrary leading space (because
689 ;; they were aligned).
690 (concat "\\s-*"
691 (if multi (concat (regexp-quote (string c)) "*"))
692 (regexp-quote s))))))
694 ;;;###autoload
695 (defun uncomment-region (beg end &optional arg)
696 "Uncomment each line in the BEG .. END region.
697 The numeric prefix ARG can specify a number of chars to remove from the
698 comment markers."
699 (interactive "*r\nP")
700 (comment-normalize-vars)
701 (when (> beg end) (setq beg (prog1 end (setq end beg))))
702 (save-excursion
703 (if uncomment-region-function
704 (funcall uncomment-region-function beg end arg)
705 (goto-char beg)
706 (setq end (copy-marker end))
707 (let* ((numarg (prefix-numeric-value arg))
708 (ccs comment-continue)
709 (srei (comment-padright ccs 're))
710 (csre (comment-padright comment-start 're))
711 (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
712 spt)
713 (while (and (< (point) end)
714 (setq spt (comment-search-forward end t)))
715 (let ((ipt (point))
716 ;; Find the end of the comment.
717 (ept (progn
718 (goto-char spt)
719 (unless
721 (comment-forward)
722 ;; Allow eob as comment-end instead of \n.
723 (and
724 (eobp)
725 (let ((s1 (aref (syntax-table) (char-after spt)))
726 (s2 (aref (syntax-table)
727 (or (char-after (1+ spt)) 0)))
728 (sn (aref (syntax-table) ?\n))
729 (flag->b (car (string-to-syntax "> b")))
730 (flag-1b (car (string-to-syntax " 1b")))
731 (flag-2b (car (string-to-syntax " 2b"))))
732 (cond
733 ;; One-character comment-start terminated by
734 ;; \n.
735 ((and
736 (equal sn (string-to-syntax ">"))
737 (equal s1 (string-to-syntax "<")))
738 (insert-char ?\n 1)
740 ;; Two-character type b comment-start
741 ;; terminated by \n.
742 ((and
743 (= (logand (car sn) flag->b) flag->b)
744 (= (logand (car s1) flag-1b) flag-1b)
745 (= (logand (car s2) flag-2b) flag-2b))
746 (insert-char ?\n 1)
747 t)))))
748 (error "Can't find the comment end"))
749 (point)))
750 (box nil)
751 (box-equal nil)) ;Whether we might be using `=' for boxes.
752 (save-restriction
753 (narrow-to-region spt ept)
755 ;; Remove the comment-start.
756 (goto-char ipt)
757 (skip-syntax-backward " ")
758 ;; A box-comment starts with a looong comment-start marker.
759 (when (and (or (and (= (- (point) (point-min)) 1)
760 (setq box-equal t)
761 (looking-at "=\\{7\\}")
762 (not (eq (char-before (point-max)) ?\n))
763 (skip-chars-forward "="))
764 (> (- (point) (point-min) (length comment-start)) 7))
765 (> (count-lines (point-min) (point-max)) 2))
766 (setq box t))
767 ;; Skip the padding. Padding can come from comment-padding and/or
768 ;; from comment-start, so we first check comment-start.
769 (if (or (save-excursion (goto-char (point-min)) (looking-at csre))
770 (looking-at (regexp-quote comment-padding)))
771 (goto-char (match-end 0)))
772 (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
773 (goto-char (match-end 0)))
774 (if (null arg) (delete-region (point-min) (point))
775 (skip-syntax-backward " ")
776 (delete-char (- numarg))
777 (unless (or (bobp)
778 (save-excursion (goto-char (point-min))
779 (looking-at comment-start-skip)))
780 ;; If there's something left but it doesn't look like
781 ;; a comment-start any more, just remove it.
782 (delete-region (point-min) (point))))
784 ;; Remove the end-comment (and leading padding and such).
785 (goto-char (point-max)) (comment-enter-backward)
786 ;; Check for special `=' used sometimes in comment-box.
787 (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
788 (let ((pos (point)))
789 ;; skip `=' but only if there are at least 7.
790 (when (> (skip-chars-backward "=") -7) (goto-char pos))))
791 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
792 (when (and (bolp) (not (bobp))) (backward-char))
793 (if (null arg) (delete-region (point) (point-max))
794 (skip-syntax-forward " ")
795 (delete-char numarg)
796 (unless (or (eobp) (looking-at comment-end-skip))
797 ;; If there's something left but it doesn't look like
798 ;; a comment-end any more, just remove it.
799 (delete-region (point) (point-max)))))
801 ;; Unquote any nested end-comment.
802 (comment-quote-nested comment-start comment-end t)
804 ;; Eliminate continuation markers as well.
805 (when sre
806 (let* ((cce (comment-string-reverse (or comment-continue
807 comment-start)))
808 (erei (and box (comment-padleft cce 're)))
809 (ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
810 (goto-char (point-min))
811 (while (progn
812 (if (and ere (re-search-forward
813 ere (line-end-position) t))
814 (replace-match "" t t nil (if (match-end 2) 2 1))
815 (setq ere nil))
816 (forward-line 1)
817 (re-search-forward sre (line-end-position) t))
818 (replace-match "" t t nil (if (match-end 2) 2 1)))))
819 ;; Go to the end for the next comment.
820 (goto-char (point-max)))))))
821 (set-marker end nil)))
823 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
824 "Make the leading and trailing extra lines.
825 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
826 (let ((eindent 0))
827 (if (not block)
828 ;; Try to match CS and CE's content so they align aesthetically.
829 (progn
830 (setq ce (comment-string-strip ce t t))
831 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce "\n" cs))
832 (setq eindent
833 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
834 0))))
835 ;; box comment
836 (let* ((width (- max-indent min-indent))
837 (s (concat cs "a=m" cce))
838 (e (concat ccs "a=m" ce))
839 (c (if (string-match ".*\\S-\\S-" cs)
840 (aref cs (1- (match-end 0)))
841 (if (and (equal comment-end "") (string-match ".*\\S-" cs))
842 (aref cs (1- (match-end 0))) ?=)))
843 (re "\\s-*a=m\\s-*")
844 (_ (string-match re s))
845 (lcs (length cs))
846 (fill
847 (make-string (+ width (- (match-end 0)
848 (match-beginning 0) lcs 3)) c)))
849 (setq cs (replace-match fill t t s))
850 (when (and (not (string-match comment-start-skip cs))
851 (string-match "a=m" s))
852 ;; The whitespace around CS cannot be ignored: put it back.
853 (setq re "a=m")
854 (setq fill (make-string (- width lcs) c))
855 (setq cs (replace-match fill t t s)))
856 (string-match re e)
857 (setq ce (replace-match fill t t e))))
858 (cons (concat cs "\n" (make-string min-indent ? ) ccs)
859 (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce))))
861 (defmacro comment-with-narrowing (beg end &rest body)
862 "Execute BODY with BEG..END narrowing.
863 Space is added (and then removed) at the beginning for the text's
864 indentation to be kept as it was before narrowing."
865 (declare (debug t) (indent 2))
866 (let ((bindent (make-symbol "bindent")))
867 `(let ((,bindent (save-excursion (goto-char ,beg) (current-column))))
868 (save-restriction
869 (narrow-to-region ,beg ,end)
870 (goto-char (point-min))
871 (insert (make-string ,bindent ? ))
872 (prog1
873 (progn ,@body)
874 ;; remove the bindent
875 (save-excursion
876 (goto-char (point-min))
877 (when (looking-at " *")
878 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent)))
879 (delete-char n)
880 (setq ,bindent (- ,bindent n))))
881 (end-of-line)
882 (let ((e (point)))
883 (beginning-of-line)
884 (while (and (> ,bindent 0) (re-search-forward " *" e t))
885 (let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1))))
886 (goto-char (match-beginning 0))
887 (delete-char n)
888 (setq ,bindent (- ,bindent n)))))))))))
890 (defun comment-region-internal (beg end cs ce
891 &optional ccs cce block lines indent)
892 "Comment region BEG .. END.
893 CS and CE are the comment start resp end string.
894 CCS and CCE are the comment continuation strings for the start resp end
895 of lines (default to CS and CE).
896 BLOCK indicates that end of lines should be marked with either CCE, CE or CS
897 \(if CE is empty) and that those markers should be aligned.
898 LINES indicates that an extra lines will be used at the beginning and end
899 of the region for CE and CS.
900 INDENT indicates to put CS and CCS at the current indentation of the region
901 rather than at left margin."
902 ;;(assert (< beg end))
903 (let ((no-empty (not (or (eq comment-empty-lines t)
904 (and comment-empty-lines (zerop (length ce)))))))
905 ;; Sanitize CE and CCE.
906 (if (and (stringp ce) (string= "" ce)) (setq ce nil))
907 (if (and (stringp cce) (string= "" cce)) (setq cce nil))
908 ;; If CE is empty, multiline cannot be used.
909 (unless ce (setq ccs nil cce nil))
910 ;; Should we mark empty lines as well ?
911 (if (or ccs block lines) (setq no-empty nil))
912 ;; Make sure we have end-markers for BLOCK mode.
913 (when block (unless ce (setq ce (comment-string-reverse cs))))
914 ;; If BLOCK is not requested, we don't need CCE.
915 (unless block (setq cce nil))
916 ;; Continuation defaults to the same as CS and CE.
917 (unless ccs (setq ccs cs cce ce))
919 (save-excursion
920 (goto-char end)
921 ;; If the end is not at the end of a line and the comment-end
922 ;; is implicit (i.e. a newline), explicitly insert a newline.
923 (unless (or ce (eolp)) (insert "\n") (indent-according-to-mode))
924 (comment-with-narrowing beg end
925 (let ((min-indent (point-max))
926 (max-indent 0))
927 (goto-char (point-min))
928 ;; Quote any nested comment marker
929 (comment-quote-nested comment-start comment-end nil)
931 ;; Loop over all lines to find the needed indentations.
932 (goto-char (point-min))
933 (while
934 (progn
935 (unless (looking-at "[ \t]*$")
936 (setq min-indent (min min-indent (current-indentation))))
937 (end-of-line)
938 (setq max-indent (max max-indent (current-column)))
939 (not (or (eobp) (progn (forward-line) nil)))))
941 ;; Inserting ccs can change max-indent by (1- tab-width).
942 (setq max-indent
943 (+ max-indent (max (length cs) (length ccs)) tab-width -1))
944 (unless indent (setq min-indent 0))
946 ;; make the leading and trailing lines if requested
947 (when lines
948 (let ((csce
949 (comment-make-extra-lines
950 cs ce ccs cce min-indent max-indent block)))
951 (setq cs (car csce))
952 (setq ce (cdr csce))))
954 (goto-char (point-min))
955 ;; Loop over all lines from BEG to END.
956 (while
957 (progn
958 (unless (and no-empty (looking-at "[ \t]*$"))
959 (move-to-column min-indent t)
960 (insert cs) (setq cs ccs) ;switch to CCS after the first line
961 (end-of-line)
962 (if (eobp) (setq cce ce))
963 (when cce
964 (when block (move-to-column max-indent t))
965 (insert cce)))
966 (end-of-line)
967 (not (or (eobp) (progn (forward-line) nil))))))))))
969 ;;;###autoload
970 (defun comment-region (beg end &optional arg)
971 "Comment or uncomment each line in the region.
972 With just \\[universal-argument] prefix arg, uncomment each line in region BEG .. END.
973 Numeric prefix arg ARG means use ARG comment characters.
974 If ARG is negative, delete that many comment characters instead.
975 By default, comments start at the left margin, are terminated on each line,
976 even for syntax in which newline does not end the comment and blank lines
977 do not get comments. This can be changed with `comment-style'.
979 The strings used as comment starts are built from
980 `comment-start' without trailing spaces and `comment-padding'."
981 (interactive "*r\nP")
982 (comment-normalize-vars)
983 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
984 (let* ((numarg (prefix-numeric-value arg))
985 (add comment-add)
986 (style (cdr (assoc comment-style comment-styles)))
987 (lines (nth 2 style))
988 (block (nth 1 style))
989 (multi (nth 0 style)))
990 (save-excursion
991 (if comment-region-function
992 (funcall comment-region-function beg end arg)
993 ;; we use `chars' instead of `syntax' because `\n' might be
994 ;; of end-comment syntax rather than of whitespace syntax.
995 ;; sanitize BEG and END
996 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
997 (setq beg (max beg (point)))
998 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
999 (setq end (min end (point)))
1000 (if (>= beg end) (error "Nothing to comment"))
1002 ;; sanitize LINES
1003 (setq lines
1004 (and
1005 lines ;; multi
1006 (progn (goto-char beg) (beginning-of-line)
1007 (skip-syntax-forward " ")
1008 (>= (point) beg))
1009 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
1010 (<= (point) end))
1011 (or block (not (string= "" comment-end)))
1012 (or block (progn (goto-char beg) (search-forward "\n" end t))))))
1014 ;; don't add end-markers just because the user asked for `block'
1015 (unless (or lines (string= "" comment-end)) (setq block nil))
1017 (cond
1018 ((consp arg) (uncomment-region beg end))
1019 ((< numarg 0) (uncomment-region beg end (- numarg)))
1021 (setq numarg (if (and (null arg) (= (length comment-start) 1))
1022 add (1- numarg)))
1023 (comment-region-internal
1024 beg end
1025 (let ((s (comment-padright comment-start numarg)))
1026 (if (string-match comment-start-skip s) s
1027 (comment-padright comment-start)))
1028 (let ((s (comment-padleft comment-end numarg)))
1029 (and s (if (string-match comment-end-skip s) s
1030 (comment-padright comment-end))))
1031 (if multi (comment-padright comment-continue numarg))
1032 (if multi
1033 (comment-padleft (comment-string-reverse comment-continue) numarg))
1034 block
1035 lines
1036 (nth 3 style)))))))
1038 (defun comment-box (beg end &optional arg)
1039 "Comment out the BEG .. END region, putting it inside a box.
1040 The numeric prefix ARG specifies how many characters to add to begin- and
1041 end- comment markers additionally to what `comment-add' already specifies."
1042 (interactive "*r\np")
1043 (let ((comment-style (if (cadr (assoc comment-style comment-styles))
1044 'box-multi 'box)))
1045 (comment-region beg end (+ comment-add arg))))
1048 ;;;###autoload
1049 (defun comment-or-uncomment-region (beg end &optional arg)
1050 "Call `comment-region', unless the region only consists of comments,
1051 in which case call `uncomment-region'. If a prefix arg is given, it
1052 is passed on to the respective function."
1053 (interactive "*r\nP")
1054 (comment-normalize-vars)
1055 (funcall (if (save-excursion ;; check for already commented region
1056 (goto-char beg)
1057 (comment-forward (point-max))
1058 (<= end (point)))
1059 'uncomment-region 'comment-region)
1060 beg end arg))
1062 ;;;###autoload
1063 (defun comment-dwim (arg)
1064 "Call the comment command you want (Do What I Mean).
1065 If the region is active and `transient-mark-mode' is on, call
1066 `comment-region' (unless it only consists of comments, in which
1067 case it calls `uncomment-region').
1068 Else, if the current line is empty, insert a comment and indent it.
1069 Else if a prefix ARG is specified, call `comment-kill'.
1070 Else, call `comment-indent'."
1071 (interactive "*P")
1072 (comment-normalize-vars)
1073 (if (and mark-active transient-mark-mode)
1074 (comment-or-uncomment-region (region-beginning) (region-end) arg)
1075 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
1076 ;; FIXME: If there's no comment to kill on this line and ARG is
1077 ;; specified, calling comment-kill is not very clever.
1078 (if arg (comment-kill (and (integerp arg) arg)) (comment-indent))
1079 (let ((add (if arg (prefix-numeric-value arg)
1080 (if (= (length comment-start) 1) comment-add 0))))
1081 ;; Some modes insist on keeping column 0 comment in column 0
1082 ;; so we need to move away from it before inserting the comment.
1083 (indent-according-to-mode)
1084 (insert (comment-padright comment-start add))
1085 (save-excursion
1086 (unless (string= "" comment-end)
1087 (insert (comment-padleft comment-end add)))
1088 (indent-according-to-mode))))))
1090 (defcustom comment-auto-fill-only-comments nil
1091 "Non-nil means to only auto-fill inside comments.
1092 This has no effect in modes that do not define a comment syntax."
1093 :type 'boolean)
1095 (defun comment-valid-prefix-p (prefix compos)
1097 ;; Accept any prefix if the current comment is not EOL-terminated.
1098 (save-excursion (goto-char compos) (comment-forward) (not (bolp)))
1099 ;; Accept any prefix that starts with a comment-start marker.
1100 (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)")
1101 prefix)))
1103 ;;;###autoload
1104 (defun comment-indent-new-line (&optional soft)
1105 "Break line at point and indent, continuing comment if within one.
1106 This indents the body of the continued comment
1107 under the previous comment line.
1109 This command is intended for styles where you write a comment per line,
1110 starting a new comment (and terminating it if necessary) on each line.
1111 If you want to continue one comment across several lines, use \\[newline-and-indent].
1113 If a fill column is specified, it overrides the use of the comment column
1114 or comment indentation.
1116 The inserted newline is marked hard if variable `use-hard-newlines' is true,
1117 unless optional argument SOFT is non-nil."
1118 (interactive)
1119 (comment-normalize-vars t)
1120 (let (compos comin)
1121 ;; If we are not inside a comment and we only auto-fill comments,
1122 ;; don't do anything (unless no comment syntax is defined).
1123 (unless (and comment-start
1124 comment-auto-fill-only-comments
1125 (not (interactive-p))
1126 (not (save-excursion
1127 (prog1 (setq compos (comment-beginning))
1128 (setq comin (point))))))
1130 ;; Now we know we should auto-fill.
1131 ;; Insert the newline before removing empty space so that markers
1132 ;; get preserved better.
1133 (if soft (insert-and-inherit ?\n) (newline 1))
1134 (save-excursion (forward-char -1) (delete-horizontal-space))
1135 (delete-horizontal-space)
1137 (if (and fill-prefix (not adaptive-fill-mode))
1138 ;; Blindly trust a non-adaptive fill-prefix.
1139 (progn
1140 (indent-to-left-margin)
1141 (insert-before-markers-and-inherit fill-prefix))
1143 ;; If necessary check whether we're inside a comment.
1144 (unless (or compos (null comment-start))
1145 (save-excursion
1146 (backward-char)
1147 (setq compos (comment-beginning))
1148 (setq comin (point))))
1150 (cond
1151 ;; If there's an adaptive prefix, use it unless we're inside
1152 ;; a comment and the prefix is not a comment starter.
1153 ((and fill-prefix
1154 (or (not compos)
1155 (comment-valid-prefix-p fill-prefix compos)))
1156 (indent-to-left-margin)
1157 (insert-and-inherit fill-prefix))
1158 ;; If we're not inside a comment, just try to indent.
1159 ((not compos) (indent-according-to-mode))
1161 (let* ((comment-column
1162 ;; The continuation indentation should be somewhere between
1163 ;; the current line's indentation (plus 2 for good measure)
1164 ;; and the current comment's indentation, with a preference
1165 ;; for comment-column.
1166 (save-excursion
1167 ;; FIXME: use prev line's info rather than first line's.
1168 (goto-char compos)
1169 (min (current-column) (max comment-column
1170 (+ 2 (current-indentation))))))
1171 (comstart (buffer-substring compos comin))
1172 (normalp
1173 (string-match (regexp-quote (comment-string-strip
1174 comment-start t t))
1175 comstart))
1176 (comment-end
1177 (if normalp comment-end
1178 ;; The comment starter is not the normal comment-start
1179 ;; so we can't just use comment-end.
1180 (save-excursion
1181 (goto-char compos)
1182 (if (not (comment-forward)) comment-end
1183 (comment-string-strip
1184 (buffer-substring
1185 (save-excursion (comment-enter-backward) (point))
1186 (point))
1187 nil t)))))
1188 (comment-start comstart)
1189 (continuep (or comment-multi-line
1190 (cadr (assoc comment-style comment-styles))))
1191 ;; Force comment-continue to be recreated from comment-start.
1192 ;; FIXME: wrong if comment-continue was set explicitly!
1193 ;; FIXME: use prev line's continuation if available.
1194 (comment-continue nil))
1195 (if (and comment-multi-line (> (length comment-end) 0))
1196 (indent-according-to-mode)
1197 (insert-and-inherit ?\n)
1198 (forward-char -1)
1199 (comment-indent continuep)
1200 (save-excursion
1201 (let ((pt (point)))
1202 (end-of-line)
1203 (let ((comend (buffer-substring pt (point))))
1204 ;; The 1+ is to make sure we delete the \n inserted above.
1205 (delete-region pt (1+ (point)))
1206 (end-of-line 0)
1207 (insert comend))))))))))))
1209 (provide 'newcomment)
1211 ;;; arch-tag: 01e3320a-00c8-44ea-a696-8f8e7354c858
1212 ;;; newcomment.el ends here