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