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