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