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