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