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