Commit | Line | Data |
---|---|---|
83b96b22 SM |
1 | ;;; newcomment.el --- (un)comment regions of buffers |
2 | ||
39cb51e7 | 3 | ;; Copyright (C) 1999, 2000 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. | |
771c9b97 | 37 | ;; - comment-box in TeXinfo generate bogus comments @ccccc@ |
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 | |
be83ecb2 | 77 | ;;;###autoload |
3e569d22 SM |
78 | (defgroup comment nil |
79 | "Indenting and filling of comments." | |
80 | :prefix "comment-" | |
ac87b3a9 | 81 | :version "21.1" |
3e569d22 SM |
82 | :group 'fill) |
83 | ||
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 | |
3e569d22 SM |
132 | (defvar block-comment-start nil) |
133 | (defvar block-comment-end nil) | |
83b96b22 | 134 | |
771c9b97 SM |
135 | (defvar comment-quote-nested t |
136 | "Non-nil if nested comments should be quoted. | |
137 | This should be locally set by each major mode if needed.") | |
138 | ||
139 | (defvar comment-continue nil | |
f5215400 SM |
140 | "Continuation string to insert for multiline comments. |
141 | This string will be added at the beginning of each line except the very | |
142 | first one when commenting a region with a commenting style that allows | |
143 | comments to span several lines. | |
144 | It should generally have the same length as `comment-start' in order to | |
145 | preserve indentation. | |
146 | If it is nil a value will be automatically derived from `comment-start' | |
147 | by replacing its first character with a space.") | |
148 | ||
771c9b97 | 149 | (defvar comment-add 0 |
f5215400 SM |
150 | "How many more comment chars should be inserted by `comment-region'. |
151 | This determines the default value of the numeric argument of `comment-region'. | |
152 | This should generally stay 0, except for a few modes like Lisp where | |
153 | it can be convenient to set it to 1 so that regions are commented with | |
154 | two semi-colons.") | |
2ab98065 | 155 | |
2ab98065 | 156 | (defconst comment-styles |
771c9b97 SM |
157 | '((plain . (nil nil nil nil)) |
158 | (indent . (nil nil nil t)) | |
159 | (aligned . (nil t nil t)) | |
160 | (multi-line . (t nil nil t)) | |
161 | (extra-line . (t nil t t)) | |
9b0d1d6e SM |
162 | (box . (nil t t t)) |
163 | (box-multi . (t t t t))) | |
f5215400 SM |
164 | "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)). |
165 | STYLE should be a mnemonic symbol. | |
166 | MULTI specifies that comments are allowed to span multiple lines. | |
167 | ALIGN specifies that the `comment-end' markers should be aligned. | |
168 | EXTRA specifies that an extra line should be used before and after the | |
169 | region to comment (to put the `comment-end' and `comment-start'). | |
170 | INDENT specifies that the `comment-start' markers should not be put at the | |
171 | left margin but at the current indentation of the region to comment.") | |
172 | ||
be83ecb2 | 173 | ;;;###autoload |
f5215400 SM |
174 | (defcustom comment-style 'plain |
175 | "*Style to be used for `comment-region'. | |
176 | See `comment-styles' for a list of available styles." | |
be83ecb2 SM |
177 | :type (if (boundp 'comment-styles) |
178 | `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles)) | |
179 | 'symbol)) | |
2ab98065 | 180 | |
be83ecb2 SM |
181 | ;;;###autoload |
182 | (defcustom comment-padding " " | |
f5215400 SM |
183 | "Padding string that `comment-region' puts between comment chars and text. |
184 | Can also be an integer which will be automatically turned into a string | |
185 | of the corresponding number of spaces. | |
2ab98065 SM |
186 | |
187 | Extra spacing between the comment characters and the comment text | |
0603917d SM |
188 | makes the comment easier to read. Default is 1. nil means 0." |
189 | :type '(choice string integer (const nil))) | |
2ab98065 | 190 | |
be83ecb2 | 191 | ;;;###autoload |
3e569d22 | 192 | (defcustom comment-multi-line nil |
ac87b3a9 | 193 | "*Non-nil means \\[comment-indent-new-line] continues comments, with no new terminator or starter. |
3e569d22 | 194 | This is obsolete because you might as well use \\[newline-and-indent]." |
0603917d | 195 | :type 'boolean) |
3e569d22 | 196 | |
2ab98065 SM |
197 | ;;;; |
198 | ;;;; Helpers | |
199 | ;;;; | |
200 | ||
f5215400 SM |
201 | (defun comment-string-strip (str beforep afterp) |
202 | "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space." | |
203 | (string-match (concat "\\`" (if beforep "\\s-*") | |
ad679e45 | 204 | "\\(.*?\\)" (if afterp "\\s-*\n?") |
2ab98065 SM |
205 | "\\'") str) |
206 | (match-string 1 str)) | |
207 | ||
208 | (defun comment-string-reverse (s) | |
f5215400 | 209 | "Return the mirror image of string S, without any trailing space." |
771c9b97 | 210 | (comment-string-strip (concat (nreverse (string-to-list s))) nil t)) |
2ab98065 | 211 | |
7164ef13 | 212 | ;;;###autoload |
2ab98065 SM |
213 | (defun comment-normalize-vars (&optional noerror) |
214 | (if (not comment-start) (or noerror (error "No comment syntax is defined")) | |
215 | ;; comment-use-syntax | |
771c9b97 | 216 | (when (eq comment-use-syntax 'undecided) |
2ab98065 SM |
217 | (set (make-local-variable 'comment-use-syntax) |
218 | (let ((st (syntax-table)) | |
219 | (cs comment-start) | |
220 | (ce (if (string= "" comment-end) "\n" comment-end))) | |
771c9b97 SM |
221 | ;; Try to skip over a comment using forward-comment |
222 | ;; to see if the syntax tables properly recognize it. | |
2ab98065 SM |
223 | (with-temp-buffer |
224 | (set-syntax-table st) | |
225 | (insert cs " hello " ce) | |
226 | (goto-char (point-min)) | |
227 | (and (forward-comment 1) (eobp)))))) | |
2ab98065 | 228 | ;; comment-padding |
9145f1c2 | 229 | (unless comment-padding (setq comment-padding 0)) |
2ab98065 SM |
230 | (when (integerp comment-padding) |
231 | (setq comment-padding (make-string comment-padding ? ))) | |
232 | ;; comment markers | |
233 | ;;(setq comment-start (comment-string-strip comment-start t nil)) | |
234 | ;;(setq comment-end (comment-string-strip comment-end nil t)) | |
235 | ;; comment-continue | |
f5215400 | 236 | (unless (or comment-continue (string= comment-end "")) |
2ab98065 | 237 | (set (make-local-variable 'comment-continue) |
9b0d1d6e | 238 | (concat (if (string-match "\\S-\\S-" comment-start) " " "|") |
7164ef13 SM |
239 | (substring comment-start 1))) |
240 | ;; Hasn't been necessary yet. | |
241 | ;; (unless (string-match comment-start-skip comment-continue) | |
242 | ;; (kill-local-variable 'comment-continue)) | |
243 | ) | |
2ab98065 | 244 | ;; comment-skip regexps |
f23a0f3a SM |
245 | (unless (and comment-start-skip |
246 | ;; In case comment-start has changed since last time. | |
247 | (string-match comment-start-skip comment-start)) | |
2ab98065 SM |
248 | (set (make-local-variable 'comment-start-skip) |
249 | (concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|" | |
250 | (regexp-quote (comment-string-strip comment-start t t)) | |
75ed43a6 SM |
251 | ;; Let's not allow any \s- but only [ \t] since \n |
252 | ;; might be both a comment-end marker and \s-. | |
253 | "+\\)[ \t]*"))) | |
f23a0f3a SM |
254 | (unless (and comment-end-skip |
255 | ;; In case comment-end has changed since last time. | |
256 | (string-match comment-end-skip comment-end)) | |
2ab98065 SM |
257 | (let ((ce (if (string= "" comment-end) "\n" |
258 | (comment-string-strip comment-end t t)))) | |
259 | (set (make-local-variable 'comment-end-skip) | |
37dbd369 SM |
260 | ;; We use [ \t] rather than \s- because we don't want to |
261 | ;; remove ^L in C mode when uncommenting. | |
262 | (concat "[ \t]*\\(\\s>" (if comment-quote-nested "" "+") | |
2ab98065 | 263 | "\\|" (regexp-quote (substring ce 0 1)) |
771c9b97 | 264 | (if (and comment-quote-nested (<= (length ce) 1)) "" "+") |
2ab98065 SM |
265 | (regexp-quote (substring ce 1)) |
266 | "\\)")))))) | |
267 | ||
f5215400 SM |
268 | (defun comment-quote-re (str unp) |
269 | (concat (regexp-quote (substring str 0 1)) | |
270 | "\\\\" (if unp "+" "*") | |
271 | (regexp-quote (substring str 1)))) | |
272 | ||
273 | (defun comment-quote-nested (cs ce unp) | |
274 | "Quote or unquote nested comments. | |
275 | If UNP is non-nil, unquote nested comment markers." | |
276 | (setq cs (comment-string-strip cs t t)) | |
277 | (setq ce (comment-string-strip ce t t)) | |
278 | (when (and comment-quote-nested (> (length ce) 0)) | |
279 | (let ((re (concat (comment-quote-re ce unp) | |
280 | "\\|" (comment-quote-re cs unp)))) | |
281 | (goto-char (point-min)) | |
282 | (while (re-search-forward re nil t) | |
283 | (goto-char (match-beginning 0)) | |
284 | (forward-char 1) | |
285 | (if unp (delete-char 1) (insert "\\")) | |
286 | (when (= (length ce) 1) | |
287 | ;; If the comment-end is a single char, adding a \ after that | |
288 | ;; "first" char won't deactivate it, so we turn such a CE | |
289 | ;; into !CS. I.e. for pascal, we turn } into !{ | |
290 | (if (not unp) | |
291 | (when (string= (match-string 0) ce) | |
292 | (replace-match (concat "!" cs) t t)) | |
293 | (when (and (< (point-min) (match-beginning 0)) | |
294 | (string= (buffer-substring (1- (match-beginning 0)) | |
295 | (1- (match-end 0))) | |
296 | (concat "!" cs))) | |
297 | (backward-char 2) | |
298 | (delete-char (- (match-end 0) (match-beginning 0))) | |
299 | (insert ce)))))))) | |
2ab98065 SM |
300 | |
301 | ;;;; | |
302 | ;;;; Navigation | |
303 | ;;;; | |
304 | ||
ad679e45 | 305 | (defun comment-search-forward (limit &optional noerror) |
771c9b97 SM |
306 | "Find a comment start between point and LIMIT. |
307 | Moves point to inside the comment and returns the position of the | |
308 | comment-starter. If no comment is found, moves point to LIMIT | |
e8fe7d39 | 309 | and raises an error or returns nil of NOERROR is non-nil." |
2ab98065 | 310 | (if (not comment-use-syntax) |
9b0d1d6e SM |
311 | (if (re-search-forward comment-start-skip limit noerror) |
312 | (or (match-end 1) (match-beginning 0)) | |
313 | (goto-char limit) | |
314 | (unless noerror (error "No comment"))) | |
ad679e45 SM |
315 | (let* ((pt (point)) |
316 | ;; Assume (at first) that pt is outside of any string. | |
317 | (s (parse-partial-sexp pt (or limit (point-max)) nil nil nil t))) | |
318 | (when (and (nth 8 s) (nth 3 s)) | |
319 | ;; The search ended inside a string. Try to see if it | |
320 | ;; works better when we assume that pt is inside a string. | |
321 | (setq s (parse-partial-sexp | |
322 | pt (or limit (point-max)) nil nil | |
323 | (list nil nil nil (nth 3 s) nil nil nil nil) | |
324 | t))) | |
325 | (if (not (and (nth 8 s) (not (nth 3 s)))) | |
326 | (unless noerror (error "No comment")) | |
327 | ;; We found the comment. | |
9b0d1d6e | 328 | (let ((pos (point)) |
ad679e45 | 329 | (start (nth 8 s)) |
9b0d1d6e | 330 | (bol (line-beginning-position)) |
ad679e45 SM |
331 | (end nil)) |
332 | (while (and (null end) (>= (point) bol)) | |
333 | (if (looking-at comment-start-skip) | |
334 | (setq end (min (or limit (point-max)) (match-end 0))) | |
335 | (backward-char))) | |
9b0d1d6e | 336 | (goto-char (or end pos)) |
ad679e45 | 337 | start))))) |
2ab98065 SM |
338 | |
339 | (defun comment-search-backward (&optional limit noerror) | |
340 | "Find a comment start between LIMIT and point. | |
771c9b97 SM |
341 | Moves point to inside the comment and returns the position of the |
342 | comment-starter. If no comment is found, moves point to LIMIT | |
2ab98065 | 343 | and raises an error or returns nil of NOERROR is non-nil." |
ad679e45 SM |
344 | ;; FIXME: If a comment-start appears inside a comment, we may erroneously |
345 | ;; stop there. This can be rather bad in general, but since | |
346 | ;; comment-search-backward is only used to find the comment-column (in | |
347 | ;; comment-set-column) and to find the comment-start string (via | |
348 | ;; comment-beginning) in indent-new-comment-line, it should be harmless. | |
2ab98065 SM |
349 | (if (not (re-search-backward comment-start-skip limit t)) |
350 | (unless noerror (error "No comment")) | |
351 | (beginning-of-line) | |
352 | (let* ((end (match-end 0)) | |
353 | (cs (comment-search-forward end t)) | |
354 | (pt (point))) | |
355 | (if (not cs) | |
356 | (progn (beginning-of-line) | |
357 | (comment-search-backward limit noerror)) | |
358 | (while (progn (goto-char cs) | |
359 | (comment-forward) | |
360 | (and (< (point) end) | |
361 | (setq cs (comment-search-forward end t)))) | |
362 | (setq pt (point))) | |
363 | (goto-char pt) | |
364 | cs)))) | |
365 | ||
366 | (defun comment-beginning () | |
771c9b97 SM |
367 | "Find the beginning of the enclosing comment. |
368 | Returns nil if not inside a comment, else moves point and returns | |
2ab98065 | 369 | the same as `comment-search-forward'." |
75ed43a6 SM |
370 | ;; HACK ATTACK! |
371 | ;; We should really test `in-string-p' but that can be expensive. | |
372 | (unless (eq (get-text-property (point) 'face) 'font-lock-string-face) | |
373 | (let ((pt (point)) | |
374 | (cs (comment-search-backward nil t))) | |
375 | (when cs | |
376 | (if (save-excursion | |
377 | (goto-char cs) | |
dde6824c SM |
378 | (and |
379 | ;; For modes where comment-start and comment-end are the same, | |
380 | ;; the search above may have found a `ce' rather than a `cs'. | |
381 | (or (not (looking-at comment-end-skip)) | |
382 | ;; Maybe font-lock knows that it's a `cs'? | |
383 | (eq (get-text-property (match-end 0) 'face) | |
384 | 'font-lock-comment-face) | |
385 | (unless (eq (get-text-property (point) 'face) | |
386 | 'font-lock-comment-face) | |
387 | ;; Let's assume it's a `cs' if we're on the same line. | |
388 | (>= (line-end-position) pt))) | |
389 | ;; Make sure that PT is not past the end of the comment. | |
390 | (if (comment-forward 1) (> (point) pt) (eobp)))) | |
75ed43a6 SM |
391 | cs |
392 | (goto-char pt) | |
393 | nil))))) | |
2ab98065 SM |
394 | |
395 | (defun comment-forward (&optional n) | |
396 | "Skip forward over N comments. | |
397 | Just like `forward-comment' but only for positive N | |
398 | and can use regexps instead of syntax." | |
399 | (setq n (or n 1)) | |
400 | (if (< n 0) (error "No comment-backward") | |
401 | (if comment-use-syntax (forward-comment n) | |
402 | (while (> n 0) | |
59a1ce8d | 403 | (setq n |
0f38a885 SM |
404 | (if (or (forward-comment 1) |
405 | (and (looking-at comment-start-skip) | |
406 | (goto-char (match-end 0)) | |
407 | (re-search-forward comment-end-skip nil 'move))) | |
59a1ce8d | 408 | (1- n) -1))) |
2ab98065 SM |
409 | (= n 0)))) |
410 | ||
411 | (defun comment-enter-backward () | |
412 | "Move from the end of a comment to the end of its content. | |
771c9b97 | 413 | Point is assumed to be just at the end of a comment." |
2ab98065 SM |
414 | (if (bolp) |
415 | ;; comment-end = "" | |
416 | (progn (backward-char) (skip-syntax-backward " ")) | |
417 | (let ((end (point))) | |
418 | (beginning-of-line) | |
419 | (save-restriction | |
420 | (narrow-to-region (point) end) | |
ad679e45 SM |
421 | (if (re-search-forward (concat comment-end-skip "\\'") nil t) |
422 | (goto-char (match-beginning 0)) | |
423 | ;; comment-end-skip not found probably because it was not set right. | |
424 | ;; Since \\s> should catch the single-char case, we'll blindly | |
425 | ;; assume we're at the end of a two-char comment-end. | |
426 | (goto-char (point-max)) | |
427 | (backward-char 2) | |
428 | (skip-chars-backward (string (char-after))) | |
429 | (skip-syntax-backward " ")))))) | |
2ab98065 SM |
430 | |
431 | ;;;; | |
432 | ;;;; Commands | |
433 | ;;;; | |
434 | ||
fae99944 | 435 | ;;;###autoload |
87cf8421 SM |
436 | (defun comment-indent-default () |
437 | "Default for `comment-indent-function'." | |
d3fcda22 SM |
438 | (if (and (looking-at "\\s<\\s<\\(\\s<\\)?") |
439 | (or (match-end 1) (/= (current-column) (current-indentation)))) | |
440 | 0 | |
87cf8421 SM |
441 | (when (or (/= (current-column) (current-indentation)) |
442 | (and (> comment-add 0) (looking-at "\\s<\\S<"))) | |
443 | comment-column))) | |
444 | ||
be83ecb2 | 445 | ;;;###autoload |
3e569d22 | 446 | (defun comment-indent (&optional continue) |
2ab98065 | 447 | "Indent this line's comment to comment column, or insert an empty comment. |
dde6824c | 448 | If CONTINUE is non-nil, use the `comment-continue' markers if any." |
83b96b22 | 449 | (interactive "*") |
ad679e45 | 450 | (comment-normalize-vars) |
83b96b22 SM |
451 | (let* ((empty (save-excursion (beginning-of-line) |
452 | (looking-at "[ \t]*$"))) | |
f5215400 | 453 | (starter (or (and continue comment-continue) |
2ab98065 | 454 | (and empty block-comment-start) comment-start)) |
f5215400 | 455 | (ender (or (and continue comment-continue "") |
2ab98065 | 456 | (and empty block-comment-end) comment-end))) |
2e1fbba4 SM |
457 | (unless starter (error "No comment syntax defined")) |
458 | (beginning-of-line) | |
459 | (let* ((eolpos (line-end-position)) | |
460 | (begpos (comment-search-forward eolpos t)) | |
461 | cpos indent) | |
462 | ;; An existing comment? | |
7a06b250 SM |
463 | (if begpos |
464 | (progn | |
465 | (if (and (not (looking-at "[\t\n ]")) | |
466 | (looking-at comment-end-skip)) | |
467 | ;; The comment is empty and we have skipped all its space | |
468 | ;; and landed right before the comment-ender: | |
469 | ;; Go back to the middle of the space. | |
470 | (forward-char (/ (skip-chars-backward " \t") -2))) | |
471 | (setq cpos (point-marker))) | |
2e1fbba4 SM |
472 | ;; If none, insert one. |
473 | (save-excursion | |
474 | ;; Some comment-indent-function insist on not moving comments that | |
567e961e SM |
475 | ;; are in column 0, so we first go to the likely target column. |
476 | (indent-to comment-column) | |
2e1fbba4 | 477 | (setq begpos (point)) |
bb304a7a | 478 | (insert starter) |
2e1fbba4 SM |
479 | (setq cpos (point-marker)) |
480 | (insert ender))) | |
481 | (goto-char begpos) | |
482 | ;; Compute desired indent. | |
053b8d35 | 483 | (setq indent (save-excursion (funcall comment-indent-function))) |
2e1fbba4 | 484 | (if (not indent) |
a764440a | 485 | ;; comment-indent-function refuses: delegate to indent. |
2e1fbba4 SM |
486 | (indent-according-to-mode) |
487 | ;; Avoid moving comments past the fill-column. | |
a764440a SM |
488 | (unless (save-excursion (skip-chars-backward " \t") (bolp)) |
489 | (setq indent | |
490 | (min indent | |
491 | (+ (current-column) | |
88fe06af | 492 | (- (or comment-fill-column fill-column) |
a764440a | 493 | (save-excursion (end-of-line) (current-column))))))) |
7164ef13 | 494 | (unless (= (current-column) indent) |
2e1fbba4 | 495 | ;; If that's different from current, change it. |
7164ef13 | 496 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) |
2e1fbba4 SM |
497 | (indent-to (if (bolp) indent |
498 | (max indent (1+ (current-column))))))) | |
499 | (goto-char cpos) | |
500 | (set-marker cpos nil)))) | |
83b96b22 | 501 | |
be83ecb2 | 502 | ;;;###autoload |
3e569d22 | 503 | (defun comment-set-column (arg) |
83b96b22 | 504 | "Set the comment column based on point. |
2ab98065 | 505 | With no ARG, set the comment column to the current column. |
83b96b22 SM |
506 | With just minus as arg, kill any comment on this line. |
507 | With any other arg, set comment column to indentation of the previous comment | |
508 | and then align or create a comment on this line at that column." | |
509 | (interactive "P") | |
e8fe7d39 | 510 | (cond |
ad679e45 | 511 | ((eq arg '-) (comment-kill nil)) |
e8fe7d39 SM |
512 | (arg |
513 | (save-excursion | |
514 | (beginning-of-line) | |
2ab98065 | 515 | (comment-search-backward) |
e8fe7d39 | 516 | (beginning-of-line) |
ad679e45 | 517 | (goto-char (comment-search-forward (line-end-position))) |
83b96b22 | 518 | (setq comment-column (current-column)) |
e8fe7d39 | 519 | (message "Comment column set to %d" comment-column)) |
ad679e45 | 520 | (comment-indent)) |
e8fe7d39 | 521 | (t (setq comment-column (current-column)) |
83b96b22 SM |
522 | (message "Comment column set to %d" comment-column)))) |
523 | ||
be83ecb2 | 524 | ;;;###autoload |
3e569d22 | 525 | (defun comment-kill (arg) |
7a0a180a SM |
526 | "Kill the comment on this line, if any. |
527 | With prefix ARG, kill comments on that many lines starting with this one." | |
528 | (interactive "P") | |
ad679e45 SM |
529 | (dotimes (_ (prefix-numeric-value arg)) |
530 | (save-excursion | |
531 | (beginning-of-line) | |
532 | (let ((cs (comment-search-forward (line-end-position) t))) | |
533 | (when cs | |
534 | (goto-char cs) | |
535 | (skip-syntax-backward " ") | |
536 | (setq cs (point)) | |
537 | (comment-forward) | |
538 | (kill-region cs (if (bolp) (1- (point)) (point))) | |
539 | (indent-according-to-mode)))) | |
540 | (if arg (forward-line 1)))) | |
7a0a180a | 541 | |
2ab98065 SM |
542 | (defun comment-padright (str &optional n) |
543 | "Construct a string composed of STR plus `comment-padding'. | |
f5215400 | 544 | It also adds N copies of the last non-whitespace chars of STR. |
2ab98065 | 545 | If STR already contains padding, the corresponding amount is |
f5215400 SM |
546 | ignored from `comment-padding'. |
547 | N defaults to 0. | |
771c9b97 | 548 | If N is `re', a regexp is returned instead, that would match |
f5215400 | 549 | the string for any N." |
2ab98065 SM |
550 | (setq n (or n 0)) |
551 | (when (and (stringp str) (not (string= "" str))) | |
f5215400 | 552 | ;; Separate the actual string from any leading/trailing padding |
3e569d22 | 553 | (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str) |
f5215400 SM |
554 | (let ((s (match-string 1 str)) ;actual string |
555 | (lpad (substring str 0 (match-beginning 1))) ;left padding | |
556 | (rpad (concat (substring str (match-end 1)) ;original right padding | |
557 | (substring comment-padding ;additional right padding | |
3e569d22 | 558 | (min (- (match-end 0) (match-end 1)) |
9b0d1d6e SM |
559 | (length comment-padding))))) |
560 | ;; We can only duplicate C if the comment-end has multiple chars | |
561 | ;; or if comments can be nested, else the comment-end `}' would | |
562 | ;; be turned into `}}}' where only the first ends the comment | |
563 | ;; and the rest becomes bogus junk. | |
564 | (multi (not (and comment-quote-nested | |
565 | ;; comment-end is a single char | |
566 | (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end))))) | |
f5215400 | 567 | (if (not (symbolp n)) |
9b0d1d6e | 568 | (concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad) |
f5215400 SM |
569 | ;; construct a regexp that would match anything from just S |
570 | ;; to any possible output of this function for any N. | |
571 | (concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?")) | |
572 | lpad "") ;padding is not required | |
9b0d1d6e SM |
573 | (regexp-quote s) |
574 | (when multi "+") ;the last char of S might be repeated | |
f5215400 SM |
575 | (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?")) |
576 | rpad "")))))) ;padding is not required | |
2ab98065 SM |
577 | |
578 | (defun comment-padleft (str &optional n) | |
579 | "Construct a string composed of `comment-padding' plus STR. | |
f5215400 | 580 | It also adds N copies of the first non-whitespace chars of STR. |
2ab98065 | 581 | If STR already contains padding, the corresponding amount is |
f5215400 SM |
582 | ignored from `comment-padding'. |
583 | N defaults to 0. | |
771c9b97 | 584 | If N is `re', a regexp is returned instead, that would match |
2ab98065 SM |
585 | the string for any N." |
586 | (setq n (or n 0)) | |
587 | (when (and (stringp str) (not (string= "" str))) | |
f5215400 | 588 | ;; Only separate the left pad because we assume there is no right pad. |
2ab98065 SM |
589 | (string-match "\\`\\s-*" str) |
590 | (let ((s (substring str (match-end 0))) | |
591 | (pad (concat (substring comment-padding | |
592 | (min (- (match-end 0) (match-beginning 0)) | |
593 | (length comment-padding))) | |
594 | (match-string 0 str))) | |
f5215400 SM |
595 | (c (aref str (match-end 0))) ;the first non-space char of STR |
596 | ;; We can only duplicate C if the comment-end has multiple chars | |
597 | ;; or if comments can be nested, else the comment-end `}' would | |
598 | ;; be turned into `}}}' where only the first ends the comment | |
599 | ;; and the rest becomes bogus junk. | |
600 | (multi (not (and comment-quote-nested | |
601 | ;; comment-end is a single char | |
602 | (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end))))) | |
603 | (if (not (symbolp n)) | |
604 | (concat pad (when multi (make-string n c)) s) | |
605 | ;; Construct a regexp that would match anything from just S | |
606 | ;; to any possible output of this function for any N. | |
607 | ;; We match any number of leading spaces because this regexp will | |
608 | ;; be used for uncommenting where we might want to remove | |
609 | ;; uncomment markers with arbitrary leading space (because | |
610 | ;; they were aligned). | |
611 | (concat "\\s-*" | |
612 | (if multi (concat (regexp-quote (string c)) "*")) | |
613 | (regexp-quote s)))))) | |
83b96b22 | 614 | |
be83ecb2 | 615 | ;;;###autoload |
7a0a180a SM |
616 | (defun uncomment-region (beg end &optional arg) |
617 | "Uncomment each line in the BEG..END region. | |
f5215400 SM |
618 | The numeric prefix ARG can specify a number of chars to remove from the |
619 | comment markers." | |
83b96b22 SM |
620 | (interactive "*r\nP") |
621 | (comment-normalize-vars) | |
622 | (if (> beg end) (let (mid) (setq mid beg beg end end mid))) | |
623 | (save-excursion | |
7a0a180a | 624 | (goto-char beg) |
f5215400 | 625 | (setq end (copy-marker end)) |
7a0a180a | 626 | (let ((numarg (prefix-numeric-value arg)) |
2ab98065 | 627 | spt) |
7a0a180a | 628 | (while (and (< (point) end) |
2ab98065 SM |
629 | (setq spt (comment-search-forward end t))) |
630 | (let* ((ipt (point)) | |
f5215400 | 631 | ;; Find the end of the comment. |
e8fe7d39 | 632 | (ept (progn |
2ab98065 SM |
633 | (goto-char spt) |
634 | (unless (comment-forward) | |
e8fe7d39 | 635 | (error "Can't find the comment end")) |
f5215400 SM |
636 | (point))) |
637 | (box nil) | |
638 | (ccs comment-continue) | |
2ab98065 | 639 | (srei (comment-padright ccs 're)) |
3e569d22 | 640 | (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))) |
e8fe7d39 SM |
641 | (save-restriction |
642 | (narrow-to-region spt ept) | |
f5215400 | 643 | ;; Remove the comment-start. |
2ab98065 SM |
644 | (goto-char ipt) |
645 | (skip-syntax-backward " ") | |
9b0d1d6e SM |
646 | ;; Check for special `=' used sometimes in comment-box. |
647 | (when (and (= (- (point) (point-min)) 1) (looking-at "=\\{7\\}")) | |
648 | (skip-chars-forward "=")) | |
f5215400 | 649 | ;; A box-comment starts with a looong comment-start marker. |
2ab98065 | 650 | (when (> (- (point) (point-min) (length comment-start)) 7) |
f5215400 | 651 | (setq box t)) |
2ab98065 SM |
652 | (when (looking-at (regexp-quote comment-padding)) |
653 | (goto-char (match-end 0))) | |
654 | (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei))) | |
655 | (goto-char (match-end 0))) | |
3e569d22 SM |
656 | (if (null arg) (delete-region (point-min) (point)) |
657 | (skip-syntax-backward " ") | |
658 | (delete-char (- numarg))) | |
2ab98065 | 659 | |
f5215400 | 660 | ;; Remove the end-comment (and leading padding and such). |
2ab98065 | 661 | (goto-char (point-max)) (comment-enter-backward) |
9b0d1d6e | 662 | ;; Check for special `=' used sometimes in comment-box. |
6dcd3806 SM |
663 | (when (= (- (point-max) (point)) 1) |
664 | (let ((pos (point))) | |
665 | ;; skip `=' but only if there are at least 7. | |
666 | (when (> (skip-chars-backward "=") -7) (goto-char pos)))) | |
9b0d1d6e | 667 | (unless (looking-at "\\(\n\\|\\s-\\)*\\'") |
2ab98065 | 668 | (when (and (bolp) (not (bobp))) (backward-char)) |
f5215400 | 669 | (if (null arg) (delete-region (point) (point-max)) |
3e569d22 SM |
670 | (skip-syntax-forward " ") |
671 | (delete-char numarg))) | |
e8fe7d39 | 672 | |
f5215400 SM |
673 | ;; Unquote any nested end-comment. |
674 | (comment-quote-nested comment-start comment-end t) | |
675 | ||
676 | ;; Eliminate continuation markers as well. | |
677 | (when sre | |
678 | (let* ((cce (comment-string-reverse (or comment-continue | |
679 | comment-start))) | |
680 | (erei (and box (comment-padleft cce 're))) | |
681 | (ere (and erei (concat "\\(" erei "\\)\\s-*$")))) | |
7a0a180a | 682 | (goto-char (point-min)) |
f5215400 SM |
683 | (while (progn |
684 | (if (and ere (re-search-forward | |
685 | ere (line-end-position) t)) | |
686 | (replace-match "" t t nil (if (match-end 2) 2 1)) | |
687 | (setq ere nil)) | |
688 | (forward-line 1) | |
689 | (re-search-forward sre (line-end-position) t)) | |
2ab98065 | 690 | (replace-match "" t t nil (if (match-end 2) 2 1))))) |
110c171f | 691 | ;; Go to the end for the next comment. |
f5215400 SM |
692 | (goto-char (point-max))))) |
693 | (set-marker end nil)))) | |
83b96b22 | 694 | |
aac88001 | 695 | (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block) |
ad679e45 SM |
696 | "Make the leading and trailing extra lines. |
697 | This is used for `extra-line' style (or `box' style if BLOCK is specified)." | |
9b0d1d6e SM |
698 | (let ((eindent 0)) |
699 | (if (not block) | |
700 | ;; Try to match CS and CE's content so they align aesthetically. | |
701 | (progn | |
702 | (setq ce (comment-string-strip ce t t)) | |
703 | (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce "\n" cs)) | |
704 | (setq eindent | |
705 | (max (- (match-end 2) (match-beginning 2) (match-beginning 0)) | |
706 | 0)))) | |
707 | ;; box comment | |
708 | (let* ((width (- max-indent min-indent)) | |
709 | (s (concat cs "a=m" cce)) | |
710 | (e (concat ccs "a=m" ce)) | |
711 | (c (if (string-match ".*\\S-\\S-" cs) | |
3674a4a9 SM |
712 | (aref cs (1- (match-end 0))) |
713 | (if (and (equal comment-end "") (string-match ".*\\S-" cs)) | |
714 | (aref cs (1- (match-end 0))) ?=))) | |
715 | (re "\\s-*a=m\\s-*") | |
716 | (_ (string-match re s)) | |
717 | (lcs (length cs)) | |
9b0d1d6e SM |
718 | (fill |
719 | (make-string (+ width (- (match-end 0) | |
3674a4a9 | 720 | (match-beginning 0) lcs 3)) c))) |
aac88001 | 721 | (setq cs (replace-match fill t t s)) |
3674a4a9 SM |
722 | (when (and (not (string-match comment-start-skip cs)) |
723 | (string-match "a=m" s)) | |
724 | ;; The whitespace around CS cannot be ignored: put it back. | |
725 | (setq re "a=m") | |
726 | (setq fill (make-string (- width lcs) c)) | |
727 | (setq cs (replace-match fill t t s))) | |
728 | (string-match re e) | |
9b0d1d6e SM |
729 | (setq ce (replace-match fill t t e)))) |
730 | (cons (concat cs "\n" (make-string min-indent ? ) ccs) | |
731 | (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce)))) | |
aac88001 SM |
732 | |
733 | (def-edebug-spec comment-with-narrowing t) | |
734 | (put 'comment-with-narrowing 'lisp-indent-function 2) | |
735 | (defmacro comment-with-narrowing (beg end &rest body) | |
736 | "Execute BODY with BEG..END narrowing. | |
737 | Space is added (and then removed) at the beginning for the text's | |
738 | indentation to be kept as it was before narrowing." | |
59a1ce8d SM |
739 | (let ((bindent (make-symbol "bindent"))) |
740 | `(let ((,bindent (save-excursion (goto-char beg) (current-column)))) | |
741 | (save-restriction | |
742 | (narrow-to-region beg end) | |
743 | (goto-char (point-min)) | |
744 | (insert (make-string ,bindent ? )) | |
745 | (prog1 | |
746 | (progn ,@body) | |
747 | ;; remove the bindent | |
748 | (save-excursion | |
749 | (goto-char (point-min)) | |
750 | (when (looking-at " *") | |
751 | (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent))) | |
aac88001 | 752 | (delete-char n) |
59a1ce8d SM |
753 | (setq ,bindent (- ,bindent n)))) |
754 | (end-of-line) | |
755 | (let ((e (point))) | |
756 | (beginning-of-line) | |
757 | (while (and (> ,bindent 0) (re-search-forward " *" e t)) | |
758 | (let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1)))) | |
759 | (goto-char (match-beginning 0)) | |
760 | (delete-char n) | |
761 | (setq ,bindent (- ,bindent n))))))))))) | |
aac88001 | 762 | |
771c9b97 SM |
763 | (defun comment-region-internal (beg end cs ce |
764 | &optional ccs cce block lines indent) | |
3674a4a9 | 765 | "Comment region BEG .. END. |
4125ec7e SM |
766 | CS and CE are the comment start resp end string. |
767 | CCS and CCE are the comment continuation strings for the start resp end | |
f5215400 SM |
768 | of lines (default to CS and CE). |
769 | BLOCK indicates that end of lines should be marked with either CCE, CE or CS | |
770 | \(if CE is empty) and that those markers should be aligned. | |
771 | LINES indicates that an extra lines will be used at the beginning and end | |
772 | of the region for CE and CS. | |
773 | INDENT indicates to put CS and CCS at the current indentation of the region | |
774 | rather than at left margin." | |
59a1ce8d | 775 | ;;(assert (< beg end)) |
83b96b22 | 776 | (let ((no-empty t)) |
f5215400 | 777 | ;; Sanitize CE and CCE. |
83b96b22 SM |
778 | (if (and (stringp ce) (string= "" ce)) (setq ce nil)) |
779 | (if (and (stringp cce) (string= "" cce)) (setq cce nil)) | |
f5215400 SM |
780 | ;; If CE is empty, multiline cannot be used. |
781 | (unless ce (setq ccs nil cce nil)) | |
782 | ;; Should we mark empty lines as well ? | |
83b96b22 | 783 | (if (or ccs block lines) (setq no-empty nil)) |
f5215400 | 784 | ;; Make sure we have end-markers for BLOCK mode. |
2ab98065 | 785 | (when block (unless ce (setq ce (comment-string-reverse cs)))) |
f5215400 SM |
786 | ;; If BLOCK is not requested, we don't need CCE. |
787 | (unless block (setq cce nil)) | |
788 | ;; Continuation defaults to the same as CS and CE. | |
789 | (unless ccs (setq ccs cs cce ce)) | |
7a0a180a | 790 | |
83b96b22 | 791 | (save-excursion |
aac88001 | 792 | (goto-char end) |
f5215400 SM |
793 | ;; If the end is not at the end of a line and the comment-end |
794 | ;; is implicit (i.e. a newline), explicitly insert a newline. | |
aac88001 SM |
795 | (unless (or ce (eolp)) (insert "\n") (indent-according-to-mode)) |
796 | (comment-with-narrowing beg end | |
f5215400 | 797 | (let ((min-indent (point-max)) |
9d5240d2 | 798 | (max-indent 0)) |
83b96b22 | 799 | (goto-char (point-min)) |
f5215400 SM |
800 | ;; Quote any nested comment marker |
801 | (comment-quote-nested comment-start comment-end nil) | |
802 | ||
803 | ;; Loop over all lines to find the needed indentations. | |
4125ec7e | 804 | (goto-char (point-min)) |
f5215400 SM |
805 | (while |
806 | (progn | |
807 | (unless (looking-at "[ \t]*$") | |
808 | (setq min-indent (min min-indent (current-indentation)))) | |
809 | (end-of-line) | |
810 | (setq max-indent (max max-indent (current-column))) | |
811 | (not (or (eobp) (progn (forward-line) nil))))) | |
812 | ||
813 | ;; Inserting ccs can change max-indent by (1- tab-width). | |
59a1ce8d SM |
814 | (setq max-indent |
815 | (+ max-indent (max (length cs) (length ccs)) tab-width -1)) | |
771c9b97 | 816 | (unless indent (setq min-indent 0)) |
83b96b22 | 817 | |
aac88001 | 818 | ;; make the leading and trailing lines if requested |
83b96b22 | 819 | (when lines |
771c9b97 SM |
820 | (let ((csce |
821 | (comment-make-extra-lines | |
822 | cs ce ccs cce min-indent max-indent block))) | |
823 | (setq cs (car csce)) | |
824 | (setq ce (cdr csce)))) | |
83b96b22 SM |
825 | |
826 | (goto-char (point-min)) | |
827 | ;; Loop over all lines from BEG to END. | |
f5215400 SM |
828 | (while |
829 | (progn | |
830 | (unless (and no-empty (looking-at "[ \t]*$")) | |
831 | (move-to-column min-indent t) | |
832 | (insert cs) (setq cs ccs) ;switch to CCS after the first line | |
833 | (end-of-line) | |
834 | (if (eobp) (setq cce ce)) | |
835 | (when cce | |
836 | (when block (move-to-column max-indent t)) | |
837 | (insert cce))) | |
838 | (end-of-line) | |
839 | (not (or (eobp) (progn (forward-line) nil)))))))))) | |
83b96b22 | 840 | |
be83ecb2 | 841 | ;;;###autoload |
83b96b22 SM |
842 | (defun comment-region (beg end &optional arg) |
843 | "Comment or uncomment each line in the region. | |
3674a4a9 | 844 | With just \\[universal-argument] prefix arg, uncomment each line in region BEG .. END. |
83b96b22 SM |
845 | Numeric prefix arg ARG means use ARG comment characters. |
846 | If ARG is negative, delete that many comment characters instead. | |
be83ecb2 SM |
847 | By default, comments start at the left margin, are terminated on each line, |
848 | even for syntax in which newline does not end the comment and blank lines | |
849 | do not get comments. This can be changed with `comment-style'. | |
83b96b22 SM |
850 | |
851 | The strings used as comment starts are built from | |
852 | `comment-start' without trailing spaces and `comment-padding'." | |
853 | (interactive "*r\nP") | |
854 | (comment-normalize-vars) | |
855 | (if (> beg end) (let (mid) (setq mid beg beg end end mid))) | |
2ab98065 | 856 | (let* ((numarg (prefix-numeric-value arg)) |
771c9b97 | 857 | (add comment-add) |
2ab98065 SM |
858 | (style (cdr (assoc comment-style comment-styles))) |
859 | (lines (nth 2 style)) | |
860 | (block (nth 1 style)) | |
861 | (multi (nth 0 style))) | |
83b96b22 SM |
862 | (save-excursion |
863 | ;; we use `chars' instead of `syntax' because `\n' might be | |
864 | ;; of end-comment syntax rather than of whitespace syntax. | |
865 | ;; sanitize BEG and END | |
866 | (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line) | |
867 | (setq beg (max beg (point))) | |
868 | (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line) | |
869 | (setq end (min end (point))) | |
870 | (if (>= beg end) (error "Nothing to comment")) | |
871 | ||
83b96b22 SM |
872 | ;; sanitize LINES |
873 | (setq lines | |
874 | (and | |
9b0d1d6e | 875 | lines ;; multi |
83b96b22 SM |
876 | (progn (goto-char beg) (beginning-of-line) |
877 | (skip-syntax-forward " ") | |
878 | (>= (point) beg)) | |
879 | (progn (goto-char end) (end-of-line) (skip-syntax-backward " ") | |
880 | (<= (point) end)) | |
90a44957 SM |
881 | (or block (not (string= "" comment-end))) |
882 | (or block (progn (goto-char beg) (search-forward "\n" end t)))))) | |
83b96b22 | 883 | |
2ab98065 SM |
884 | ;; don't add end-markers just because the user asked for `block' |
885 | (unless (or lines (string= "" comment-end)) (setq block nil)) | |
886 | ||
83b96b22 SM |
887 | (cond |
888 | ((consp arg) (uncomment-region beg end)) | |
889 | ((< numarg 0) (uncomment-region beg end (- numarg))) | |
890 | (t | |
59a1ce8d SM |
891 | (setq numarg (if (and (null arg) (= (length comment-start) 1)) |
892 | add (1- numarg))) | |
83b96b22 SM |
893 | (comment-region-internal |
894 | beg end | |
3e569d22 SM |
895 | (let ((s (comment-padright comment-start numarg))) |
896 | (if (string-match comment-start-skip s) s | |
897 | (comment-padright comment-start))) | |
898 | (let ((s (comment-padleft comment-end numarg))) | |
899 | (and s (if (string-match comment-end-skip s) s | |
900 | (comment-padright comment-end)))) | |
f5215400 SM |
901 | (if multi (comment-padright comment-continue numarg)) |
902 | (if multi (comment-padleft (comment-string-reverse comment-continue) numarg)) | |
83b96b22 | 903 | block |
771c9b97 SM |
904 | lines |
905 | (nth 3 style)))))) | |
906 | ||
907 | (defun comment-box (beg end &optional arg) | |
3674a4a9 | 908 | "Comment out the BEG .. END region, putting it inside a box. |
771c9b97 SM |
909 | The numeric prefix ARG specifies how many characters to add to begin- and |
910 | end- comment markers additionally to what `comment-add' already specifies." | |
911 | (interactive "*r\np") | |
9b0d1d6e SM |
912 | (let ((comment-style (if (cadr (assoc comment-style comment-styles)) |
913 | 'box-multi 'box))) | |
771c9b97 | 914 | (comment-region beg end (+ comment-add arg)))) |
83b96b22 | 915 | |
88fe06af SM |
916 | |
917 | ;;;###autoload | |
918 | (defun comment-or-uncomment-region (beg end &optional arg) | |
919 | "Call `comment-region', unless the region only consists of comments, | |
920 | in which case call `uncomment-region'. If a prefix arg is given, it | |
921 | is passed on to the respective function." | |
922 | (interactive "*r\nP") | |
923 | (funcall (if (save-excursion ;; check for already commented region | |
924 | (goto-char beg) | |
925 | (comment-forward (point-max)) | |
926 | (<= end (point))) | |
927 | 'uncomment-region 'comment-region) | |
928 | beg end arg)) | |
929 | ||
be83ecb2 | 930 | ;;;###autoload |
2ab98065 | 931 | (defun comment-dwim (arg) |
ad679e45 SM |
932 | "Call the comment command you want (Do What I Mean). |
933 | If the region is active and `transient-mark-mode' is on, call | |
39cb51e7 | 934 | `comment-region' (unless it only consists of comments, in which |
ad679e45 | 935 | case it calls `uncomment-region'). |
2ab98065 | 936 | Else, if the current line is empty, insert a comment and indent it. |
ad679e45 SM |
937 | Else if a prefix ARG is specified, call `comment-kill'. |
938 | Else, call `comment-indent'." | |
2ab98065 SM |
939 | (interactive "*P") |
940 | (comment-normalize-vars) | |
771c9b97 | 941 | (if (and mark-active transient-mark-mode) |
88fe06af | 942 | (comment-or-uncomment-region (region-beginning) (region-end) arg) |
2ab98065 | 943 | (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$"))) |
ad679e45 SM |
944 | ;; FIXME: If there's no comment to kill on this line and ARG is |
945 | ;; specified, calling comment-kill is not very clever. | |
946 | (if arg (comment-kill (and (integerp arg) arg)) (comment-indent)) | |
2ab98065 | 947 | (let ((add (if arg (prefix-numeric-value arg) |
771c9b97 | 948 | (if (= (length comment-start) 1) comment-add 0)))) |
2e1fbba4 SM |
949 | ;; Some modes insist on keeping column 0 comment in column 0 |
950 | ;; so we need to move away from it before inserting the comment. | |
951 | (indent-according-to-mode) | |
2ab98065 SM |
952 | (insert (comment-padright comment-start add)) |
953 | (save-excursion | |
954 | (unless (string= "" comment-end) | |
955 | (insert (comment-padleft comment-end add))) | |
956 | (indent-according-to-mode)))))) | |
957 | ||
392f1ef5 SM |
958 | (defcustom comment-auto-fill-only-comments nil |
959 | "Non-nil means to only auto-fill inside comments. | |
960 | This has no effect in modes that do not define a comment syntax." | |
0603917d | 961 | :type 'boolean) |
392f1ef5 | 962 | |
7164ef13 SM |
963 | (defun comment-valid-prefix (prefix compos) |
964 | (or | |
965 | ;; Accept any prefix if the current comment is not EOL-terminated. | |
966 | (save-excursion (goto-char compos) (comment-forward) (not (bolp))) | |
967 | ;; Accept any prefix that starts with a comment-start marker. | |
968 | (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)") | |
969 | fill-prefix))) | |
970 | ||
be83ecb2 | 971 | ;;;###autoload |
ad679e45 | 972 | (defun comment-indent-new-line (&optional soft) |
2ab98065 SM |
973 | "Break line at point and indent, continuing comment if within one. |
974 | This indents the body of the continued comment | |
975 | under the previous comment line. | |
976 | ||
977 | This command is intended for styles where you write a comment per line, | |
978 | starting a new comment (and terminating it if necessary) on each line. | |
979 | If you want to continue one comment across several lines, use \\[newline-and-indent]. | |
980 | ||
981 | If a fill column is specified, it overrides the use of the comment column | |
982 | or comment indentation. | |
983 | ||
984 | The inserted newline is marked hard if variable `use-hard-newlines' is true, | |
985 | unless optional argument SOFT is non-nil." | |
986 | (interactive) | |
987 | (comment-normalize-vars t) | |
59a1ce8d SM |
988 | (let (compos comin) |
989 | ;; If we are not inside a comment and we only auto-fill comments, | |
990 | ;; don't do anything (unless no comment syntax is defined). | |
392f1ef5 SM |
991 | (unless (and comment-start |
992 | comment-auto-fill-only-comments | |
0603917d | 993 | (not (interactive-p)) |
392f1ef5 | 994 | (not (save-excursion |
59a1ce8d | 995 | (prog1 (setq compos (comment-beginning)) |
392f1ef5 | 996 | (setq comin (point)))))) |
59a1ce8d SM |
997 | |
998 | ;; Now we know we should auto-fill. | |
88fe06af SM |
999 | ;; Insert the newline before removing empty space so that markers |
1000 | ;; get preserved better. | |
392f1ef5 | 1001 | (if soft (insert-and-inherit ?\n) (newline 1)) |
88fe06af SM |
1002 | (save-excursion (forward-char -1) (delete-horizontal-space)) |
1003 | (delete-horizontal-space) | |
1004 | ||
7164ef13 SM |
1005 | (if (and fill-prefix (not adaptive-fill-mode)) |
1006 | ;; Blindly trust a non-adaptive fill-prefix. | |
392f1ef5 SM |
1007 | (progn |
1008 | (indent-to-left-margin) | |
88fe06af | 1009 | (insert-before-markers-and-inherit fill-prefix)) |
59a1ce8d SM |
1010 | |
1011 | ;; If necessary check whether we're inside a comment. | |
a764440a | 1012 | (unless (or compos (null comment-start)) |
392f1ef5 SM |
1013 | (save-excursion |
1014 | (backward-char) | |
59a1ce8d SM |
1015 | (setq compos (comment-beginning)) |
1016 | (setq comin (point)))) | |
1017 | ||
7164ef13 SM |
1018 | (cond |
1019 | ;; If there's an adaptive prefix, use it unless we're inside | |
1020 | ;; a comment and the prefix is not a comment starter. | |
1021 | ((and fill-prefix | |
1022 | (or (not compos) | |
1023 | (comment-valid-prefix fill-prefix compos))) | |
1024 | (indent-to-left-margin) | |
1025 | (insert-and-inherit fill-prefix)) | |
1026 | ;; If we're not inside a comment, just try to indent. | |
1027 | ((not compos) (indent-according-to-mode)) | |
1028 | (t | |
59a1ce8d SM |
1029 | (let* ((comment-column |
1030 | ;; The continuation indentation should be somewhere between | |
1031 | ;; the current line's indentation (plus 2 for good measure) | |
1032 | ;; and the current comment's indentation, with a preference | |
1033 | ;; for comment-column. | |
1034 | (save-excursion | |
a764440a | 1035 | ;; FIXME: use prev line's info rather than first line's. |
59a1ce8d SM |
1036 | (goto-char compos) |
1037 | (min (current-column) (max comment-column | |
1038 | (+ 2 (current-indentation)))))) | |
1039 | (comstart (buffer-substring compos comin)) | |
1040 | (normalp | |
1041 | (string-match (regexp-quote (comment-string-strip | |
1042 | comment-start t t)) | |
1043 | comstart)) | |
1044 | (comment-end | |
1045 | (if normalp comment-end | |
1046 | ;; The comment starter is not the normal comment-start | |
1047 | ;; so we can't just use comment-end. | |
1048 | (save-excursion | |
1049 | (goto-char compos) | |
1050 | (if (not (comment-forward)) comment-end | |
1051 | (comment-string-strip | |
1052 | (buffer-substring | |
1053 | (save-excursion (comment-enter-backward) (point)) | |
1054 | (point)) | |
1055 | nil t))))) | |
1056 | (comment-start comstart) | |
a764440a SM |
1057 | (continuep (or comment-multi-line |
1058 | (cadr (assoc comment-style comment-styles)))) | |
59a1ce8d | 1059 | ;; Force comment-continue to be recreated from comment-start. |
dde6824c | 1060 | ;; FIXME: wrong if comment-continue was set explicitly! |
a764440a | 1061 | ;; FIXME: use prev line's continuation if available. |
59a1ce8d | 1062 | (comment-continue nil)) |
a764440a SM |
1063 | (if (and comment-multi-line (> (length comment-end) 0)) |
1064 | (indent-according-to-mode) | |
1065 | (insert-and-inherit ?\n) | |
1066 | (forward-char -1) | |
1067 | (comment-indent continuep) | |
1068 | (save-excursion | |
1069 | (let ((pt (point))) | |
1070 | (end-of-line) | |
1071 | (let ((comend (buffer-substring pt (point)))) | |
1072 | ;; The 1+ is to make sure we delete the \n inserted above. | |
1073 | (delete-region pt (1+ (point))) | |
1074 | (end-of-line 0) | |
1075 | (insert comend)))))))))))) | |
2ab98065 | 1076 | |
83b96b22 SM |
1077 | (provide 'newcomment) |
1078 | ||
83b96b22 | 1079 | ;;; newcomment.el ends here |