;; Maintainer: Stefan Monnier <monnier@cs.yale.edu>
;; Keywords: comment uncomment
;; Version: $Name: $
-;; Revision: $Id: newcomment.el,v 1.10 2000/05/17 19:32:32 monnier Exp $
+;; Revision: $Id: newcomment.el,v 1.16 2000/06/04 22:02:11 monnier Exp $
;; This file is part of GNU Emacs.
;; - comment-box in TeXinfo generate bogus comments @ccccc@
;; - uncomment-region with a numeric argument can render multichar
;; comment markers invalid.
+;; - comment-indent or comment-region when called inside a comment
+;; will happily break the surrounding comment.
+;; - comment-quote-nested will not (un)quote properly all nested comment
+;; markers if there are more than just comment-start and comment-end.
+;; For example, in Pascal where {...*) and (*...} are possible.
;;; Todo:
;;; Code:
+;;;###autoload
(defalias 'indent-for-comment 'comment-indent)
+;;;###autoload
(defalias 'set-comment-column 'comment-set-column)
+;;;###autoload
(defalias 'kill-comment 'comment-kill)
+;;;###autoload
(defalias 'indent-new-comment-line 'comment-indent-new-line)
+;;;###autoload
(defgroup comment nil
"Indenting and filling of comments."
:prefix "comment-"
+ :version "21.1"
:group 'fill)
(defvar comment-use-syntax 'undecided
to understand comments or not in the given buffer.
Major modes should set this variable.")
+;;;###autoload
(defcustom comment-column 32
"*Column to indent right-margin comments to.
Setting this variable automatically makes it local to the current buffer.
:group 'comment)
(make-variable-buffer-local 'comment-column)
+;;;###autoload
(defvar comment-start nil
"*String to insert to start a new comment, or nil if no comment syntax.")
+;;;###autoload
(defvar comment-start-skip nil
"*Regexp to match the start of a comment plus everything up to its body.
If there are any \\(...\\) pairs, the comment delimiter text is held to begin
at the place matched by the close of the first pair.")
+;;;###autoload
(defvar comment-end-skip nil
"Regexp to match the end of a comment plus everything up to its body.")
+;;;###autoload
(defvar comment-end ""
"*String to insert to end a new comment.
Should be an empty string if comments are terminated by end-of-line.")
-(defvar comment-indent-hook nil
- "Obsolete variable for function to compute desired indentation for a comment.
-This function is called with no args with point at the beginning of
-the comment's starting delimiter.")
-
+;;;###autoload
(defvar comment-indent-function
(lambda () comment-column)
"Function to compute desired indentation for a comment.
(aligned . (nil t nil t))
(multi-line . (t nil nil t))
(extra-line . (t nil t t))
- (box . (t t t t)))
+ (box . (nil t t t))
+ (box-multi . (t t t t)))
"Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
STYLE should be a mnemonic symbol.
MULTI specifies that comments are allowed to span multiple lines.
INDENT specifies that the `comment-start' markers should not be put at the
left margin but at the current indentation of the region to comment.")
+;;;###autoload
(defcustom comment-style 'plain
"*Style to be used for `comment-region'.
See `comment-styles' for a list of available styles."
:group 'comment
- :type `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles)))
+ :type (if (boundp 'comment-styles)
+ `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles))
+ 'symbol))
-(defcustom comment-padding 1
+;;;###autoload
+(defcustom comment-padding " "
"Padding string that `comment-region' puts between comment chars and text.
Can also be an integer which will be automatically turned into a string
of the corresponding number of spaces.
Extra spacing between the comment characters and the comment text
makes the comment easier to read. Default is 1. nil means 0.")
+;;;###autoload
(defcustom comment-multi-line nil
- "*Non-nil means \\[indent-new-comment-line] should continue same comment
-on new line, with no new terminator or starter.
+ "*Non-nil means \\[comment-indent-new-line] continues comments, with no new terminator or starter.
This is obsolete because you might as well use \\[newline-and-indent]."
:type 'boolean
:group 'comment)
;; comment-continue
(unless (or comment-continue (string= comment-end ""))
(set (make-local-variable 'comment-continue)
- (concat " " (substring comment-start 1))))
+ (concat (if (string-match "\\S-\\S-" comment-start) " " "|")
+ (substring comment-start 1))))
;; comment-skip regexps
(unless comment-start-skip
(set (make-local-variable 'comment-start-skip)
comment-starter. If no comment is found, moves point to LIMIT
and raises an error or returns nil of NOERROR is non-nil."
(if (not comment-use-syntax)
- (when (re-search-forward comment-start-skip limit noerror)
- (or (match-end 1) (match-beginning 0)))
+ (if (re-search-forward comment-start-skip limit noerror)
+ (or (match-end 1) (match-beginning 0))
+ (goto-char limit)
+ (unless noerror (error "No comment")))
(let* ((pt (point))
;; Assume (at first) that pt is outside of any string.
(s (parse-partial-sexp pt (or limit (point-max)) nil nil nil t)))
(if (not (and (nth 8 s) (not (nth 3 s))))
(unless noerror (error "No comment"))
;; We found the comment.
- (let ((pt (point))
+ (let ((pos (point))
(start (nth 8 s))
- (bol (save-excursion (beginning-of-line) (point)))
+ (bol (line-beginning-position))
(end nil))
(while (and (null end) (>= (point) bol))
(if (looking-at comment-start-skip)
(setq end (min (or limit (point-max)) (match-end 0)))
(backward-char)))
- (goto-char end)
+ (goto-char (or end pos))
start)))))
(defun comment-search-backward (&optional limit noerror)
;;;; Commands
;;;;
+;;;###autoload
(defun comment-indent (&optional continue)
"Indent this line's comment to comment column, or insert an empty comment.
If CONTINUE is non-nil, use the `comment-continuation' markers if any."
(t (let* ((eolpos (line-end-position))
cpos indent begpos)
(beginning-of-line)
- (when (setq begpos (comment-search-forward eolpos t))
+ (if (not (setq begpos (comment-search-forward eolpos t)))
+ (setq begpos (point))
(setq cpos (point-marker))
(goto-char begpos))
- (setq begpos (point))
;; Compute desired indent.
(if (= (current-column)
- (setq indent (if comment-indent-hook
- (funcall comment-indent-hook)
- (funcall comment-indent-function))))
+ (setq indent (funcall comment-indent-function)))
(goto-char begpos)
;; If that's different from current, change it.
(skip-chars-backward " \t")
(indent-to indent))
;; An existing comment?
(if cpos
- (progn (goto-char cpos)
- (set-marker cpos nil))
+ (progn (goto-char cpos) (set-marker cpos nil))
;; No, insert one.
(insert starter)
(save-excursion
(insert ender))))))))
+;;;###autoload
(defun comment-set-column (arg)
"Set the comment column based on point.
With no ARG, set the comment column to the current column.
(t (setq comment-column (current-column))
(message "Comment column set to %d" comment-column))))
+;;;###autoload
(defun comment-kill (arg)
"Kill the comment on this line, if any.
With prefix ARG, kill comments on that many lines starting with this one."
(rpad (concat (substring str (match-end 1)) ;original right padding
(substring comment-padding ;additional right padding
(min (- (match-end 0) (match-end 1))
- (length comment-padding))))))
+ (length comment-padding)))))
+ ;; We can only duplicate C if the comment-end has multiple chars
+ ;; or if comments can be nested, else the comment-end `}' would
+ ;; be turned into `}}}' where only the first ends the comment
+ ;; and the rest becomes bogus junk.
+ (multi (not (and comment-quote-nested
+ ;; comment-end is a single char
+ (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
(if (not (symbolp n))
- (concat lpad s (make-string n (aref str (1- (match-end 1)))) rpad)
+ (concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
;; construct a regexp that would match anything from just S
;; to any possible output of this function for any N.
(concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
lpad "") ;padding is not required
- (regexp-quote s) "+" ;the last char of S might be repeated
+ (regexp-quote s)
+ (when multi "+") ;the last char of S might be repeated
(mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
rpad "")))))) ;padding is not required
(if multi (concat (regexp-quote (string c)) "*"))
(regexp-quote s))))))
+;;;###autoload
(defun uncomment-region (beg end &optional arg)
"Uncomment each line in the BEG..END region.
The numeric prefix ARG can specify a number of chars to remove from the
;; Remove the comment-start.
(goto-char ipt)
(skip-syntax-backward " ")
+ ;; Check for special `=' used sometimes in comment-box.
+ (when (and (= (- (point) (point-min)) 1) (looking-at "=\\{7\\}"))
+ (skip-chars-forward "="))
;; A box-comment starts with a looong comment-start marker.
(when (> (- (point) (point-min) (length comment-start)) 7)
(setq box t))
;; Remove the end-comment (and leading padding and such).
(goto-char (point-max)) (comment-enter-backward)
- (unless (string-match "\\`\\(\n\\|\\s-\\)*\\'"
- (buffer-substring (point) (point-max)))
+ ;; Check for special `=' used sometimes in comment-box.
+ (when (and (= (- (point-max) (point)) 1) (> (point) 7)
+ (save-excursion (backward-char 7)
+ (looking-at "=\\{7\\}")))
+ (skip-chars-backward "="))
+ (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
(when (and (bolp) (not (bobp))) (backward-char))
(if (null arg) (delete-region (point) (point-max))
(skip-syntax-forward " ")
(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
"Make the leading and trailing extra lines.
This is used for `extra-line' style (or `box' style if BLOCK is specified)."
- (if block
- (let* ((s (concat cs "a=m" cce "\n"
- (make-string min-indent ? ) ccs))
- (e (concat cce "\n" (make-string min-indent ? )
- ccs "a=m" ce))
- ;;(_ (assert (string-match "\\s-*\\(a=m\\)\\s-*" s)))
- (fill (make-string (+ (- max-indent
- min-indent
- (match-beginning 0))
- (- (match-end 0)
- (match-end 1)))
- (aref s (match-end 0)))))
+ (let ((eindent 0))
+ (if (not block)
+ ;; Try to match CS and CE's content so they align aesthetically.
+ (progn
+ (setq ce (comment-string-strip ce t t))
+ (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce "\n" cs))
+ (setq eindent
+ (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
+ 0))))
+ ;; box comment
+ (let* ((width (- max-indent min-indent))
+ (s (concat cs "a=m" cce))
+ (e (concat ccs "a=m" ce))
+ (c (if (string-match ".*\\S-\\S-" cs)
+ (aref cs (1- (match-end 0))) ?=))
+ (_ (string-match "\\s-*a=m\\s-*" s))
+ (fill
+ (make-string (+ width (- (match-end 0)
+ (match-beginning 0) (length cs) 3)) c)))
(setq cs (replace-match fill t t s))
- ;;(assert (string-match "\\s-*\\(a=m\\)\\s-*" e))
- (setq ce (replace-match fill t t e)))
- (when (and ce (string-match "\\`\\s-*\\(.*\\S-\\)\\s-*\\'" ce))
- (setq ce (match-string 1 ce)))
- (let* ((c (concat ce "a=m" cs))
- (indent (if (string-match "\\(.+\\).*a=m\\(.*\\)\\1" c)
- (max (+ min-indent
- (- (match-end 2) (match-beginning 2))
- (- (match-beginning 0)))
- 0)
- min-indent)))
- (setq ce (concat cce "\n" (make-string indent ? ) (or ce cs)))
- (setq cs (concat cs "\n" (make-string min-indent ? ) ccs))))
- (cons cs ce))
+ (string-match "\\s-*a=m\\s-*" e)
+ (setq ce (replace-match fill t t e))))
+ (cons (concat cs "\n" (make-string min-indent ? ) ccs)
+ (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce))))
(def-edebug-spec comment-with-narrowing t)
(put 'comment-with-narrowing 'lisp-indent-function 2)
(defun comment-region-internal (beg end cs ce
&optional ccs cce block lines indent)
"Comment region BEG..END.
-CS and CE are the comment start resp. end string.
-CCS and CCE are the comment continuation strings for the start resp. end
+CS and CE are the comment start resp end string.
+CCS and CCE are the comment continuation strings for the start resp end
of lines (default to CS and CE).
BLOCK indicates that end of lines should be marked with either CCE, CE or CS
\(if CE is empty) and that those markers should be aligned.
(comment-quote-nested comment-start comment-end nil)
;; Loop over all lines to find the needed indentations.
+ (goto-char (point-min))
(while
(progn
(unless (looking-at "[ \t]*$")
(end-of-line)
(not (or (eobp) (progn (forward-line) nil))))))))))
+;;;###autoload
(defun comment-region (beg end &optional arg)
"Comment or uncomment each line in the region.
With just \\[universal-prefix] prefix arg, uncomment each line in region BEG..END.
Numeric prefix arg ARG means use ARG comment characters.
If ARG is negative, delete that many comment characters instead.
-Comments are terminated on each line, even for syntax in which newline does
-not end the comment. Blank lines do not get comments.
+By default, comments start at the left margin, are terminated on each line,
+even for syntax in which newline does not end the comment and blank lines
+do not get comments. This can be changed with `comment-style'.
The strings used as comment starts are built from
`comment-start' without trailing spaces and `comment-padding'."
;; sanitize LINES
(setq lines
(and
- lines multi
+ lines ;; multi
(progn (goto-char beg) (beginning-of-line)
(skip-syntax-forward " ")
(>= (point) beg))
The numeric prefix ARG specifies how many characters to add to begin- and
end- comment markers additionally to what `comment-add' already specifies."
(interactive "*r\np")
- (let ((comment-style 'box))
+ (let ((comment-style (if (cadr (assoc comment-style comment-styles))
+ 'box-multi 'box)))
(comment-region beg end (+ comment-add arg))))
+;;;###autoload
(defun comment-dwim (arg)
"Call the comment command you want (Do What I Mean).
If the region is active and `transient-mark-mode' is on, call
:type 'boolean
:group 'comment)
+;;;###autoload
(defun comment-indent-new-line (&optional soft)
"Break line at point and indent, continuing comment if within one.
This indents the body of the continued comment
;;; Change Log:
;; $Log: newcomment.el,v $
+;; Revision 1.16 2000/06/04 22:02:11 monnier
+;; (comment-indent): Ignore comment-indent-hook.
+;;
+;; Revision 1.15 2000/05/25 19:05:46 monnier
+;; Add abundant autoload cookies.
+;; (comment-style): Be careful not to depend on runtime data at compile-time.
+;; (comment-indent-hook): Remove.
+;; (comment-indent): Check if comment-indent-hook is bound.
+;; (comment-region): Docstring fix.
+;;
+;; Revision 1.14 2000/05/23 20:06:10 monnier
+;; (comment-make-extra-lines): Don't use `assert'.
+;;
+;; Revision 1.13 2000/05/22 04:23:37 monnier
+;; (comment-region-internal): Go back to BEG after quoting
+;; the nested comment markers.
+;;
+;; Revision 1.12 2000/05/21 00:27:31 monnier
+;; (comment-styles): New `box-multi'.
+;; (comment-normalize-vars): Better default for comment-continue to
+;; avoid whitespace-only continuations.
+;; (comment-search-forward): Always move even in the no-syntax case.
+;; (comment-padright): Only obey N if it's only obeyed for padleft.
+;; (comment-make-extra-lines): Better handling of empty continuations.
+;; Use `=' for the filler if comment-start has only one character.
+;; (uncomment-region): Try handling the special `=' filler.
+;; (comment-region): Allow LINES even if MULTI is nil.
+;; (comment-box): Choose box style based on comment-style.
+;;
+;; Revision 1.11 2000/05/19 15:37:41 monnier
+;; Fix license text and author.
+;; Move aliases (indent-for-comment, set-comment-column, kill-comment
+;; and indent-new-comment-line) to the beginning of the file.
+;; Get rid of the last few CLisms.
+;; (comment-forward): Avoid decf.
+;; (comment-make-extra-lines): Comment-out asserts.
+;; (comment-with-narrowing): Properly create uninterned symbol.
+;; (comment-region-internal): Comment-out asserts. Avoid incf and decf.
+;; (comment-indent-new-line): Fix bug where compt could be set but
+;; not comstart. Set comment-column more carefully.
+;;
;; Revision 1.10 2000/05/17 19:32:32 monnier
;; (comment-beginning): Handle unclosed comment.
;; (comment-auto-fill-only-comments): New var.