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