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