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