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