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