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