* lisp/term/xterm.el: Add gui-get-selection support via OSC-52
[emacs.git] / lisp / newcomment.el
Commit [+]AuthorDateLineData
ba83908c Stefan Monnier2011-03-11 15:04:22 -05001;;; newcomment.el --- (un)comment regions of buffers -*- lexical-binding: t -*-
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00004
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +00005;; Author: code extracted from Emacs-20's simple.el
e8bf8343 Richard M. Stallman2005-08-09 20:38:35 +00006;; Maintainer: Stefan Monnier <monnier@iro.umontreal.ca>
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00007;; Keywords: comment uncomment
bd78fa1d Chong Yidong2010-08-29 12:17:13 -04008;; Package: emacs
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00009
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +000010;; This file is part of GNU Emacs.
11
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000012;; GNU Emacs is free software: you can redistribute it and/or modify
83b96b22 Stefan Monnier1999-11-28 18:51:06 +000013;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
Glenn Morris2008-05-06 08:06:51 +000014;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +000016
17;; GNU Emacs is distributed in the hope that it will be useful,
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +000018;; 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 Stefan Monnier2000-05-19 15:37:41 +000021
83b96b22 Stefan Monnier1999-11-28 18:51:06 +000022;; You should have received a copy of the GNU General Public License
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000023;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +000024
25;;; Commentary:
26
608d9d7c
CY
Chong Yidong2012-10-23 10:17:36 +080027;; 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
Stefan Monnier1999-11-28 18:51:06 +000034
35;;; Bugs:
36
3674a4a9
SM
Stefan Monnier2002-09-03 01:45:18 +000037;; - boxed comments in Perl are not properly uncommented because they are
38;; uncommented one-line at a time.
75e95a97 Stefan Monnier2001-11-29 21:29:36 +000039;; - nested comments in sgml-mode are not properly quoted.
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +000040;; - single-char nestable comment-start can only do the "\\s<+" stuff
41;; if the corresponding closing marker happens to be right.
3e569d22
SM
Stefan Monnier1999-12-08 00:19:51 +000042;; - uncomment-region with a numeric argument can render multichar
43;; comment markers invalid.
3fc5b4e2
SM
Stefan Monnier2000-05-23 20:06:10 +000044;; - 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
Stefan Monnier1999-11-28 18:51:06 +000049
50;;; Todo:
51
3674a4a9
SM
Stefan Monnier2002-09-03 01:45:18 +000052;; - 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 Stefan Monnier1999-11-29 00:49:18 +000057;; - uncomment-region with a consp (for blocks) or somehow make the
3674a4a9
SM
Stefan Monnier2002-09-03 01:45:18 +000058;; 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
Stefan Monnier2000-05-13 19:41:08 +000062;; - somehow allow comment-dwim to use the region even if transient-mark-mode
63;; is not turned on.
83b96b22 Stefan Monnier1999-11-28 18:51:06 +000064
bdbe3a89
SM
Stefan Monnier2000-09-29 19:11:42 +000065;; - 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
Stefan Monnier1999-11-28 18:51:06 +000070;;; Code:
71
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +000072;;;###autoload
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +000073(defalias 'indent-for-comment 'comment-indent)
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +000074;;;###autoload
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +000075(defalias 'set-comment-column 'comment-set-column)
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +000076;;;###autoload
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +000077(defalias 'kill-comment 'comment-kill)
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +000078;;;###autoload
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +000079(defalias 'indent-new-comment-line 'comment-indent-new-line)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +000080
3e569d22
SM
Stefan Monnier1999-12-08 00:19:51 +000081(defgroup comment nil
82 "Indenting and filling of comments."
83 :prefix "comment-"
ac87b3a9 Dave Love2000-06-20 09:40:36 +000084 :version "21.1"
3e569d22
SM
Stefan Monnier1999-12-08 00:19:51 +000085 :group 'fill)
86
75975685
RS
Richard M. Stallman2003-04-19 04:20:58 +000087;; Autoload this to avoid warnings, since some major modes define it.
88;;;###autoload
771c9b97
SM
Stefan Monnier2000-05-13 19:41:08 +000089(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
Stefan Monnier2000-05-14 00:56:10 +000093to understand comments or not in the given buffer.
94Major modes should set this variable.")
2ab98065 Stefan Monnier1999-11-30 16:20:55 +000095
88fe06af
SM
Stefan Monnier2002-04-29 23:43:11 +000096(defcustom comment-fill-column nil
97 "Column to use for `comment-indent'. If nil, use `fill-column' instead."
f5307782
JB
Juanma Barranquero2005-05-19 19:06:19 +000098 :type '(choice (const nil) integer)
99 :group 'comment)
88fe06af Stefan Monnier2002-04-29 23:43:11 +0000100
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000101;;;###autoload
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000102(defcustom comment-column 32
ae2c9c21 Stefan Monnier2006-02-03 15:57:56 +0000103 "Column to indent right-margin comments to.
2ed8e1f7 Stefan Monnier2007-05-08 15:59:45 +0000104Each mode may establish a different default value for this variable; you
a704fec0 Stefan Monnier2002-04-08 22:58:27 +0000105can set the value for a particular mode using that mode's hook.
2ed8e1f7
SM
Stefan Monnier2007-05-08 15:59:45 +0000106Comments 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
Juanma Barranquero2005-05-19 19:06:19 +0000108 :type 'integer
109 :group 'comment)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000110(make-variable-buffer-local 'comment-column)
28be5ce7
SM
Stefan Monnier2012-05-07 16:48:41 -0400111;;;###autoload
112(put 'comment-column 'safe-local-variable 'integerp)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000113
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000114;;;###autoload
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000115(defvar comment-start nil
fb7ada5f Chong Yidong2012-04-09 21:05:48 +0800116 "String to insert to start a new comment, or nil if no comment syntax.")
28be5ce7
SM
Stefan Monnier2012-05-07 16:48:41 -0400117;;;###autoload
118(put 'comment-start 'safe-local-variable 'string-or-null-p)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000119
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000120;;;###autoload
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000121(defvar comment-start-skip nil
fb7ada5f Chong Yidong2012-04-09 21:05:48 +0800122 "Regexp to match the start of a comment plus everything up to its body.
2b7858ec
DG
Dmitry Gutov2014-03-18 08:06:33 +0200123If 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 Stefan Monnier2012-05-07 16:48:41 -0400126;;;###autoload
238f3a58 Stefan Monnier2012-10-09 12:37:51 -0400127(put 'comment-start-skip 'safe-local-variable 'stringp)
771c9b97 Stefan Monnier2000-05-13 19:41:08 +0000128
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000129;;;###autoload
3e569d22 Stefan Monnier1999-12-08 00:19:51 +0000130(defvar comment-end-skip nil
bfbbace7 Chong Yidong2011-05-28 19:07:43 -0400131 "Regexp to match the end of a comment plus everything back to its body.")
28be5ce7 Stefan Monnier2012-05-07 16:48:41 -0400132;;;###autoload
238f3a58 Stefan Monnier2012-10-09 12:37:51 -0400133(put 'comment-end-skip 'safe-local-variable 'stringp)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000134
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000135;;;###autoload
aaa448c9 Dan Nicolaescu2009-11-11 19:24:20 +0000136(defvar comment-end (purecopy "")
fb7ada5f Chong Yidong2012-04-09 21:05:48 +0800137 "String to insert to end a new comment.
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000138Should be an empty string if comments are terminated by end-of-line.")
28be5ce7 Stefan Monnier2012-05-07 16:48:41 -0400139;;;###autoload
238f3a58 Stefan Monnier2012-10-09 12:37:51 -0400140(put 'comment-end 'safe-local-variable 'stringp)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000141
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000142;;;###autoload
87cf8421 Stefan Monnier2000-10-05 22:43:22 +0000143(defvar comment-indent-function 'comment-indent-default
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +0000144 "Function to compute desired indentation for a comment.
145This function is called with no args with point at the beginning of
87cf8421
SM
Stefan Monnier2000-10-05 22:43:22 +0000146the 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 Stefan Monnier1999-11-28 18:51:06 +0000149
a71b3805
EZ
Eli Zaretskii2004-02-16 12:37:21 +0000150;;;###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 Stefan Monnier2004-09-07 05:18:49 +0000158(defvar comment-region-function 'comment-region-default
a71b3805
EZ
Eli Zaretskii2004-02-16 12:37:21 +0000159 "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 Stefan Monnier2004-09-07 05:18:49 +0000166(defvar uncomment-region-function 'uncomment-region-default
a71b3805
EZ
Eli Zaretskii2004-02-16 12:37:21 +0000167 "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
Stefan Monnier1999-12-08 00:19:51 +0000175(defvar block-comment-start nil)
176(defvar block-comment-end nil)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000177
771c9b97
SM
Stefan Monnier2000-05-13 19:41:08 +0000178(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
Stefan Monnier2000-05-14 00:56:10 +0000183 "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 Stefan Monnier2000-05-13 19:41:08 +0000192(defvar comment-add 0
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000193 "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
Richard M. Stallman2007-10-26 09:52:12 +0000195The `plain' comment style doubles this value.
196
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000197This should generally stay 0, except for a few modes like Lisp where
c391a81f Richard M. Stallman2007-10-26 09:52:12 +0000198it is 1 so that regions are commented with two or three semi-colons.")
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000199
1339bf43 Chong Yidong2012-02-25 11:08:39 +0800200;;;###autoload
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000201(defconst comment-styles
ee9355dc
SM
Stefan Monnier2010-12-03 18:14:50 -0500202 '((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
Stefan Monnier2000-05-14 00:56:10 +0000221STYLE should be a mnemonic symbol.
222MULTI specifies that comments are allowed to span multiple lines.
ee9355dc
SM
Stefan Monnier2010-12-03 18:14:50 -0500223 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 Stefan Monnier2000-05-14 00:56:10 +0000231ALIGN specifies that the `comment-end' markers should be aligned.
ee9355dc
SM
Stefan Monnier2010-12-03 18:14:50 -0500232 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 Juanma Barranquero2012-02-21 15:48:13 +0100240
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000241EXTRA 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
Stefan Monnier2010-12-03 18:14:50 -0500243 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 Stefan Monnier2000-05-14 00:56:10 +0000254INDENT specifies that the `comment-start' markers should not be put at the
c391a81f
RS
Richard M. Stallman2007-10-26 09:52:12 +0000255 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 Stefan Monnier2000-05-14 00:56:10 +0000258
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000259;;;###autoload
3976387b Dan Nicolaescu2008-12-18 09:24:20 +0000260(defcustom comment-style 'indent
ae2c9c21 Stefan Monnier2006-02-03 15:57:56 +0000261 "Style to be used for `comment-region'.
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000262See `comment-styles' for a list of available styles."
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000263 :type (if (boundp 'comment-styles)
ee9355dc
SM
Stefan Monnier2010-12-03 18:14:50 -0500264 `(choice
265 ,@(mapcar (lambda (s)
266 `(const :tag ,(format "%s: %s" (car s) (nth 5 s))
267 ,(car s)))
268 comment-styles))
f5307782 Juanma Barranquero2005-05-19 19:06:19 +0000269 'symbol)
3976387b Dan Nicolaescu2008-12-18 09:24:20 +0000270 :version "23.1"
f5307782 Juanma Barranquero2005-05-19 19:06:19 +0000271 :group 'comment)
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000272
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000273;;;###autoload
aaa448c9 Dan Nicolaescu2009-11-11 19:24:20 +0000274(defcustom comment-padding (purecopy " ")
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000275 "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
Stefan Monnier1999-11-30 16:20:55 +0000278
279Extra spacing between the comment characters and the comment text
0603917d Stefan Monnier2001-11-19 20:55:30 +0000280makes the comment easier to read. Default is 1. nil means 0."
f5307782
JB
Juanma Barranquero2005-05-19 19:06:19 +0000281 :type '(choice string integer (const nil))
282 :group 'comment)
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000283
8eb5d48f
PH
Philipp Haselwarter2012-03-25 14:37:24 -0400284(defcustom comment-inline-offset 1
285 "Inline comments have to be preceded by at least this many spaces.
2311d8e5 Glenn Morris2012-04-16 20:06:56 -0700286This is useful when style-conventions require a certain minimal offset.
8eb5d48f
PH
Philipp Haselwarter2012-03-25 14:37:24 -0400287Python's PEP8 for example recommends two spaces, so you could do:
288
289\(add-hook 'python-mode-hook
2311d8e5 Glenn Morris2012-04-16 20:06:56 -0700290 (lambda () (set (make-local-variable 'comment-inline-offset) 2)))
8eb5d48f
PH
Philipp Haselwarter2012-03-25 14:37:24 -0400291
292See `comment-padding' for whole-line comments."
2a1e2476 Glenn Morris2012-08-15 09:29:11 -0700293 :version "24.3"
8eb5d48f
PH
Philipp Haselwarter2012-03-25 14:37:24 -0400294 :type 'integer
295 :group 'comment)
296
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000297;;;###autoload
3e569d22 Stefan Monnier1999-12-08 00:19:51 +0000298(defcustom comment-multi-line nil
ae2c9c21 Stefan Monnier2006-02-03 15:57:56 +0000299 "Non-nil means `comment-indent-new-line' continues comments.
269a8f1c
LT
Luc Teirlinck2004-09-29 22:17:15 +0000300That 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
Juanma Barranquero2005-05-19 19:06:19 +0000306 :type 'boolean
307 :group 'comment)
3e569d22 Stefan Monnier1999-12-08 00:19:51 +0000308
6976bf99
SM
Stefan Monnier2003-05-30 20:06:40 +0000309(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 Stefan Monnier2006-10-10 00:33:16 +0000312If `eol' it only comments out empty lines if comments are
6976bf99
SM
Stefan Monnier2003-05-30 20:06:40 +0000313terminated by the end of line (i.e. `comment-end' is empty)."
314 :type '(choice (const :tag "Never" nil)
315 (const :tag "Always" t)
9c61f806 Glenn Morris2013-12-27 17:24:15 -0800316 (const :tag "EOl-terminated" eol))
f5307782 Juanma Barranquero2005-05-19 19:06:19 +0000317 :group 'comment)
6976bf99 Stefan Monnier2003-05-30 20:06:40 +0000318
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000319;;;;
320;;;; Helpers
321;;;;
322
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000323(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 Stefan Monnier2000-05-16 22:02:37 +0000326 "\\(.*?\\)" (if afterp "\\s-*\n?")
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000327 "\\'") str)
328 (match-string 1 str))
329
330(defun comment-string-reverse (s)
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000331 "Return the mirror image of string S, without any trailing space."
771c9b97 Stefan Monnier2000-05-13 19:41:08 +0000332 (comment-string-strip (concat (nreverse (string-to-list s))) nil t))
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000333
7164ef13 Stefan Monnier2001-10-11 01:46:37 +0000334;;;###autoload
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000335(defun comment-normalize-vars (&optional noerror)
608d9d7c
CY
Chong Yidong2012-10-23 10:17:36 +0800336 "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
Stefan Monnier2003-11-25 21:20:51 +0000341 (unless (and (not comment-start) noerror)
342 (unless comment-start
31df2900
SM
Stefan Monnier2004-03-04 20:02:52 +0000343 (let ((cs (read-string "No comment syntax is defined. Use: ")))
344 (if (zerop (length cs))
345 (error "No comment syntax defined")
d473dce8
CY
Chong Yidong2011-09-18 14:10:01 -0400346 (set (make-local-variable 'comment-start) cs)
347 (set (make-local-variable 'comment-start-skip) cs))))
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000348 ;; comment-use-syntax
771c9b97 Stefan Monnier2000-05-13 19:41:08 +0000349 (when (eq comment-use-syntax 'undecided)
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000350 (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
Stefan Monnier2000-05-13 19:41:08 +0000354 ;; Try to skip over a comment using forward-comment
355 ;; to see if the syntax tables properly recognize it.
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000356 (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 Stefan Monnier1999-11-30 16:20:55 +0000361 ;; comment-padding
9145f1c2 Stefan Monnier2001-11-13 20:19:22 +0000362 (unless comment-padding (setq comment-padding 0))
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000363 (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 Stefan Monnier2000-05-14 00:56:10 +0000369 (unless (or comment-continue (string= comment-end ""))
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000370 (set (make-local-variable 'comment-continue)
9b0d1d6e Stefan Monnier2000-05-21 00:27:53 +0000371 (concat (if (string-match "\\S-\\S-" comment-start) " " "|")
7164ef13
SM
Stefan Monnier2001-10-11 01:46:37 +0000372 (substring comment-start 1)))
373 ;; Hasn't been necessary yet.
374 ;; (unless (string-match comment-start-skip comment-continue)
016c63b6 Juanma Barranquero2006-08-20 12:48:19 +0000375 ;; (kill-local-variable 'comment-continue))
7164ef13 Stefan Monnier2001-10-11 01:46:37 +0000376 )
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000377 ;; comment-skip regexps
f23a0f3a
SM
Stefan Monnier2002-06-18 17:58:18 +0000378 (unless (and comment-start-skip
379 ;; In case comment-start has changed since last time.
380 (string-match comment-start-skip comment-start))
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000381 (set (make-local-variable 'comment-start-skip)
2b7858ec
DG
Dmitry Gutov2014-03-18 08:06:33 +0200382 (concat (unless (eq comment-use-syntax t)
383 ;; `syntax-ppss' will detect escaping.
384 "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)")
385 "\\(\\s<+\\|"
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000386 (regexp-quote (comment-string-strip comment-start t t))
75ed43a6
SM
Stefan Monnier2001-03-02 20:31:48 +0000387 ;; 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
Stefan Monnier2002-06-18 17:58:18 +0000390 (unless (and comment-end-skip
391 ;; In case comment-end has changed since last time.
c9805d23
SM
Stefan Monnier2010-09-21 15:09:22 +0200392 (string-match comment-end-skip
393 (if (string= "" comment-end) "\n" comment-end)))
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000394 (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
Stefan Monnier2001-09-27 21:13:44 +0000397 ;; 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 Stefan Monnier1999-11-30 16:20:55 +0000400 "\\|" (regexp-quote (substring ce 0 1))
771c9b97 Stefan Monnier2000-05-13 19:41:08 +0000401 (if (and comment-quote-nested (<= (length ce) 1)) "" "+")
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000402 (regexp-quote (substring ce 1))
403 "\\)"))))))
f1180544 Juanma Barranquero2003-02-04 12:29:42 +0000404
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000405(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
Stefan Monnier1999-11-30 16:20:55 +0000437
438;;;;
439;;;; Navigation
440;;;;
441
da9ea6d9 Dmitry Gutov2013-10-06 02:40:16 +0300442(defvar comment-use-global-state t
f5f95edf
SM
Stefan Monnier2004-03-08 01:20:53 +0000443 "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
Dmitry Gutov2013-10-06 02:40:16 +0300445point 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 Stefan Monnier2004-03-08 01:20:53 +0000451
ad679e45 Stefan Monnier2000-05-16 22:02:37 +0000452(defun comment-search-forward (limit &optional noerror)
771c9b97
SM
Stefan Monnier2000-05-13 19:41:08 +0000453 "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
Glenn Morris2013-06-18 13:57:56 -0400456and 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 Stefan Monnier1999-11-30 16:20:55 +0000459 (if (not comment-use-syntax)
9b0d1d6e
SM
Stefan Monnier2000-05-21 00:27:53 +0000460 (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
Stefan Monnier2000-05-16 22:02:37 +0000464 (let* ((pt (point))
465 ;; Assume (at first) that pt is outside of any string.
f5f95edf
SM
Stefan Monnier2004-03-08 01:20:53 +0000466 (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
Stefan Monnier2004-09-07 05:18:49 +0000470 ;; 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
Stefan Monnier2007-04-01 19:33:44 +0000476 (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
Stefan Monnier2000-05-16 22:02:37 +0000479 (unless noerror (error "No comment"))
480 ;; We found the comment.
9b0d1d6e Stefan Monnier2000-05-21 00:27:53 +0000481 (let ((pos (point))
ad679e45 Stefan Monnier2000-05-16 22:02:37 +0000482 (start (nth 8 s))
9b0d1d6e Stefan Monnier2000-05-21 00:27:53 +0000483 (bol (line-beginning-position))
ad679e45
SM
Stefan Monnier2000-05-16 22:02:37 +0000484 (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 Stefan Monnier2000-05-21 00:27:53 +0000489 (goto-char (or end pos))
ad679e45 Stefan Monnier2000-05-16 22:02:37 +0000490 start)))))
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000491
492(defun comment-search-backward (&optional limit noerror)
493 "Find a comment start between LIMIT and point.
771c9b97
SM
Stefan Monnier2000-05-13 19:41:08 +0000494Moves point to inside the comment and returns the position of the
495comment-starter. If no comment is found, moves point to LIMIT
dd7426ea
GM
Glenn Morris2013-06-18 13:57:56 -0400496and 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
Leo Liu2013-06-07 19:48:28 +0800499 ;; 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
Stefan Monnier1999-11-30 16:20:55 +0000520
521(defun comment-beginning ()
771c9b97
SM
Stefan Monnier2000-05-13 19:41:08 +0000522 "Find the beginning of the enclosing comment.
523Returns nil if not inside a comment, else moves point and returns
2308f447 Daniel Pfeiffer2004-10-03 15:36:18 +0000524the same as `comment-search-backward'."
da9ea6d9 Dmitry Gutov2013-10-06 02:40:16 +0300525 (if (and comment-use-syntax comment-use-global-state)
e9155c4a
DG
Dmitry Gutov2013-10-01 04:13:48 +0300526 (let ((state (syntax-ppss)))
527 (when (nth 4 state)
528 (goto-char (nth 8 state))
529 (prog1 (point)
2b7858ec
DG
Dmitry Gutov2014-03-18 08:06:33 +0200530 (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
Dmitry Gutov2013-10-01 04:13:48 +0300535 (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
Stefan Monnier1999-11-30 16:20:55 +0000559
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 Stefan Monnier2000-05-19 15:37:41 +0000568 (setq n
0f38a885
SM
Stefan Monnier2002-03-04 01:10:55 +0000569 (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 Stefan Monnier2000-05-19 15:37:41 +0000573 (1- n) -1)))
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000574 (= n 0))))
575
576(defun comment-enter-backward ()
577 "Move from the end of a comment to the end of its content.
771c9b97 Stefan Monnier2000-05-13 19:41:08 +0000578Point is assumed to be just at the end of a comment."
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000579 (if (bolp)
580 ;; comment-end = ""
581 (progn (backward-char) (skip-syntax-backward " "))
3cc4b076 Stefan Monnier2005-11-20 18:09:09 +0000582 (cond
33d71ec3
SM
Stefan Monnier2008-04-03 22:26:39 +0000583 ((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 Stefan Monnier2005-11-20 18:09:09 +0000588 (goto-char (match-beginning 0)))
2ed8e1f7
SM
Stefan Monnier2007-05-08 15:59:45 +0000589 ;; 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
Stefan Monnier2005-11-20 18:09:09 +0000600 (backward-char 2)
601 (skip-chars-backward (string (char-after)))
2ed8e1f7
SM
Stefan Monnier2007-05-08 15:59:45 +0000602 (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
Stefan Monnier1999-11-30 16:20:55 +0000607
608;;;;
609;;;; Commands
610;;;;
611
fae99944 Stefan Monnier2000-10-08 19:07:41 +0000612;;;###autoload
87cf8421
SM
Stefan Monnier2000-10-05 22:43:22 +0000613(defun comment-indent-default ()
614 "Default for `comment-indent-function'."
d3fcda22
SM
Stefan Monnier2000-11-14 10:04:20 +0000615 (if (and (looking-at "\\s<\\s<\\(\\s<\\)?")
616 (or (match-end 1) (/= (current-column) (current-indentation))))
617 0
87cf8421 Stefan Monnier2000-10-05 22:43:22 +0000618 (when (or (/= (current-column) (current-indentation))
8c39e595 Stefan Monnier2005-06-29 06:46:37 +0000619 (and (> comment-add 0) (looking-at "\\s<\\(\\S<\\|\\'\\)")))
87cf8421
SM
Stefan Monnier2000-10-05 22:43:22 +0000620 comment-column)))
621
2ed8e1f7
SM
Stefan Monnier2007-05-08 15:59:45 +0000622(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 Glenn Morris2012-04-16 20:06:56 -0700636 (if (bolp) 0 (+ comment-inline-offset (current-column))))))
2ed8e1f7
SM
Stefan Monnier2007-05-08 15:59:45 +0000637 ;; 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 Stefan Monnier2000-05-25 19:05:46 +0000674;;;###autoload
3e569d22 Stefan Monnier1999-12-08 00:19:51 +0000675(defun comment-indent (&optional continue)
a71b3805 Eli Zaretskii2004-02-16 12:37:21 +0000676 "Indent this line's comment to `comment-column', or insert an empty comment.
dde6824c Stefan Monnier2001-08-21 18:31:50 +0000677If CONTINUE is non-nil, use the `comment-continue' markers if any."
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000678 (interactive "*")
ad679e45 Stefan Monnier2000-05-16 22:02:37 +0000679 (comment-normalize-vars)
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +0000680 (let* ((empty (save-excursion (beginning-of-line)
681 (looking-at "[ \t]*$")))
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000682 (starter (or (and continue comment-continue)
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000683 (and empty block-comment-start) comment-start))
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000684 (ender (or (and continue comment-continue "")
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000685 (and empty block-comment-end) comment-end)))
2e1fbba4
SM
Stefan Monnier2000-11-21 21:31:16 +0000686 (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
Glenn Morris2007-11-02 08:10:40 +0000691 (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
Stefan Monnier2000-11-21 21:31:16 +0000704 ;; If none, insert one.
705 (save-excursion
a71b3805
EZ
Eli Zaretskii2004-02-16 12:37:21 +0000706 ;; 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 Stefan Monnier2001-09-01 21:23:17 +0000709 (indent-to comment-column)
e6bba95e
DL
Dave Love2003-02-13 15:54:19 +0000710 ;; Ensure there's a space before the comment for things
711 ;; like sh where it matters (as well as being neater).
7c0bbe7f
JB
Juanma Barranquero2006-11-27 14:01:47 +0000712 (unless (memq (char-before) '(nil ?\n ?\t ?\s))
713 (insert ?\s))
07f10bab Stefan Monnier2003-05-09 20:09:04 +0000714 (setq begpos (point))
bb304a7a Stefan Monnier2000-11-14 15:10:02 +0000715 (insert starter)
2e1fbba4 Stefan Monnier2000-11-21 21:31:16 +0000716 (setq cpos (point-marker))
b9b4d12c
GM
Glenn Morris2007-11-02 08:10:40 +0000717 (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
Stefan Monnier2012-03-25 14:41:06 -0400736 (setq indent (max indent
737 (+ (current-column) comment-inline-offset)))))
b9b4d12c
GM
Glenn Morris2007-11-02 08:10:40 +0000738 ;; 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 Stefan Monnier1999-11-28 18:51:06 +0000744
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000745;;;###autoload
3e569d22 Stefan Monnier1999-12-08 00:19:51 +0000746(defun comment-set-column (arg)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000747 "Set the comment column based on point.
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000748With no ARG, set the comment column to the current column.
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +0000749With 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 Stefan Monnier1999-11-29 01:31:47 +0000753 (cond
ad679e45 Stefan Monnier2000-05-16 22:02:37 +0000754 ((eq arg '-) (comment-kill nil))
e8fe7d39 Stefan Monnier1999-11-29 01:31:47 +0000755 (arg
2abd0306 Stefan Monnier2003-05-06 14:06:34 +0000756 (comment-normalize-vars)
e8fe7d39
SM
Stefan Monnier1999-11-29 01:31:47 +0000757 (save-excursion
758 (beginning-of-line)
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000759 (comment-search-backward)
e8fe7d39 Stefan Monnier1999-11-29 01:31:47 +0000760 (beginning-of-line)
ad679e45 Stefan Monnier2000-05-16 22:02:37 +0000761 (goto-char (comment-search-forward (line-end-position)))
83b96b22 Stefan Monnier1999-11-28 18:51:06 +0000762 (setq comment-column (current-column))
e8fe7d39 Stefan Monnier1999-11-29 01:31:47 +0000763 (message "Comment column set to %d" comment-column))
ad679e45 Stefan Monnier2000-05-16 22:02:37 +0000764 (comment-indent))
e8fe7d39 Stefan Monnier1999-11-29 01:31:47 +0000765 (t (setq comment-column (current-column))
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +0000766 (message "Comment column set to %d" comment-column))))
767
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000768;;;###autoload
3e569d22 Stefan Monnier1999-12-08 00:19:51 +0000769(defun comment-kill (arg)
bbcedd05 Stefan Monnier2008-03-03 21:20:21 +0000770 "Kill the first comment on this line, if any.
7a0a180a
SM
Stefan Monnier1999-11-29 00:49:18 +0000771With prefix ARG, kill comments on that many lines starting with this one."
772 (interactive "P")
2abd0306 Stefan Monnier2003-05-06 14:06:34 +0000773 (comment-normalize-vars)
f64049c6 Glenn Morris2011-05-11 09:37:03 -0700774 (dotimes (_i (prefix-numeric-value arg))
ad679e45
SM
Stefan Monnier2000-05-16 22:02:37 +0000775 (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 Stefan Monnier1999-11-29 00:49:18 +0000786
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000787(defun comment-padright (str &optional n)
788 "Construct a string composed of STR plus `comment-padding'.
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000789It also adds N copies of the last non-whitespace chars of STR.
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000790If STR already contains padding, the corresponding amount is
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000791ignored from `comment-padding'.
792N defaults to 0.
771c9b97 Stefan Monnier2000-05-13 19:41:08 +0000793If N is `re', a regexp is returned instead, that would match
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000794the string for any N."
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000795 (setq n (or n 0))
796 (when (and (stringp str) (not (string= "" str)))
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000797 ;; Separate the actual string from any leading/trailing padding
3e569d22 Stefan Monnier1999-12-08 00:19:51 +0000798 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000799 (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 Stefan Monnier1999-12-08 00:19:51 +0000803 (min (- (match-end 0) (match-end 1))
9b0d1d6e
SM
Stefan Monnier2000-05-21 00:27:53 +0000804 (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 Stefan Monnier2000-05-14 00:56:10 +0000812 (if (not (symbolp n))
9b0d1d6e Stefan Monnier2000-05-21 00:27:53 +0000813 (concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000814 ;; 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
Stefan Monnier2000-05-21 00:27:53 +0000818 (regexp-quote s)
819 (when multi "+") ;the last char of S might be repeated
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000820 (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
821 rpad "")))))) ;padding is not required
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000822
823(defun comment-padleft (str &optional n)
824 "Construct a string composed of `comment-padding' plus STR.
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000825It also adds N copies of the first non-whitespace chars of STR.
2ab98065 Stefan Monnier1999-11-30 16:20:55 +0000826If STR already contains padding, the corresponding amount is
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000827ignored from `comment-padding'.
828N defaults to 0.
771c9b97 Stefan Monnier2000-05-13 19:41:08 +0000829If N is `re', a regexp is returned instead, that would match
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000830 the string for any N."
831 (setq n (or n 0))
832 (when (and (stringp str) (not (string= "" str)))
f5215400 Stefan Monnier2000-05-14 00:56:10 +0000833 ;; Only separate the left pad because we assume there is no right pad.
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +0000834 (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
Stefan Monnier2000-05-14 00:56:10 +0000840 (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 Stefan Monnier1999-11-28 18:51:06 +0000859
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +0000860;;;###autoload
7a0a180a Stefan Monnier1999-11-29 00:49:18 +0000861(defun uncomment-region (beg end &optional arg)
eb0b51f8 Stefan Monnier2002-09-10 06:20:52 +0000862 "Uncomment each line in the BEG .. END region.
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +0000863The numeric prefix ARG can specify a number of chars to remove from the
864comment markers."
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +0000865 (interactive "*r\nP")
866 (comment-normalize-vars)
065b7364 Eli Zaretskii2004-02-16 14:12:57 +0000867 (when (> beg end) (setq beg (prog1 end (setq end beg))))
b70dd952
SM
Stefan Monnier2004-09-07 05:18:49 +0000868 ;; 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
Eli Zaretskii2004-09-04 12:31:09 +0000870 ;; included in a mult-line string, but it is actually not.
871 (let ((comment-use-global-state nil))
872 (save-excursion
b70dd952
SM
Stefan Monnier2004-09-07 05:18:49 +0000873 (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 Juanma Barranquero2006-08-20 12:48:19 +0000902
b70dd952
SM
Stefan Monnier2004-09-07 05:18:49 +0000903 ;; 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
Juanma Barranquero2012-02-21 15:48:13 +0100923 (let ((opoint (point-marker)))
924 (skip-syntax-backward " ")
eb864a71
LM
Lawrence Mitchell2012-02-17 10:44:32 -0500925 (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 Juanma Barranquero2006-08-20 12:48:19 +0000932
b70dd952
SM
Stefan Monnier2004-09-07 05:18:49 +0000933 ;; 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 Stefan Monnier1999-11-28 18:51:06 +0000971
aac88001 Stefan Monnier1999-11-28 21:33:55 +0000972(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
ad679e45
SM
Stefan Monnier2000-05-16 22:02:37 +0000973 "Make the leading and trailing extra lines.
974This is used for `extra-line' style (or `box' style if BLOCK is specified)."
9b0d1d6e
SM
Stefan Monnier2000-05-21 00:27:53 +0000975 (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
Stefan Monnier2002-09-03 01:45:18 +0000989 (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
Stefan Monnier2000-05-21 00:27:53 +0000995 (fill
996 (make-string (+ width (- (match-end 0)
3674a4a9 Stefan Monnier2002-09-03 01:45:18 +0000997 (match-beginning 0) lcs 3)) c)))
aac88001 Stefan Monnier1999-11-28 21:33:55 +0000998 (setq cs (replace-match fill t t s))
3674a4a9
SM
Stefan Monnier2002-09-03 01:45:18 +0000999 (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
Stefan Monnier2000-05-21 00:27:53 +00001006 (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 Stefan Monnier1999-11-28 21:33:55 +00001009
aac88001
SM
Stefan Monnier1999-11-28 21:33:55 +00001010(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 Stefan Monnier2002-09-13 18:22:37 +00001014 (declare (debug t) (indent 2))
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +00001015 (let ((bindent (make-symbol "bindent")))
bfe6e13f Stefan Monnier2003-05-10 22:54:35 +00001016 `(let ((,bindent (save-excursion (goto-char ,beg) (current-column))))
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +00001017 (save-restriction
bfe6e13f Stefan Monnier2003-05-10 22:54:35 +00001018 (narrow-to-region ,beg ,end)
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +00001019 (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 Stefan Monnier1999-11-28 21:33:55 +00001028 (delete-char n)
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +00001029 (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 Stefan Monnier1999-11-28 21:33:55 +00001038
c391a81f Richard M. Stallman2007-10-26 09:52:12 +00001039(defun comment-add (arg)
1b8b3954
SM
Stefan Monnier2011-09-14 17:21:59 -04001040 "Compute the number of extra comment starter characters.
1041\(Extra semicolons in Lisp mode, extra stars in C mode, etc.)
f0b7054e
SM
Stefan Monnier2010-08-17 21:38:30 +02001042If ARG is non-nil, just follow ARG.
1043If the comment starter is multi-char, just follow ARG.
1044Otherwise obey `comment-add'."
b433a560 Stefan Monnier2006-04-17 03:51:30 +00001045 (if (and (null arg) (= (string-match "[ \t]*\\'" comment-start) 1))
c391a81f Richard M. Stallman2007-10-26 09:52:12 +00001046 (* comment-add 1)
b433a560
SM
Stefan Monnier2006-04-17 03:51:30 +00001047 (1- (prefix-numeric-value arg))))
1048
771c9b97 Stefan Monnier2000-05-13 19:41:08 +00001049(defun comment-region-internal (beg end cs ce
aa417798 Juanma Barranquero2004-06-17 00:02:13 +00001050 &optional ccs cce block lines indent)
3674a4a9 Stefan Monnier2002-09-03 01:45:18 +00001051 "Comment region BEG .. END.
aa417798
JB
Juanma Barranquero2004-06-17 00:02:13 +00001052CS 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 Stefan Monnier2000-05-19 15:37:41 +00001061 ;;(assert (< beg end))
6976bf99 Stefan Monnier2003-05-30 20:06:40 +00001062 (let ((no-empty (not (or (eq comment-empty-lines t)
53dd1d53
GM
Glenn Morris2007-11-28 05:20:33 +00001063 (and comment-empty-lines (zerop (length ce))))))
1064 ce-sanitized)
f5215400 Stefan Monnier2000-05-14 00:56:10 +00001065 ;; Sanitize CE and CCE.
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001066 (if (and (stringp ce) (string= "" ce)) (setq ce nil))
53dd1d53 Glenn Morris2007-11-28 05:20:33 +00001067 (setq ce-sanitized ce)
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001068 (if (and (stringp cce) (string= "" cce)) (setq cce nil))
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +00001069 ;; 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 Stefan Monnier1999-11-28 18:51:06 +00001072 (if (or ccs block lines) (setq no-empty nil))
f5215400 Stefan Monnier2000-05-14 00:56:10 +00001073 ;; Make sure we have end-markers for BLOCK mode.
2ab98065 Stefan Monnier1999-11-30 16:20:55 +00001074 (when block (unless ce (setq ce (comment-string-reverse cs))))
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +00001075 ;; 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 Juanma Barranquero2003-02-04 12:29:42 +00001079
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001080 (save-excursion
aac88001 Stefan Monnier1999-11-28 21:33:55 +00001081 (goto-char end)
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +00001082 ;; 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 Glenn Morris2007-11-28 05:20:33 +00001084 (unless (or ce-sanitized (eolp)) (insert "\n") (indent-according-to-mode))
aac88001 Stefan Monnier1999-11-28 21:33:55 +00001085 (comment-with-narrowing beg end
f5215400 Stefan Monnier2000-05-14 00:56:10 +00001086 (let ((min-indent (point-max))
9d5240d2 Stefan Monnier2001-10-12 22:37:51 +00001087 (max-indent 0))
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001088 (goto-char (point-min))
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +00001089 ;; 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 Stefan Monnier2000-05-22 04:24:01 +00001093 (goto-char (point-min))
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +00001094 (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 Juanma Barranquero2003-02-04 12:29:42 +00001101
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +00001102 (setq max-indent
45f6a663
SM
Stefan Monnier2005-11-04 22:20:12 +00001103 (+ 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 Stefan Monnier2000-05-13 19:41:08 +00001109 (unless indent (setq min-indent 0))
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001110
aac88001 Stefan Monnier1999-11-28 21:33:55 +00001111 ;; make the leading and trailing lines if requested
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001112 (when lines
771c9b97
SM
Stefan Monnier2000-05-13 19:41:08 +00001113 (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 Juanma Barranquero2003-02-04 12:29:42 +00001118
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +00001119 (goto-char (point-min))
1120 ;; Loop over all lines from BEG to END.
f5215400
SM
Stefan Monnier2000-05-14 00:56:10 +00001121 (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 Stefan Monnier1999-11-28 18:51:06 +00001133
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +00001134;;;###autoload
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +00001135(defun comment-region (beg end &optional arg)
1136 "Comment or uncomment each line in the region.
3674a4a9 Stefan Monnier2002-09-03 01:45:18 +00001137With just \\[universal-argument] prefix arg, uncomment each line in region BEG .. END.
11a26e05 Juanma Barranquero2004-06-10 20:38:28 +00001138Numeric prefix ARG means use ARG comment characters.
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001139If ARG is negative, delete that many comment characters instead.
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001140
707f4689
CY
Chong Yidong2009-05-26 03:16:31 +00001141The 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
Stefan Monnier1999-11-28 18:51:06 +00001150 (interactive "*r\nP")
1151 (comment-normalize-vars)
1152 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
b70dd952
SM
Stefan Monnier2004-09-07 05:18:49 +00001153 (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 Stefan Monnier1999-11-30 16:20:55 +00001158 (let* ((numarg (prefix-numeric-value arg))
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +00001159 (style (cdr (assoc comment-style comment-styles)))
1160 (lines (nth 2 style))
1161 (block (nth 1 style))
1162 (multi (nth 0 style)))
5266b06b
RS
Richard M. Stallman2007-09-17 16:40:58 +00001163
1164 ;; We use `chars' instead of `syntax' because `\n' might be
b70dd952
SM
Stefan Monnier2004-09-07 05:18:49 +00001165 ;; 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 Richard M. Stallman2007-10-26 09:52:12 +00001192 (let ((multi-char (/= (string-match "[ \t]*\\'" comment-start) 1))
9f15f676 Richard M. Stallman2007-12-22 18:39:16 +00001193 indent triple)
c391a81f Richard M. Stallman2007-10-26 09:52:12 +00001194 (if (eq (nth 3 style) 'multi-char)
9f15f676
RS
Richard M. Stallman2007-12-22 18:39:16 +00001195 (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
Richard M. Stallman2007-10-26 09:52:12 +00001202 (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 Richard M. Stallman2007-12-22 18:39:16 +00001208 (setq numarg (* comment-add (if triple 2 1)))
c391a81f
RS
Richard M. Stallman2007-10-26 09:52:12 +00001209 (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 Stefan Monnier2000-05-13 19:41:08 +00001225
016c63b6 Juanma Barranquero2006-08-20 12:48:19 +00001226;;;###autoload
771c9b97 Stefan Monnier2000-05-13 19:41:08 +00001227(defun comment-box (beg end &optional arg)
3674a4a9 Stefan Monnier2002-09-03 01:45:18 +00001228 "Comment out the BEG .. END region, putting it inside a box.
771c9b97 Stefan Monnier2000-05-13 19:41:08 +00001229The numeric prefix ARG specifies how many characters to add to begin- and
9fc9a531
AH
Aaron S. Hawley2013-01-11 18:08:55 -05001230end- comment markers additionally to what variable `comment-add' already
1231specifies."
771c9b97 Stefan Monnier2000-05-13 19:41:08 +00001232 (interactive "*r\np")
016c63b6 Juanma Barranquero2006-08-20 12:48:19 +00001233 (comment-normalize-vars)
9b0d1d6e
SM
Stefan Monnier2000-05-21 00:27:53 +00001234 (let ((comment-style (if (cadr (assoc comment-style comment-styles))
1235 'box-multi 'box)))
771c9b97 Stefan Monnier2000-05-13 19:41:08 +00001236 (comment-region beg end (+ comment-add arg))))
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001237
6d3d6113
SM
Stefan Monnier2011-01-14 13:10:15 -05001238(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
Stefan Monnier2002-04-29 23:43:11 +00001244
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 Stefan Monnier2003-05-06 14:06:34 +00001251 (comment-normalize-vars)
6d3d6113 Stefan Monnier2011-01-14 13:10:15 -05001252 (funcall (if (comment-only-p beg end)
88fe06af
SM
Stefan Monnier2002-04-29 23:43:11 +00001253 'uncomment-region 'comment-region)
1254 beg end arg))
1255
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +00001256;;;###autoload
2ab98065 Stefan Monnier1999-11-30 16:20:55 +00001257(defun comment-dwim (arg)
ad679e45
SM
Stefan Monnier2000-05-16 22:02:37 +00001258 "Call the comment command you want (Do What I Mean).
1259If the region is active and `transient-mark-mode' is on, call
23737b4a
LMI
Lars Magne Ingebrigtsen2010-10-21 22:12:38 +02001260`comment-region' (unless it only consists of comments, in which
1261case it calls `uncomment-region').
eb0c4c30
GM
Glenn Morris2007-11-03 03:00:32 +00001262Else, if the current line is empty, call `comment-insert-comment-function'
1263if it is defined, otherwise insert a comment and indent it.
ad679e45 Stefan Monnier2000-05-16 22:02:37 +00001264Else if a prefix ARG is specified, call `comment-kill'.
ae2c9c21
SM
Stefan Monnier2006-02-03 15:57:56 +00001265Else, call `comment-indent'.
1266You can configure `comment-style' to change the way regions are commented."
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +00001267 (interactive "*P")
1268 (comment-normalize-vars)
771c9b97 Stefan Monnier2000-05-13 19:41:08 +00001269 (if (and mark-active transient-mark-mode)
88fe06af Stefan Monnier2002-04-29 23:43:11 +00001270 (comment-or-uncomment-region (region-beginning) (region-end) arg)
2ab98065 Stefan Monnier1999-11-30 16:20:55 +00001271 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
ad679e45
SM
Stefan Monnier2000-05-16 22:02:37 +00001272 ;; 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
Glenn Morris2007-11-03 03:00:32 +00001275 ;; 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 Stefan Monnier1999-11-30 16:20:55 +00001288
1fc075d8 Richard M. Stallman2004-10-16 15:32:14 +00001289;;;###autoload
392f1ef5
SM
Stefan Monnier2000-05-17 19:32:32 +00001290(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
Juanma Barranquero2005-05-19 19:06:19 +00001293 :type 'boolean
1294 :group 'comment)
392f1ef5 Stefan Monnier2000-05-17 19:32:32 +00001295
bfe6e13f Stefan Monnier2003-05-10 22:54:35 +00001296(defun comment-valid-prefix-p (prefix compos)
1b8b3954 Stefan Monnier2011-09-14 17:21:59 -04001297 "Check that the adaptive fill prefix is consistent with the context.
e29d96b6
SM
Stefan Monnier2006-10-10 00:33:16 +00001298PREFIX 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 Juanma Barranquero2009-10-02 03:48:36 +00001334
7164ef13 Stefan Monnier2001-10-11 01:46:37 +00001335
be83ecb2 Stefan Monnier2000-05-25 19:05:46 +00001336;;;###autoload
ad679e45 Stefan Monnier2000-05-16 22:02:37 +00001337(defun comment-indent-new-line (&optional soft)
2ab98065
SM
Stefan Monnier1999-11-30 16:20:55 +00001338 "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
Stefan Monnier2000-05-19 15:37:41 +00001353 (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
Stefan Monnier2000-05-17 19:32:32 +00001356 (unless (and comment-start
1357 comment-auto-fill-only-comments
32226619 Juanma Barranquero2009-10-02 03:48:36 +00001358 (not (called-interactively-p 'interactive))
392f1ef5 Stefan Monnier2000-05-17 19:32:32 +00001359 (not (save-excursion
59a1ce8d Stefan Monnier2000-05-19 15:37:41 +00001360 (prog1 (setq compos (comment-beginning))
392f1ef5 Stefan Monnier2000-05-17 19:32:32 +00001361 (setq comin (point))))))
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +00001362
1363 ;; Now we know we should auto-fill.
88fe06af
SM
Stefan Monnier2002-04-29 23:43:11 +00001364 ;; Insert the newline before removing empty space so that markers
1365 ;; get preserved better.
392f1ef5 Stefan Monnier2000-05-17 19:32:32 +00001366 (if soft (insert-and-inherit ?\n) (newline 1))
88fe06af
SM
Stefan Monnier2002-04-29 23:43:11 +00001367 (save-excursion (forward-char -1) (delete-horizontal-space))
1368 (delete-horizontal-space)
1369
7164ef13
SM
Stefan Monnier2001-10-11 01:46:37 +00001370 (if (and fill-prefix (not adaptive-fill-mode))
1371 ;; Blindly trust a non-adaptive fill-prefix.
392f1ef5
SM
Stefan Monnier2000-05-17 19:32:32 +00001372 (progn
1373 (indent-to-left-margin)
88fe06af Stefan Monnier2002-04-29 23:43:11 +00001374 (insert-before-markers-and-inherit fill-prefix))
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +00001375
1376 ;; If necessary check whether we're inside a comment.
a764440a Stefan Monnier2001-11-19 06:08:38 +00001377 (unless (or compos (null comment-start))
392f1ef5
SM
Stefan Monnier2000-05-17 19:32:32 +00001378 (save-excursion
1379 (backward-char)
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +00001380 (setq compos (comment-beginning))
1381 (setq comin (point))))
1382
7164ef13
SM
Stefan Monnier2001-10-11 01:46:37 +00001383 (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 Stefan Monnier2006-10-10 00:33:16 +00001387 (comment-valid-prefix-p fill-prefix compos))
7164ef13
SM
Stefan Monnier2001-10-11 01:46:37 +00001388 (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 Stefan Monnier2014-04-10 21:05:03 -04001393 (let* ((comstart (buffer-substring compos comin))
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +00001394 (normalp
1395 (string-match (regexp-quote (comment-string-strip
1396 comment-start t t))
1397 comstart))
0d6a1375 Stefan Monnier2014-04-10 21:05:03 -04001398 (comend
59a1ce8d
SM
Stefan Monnier2000-05-19 15:37:41 +00001399 (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
Stefan Monnier2014-04-10 21:05:03 -04001409 nil t))))))
1410 (if (and comment-multi-line (> (length comend) 0))
a764440a
SM
Stefan Monnier2001-11-19 06:08:38 +00001411 (indent-according-to-mode)
1412 (insert-and-inherit ?\n)
1413 (forward-char -1)
0d6a1375
SM
Stefan Monnier2014-04-10 21:05:03 -04001414 (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
Stefan Monnier2001-11-19 06:08:38 +00001445 (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 Stefan Monnier1999-11-30 16:20:55 +00001453
97cb2553
AM
Artur Malabarba2015-02-08 19:03:17 -02001454;;;###autoload
1455(defun comment-line (n)
1456 "Comment or uncomment current line and leave point after it.
1457With positive prefix, apply to N lines including current one.
1458With negative prefix, apply to -N lines above. Also, further
1459consecutive invocations of this command will inherit the negative
1460argument.
1461
1462If region is active, comment lines in active region instead.
1463Unlike `comment-dwim', this always comments whole lines."
1464 (interactive "p")
1465 (if (use-region-p)
1466 (comment-or-uncomment-region
1467 (save-excursion
1468 (goto-char (region-beginning))
1469 (line-beginning-position))
1470 (save-excursion
1471 (goto-char (region-end))
1472 (line-end-position)))
1473 (when (and (eq last-command 'comment-line-backward)
1474 (natnump n))
1475 (setq n (- n)))
1476 (let ((range
1477 (list (line-beginning-position)
1478 (goto-char (line-end-position n)))))
1479 (comment-or-uncomment-region
1480 (apply #'min range)
1481 (apply #'max range)))
1482 (forward-line 1)
1483 (back-to-indentation)
7d631aa0 Artur Malabarba2015-02-08 20:24:36 -02001484 (unless (natnump n) (setq this-command 'comment-line-backward))))
97cb2553 Artur Malabarba2015-02-08 19:03:17 -02001485
83b96b22
SM
Stefan Monnier1999-11-28 18:51:06 +00001486(provide 'newcomment)
1487
83b96b22 Stefan Monnier1999-11-28 18:51:06 +00001488;;; newcomment.el ends here