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