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