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