1 ;;; newcomment.el --- (un)comment regions of buffers
3 ;; Copyright (C) 1999 Stefan Monnier <monnier@cs.yale.edu>
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: comment uncomment
8 ;; Revision: $Id: newcomment.el,v 1.1 1999/11/28 18:51:06 monnier Exp $
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2 of the License, or
13 ;; (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, write to the Free Software
22 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;; - most of the code is not written (just copied from simple.el)
31 ;; - too many other bugs to mention
32 ;; - if the region does not start at the beginning of a line,
33 ;; comment-region will not align the comment markers properly
34 ;; - comment-multi-line already exists with a different meaning
35 ;; and is not orthogonal to comment-extra-lines
39 ;; - extract comment data from the syntax-table
40 ;; - maybe do the opposite as well (set the syntax-table from other data)
41 ;; - customizable auto-fill of comments
45 (eval-when-compile (require 'cl
))
47 (defcustom comment-column
32
48 "*Column to indent right-margin comments to.
49 Setting this variable automatically makes it local to the current buffer.
50 Each mode establishes a different default value for this variable; you
51 can set the value for a particular mode using that mode's hook."
53 :group
'fill-comments
)
54 (make-variable-buffer-local 'comment-column
)
56 (defcustom comment-start nil
57 "*String to insert to start a new comment, or nil if no comment syntax."
58 :type
'(choice (const :tag
"None" nil
)
60 :group
'fill-comments
)
62 (defcustom comment-start-skip nil
63 "*Regexp to match the start of a comment plus everything up to its body.
64 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
65 at the place matched by the close of the first pair."
66 :type
'(choice (const :tag
"None" nil
)
68 :group
'fill-comments
)
70 (defcustom comment-end
""
71 "*String to insert to end a new comment.
72 Should be an empty string if comments are terminated by end-of-line."
74 :group
'fill-comments
)
76 (defvar comment-indent-hook nil
77 "Obsolete variable for function to compute desired indentation for a comment.
78 This function is called with no args with point at the beginning of
79 the comment's starting delimiter.")
81 (defvar comment-indent-function
82 '(lambda () comment-column
)
83 "Function to compute desired indentation for a comment.
84 This function is called with no args with point at the beginning of
85 the comment's starting delimiter.")
87 (defcustom block-comment-start nil
88 "*String to insert to start a new comment on a line by itself.
89 If nil, use `comment-start' instead.
90 Note that the regular expression `comment-start-skip' should skip this string
91 as well as the `comment-start' string."
92 :type
'(choice (const :tag
"Use comment-start" nil
)
94 :group
'fill-comments
)
96 (defcustom block-comment-end nil
97 "*String to insert to end a new comment on a line by itself.
98 Should be an empty string if comments are terminated by end-of-line.
99 If nil, use `comment-end' instead."
100 :type
'(choice (const :tag
"Use comment-end" nil
)
102 :group
'fill-comments
)
104 (defun indent-for-comment ()
105 "Indent this line's comment to comment column, or insert an empty comment."
107 (let* ((empty (save-excursion (beginning-of-line)
108 (looking-at "[ \t]*$")))
109 (starter (or (and empty block-comment-start
) comment-start
))
110 (ender (or (and empty block-comment-end
) comment-end
)))
113 (error "No comment syntax defined"))
114 ((null comment-start-skip
)
115 (error "This mode doesn't define `comment-start-skip'"))
116 (t (let* ((eolpos (save-excursion (end-of-line) (point)))
119 (if (re-search-forward comment-start-skip eolpos
'move
)
120 (progn (setq cpos
(point-marker))
121 ;; Find the start of the comment delimiter.
122 ;; If there were paren-pairs in comment-start-skip,
123 ;; position at the end of the first pair.
125 (goto-char (match-end 1))
126 ;; If comment-start-skip matched a string with
127 ;; internal whitespace (not final whitespace) then
128 ;; the delimiter start at the end of that
129 ;; whitespace. Otherwise, it starts at the
130 ;; beginning of what was matched.
131 (skip-syntax-backward " " (match-beginning 0))
132 (skip-syntax-backward "^ " (match-beginning 0)))))
133 (setq begpos
(point))
134 ;; Compute desired indent.
135 (if (= (current-column)
136 (setq indent
(if comment-indent-hook
137 (funcall comment-indent-hook
)
138 (funcall comment-indent-function
))))
140 ;; If that's different from current, change it.
141 (skip-chars-backward " \t")
142 (delete-region (point) begpos
)
144 ;; An existing comment?
146 (progn (goto-char cpos
)
147 (set-marker cpos nil
))
151 (insert ender
))))))))
153 (defun set-comment-column (arg)
154 "Set the comment column based on point.
155 With no arg, set the comment column to the current column.
156 With just minus as arg, kill any comment on this line.
157 With any other arg, set comment column to indentation of the previous comment
158 and then align or create a comment on this line at that column."
166 (re-search-backward comment-start-skip
)
168 (re-search-forward comment-start-skip
)
169 (goto-char (match-beginning 0))
170 (setq comment-column
(current-column))
171 (message "Comment column set to %d" comment-column
))
172 (indent-for-comment))
173 (setq comment-column
(current-column))
174 (message "Comment column set to %d" comment-column
))))
176 (defun kill-comment (arg)
177 "Kill the comment on this line, if any.
178 With argument, kill comments on that many lines starting with this one."
179 ;; this function loses in a lot of situations. it incorrectly recognises
180 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
181 ;; with multi-line comments, can kill extra whitespace if comment wasn't
182 ;; through end-of-line, et cetera.
184 (or comment-start-skip
(error "No comment syntax defined"))
185 (let ((count (prefix-numeric-value arg
)) endc
)
191 (and (string< "" comment-end
)
194 (re-search-forward (regexp-quote comment-end
) endc
'move
)
195 (skip-chars-forward " \t")
198 (if (re-search-forward comment-start-skip endc t
)
200 (goto-char (match-beginning 0))
201 (skip-chars-backward " \t")
202 (kill-region (point) endc
)
203 ;; to catch comments a line beginnings
204 (indent-according-to-mode))))
205 (if arg
(forward-line 1))
206 (setq count
(1- count
)))))
208 (defvar comment-padding
1
209 "Number of spaces `comment-region' puts between comment chars and text.
210 Can also be a string instead.
212 Extra spacing between the comment characters and the comment text
213 makes the comment easier to read. Default is 1. Nil means 0.")
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 (defcustom comment-nested nil
218 "Whether the comments can be nested.")
219 (defcustom comment-continue nil
220 "Pair of strings to insert for multiline comments.")
221 (defcustom comment-add
'(0 .
2)
222 "How many more chars should be inserted by default.")
223 (defcustom comment-extra-lines nil
224 "When comments should have an extra line before and after.
225 If nil, never add them.
226 If t, always add them,
227 If 'multiline, only add them for truly multiline comments.")
228 ;; (defcustom comment-multiline t
229 ;; "non-nil if `comment-region' should use multi-line comments.")
231 (defun comment-normalize-vars ()
232 (or comment-start
(error "No comment syntax is defined"))
233 (when (integerp comment-padding
)
234 (setq comment-padding
(make-string comment-padding ?
)))
236 (when (string-match "\\`\\s-*\\(.*\\S-\\)\\s-*\\'" comment-start
)
237 (setq comment-start
(match-string 1 comment-start
)))
238 (when (string-match "\\`\\s-*\\(.*\\S-\\)\\s-*\\'" comment-end
)
239 (setq comment-end
(match-string 1 comment-end
)))
241 (let ((csl (length comment-start
)))
242 (if (not (or comment-continue
(string= comment-end
"")))
243 (set (make-local-variable 'comment-continue
)
244 (cons (concat " " (substring comment-start
1))
247 (defmacro until
(&rest body
)
248 (let ((retsym (make-symbol "ret")))
250 (while (not (setq ,retsym
(progn ,@body
))))
252 (def-edebug-spec until t
)
254 (defun string-reverse (s) (concat (reverse (string-to-list s
))))
256 (defun uncomment-region (beg end
&optional arg
)
257 "Comment or uncomment each line in the region.
258 With just C-u prefix arg, uncomment each line in region.
259 Numeric prefix arg ARG means use ARG comment characters.
260 If ARG is negative, delete that many comment characters instead.
261 Comments are terminated on each line, even for syntax in which newline does
262 not end the comment. Blank lines do not get comments.
264 The strings used as comment starts are build from
265 `comment-start' without trailing spaces and `comment-padding'."
266 (interactive "*r\nP")
267 (comment-normalize-vars)
268 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
271 (let* ((cs comment-start
) (ce comment-end
)
273 (if (consp arg
) (setq numarg t
)
274 (setq numarg
(prefix-numeric-value arg
))
275 ;; For positive arg > 1, replicate the comment delims now,
276 ;; then insert the replicated strings just once.
278 (setq cs
(concat cs comment-start
)
279 ce
(concat ce comment-end
))
280 (setq numarg
(1- numarg
))))
281 ;; Loop over all lines from BEG to END.
282 (narrow-to-region beg end
)
285 ((consp arg
) (comment-region beg end
))
286 ((< numarg
0) (comment-region beg end
(- numarg
)))
290 ;; Delete comment start from beginning of line.
292 (while (looking-at (regexp-quote cs
))
293 (setq found-comment t
)
294 (delete-char (length cs
)))
295 (let ((count numarg
))
296 (while (and (> 1 (setq count
(1+ count
)))
297 (looking-at (regexp-quote cs
)))
298 (setq found-comment t
)
299 (delete-char (length cs
)))))
300 ;; Delete comment padding from beginning of line
301 (when (and found-comment comment-padding
302 (looking-at (regexp-quote comment-padding
)))
303 (delete-char (length comment-padding
)))
304 ;; Delete comment end from end of line.
310 ;; This is questionable if comment-end ends in
311 ;; whitespace. That is pretty brain-damaged,
313 (while (progn (skip-chars-backward " \t")
314 (and (>= (- (point) (point-min)) (length ce
))
316 (backward-char (length ce
))
317 (looking-at (regexp-quote ce
)))))
318 (delete-char (- (length ce
)))))
319 (let ((count numarg
))
320 (while (> 1 (setq count
(1+ count
)))
322 ;; this is questionable if comment-end ends in whitespace
323 ;; that is pretty brain-damaged though
324 (skip-chars-backward " \t")
325 (if (>= (- (point) (point-min)) (length ce
))
327 (backward-char (length ce
))
328 (if (looking-at (regexp-quote ce
))
329 (delete-char (length ce
)))))))))
330 (forward-line 1)))))))))
332 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent
&optional block
)
334 (let* ((s (concat cs
"a=m" cce
"\n"
335 (make-string min-indent ?
) ccs
))
336 (e (concat cce
"\n" (make-string min-indent ?
)
338 (_ (assert (string-match "\\s-*\\(a=m\\)\\s-*" s
)))
339 (fill (make-string (+ (- max-indent
344 (aref s
(match-end 0)))))
345 (setq cs
(replace-match fill t t s
))
346 (assert (string-match "\\s-*\\(a=m\\)\\s-*" e
))
347 (setq ce
(replace-match fill t t e
)))
348 (when (and ce
(string-match "\\`\\s-*\\(.*\\S-\\)\\s-*\\'" ce
))
349 (setq ce
(match-string 1 ce
)))
350 (let* ((c (concat ce
"a=m" cs
))
351 (indent (if (string-match "\\(.+\\).*a=m\\(.*\\)\\1" c
)
353 (- (match-end 2) (match-beginning 2))
354 (- (match-beginning 0)))
357 (setq ce
(concat cce
"\n" (make-string indent ?
) (or ce cs
)))
358 (setq cs
(concat cs
"\n" (make-string min-indent ?
) ccs
))))
361 (def-edebug-spec comment-with-narrowing t
)
362 (put 'comment-with-narrowing
'lisp-indent-function
2)
363 (defmacro comment-with-narrowing
(beg end
&rest body
)
364 "Execute BODY with BEG..END narrowing.
365 Space is added (and then removed) at the beginning for the text's
366 indentation to be kept as it was before narrowing."
367 `(let ((-bindent (save-excursion (goto-char beg
) (current-column))))
369 (narrow-to-region beg end
)
370 (goto-char (point-min))
371 (insert (make-string -bindent ?
))
374 ;; remove the -bindent
376 (goto-char (point-min))
377 (when (looking-at " *")
378 (let ((n (min (- (match-end 0) (match-beginning 0)) -bindent
)))
384 (while (and (> -bindent
0) (re-search-forward " +" e t
))
385 (let ((n (min -bindent
(- (match-end 0) (match-beginning 0) 1))))
386 (goto-char (match-beginning 0))
388 (decf -bindent n
)))))))))
390 (defun comment-region-internal (beg end cs ce
&optional ccs cce block lines
)
393 ;; sanitize ce and cce
394 (if (and (stringp ce
) (string= "" ce
)) (setq ce nil
))
395 (if (and (stringp cce
) (string= "" cce
)) (setq cce nil
))
396 ;; should we mark empty lines as well ?
397 (if (or ccs block lines
) (setq no-empty nil
))
398 ;; continuation defaults to the same
399 (if ccs
(unless block
(setq cce nil
))
400 (setq ccs cs cce ce
))
401 ;; make sure we have end-markers for BLOCK mode
403 (if (null ce
) (setq ce
(string-reverse cs
)))
404 (if (null cce
) (setq cce
(string-reverse ccs
))))
408 (unless (or ce
(eolp)) (insert "\n") (indent-according-to-mode))
409 (comment-with-narrowing beg end
411 (when (and (not comment-nested
) (> (length comment-end
) 1))
412 (concat (regexp-quote (substring comment-end
0 1))
414 (regexp-quote (substring comment-end
1)))))
415 (min-indent (point-max))
417 (goto-char (point-min))
418 ;; loop over all lines to find the needed indentations
420 (unless (looking-at "[ \t]*$")
421 (setq min-indent
(min min-indent
(current-indentation))))
423 (let ((eol (save-excursion (end-of-line) (point))))
424 (while (re-search-forward ce-quote-re eol
'move
)
426 (replace-match "\\" t t nil
1))))
428 (setq max-indent
(max max-indent
(current-column)))
429 (or (eobp) (progn (forward-line) nil
)))
431 ;; inserting ccs can change max-indent by (1- tab-width)
432 (incf max-indent
(+ (max (length cs
) (length ccs
)) -
1 tab-width
))
434 ;; make the leading and trailing lines if requested
436 (multiple-value-setq (cs ce
)
437 (comment-make-extra-lines
438 cs ce ccs cce min-indent max-indent block
)))
440 (goto-char (point-min))
441 ;; Loop over all lines from BEG to END.
443 (unless (and no-empty
(looking-at "[ \t]*$"))
444 (move-to-column min-indent t
)
445 (insert cs
) (setq cs ccs
)
447 (if (eobp) (setq cce ce
))
449 (when block
(move-to-column max-indent t
))
452 (or (eobp) (progn (forward-line) nil
))))))))
454 (defun comment-addright (str n
)
455 (when (and (stringp str
) (not (string= "" str
)))
456 (concat str
(make-string n
(aref str
(1- (length str
)))) comment-padding
)))
457 (defun comment-addleft (str n
)
458 (when (and (stringp str
) (not (string= "" str
)))
459 (concat comment-padding
460 (when (or comment-nested
(> (length comment-end
) 1))
461 (make-string n
(aref str
0)))
464 (defun comment-region (beg end
&optional arg
)
465 "Comment or uncomment each line in the region.
466 With just \\[universal-prefix] prefix arg, uncomment each line in region BEG..END.
467 Numeric prefix arg ARG means use ARG comment characters.
468 If ARG is negative, delete that many comment characters instead.
469 Comments are terminated on each line, even for syntax in which newline does
470 not end the comment. Blank lines do not get comments.
472 The strings used as comment starts are built from
473 `comment-start' without trailing spaces and `comment-padding'."
474 (interactive "*r\nP")
475 (comment-normalize-vars)
476 (if (> beg end
) (let (mid) (setq mid beg beg end end mid
)))
477 (let ((numarg (prefix-numeric-value arg
))
478 (add (car comment-add
))
479 (lines comment-extra-lines
)
482 ;; we use `chars' instead of `syntax' because `\n' might be
483 ;; of end-comment syntax rather than of whitespace syntax.
484 ;; sanitize BEG and END
485 (goto-char beg
) (skip-chars-forward " \t\n\r") (beginning-of-line)
486 (setq beg
(max beg
(point)))
487 (goto-char end
) (skip-chars-backward " \t\n\r") (end-of-line)
488 (setq end
(min end
(point)))
489 (if (>= beg end
) (error "Nothing to comment"))
491 ;; check for already commented region
493 (forward-comment (point-max))
494 (if (< end
(point)) (setq arg
'(4) numarg
4))
500 (progn (goto-char beg
) (beginning-of-line)
501 (skip-syntax-forward " ")
503 (progn (goto-char end
) (end-of-line) (skip-syntax-backward " ")
505 (if (eq comment-extra-lines
'multiline
)
506 (and (not (string= "" comment-end
))
507 (progn (goto-char beg
)
508 (search-forward "\n" end t
)))
511 (when (and (consp arg
) (>= numarg
16))
512 (setq lines
(>= numarg
64))
513 (setq arg nil numarg
1 block t add
(or (cdr comment-add
) 2)))
515 ((consp arg
) (uncomment-region beg end
))
516 ((< numarg
0) (uncomment-region beg end
(- numarg
)))
518 (if (and (null arg
) (= (length comment-start
) 1))
519 (setq numarg add
) (decf numarg
))
520 (comment-region-internal
522 (comment-addright comment-start numarg
)
523 (comment-addleft comment-end numarg
)
524 (if comment-multi-line
525 (comment-addright (car comment-continue
) numarg
))
526 (if comment-multi-line
527 (comment-addleft (cdr comment-continue
) numarg
))
531 (provide 'newcomment
)
534 ;; $Log: newcomment.el,v $
535 ;; Revision 1.1 1999/11/28 18:51:06 monnier
536 ;; First "working" version:
537 ;; - uncomment-region doesn't work for some unknown reason
538 ;; - comment-multi-line allows the use of multi line comments
539 ;; - comment-extra-lines allows yet another style choice
540 ;; - comment-add allows to default to `;;'
541 ;; - comment-region on a comment calls uncomment-region
542 ;; - C-u C-u comment-region aligns comment end markers
543 ;; - C-u C-u C-u comment-region puts the comment inside a rectangle
544 ;; - comment-region will try to quote coment-end markers inside the region
545 ;; - comment-start markers are placed at the indentation level
548 ;;; newcomment.el ends here