1999-11-28 18:51:06 +00:00
|
|
|
;;; newcomment.el --- (un)comment regions of buffers
|
|
|
|
|
2000-05-13 19:41:08 +00:00
|
|
|
;; Copyright (C) 1999-2000 Free Software Foundation Inc.
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-19 15:37:41 +00:00
|
|
|
;; Author: code extracted from Emacs-20's simple.el
|
2000-05-13 19:41:08 +00:00
|
|
|
;; Maintainer: Stefan Monnier <monnier@cs.yale.edu>
|
1999-11-28 18:51:06 +00:00
|
|
|
;; Keywords: comment uncomment
|
|
|
|
;; Version: $Name: $
|
2000-09-29 19:11:42 +00:00
|
|
|
;; Revision: $Id: newcomment.el,v 1.19 2000/07/06 13:25:31 monnier Exp $
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-19 15:37:41 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software; you can redistribute it and/or modify
|
1999-11-28 18:51:06 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2000-05-19 15:37:41 +00:00
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
1999-11-28 18:51:06 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2000-05-19 15:37:41 +00:00
|
|
|
|
1999-11-28 18:51:06 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2000-05-19 15:37:41 +00:00
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
1999-11-28 18:51:06 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2000-05-13 19:41:08 +00:00
|
|
|
;; A replacement for simple.el's comment-related functions.
|
1999-11-28 18:51:06 +00:00
|
|
|
|
|
|
|
;;; Bugs:
|
|
|
|
|
1999-11-30 16:20:55 +00:00
|
|
|
;; - single-char nestable comment-start can only do the "\\s<+" stuff
|
|
|
|
;; if the corresponding closing marker happens to be right.
|
2000-05-13 19:41:08 +00:00
|
|
|
;; - comment-box in TeXinfo generate bogus comments @ccccc@
|
1999-12-08 00:19:51 +00:00
|
|
|
;; - uncomment-region with a numeric argument can render multichar
|
|
|
|
;; comment markers invalid.
|
2000-05-23 20:06:10 +00:00
|
|
|
;; - 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.
|
1999-11-28 18:51:06 +00:00
|
|
|
|
|
|
|
;;; Todo:
|
|
|
|
|
2000-05-13 19:41:08 +00:00
|
|
|
;; - try to align tail comments
|
|
|
|
;; - check what c-comment-line-break-function has to say
|
1999-12-08 00:19:51 +00:00
|
|
|
;; - spill auto-fill of comments onto the end of the next line
|
1999-11-29 00:49:18 +00:00
|
|
|
;; - uncomment-region with a consp (for blocks) or somehow make the
|
|
|
|
;; deletion of continuation markers less dangerous
|
1999-11-30 16:20:55 +00:00
|
|
|
;; - drop block-comment-<foo> unless it's really used
|
2000-05-14 00:56:10 +00:00
|
|
|
;; - uncomment-region on a subpart of a comment
|
2000-05-13 19:41:08 +00:00
|
|
|
;; - support gnu-style "multi-line with space in continue"
|
|
|
|
;; - somehow allow comment-dwim to use the region even if transient-mark-mode
|
|
|
|
;; is not turned on.
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-09-29 19:11:42 +00:00
|
|
|
;; - when auto-filling a comment, try to move the comment to the left
|
|
|
|
;; rather than break it (if possible).
|
|
|
|
;; - sometimes default the comment-column to the same
|
|
|
|
;; one used on the preceding line(s).
|
|
|
|
|
1999-11-28 18:51:06 +00:00
|
|
|
;;; Code:
|
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-19 15:37:41 +00:00
|
|
|
(defalias 'indent-for-comment 'comment-indent)
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-19 15:37:41 +00:00
|
|
|
(defalias 'set-comment-column 'comment-set-column)
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-19 15:37:41 +00:00
|
|
|
(defalias 'kill-comment 'comment-kill)
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-19 15:37:41 +00:00
|
|
|
(defalias 'indent-new-comment-line 'comment-indent-new-line)
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-12-08 00:19:51 +00:00
|
|
|
(defgroup comment nil
|
|
|
|
"Indenting and filling of comments."
|
|
|
|
:prefix "comment-"
|
2000-06-20 09:40:36 +00:00
|
|
|
:version "21.1"
|
1999-12-08 00:19:51 +00:00
|
|
|
:group 'fill)
|
|
|
|
|
2000-05-13 19:41:08 +00:00
|
|
|
(defvar comment-use-syntax 'undecided
|
|
|
|
"Non-nil if syntax-tables can be used instead of regexps.
|
|
|
|
Can also be `undecided' which means that a somewhat expensive test will
|
|
|
|
be used to try to determine whether syntax-tables should be trusted
|
2000-05-14 00:56:10 +00:00
|
|
|
to understand comments or not in the given buffer.
|
|
|
|
Major modes should set this variable.")
|
1999-11-30 16:20:55 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-11-28 18:51:06 +00:00
|
|
|
(defcustom comment-column 32
|
|
|
|
"*Column to indent right-margin comments to.
|
|
|
|
Setting this variable automatically makes it local to the current buffer.
|
|
|
|
Each mode establishes a different default value for this variable; you
|
|
|
|
can set the value for a particular mode using that mode's hook."
|
|
|
|
:type 'integer
|
1999-12-08 00:19:51 +00:00
|
|
|
:group 'comment)
|
1999-11-28 18:51:06 +00:00
|
|
|
(make-variable-buffer-local 'comment-column)
|
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-14 00:56:10 +00:00
|
|
|
(defvar comment-start nil
|
|
|
|
"*String to insert to start a new comment, or nil if no comment syntax.")
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-14 00:56:10 +00:00
|
|
|
(defvar comment-start-skip nil
|
1999-11-28 18:51:06 +00:00
|
|
|
"*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
|
2000-05-14 00:56:10 +00:00
|
|
|
at the place matched by the close of the first pair.")
|
2000-05-13 19:41:08 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-12-08 00:19:51 +00:00
|
|
|
(defvar comment-end-skip nil
|
|
|
|
"Regexp to match the end of a comment plus everything up to its body.")
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-14 00:56:10 +00:00
|
|
|
(defvar comment-end ""
|
1999-11-28 18:51:06 +00:00
|
|
|
"*String to insert to end a new comment.
|
2000-05-14 00:56:10 +00:00
|
|
|
Should be an empty string if comments are terminated by end-of-line.")
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-11-28 18:51:06 +00:00
|
|
|
(defvar comment-indent-function
|
2000-09-29 19:11:42 +00:00
|
|
|
(lambda () (if (looking-at "\\s<\\s<\\s<") 0 comment-column))
|
1999-11-28 18:51:06 +00:00
|
|
|
"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.")
|
|
|
|
|
1999-12-08 00:19:51 +00:00
|
|
|
(defvar block-comment-start nil)
|
|
|
|
(defvar block-comment-end nil)
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-13 19:41:08 +00:00
|
|
|
(defvar comment-quote-nested t
|
|
|
|
"Non-nil if nested comments should be quoted.
|
|
|
|
This should be locally set by each major mode if needed.")
|
|
|
|
|
|
|
|
(defvar comment-continue nil
|
2000-05-14 00:56:10 +00:00
|
|
|
"Continuation string to insert for multiline comments.
|
|
|
|
This string will be added at the beginning of each line except the very
|
|
|
|
first one when commenting a region with a commenting style that allows
|
|
|
|
comments to span several lines.
|
|
|
|
It should generally have the same length as `comment-start' in order to
|
|
|
|
preserve indentation.
|
|
|
|
If it is nil a value will be automatically derived from `comment-start'
|
|
|
|
by replacing its first character with a space.")
|
|
|
|
|
2000-05-13 19:41:08 +00:00
|
|
|
(defvar comment-add 0
|
2000-05-14 00:56:10 +00:00
|
|
|
"How many more comment chars should be inserted by `comment-region'.
|
|
|
|
This determines the default value of the numeric argument of `comment-region'.
|
|
|
|
This should generally stay 0, except for a few modes like Lisp where
|
|
|
|
it can be convenient to set it to 1 so that regions are commented with
|
|
|
|
two semi-colons.")
|
1999-11-30 16:20:55 +00:00
|
|
|
|
|
|
|
(defconst comment-styles
|
2000-05-13 19:41:08 +00:00
|
|
|
'((plain . (nil nil nil nil))
|
|
|
|
(indent . (nil nil nil t))
|
|
|
|
(aligned . (nil t nil t))
|
|
|
|
(multi-line . (t nil nil t))
|
|
|
|
(extra-line . (t nil t t))
|
2000-05-21 00:27:53 +00:00
|
|
|
(box . (nil t t t))
|
|
|
|
(box-multi . (t t t t)))
|
2000-05-14 00:56:10 +00:00
|
|
|
"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.
|
|
|
|
ALIGN specifies that the `comment-end' markers should be aligned.
|
|
|
|
EXTRA specifies that an extra line should be used before and after the
|
|
|
|
region to comment (to put the `comment-end' and `comment-start').
|
|
|
|
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.")
|
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-14 00:56:10 +00:00
|
|
|
(defcustom comment-style 'plain
|
|
|
|
"*Style to be used for `comment-region'.
|
|
|
|
See `comment-styles' for a list of available styles."
|
|
|
|
:group 'comment
|
2000-05-25 19:05:46 +00:00
|
|
|
:type (if (boundp 'comment-styles)
|
|
|
|
`(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles))
|
|
|
|
'symbol))
|
1999-11-30 16:20:55 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defcustom comment-padding " "
|
2000-05-14 00:56:10 +00:00
|
|
|
"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.
|
1999-11-30 16:20:55 +00:00
|
|
|
|
|
|
|
Extra spacing between the comment characters and the comment text
|
2000-05-13 19:41:08 +00:00
|
|
|
makes the comment easier to read. Default is 1. nil means 0.")
|
1999-11-30 16:20:55 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-12-08 00:19:51 +00:00
|
|
|
(defcustom comment-multi-line nil
|
2000-06-20 09:40:36 +00:00
|
|
|
"*Non-nil means \\[comment-indent-new-line] continues comments, with no new terminator or starter.
|
1999-12-08 00:19:51 +00:00
|
|
|
This is obsolete because you might as well use \\[newline-and-indent]."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'comment)
|
|
|
|
|
1999-11-30 16:20:55 +00:00
|
|
|
;;;;
|
|
|
|
;;;; Helpers
|
|
|
|
;;;;
|
|
|
|
|
2000-05-14 00:56:10 +00:00
|
|
|
(defun comment-string-strip (str beforep afterp)
|
|
|
|
"Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
|
|
|
|
(string-match (concat "\\`" (if beforep "\\s-*")
|
2000-05-16 22:02:37 +00:00
|
|
|
"\\(.*?\\)" (if afterp "\\s-*\n?")
|
1999-11-30 16:20:55 +00:00
|
|
|
"\\'") str)
|
|
|
|
(match-string 1 str))
|
|
|
|
|
|
|
|
(defun comment-string-reverse (s)
|
2000-05-14 00:56:10 +00:00
|
|
|
"Return the mirror image of string S, without any trailing space."
|
2000-05-13 19:41:08 +00:00
|
|
|
(comment-string-strip (concat (nreverse (string-to-list s))) nil t))
|
1999-11-30 16:20:55 +00:00
|
|
|
|
|
|
|
(defun comment-normalize-vars (&optional noerror)
|
|
|
|
(if (not comment-start) (or noerror (error "No comment syntax is defined"))
|
|
|
|
;; comment-use-syntax
|
2000-05-13 19:41:08 +00:00
|
|
|
(when (eq comment-use-syntax 'undecided)
|
1999-11-30 16:20:55 +00:00
|
|
|
(set (make-local-variable 'comment-use-syntax)
|
|
|
|
(let ((st (syntax-table))
|
|
|
|
(cs comment-start)
|
|
|
|
(ce (if (string= "" comment-end) "\n" comment-end)))
|
2000-05-13 19:41:08 +00:00
|
|
|
;; Try to skip over a comment using forward-comment
|
|
|
|
;; to see if the syntax tables properly recognize it.
|
1999-11-30 16:20:55 +00:00
|
|
|
(with-temp-buffer
|
|
|
|
(set-syntax-table st)
|
|
|
|
(insert cs " hello " ce)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(and (forward-comment 1) (eobp))))))
|
|
|
|
;; comment-padding
|
|
|
|
(when (integerp comment-padding)
|
|
|
|
(setq comment-padding (make-string comment-padding ? )))
|
|
|
|
;; comment markers
|
|
|
|
;;(setq comment-start (comment-string-strip comment-start t nil))
|
|
|
|
;;(setq comment-end (comment-string-strip comment-end nil t))
|
|
|
|
;; comment-continue
|
2000-05-14 00:56:10 +00:00
|
|
|
(unless (or comment-continue (string= comment-end ""))
|
1999-11-30 16:20:55 +00:00
|
|
|
(set (make-local-variable 'comment-continue)
|
2000-05-21 00:27:53 +00:00
|
|
|
(concat (if (string-match "\\S-\\S-" comment-start) " " "|")
|
|
|
|
(substring comment-start 1))))
|
1999-11-30 16:20:55 +00:00
|
|
|
;; comment-skip regexps
|
|
|
|
(unless comment-start-skip
|
|
|
|
(set (make-local-variable 'comment-start-skip)
|
|
|
|
(concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
|
|
|
|
(regexp-quote (comment-string-strip comment-start t t))
|
|
|
|
"+\\)\\s-*")))
|
|
|
|
(unless comment-end-skip
|
|
|
|
(let ((ce (if (string= "" comment-end) "\n"
|
|
|
|
(comment-string-strip comment-end t t))))
|
|
|
|
(set (make-local-variable 'comment-end-skip)
|
2000-05-13 19:41:08 +00:00
|
|
|
(concat "\\s-*\\(\\s>" (if comment-quote-nested "" "+")
|
1999-11-30 16:20:55 +00:00
|
|
|
"\\|" (regexp-quote (substring ce 0 1))
|
2000-05-13 19:41:08 +00:00
|
|
|
(if (and comment-quote-nested (<= (length ce) 1)) "" "+")
|
1999-11-30 16:20:55 +00:00
|
|
|
(regexp-quote (substring ce 1))
|
|
|
|
"\\)"))))))
|
|
|
|
|
2000-05-14 00:56:10 +00:00
|
|
|
(defun comment-quote-re (str unp)
|
|
|
|
(concat (regexp-quote (substring str 0 1))
|
|
|
|
"\\\\" (if unp "+" "*")
|
|
|
|
(regexp-quote (substring str 1))))
|
|
|
|
|
|
|
|
(defun comment-quote-nested (cs ce unp)
|
|
|
|
"Quote or unquote nested comments.
|
|
|
|
If UNP is non-nil, unquote nested comment markers."
|
|
|
|
(setq cs (comment-string-strip cs t t))
|
|
|
|
(setq ce (comment-string-strip ce t t))
|
|
|
|
(when (and comment-quote-nested (> (length ce) 0))
|
|
|
|
(let ((re (concat (comment-quote-re ce unp)
|
|
|
|
"\\|" (comment-quote-re cs unp))))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward re nil t)
|
|
|
|
(goto-char (match-beginning 0))
|
|
|
|
(forward-char 1)
|
|
|
|
(if unp (delete-char 1) (insert "\\"))
|
|
|
|
(when (= (length ce) 1)
|
|
|
|
;; If the comment-end is a single char, adding a \ after that
|
|
|
|
;; "first" char won't deactivate it, so we turn such a CE
|
|
|
|
;; into !CS. I.e. for pascal, we turn } into !{
|
|
|
|
(if (not unp)
|
|
|
|
(when (string= (match-string 0) ce)
|
|
|
|
(replace-match (concat "!" cs) t t))
|
|
|
|
(when (and (< (point-min) (match-beginning 0))
|
|
|
|
(string= (buffer-substring (1- (match-beginning 0))
|
|
|
|
(1- (match-end 0)))
|
|
|
|
(concat "!" cs)))
|
|
|
|
(backward-char 2)
|
|
|
|
(delete-char (- (match-end 0) (match-beginning 0)))
|
|
|
|
(insert ce))))))))
|
1999-11-30 16:20:55 +00:00
|
|
|
|
|
|
|
;;;;
|
|
|
|
;;;; Navigation
|
|
|
|
;;;;
|
|
|
|
|
2000-05-16 22:02:37 +00:00
|
|
|
(defun comment-search-forward (limit &optional noerror)
|
2000-05-13 19:41:08 +00:00
|
|
|
"Find a comment start between point and LIMIT.
|
|
|
|
Moves point to inside the comment and returns the position of the
|
|
|
|
comment-starter. If no comment is found, moves point to LIMIT
|
1999-11-29 01:31:47 +00:00
|
|
|
and raises an error or returns nil of NOERROR is non-nil."
|
1999-11-30 16:20:55 +00:00
|
|
|
(if (not comment-use-syntax)
|
2000-05-21 00:27:53 +00:00
|
|
|
(if (re-search-forward comment-start-skip limit noerror)
|
|
|
|
(or (match-end 1) (match-beginning 0))
|
|
|
|
(goto-char limit)
|
|
|
|
(unless noerror (error "No comment")))
|
2000-05-16 22:02:37 +00:00
|
|
|
(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)))
|
|
|
|
(when (and (nth 8 s) (nth 3 s))
|
|
|
|
;; The search ended inside a string. Try to see if it
|
|
|
|
;; works better when we assume that pt is inside a string.
|
|
|
|
(setq s (parse-partial-sexp
|
|
|
|
pt (or limit (point-max)) nil nil
|
|
|
|
(list nil nil nil (nth 3 s) nil nil nil nil)
|
|
|
|
t)))
|
|
|
|
(if (not (and (nth 8 s) (not (nth 3 s))))
|
|
|
|
(unless noerror (error "No comment"))
|
|
|
|
;; We found the comment.
|
2000-05-21 00:27:53 +00:00
|
|
|
(let ((pos (point))
|
2000-05-16 22:02:37 +00:00
|
|
|
(start (nth 8 s))
|
2000-05-21 00:27:53 +00:00
|
|
|
(bol (line-beginning-position))
|
2000-05-16 22:02:37 +00:00
|
|
|
(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)))
|
2000-05-21 00:27:53 +00:00
|
|
|
(goto-char (or end pos))
|
2000-05-16 22:02:37 +00:00
|
|
|
start)))))
|
1999-11-30 16:20:55 +00:00
|
|
|
|
|
|
|
(defun comment-search-backward (&optional limit noerror)
|
|
|
|
"Find a comment start between LIMIT and point.
|
2000-05-13 19:41:08 +00:00
|
|
|
Moves point to inside the comment and returns the position of the
|
|
|
|
comment-starter. If no comment is found, moves point to LIMIT
|
1999-11-30 16:20:55 +00:00
|
|
|
and raises an error or returns nil of NOERROR is non-nil."
|
2000-05-16 22:02:37 +00:00
|
|
|
;; FIXME: If a comment-start appears inside a comment, we may erroneously
|
|
|
|
;; stop there. This can be rather bad in general, but since
|
|
|
|
;; comment-search-backward is only used to find the comment-column (in
|
|
|
|
;; comment-set-column) and to find the comment-start string (via
|
|
|
|
;; comment-beginning) in indent-new-comment-line, it should be harmless.
|
1999-11-30 16:20:55 +00:00
|
|
|
(if (not (re-search-backward comment-start-skip limit t))
|
|
|
|
(unless noerror (error "No comment"))
|
|
|
|
(beginning-of-line)
|
|
|
|
(let* ((end (match-end 0))
|
|
|
|
(cs (comment-search-forward end t))
|
|
|
|
(pt (point)))
|
|
|
|
(if (not cs)
|
|
|
|
(progn (beginning-of-line)
|
|
|
|
(comment-search-backward limit noerror))
|
|
|
|
(while (progn (goto-char cs)
|
|
|
|
(comment-forward)
|
|
|
|
(and (< (point) end)
|
|
|
|
(setq cs (comment-search-forward end t))))
|
|
|
|
(setq pt (point)))
|
|
|
|
(goto-char pt)
|
|
|
|
cs))))
|
|
|
|
|
|
|
|
(defun comment-beginning ()
|
2000-05-13 19:41:08 +00:00
|
|
|
"Find the beginning of the enclosing comment.
|
|
|
|
Returns nil if not inside a comment, else moves point and returns
|
1999-11-30 16:20:55 +00:00
|
|
|
the same as `comment-search-forward'."
|
|
|
|
(let ((pt (point))
|
|
|
|
(cs (comment-search-backward nil t)))
|
2000-05-16 22:02:37 +00:00
|
|
|
(when cs
|
2000-05-17 19:32:32 +00:00
|
|
|
(if (save-excursion
|
|
|
|
(goto-char cs)
|
|
|
|
(if (comment-forward 1) (> (point) pt) (eobp)))
|
2000-05-16 22:02:37 +00:00
|
|
|
cs
|
|
|
|
(goto-char pt)
|
|
|
|
nil))))
|
1999-11-30 16:20:55 +00:00
|
|
|
|
|
|
|
(defun comment-forward (&optional n)
|
|
|
|
"Skip forward over N comments.
|
|
|
|
Just like `forward-comment' but only for positive N
|
|
|
|
and can use regexps instead of syntax."
|
|
|
|
(setq n (or n 1))
|
|
|
|
(if (< n 0) (error "No comment-backward")
|
|
|
|
(if comment-use-syntax (forward-comment n)
|
|
|
|
(while (> n 0)
|
|
|
|
(skip-syntax-forward " ")
|
2000-05-19 15:37:41 +00:00
|
|
|
(setq n
|
|
|
|
(if (and (looking-at comment-start-skip)
|
|
|
|
(re-search-forward comment-end-skip nil 'move))
|
|
|
|
(1- n) -1)))
|
1999-11-30 16:20:55 +00:00
|
|
|
(= n 0))))
|
|
|
|
|
|
|
|
(defun comment-enter-backward ()
|
|
|
|
"Move from the end of a comment to the end of its content.
|
2000-05-13 19:41:08 +00:00
|
|
|
Point is assumed to be just at the end of a comment."
|
1999-11-30 16:20:55 +00:00
|
|
|
(if (bolp)
|
|
|
|
;; comment-end = ""
|
|
|
|
(progn (backward-char) (skip-syntax-backward " "))
|
|
|
|
(let ((end (point)))
|
|
|
|
(beginning-of-line)
|
|
|
|
(save-restriction
|
|
|
|
(narrow-to-region (point) end)
|
2000-05-16 22:02:37 +00:00
|
|
|
(if (re-search-forward (concat comment-end-skip "\\'") nil t)
|
|
|
|
(goto-char (match-beginning 0))
|
|
|
|
;; comment-end-skip not found probably because it was not set right.
|
|
|
|
;; Since \\s> should catch the single-char case, we'll blindly
|
|
|
|
;; assume we're at the end of a two-char comment-end.
|
|
|
|
(goto-char (point-max))
|
|
|
|
(backward-char 2)
|
|
|
|
(skip-chars-backward (string (char-after)))
|
|
|
|
(skip-syntax-backward " "))))))
|
1999-11-30 16:20:55 +00:00
|
|
|
|
|
|
|
;;;;
|
|
|
|
;;;; Commands
|
|
|
|
;;;;
|
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-12-08 00:19:51 +00:00
|
|
|
(defun comment-indent (&optional continue)
|
1999-11-30 16:20:55 +00:00
|
|
|
"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."
|
1999-11-28 18:51:06 +00:00
|
|
|
(interactive "*")
|
2000-05-16 22:02:37 +00:00
|
|
|
(comment-normalize-vars)
|
1999-11-28 18:51:06 +00:00
|
|
|
(let* ((empty (save-excursion (beginning-of-line)
|
|
|
|
(looking-at "[ \t]*$")))
|
2000-05-14 00:56:10 +00:00
|
|
|
(starter (or (and continue comment-continue)
|
1999-11-30 16:20:55 +00:00
|
|
|
(and empty block-comment-start) comment-start))
|
2000-05-14 00:56:10 +00:00
|
|
|
(ender (or (and continue comment-continue "")
|
1999-11-30 16:20:55 +00:00
|
|
|
(and empty block-comment-end) comment-end)))
|
1999-11-28 18:51:06 +00:00
|
|
|
(cond
|
|
|
|
((null starter)
|
|
|
|
(error "No comment syntax defined"))
|
2000-05-16 22:02:37 +00:00
|
|
|
(t (let* ((eolpos (line-end-position))
|
1999-11-28 18:51:06 +00:00
|
|
|
cpos indent begpos)
|
|
|
|
(beginning-of-line)
|
2000-05-21 00:27:53 +00:00
|
|
|
(if (not (setq begpos (comment-search-forward eolpos t)))
|
|
|
|
(setq begpos (point))
|
1999-11-29 01:31:47 +00:00
|
|
|
(setq cpos (point-marker))
|
|
|
|
(goto-char begpos))
|
1999-11-28 18:51:06 +00:00
|
|
|
;; Compute desired indent.
|
2000-09-29 19:11:42 +00:00
|
|
|
(setq indent (funcall comment-indent-function))
|
|
|
|
;; Avoid moving comments past the fill-column.
|
|
|
|
(setq indent
|
|
|
|
(min indent
|
|
|
|
(+ (current-column)
|
|
|
|
(- fill-column
|
|
|
|
(save-excursion (end-of-line) (current-column))))))
|
|
|
|
(if (= (current-column) indent)
|
1999-11-28 18:51:06 +00:00
|
|
|
(goto-char begpos)
|
|
|
|
;; If that's different from current, change it.
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
(delete-region (point) begpos)
|
2000-09-29 19:11:42 +00:00
|
|
|
(indent-to (if (bolp) indent (max indent (1+ (current-column))))))
|
1999-11-28 18:51:06 +00:00
|
|
|
;; An existing comment?
|
|
|
|
(if cpos
|
2000-05-21 00:27:53 +00:00
|
|
|
(progn (goto-char cpos) (set-marker cpos nil))
|
1999-11-28 18:51:06 +00:00
|
|
|
;; No, insert one.
|
|
|
|
(insert starter)
|
|
|
|
(save-excursion
|
|
|
|
(insert ender))))))))
|
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-12-08 00:19:51 +00:00
|
|
|
(defun comment-set-column (arg)
|
1999-11-28 18:51:06 +00:00
|
|
|
"Set the comment column based on point.
|
1999-11-30 16:20:55 +00:00
|
|
|
With no ARG, set the comment column to the current column.
|
1999-11-28 18:51:06 +00:00
|
|
|
With just minus as arg, kill any comment on this line.
|
|
|
|
With any other arg, set comment column to indentation of the previous comment
|
|
|
|
and then align or create a comment on this line at that column."
|
|
|
|
(interactive "P")
|
1999-11-29 01:31:47 +00:00
|
|
|
(cond
|
2000-05-16 22:02:37 +00:00
|
|
|
((eq arg '-) (comment-kill nil))
|
1999-11-29 01:31:47 +00:00
|
|
|
(arg
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
1999-11-30 16:20:55 +00:00
|
|
|
(comment-search-backward)
|
1999-11-29 01:31:47 +00:00
|
|
|
(beginning-of-line)
|
2000-05-16 22:02:37 +00:00
|
|
|
(goto-char (comment-search-forward (line-end-position)))
|
1999-11-28 18:51:06 +00:00
|
|
|
(setq comment-column (current-column))
|
1999-11-29 01:31:47 +00:00
|
|
|
(message "Comment column set to %d" comment-column))
|
2000-05-16 22:02:37 +00:00
|
|
|
(comment-indent))
|
1999-11-29 01:31:47 +00:00
|
|
|
(t (setq comment-column (current-column))
|
1999-11-28 18:51:06 +00:00
|
|
|
(message "Comment column set to %d" comment-column))))
|
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-12-08 00:19:51 +00:00
|
|
|
(defun comment-kill (arg)
|
1999-11-29 00:49:18 +00:00
|
|
|
"Kill the comment on this line, if any.
|
|
|
|
With prefix ARG, kill comments on that many lines starting with this one."
|
|
|
|
(interactive "P")
|
2000-05-16 22:02:37 +00:00
|
|
|
(dotimes (_ (prefix-numeric-value arg))
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(let ((cs (comment-search-forward (line-end-position) t)))
|
|
|
|
(when cs
|
|
|
|
(goto-char cs)
|
|
|
|
(skip-syntax-backward " ")
|
|
|
|
(setq cs (point))
|
|
|
|
(comment-forward)
|
|
|
|
(kill-region cs (if (bolp) (1- (point)) (point)))
|
|
|
|
(indent-according-to-mode))))
|
|
|
|
(if arg (forward-line 1))))
|
1999-11-29 00:49:18 +00:00
|
|
|
|
1999-11-30 16:20:55 +00:00
|
|
|
(defun comment-padright (str &optional n)
|
|
|
|
"Construct a string composed of STR plus `comment-padding'.
|
2000-05-14 00:56:10 +00:00
|
|
|
It also adds N copies of the last non-whitespace chars of STR.
|
1999-11-30 16:20:55 +00:00
|
|
|
If STR already contains padding, the corresponding amount is
|
2000-05-14 00:56:10 +00:00
|
|
|
ignored from `comment-padding'.
|
|
|
|
N defaults to 0.
|
2000-05-13 19:41:08 +00:00
|
|
|
If N is `re', a regexp is returned instead, that would match
|
2000-05-14 00:56:10 +00:00
|
|
|
the string for any N."
|
1999-11-30 16:20:55 +00:00
|
|
|
(setq n (or n 0))
|
|
|
|
(when (and (stringp str) (not (string= "" str)))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Separate the actual string from any leading/trailing padding
|
1999-12-08 00:19:51 +00:00
|
|
|
(string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
|
2000-05-14 00:56:10 +00:00
|
|
|
(let ((s (match-string 1 str)) ;actual string
|
|
|
|
(lpad (substring str 0 (match-beginning 1))) ;left padding
|
|
|
|
(rpad (concat (substring str (match-end 1)) ;original right padding
|
|
|
|
(substring comment-padding ;additional right padding
|
1999-12-08 00:19:51 +00:00
|
|
|
(min (- (match-end 0) (match-end 1))
|
2000-05-21 00:27:53 +00:00
|
|
|
(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)))))
|
2000-05-14 00:56:10 +00:00
|
|
|
(if (not (symbolp n))
|
2000-05-21 00:27:53 +00:00
|
|
|
(concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
|
2000-05-14 00:56:10 +00:00
|
|
|
;; 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
|
2000-05-21 00:27:53 +00:00
|
|
|
(regexp-quote s)
|
|
|
|
(when multi "+") ;the last char of S might be repeated
|
2000-05-14 00:56:10 +00:00
|
|
|
(mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
|
|
|
|
rpad "")))))) ;padding is not required
|
1999-11-30 16:20:55 +00:00
|
|
|
|
|
|
|
(defun comment-padleft (str &optional n)
|
|
|
|
"Construct a string composed of `comment-padding' plus STR.
|
2000-05-14 00:56:10 +00:00
|
|
|
It also adds N copies of the first non-whitespace chars of STR.
|
1999-11-30 16:20:55 +00:00
|
|
|
If STR already contains padding, the corresponding amount is
|
2000-05-14 00:56:10 +00:00
|
|
|
ignored from `comment-padding'.
|
|
|
|
N defaults to 0.
|
2000-05-13 19:41:08 +00:00
|
|
|
If N is `re', a regexp is returned instead, that would match
|
1999-11-30 16:20:55 +00:00
|
|
|
the string for any N."
|
|
|
|
(setq n (or n 0))
|
|
|
|
(when (and (stringp str) (not (string= "" str)))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Only separate the left pad because we assume there is no right pad.
|
1999-11-30 16:20:55 +00:00
|
|
|
(string-match "\\`\\s-*" str)
|
|
|
|
(let ((s (substring str (match-end 0)))
|
|
|
|
(pad (concat (substring comment-padding
|
|
|
|
(min (- (match-end 0) (match-beginning 0))
|
|
|
|
(length comment-padding)))
|
|
|
|
(match-string 0 str)))
|
2000-05-14 00:56:10 +00:00
|
|
|
(c (aref str (match-end 0))) ;the first non-space char of STR
|
|
|
|
;; 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 pad (when multi (make-string n c)) s)
|
|
|
|
;; Construct a regexp that would match anything from just S
|
|
|
|
;; to any possible output of this function for any N.
|
|
|
|
;; We match any number of leading spaces because this regexp will
|
|
|
|
;; be used for uncommenting where we might want to remove
|
|
|
|
;; uncomment markers with arbitrary leading space (because
|
|
|
|
;; they were aligned).
|
|
|
|
(concat "\\s-*"
|
|
|
|
(if multi (concat (regexp-quote (string c)) "*"))
|
|
|
|
(regexp-quote s))))))
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-11-29 00:49:18 +00:00
|
|
|
(defun uncomment-region (beg end &optional arg)
|
|
|
|
"Uncomment each line in the BEG..END region.
|
2000-05-14 00:56:10 +00:00
|
|
|
The numeric prefix ARG can specify a number of chars to remove from the
|
|
|
|
comment markers."
|
1999-11-28 18:51:06 +00:00
|
|
|
(interactive "*r\nP")
|
|
|
|
(comment-normalize-vars)
|
|
|
|
(if (> beg end) (let (mid) (setq mid beg beg end end mid)))
|
|
|
|
(save-excursion
|
1999-11-29 00:49:18 +00:00
|
|
|
(goto-char beg)
|
2000-05-14 00:56:10 +00:00
|
|
|
(setq end (copy-marker end))
|
1999-11-29 00:49:18 +00:00
|
|
|
(let ((numarg (prefix-numeric-value arg))
|
1999-11-30 16:20:55 +00:00
|
|
|
spt)
|
1999-11-29 00:49:18 +00:00
|
|
|
(while (and (< (point) end)
|
1999-11-30 16:20:55 +00:00
|
|
|
(setq spt (comment-search-forward end t)))
|
|
|
|
(let* ((ipt (point))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Find the end of the comment.
|
1999-11-29 01:31:47 +00:00
|
|
|
(ept (progn
|
1999-11-30 16:20:55 +00:00
|
|
|
(goto-char spt)
|
|
|
|
(unless (comment-forward)
|
1999-11-29 01:31:47 +00:00
|
|
|
(error "Can't find the comment end"))
|
2000-05-14 00:56:10 +00:00
|
|
|
(point)))
|
|
|
|
(box nil)
|
|
|
|
(ccs comment-continue)
|
1999-11-30 16:20:55 +00:00
|
|
|
(srei (comment-padright ccs 're))
|
1999-12-08 00:19:51 +00:00
|
|
|
(sre (and srei (concat "^\\s-*?\\(" srei "\\)"))))
|
1999-11-29 01:31:47 +00:00
|
|
|
(save-restriction
|
|
|
|
(narrow-to-region spt ept)
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Remove the comment-start.
|
1999-11-30 16:20:55 +00:00
|
|
|
(goto-char ipt)
|
|
|
|
(skip-syntax-backward " ")
|
2000-05-21 00:27:53 +00:00
|
|
|
;; Check for special `=' used sometimes in comment-box.
|
|
|
|
(when (and (= (- (point) (point-min)) 1) (looking-at "=\\{7\\}"))
|
|
|
|
(skip-chars-forward "="))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; A box-comment starts with a looong comment-start marker.
|
1999-11-30 16:20:55 +00:00
|
|
|
(when (> (- (point) (point-min) (length comment-start)) 7)
|
2000-05-14 00:56:10 +00:00
|
|
|
(setq box t))
|
1999-11-30 16:20:55 +00:00
|
|
|
(when (looking-at (regexp-quote comment-padding))
|
|
|
|
(goto-char (match-end 0)))
|
|
|
|
(when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
|
|
|
|
(goto-char (match-end 0)))
|
1999-12-08 00:19:51 +00:00
|
|
|
(if (null arg) (delete-region (point-min) (point))
|
|
|
|
(skip-syntax-backward " ")
|
|
|
|
(delete-char (- numarg)))
|
1999-11-30 16:20:55 +00:00
|
|
|
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Remove the end-comment (and leading padding and such).
|
1999-11-30 16:20:55 +00:00
|
|
|
(goto-char (point-max)) (comment-enter-backward)
|
2000-05-21 00:27:53 +00:00
|
|
|
;; Check for special `=' used sometimes in comment-box.
|
2000-07-06 13:24:28 +00:00
|
|
|
(when (= (- (point-max) (point)) 1)
|
|
|
|
(let ((pos (point)))
|
|
|
|
;; skip `=' but only if there are at least 7.
|
|
|
|
(when (> (skip-chars-backward "=") -7) (goto-char pos))))
|
2000-05-21 00:27:53 +00:00
|
|
|
(unless (looking-at "\\(\n\\|\\s-\\)*\\'")
|
1999-11-30 16:20:55 +00:00
|
|
|
(when (and (bolp) (not (bobp))) (backward-char))
|
2000-05-14 00:56:10 +00:00
|
|
|
(if (null arg) (delete-region (point) (point-max))
|
1999-12-08 00:19:51 +00:00
|
|
|
(skip-syntax-forward " ")
|
|
|
|
(delete-char numarg)))
|
1999-11-29 01:31:47 +00:00
|
|
|
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Unquote any nested end-comment.
|
|
|
|
(comment-quote-nested comment-start comment-end t)
|
|
|
|
|
|
|
|
;; Eliminate continuation markers as well.
|
|
|
|
(when sre
|
|
|
|
(let* ((cce (comment-string-reverse (or comment-continue
|
|
|
|
comment-start)))
|
|
|
|
(erei (and box (comment-padleft cce 're)))
|
|
|
|
(ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
|
1999-11-29 00:49:18 +00:00
|
|
|
(goto-char (point-min))
|
2000-05-14 00:56:10 +00:00
|
|
|
(while (progn
|
|
|
|
(if (and ere (re-search-forward
|
|
|
|
ere (line-end-position) t))
|
|
|
|
(replace-match "" t t nil (if (match-end 2) 2 1))
|
|
|
|
(setq ere nil))
|
|
|
|
(forward-line 1)
|
|
|
|
(re-search-forward sre (line-end-position) t))
|
1999-11-30 16:20:55 +00:00
|
|
|
(replace-match "" t t nil (if (match-end 2) 2 1)))))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Go the the end for the next comment.
|
|
|
|
(goto-char (point-max)))))
|
|
|
|
(set-marker end nil))))
|
1999-11-28 18:51:06 +00:00
|
|
|
|
1999-11-28 21:33:55 +00:00
|
|
|
(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
|
2000-05-16 22:02:37 +00:00
|
|
|
"Make the leading and trailing extra lines.
|
|
|
|
This is used for `extra-line' style (or `box' style if BLOCK is specified)."
|
2000-05-21 00:27:53 +00:00
|
|
|
(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))) ?=))
|
2000-05-23 20:06:10 +00:00
|
|
|
(_ (string-match "\\s-*a=m\\s-*" s))
|
2000-05-21 00:27:53 +00:00
|
|
|
(fill
|
|
|
|
(make-string (+ width (- (match-end 0)
|
|
|
|
(match-beginning 0) (length cs) 3)) c)))
|
1999-11-28 21:33:55 +00:00
|
|
|
(setq cs (replace-match fill t t s))
|
2000-05-23 20:06:10 +00:00
|
|
|
(string-match "\\s-*a=m\\s-*" e)
|
2000-05-21 00:27:53 +00:00
|
|
|
(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))))
|
1999-11-28 21:33:55 +00:00
|
|
|
|
|
|
|
(def-edebug-spec comment-with-narrowing t)
|
|
|
|
(put 'comment-with-narrowing 'lisp-indent-function 2)
|
|
|
|
(defmacro comment-with-narrowing (beg end &rest body)
|
|
|
|
"Execute BODY with BEG..END narrowing.
|
|
|
|
Space is added (and then removed) at the beginning for the text's
|
|
|
|
indentation to be kept as it was before narrowing."
|
2000-05-19 15:37:41 +00:00
|
|
|
(let ((bindent (make-symbol "bindent")))
|
|
|
|
`(let ((,bindent (save-excursion (goto-char beg) (current-column))))
|
|
|
|
(save-restriction
|
|
|
|
(narrow-to-region beg end)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(insert (make-string ,bindent ? ))
|
|
|
|
(prog1
|
|
|
|
(progn ,@body)
|
|
|
|
;; remove the bindent
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(when (looking-at " *")
|
|
|
|
(let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent)))
|
1999-11-28 21:33:55 +00:00
|
|
|
(delete-char n)
|
2000-05-19 15:37:41 +00:00
|
|
|
(setq ,bindent (- ,bindent n))))
|
|
|
|
(end-of-line)
|
|
|
|
(let ((e (point)))
|
|
|
|
(beginning-of-line)
|
|
|
|
(while (and (> ,bindent 0) (re-search-forward " *" e t))
|
|
|
|
(let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1))))
|
|
|
|
(goto-char (match-beginning 0))
|
|
|
|
(delete-char n)
|
|
|
|
(setq ,bindent (- ,bindent n)))))))))))
|
1999-11-28 21:33:55 +00:00
|
|
|
|
2000-05-13 19:41:08 +00:00
|
|
|
(defun comment-region-internal (beg end cs ce
|
|
|
|
&optional ccs cce block lines indent)
|
2000-05-14 00:56:10 +00:00
|
|
|
"Comment region BEG..END.
|
2000-05-22 04:24:01 +00:00
|
|
|
CS and CE are the comment start resp end string.
|
|
|
|
CCS and CCE are the comment continuation strings for the start resp end
|
2000-05-14 00:56:10 +00:00
|
|
|
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.
|
|
|
|
LINES indicates that an extra lines will be used at the beginning and end
|
|
|
|
of the region for CE and CS.
|
|
|
|
INDENT indicates to put CS and CCS at the current indentation of the region
|
|
|
|
rather than at left margin."
|
2000-05-19 15:37:41 +00:00
|
|
|
;;(assert (< beg end))
|
1999-11-28 18:51:06 +00:00
|
|
|
(let ((no-empty t))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Sanitize CE and CCE.
|
1999-11-28 18:51:06 +00:00
|
|
|
(if (and (stringp ce) (string= "" ce)) (setq ce nil))
|
|
|
|
(if (and (stringp cce) (string= "" cce)) (setq cce nil))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; If CE is empty, multiline cannot be used.
|
|
|
|
(unless ce (setq ccs nil cce nil))
|
|
|
|
;; Should we mark empty lines as well ?
|
1999-11-28 18:51:06 +00:00
|
|
|
(if (or ccs block lines) (setq no-empty nil))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Make sure we have end-markers for BLOCK mode.
|
1999-11-30 16:20:55 +00:00
|
|
|
(when block (unless ce (setq ce (comment-string-reverse cs))))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; If BLOCK is not requested, we don't need CCE.
|
|
|
|
(unless block (setq cce nil))
|
|
|
|
;; Continuation defaults to the same as CS and CE.
|
|
|
|
(unless ccs (setq ccs cs cce ce))
|
1999-11-29 00:49:18 +00:00
|
|
|
|
1999-11-28 18:51:06 +00:00
|
|
|
(save-excursion
|
1999-11-28 21:33:55 +00:00
|
|
|
(goto-char end)
|
2000-05-14 00:56:10 +00:00
|
|
|
;; If the end is not at the end of a line and the comment-end
|
|
|
|
;; is implicit (i.e. a newline), explicitly insert a newline.
|
1999-11-28 21:33:55 +00:00
|
|
|
(unless (or ce (eolp)) (insert "\n") (indent-according-to-mode))
|
|
|
|
(comment-with-narrowing beg end
|
2000-05-14 00:56:10 +00:00
|
|
|
(let ((min-indent (point-max))
|
1999-11-28 18:51:06 +00:00
|
|
|
(max-indent 0))
|
|
|
|
(goto-char (point-min))
|
2000-05-14 00:56:10 +00:00
|
|
|
;; Quote any nested comment marker
|
|
|
|
(comment-quote-nested comment-start comment-end nil)
|
|
|
|
|
|
|
|
;; Loop over all lines to find the needed indentations.
|
2000-05-22 04:24:01 +00:00
|
|
|
(goto-char (point-min))
|
2000-05-14 00:56:10 +00:00
|
|
|
(while
|
|
|
|
(progn
|
|
|
|
(unless (looking-at "[ \t]*$")
|
|
|
|
(setq min-indent (min min-indent (current-indentation))))
|
|
|
|
(end-of-line)
|
|
|
|
(setq max-indent (max max-indent (current-column)))
|
|
|
|
(not (or (eobp) (progn (forward-line) nil)))))
|
|
|
|
|
|
|
|
;; Inserting ccs can change max-indent by (1- tab-width).
|
2000-05-19 15:37:41 +00:00
|
|
|
(setq max-indent
|
|
|
|
(+ max-indent (max (length cs) (length ccs)) tab-width -1))
|
2000-05-13 19:41:08 +00:00
|
|
|
(unless indent (setq min-indent 0))
|
1999-11-28 18:51:06 +00:00
|
|
|
|
1999-11-28 21:33:55 +00:00
|
|
|
;; make the leading and trailing lines if requested
|
1999-11-28 18:51:06 +00:00
|
|
|
(when lines
|
2000-05-13 19:41:08 +00:00
|
|
|
(let ((csce
|
|
|
|
(comment-make-extra-lines
|
|
|
|
cs ce ccs cce min-indent max-indent block)))
|
|
|
|
(setq cs (car csce))
|
|
|
|
(setq ce (cdr csce))))
|
1999-11-28 18:51:06 +00:00
|
|
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
;; Loop over all lines from BEG to END.
|
2000-05-14 00:56:10 +00:00
|
|
|
(while
|
|
|
|
(progn
|
|
|
|
(unless (and no-empty (looking-at "[ \t]*$"))
|
|
|
|
(move-to-column min-indent t)
|
|
|
|
(insert cs) (setq cs ccs) ;switch to CCS after the first line
|
|
|
|
(end-of-line)
|
|
|
|
(if (eobp) (setq cce ce))
|
|
|
|
(when cce
|
|
|
|
(when block (move-to-column max-indent t))
|
|
|
|
(insert cce)))
|
|
|
|
(end-of-line)
|
|
|
|
(not (or (eobp) (progn (forward-line) nil))))))))))
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-11-28 18:51:06 +00:00
|
|
|
(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.
|
2000-05-25 19:05:46 +00:00
|
|
|
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'.
|
1999-11-28 18:51:06 +00:00
|
|
|
|
|
|
|
The strings used as comment starts are built from
|
|
|
|
`comment-start' without trailing spaces and `comment-padding'."
|
|
|
|
(interactive "*r\nP")
|
|
|
|
(comment-normalize-vars)
|
|
|
|
(if (> beg end) (let (mid) (setq mid beg beg end end mid)))
|
1999-11-30 16:20:55 +00:00
|
|
|
(let* ((numarg (prefix-numeric-value arg))
|
2000-05-13 19:41:08 +00:00
|
|
|
(add comment-add)
|
1999-11-30 16:20:55 +00:00
|
|
|
(style (cdr (assoc comment-style comment-styles)))
|
|
|
|
(lines (nth 2 style))
|
|
|
|
(block (nth 1 style))
|
|
|
|
(multi (nth 0 style)))
|
1999-11-28 18:51:06 +00:00
|
|
|
(save-excursion
|
|
|
|
;; we use `chars' instead of `syntax' because `\n' might be
|
|
|
|
;; of end-comment syntax rather than of whitespace syntax.
|
|
|
|
;; sanitize BEG and END
|
|
|
|
(goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
|
|
|
|
(setq beg (max beg (point)))
|
|
|
|
(goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
|
|
|
|
(setq end (min end (point)))
|
|
|
|
(if (>= beg end) (error "Nothing to comment"))
|
|
|
|
|
|
|
|
;; sanitize LINES
|
|
|
|
(setq lines
|
|
|
|
(and
|
2000-05-21 00:27:53 +00:00
|
|
|
lines ;; multi
|
1999-11-28 18:51:06 +00:00
|
|
|
(progn (goto-char beg) (beginning-of-line)
|
|
|
|
(skip-syntax-forward " ")
|
|
|
|
(>= (point) beg))
|
|
|
|
(progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
|
|
|
|
(<= (point) end))
|
2000-05-13 19:41:08 +00:00
|
|
|
(or (not (string= "" comment-end)) block)
|
1999-11-30 16:20:55 +00:00
|
|
|
(progn (goto-char beg) (search-forward "\n" end t)))))
|
1999-11-28 18:51:06 +00:00
|
|
|
|
1999-11-30 16:20:55 +00:00
|
|
|
;; don't add end-markers just because the user asked for `block'
|
|
|
|
(unless (or lines (string= "" comment-end)) (setq block nil))
|
|
|
|
|
1999-11-28 18:51:06 +00:00
|
|
|
(cond
|
|
|
|
((consp arg) (uncomment-region beg end))
|
|
|
|
((< numarg 0) (uncomment-region beg end (- numarg)))
|
|
|
|
(t
|
2000-05-19 15:37:41 +00:00
|
|
|
(setq numarg (if (and (null arg) (= (length comment-start) 1))
|
|
|
|
add (1- numarg)))
|
1999-11-28 18:51:06 +00:00
|
|
|
(comment-region-internal
|
|
|
|
beg end
|
1999-12-08 00:19:51 +00:00
|
|
|
(let ((s (comment-padright comment-start numarg)))
|
|
|
|
(if (string-match comment-start-skip s) s
|
|
|
|
(comment-padright comment-start)))
|
|
|
|
(let ((s (comment-padleft comment-end numarg)))
|
|
|
|
(and s (if (string-match comment-end-skip s) s
|
|
|
|
(comment-padright comment-end))))
|
2000-05-14 00:56:10 +00:00
|
|
|
(if multi (comment-padright comment-continue numarg))
|
|
|
|
(if multi (comment-padleft (comment-string-reverse comment-continue) numarg))
|
1999-11-28 18:51:06 +00:00
|
|
|
block
|
2000-05-13 19:41:08 +00:00
|
|
|
lines
|
|
|
|
(nth 3 style))))))
|
|
|
|
|
|
|
|
(defun comment-box (beg end &optional arg)
|
|
|
|
"Comment out the BEG..END region, putting it inside a box.
|
|
|
|
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")
|
2000-05-21 00:27:53 +00:00
|
|
|
(let ((comment-style (if (cadr (assoc comment-style comment-styles))
|
|
|
|
'box-multi 'box)))
|
2000-05-13 19:41:08 +00:00
|
|
|
(comment-region beg end (+ comment-add arg))))
|
1999-11-28 18:51:06 +00:00
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
1999-11-30 16:20:55 +00:00
|
|
|
(defun comment-dwim (arg)
|
2000-05-16 22:02:37 +00:00
|
|
|
"Call the comment command you want (Do What I Mean).
|
|
|
|
If the region is active and `transient-mark-mode' is on, call
|
|
|
|
`comment-region' (unless it only consists in comments, in which
|
|
|
|
case it calls `uncomment-region').
|
1999-11-30 16:20:55 +00:00
|
|
|
Else, if the current line is empty, insert a comment and indent it.
|
2000-05-16 22:02:37 +00:00
|
|
|
Else if a prefix ARG is specified, call `comment-kill'.
|
|
|
|
Else, call `comment-indent'."
|
1999-11-30 16:20:55 +00:00
|
|
|
(interactive "*P")
|
|
|
|
(comment-normalize-vars)
|
2000-05-13 19:41:08 +00:00
|
|
|
(if (and mark-active transient-mark-mode)
|
1999-11-30 16:20:55 +00:00
|
|
|
(let ((beg (min (point) (mark)))
|
|
|
|
(end (max (point) (mark))))
|
|
|
|
(if (save-excursion ;; check for already commented region
|
|
|
|
(goto-char beg)
|
|
|
|
(comment-forward (point-max))
|
|
|
|
(<= end (point)))
|
|
|
|
(uncomment-region beg end arg)
|
|
|
|
(comment-region beg end arg)))
|
|
|
|
(if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
|
2000-05-16 22:02:37 +00:00
|
|
|
;; FIXME: If there's no comment to kill on this line and ARG is
|
|
|
|
;; specified, calling comment-kill is not very clever.
|
|
|
|
(if arg (comment-kill (and (integerp arg) arg)) (comment-indent))
|
1999-11-30 16:20:55 +00:00
|
|
|
(let ((add (if arg (prefix-numeric-value arg)
|
2000-05-13 19:41:08 +00:00
|
|
|
(if (= (length comment-start) 1) comment-add 0))))
|
1999-11-30 16:20:55 +00:00
|
|
|
(insert (comment-padright comment-start add))
|
|
|
|
(save-excursion
|
|
|
|
(unless (string= "" comment-end)
|
|
|
|
(insert (comment-padleft comment-end add)))
|
|
|
|
(indent-according-to-mode))))))
|
|
|
|
|
2000-05-17 19:32:32 +00:00
|
|
|
(defcustom comment-auto-fill-only-comments nil
|
|
|
|
"Non-nil means to only auto-fill inside comments.
|
|
|
|
This has no effect in modes that do not define a comment syntax."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'comment)
|
|
|
|
|
2000-05-25 19:05:46 +00:00
|
|
|
;;;###autoload
|
2000-05-16 22:02:37 +00:00
|
|
|
(defun comment-indent-new-line (&optional soft)
|
1999-11-30 16:20:55 +00:00
|
|
|
"Break line at point and indent, continuing comment if within one.
|
|
|
|
This indents the body of the continued comment
|
|
|
|
under the previous comment line.
|
|
|
|
|
|
|
|
This command is intended for styles where you write a comment per line,
|
|
|
|
starting a new comment (and terminating it if necessary) on each line.
|
|
|
|
If you want to continue one comment across several lines, use \\[newline-and-indent].
|
|
|
|
|
|
|
|
If a fill column is specified, it overrides the use of the comment column
|
|
|
|
or comment indentation.
|
|
|
|
|
|
|
|
The inserted newline is marked hard if variable `use-hard-newlines' is true,
|
|
|
|
unless optional argument SOFT is non-nil."
|
|
|
|
(interactive)
|
|
|
|
(comment-normalize-vars t)
|
2000-05-19 15:37:41 +00:00
|
|
|
(let (compos comin)
|
|
|
|
;; If we are not inside a comment and we only auto-fill comments,
|
|
|
|
;; don't do anything (unless no comment syntax is defined).
|
2000-05-17 19:32:32 +00:00
|
|
|
(unless (and comment-start
|
|
|
|
comment-auto-fill-only-comments
|
|
|
|
(not (save-excursion
|
2000-05-19 15:37:41 +00:00
|
|
|
(prog1 (setq compos (comment-beginning))
|
2000-05-17 19:32:32 +00:00
|
|
|
(setq comin (point))))))
|
2000-05-19 15:37:41 +00:00
|
|
|
|
|
|
|
;; Now we know we should auto-fill.
|
2000-05-17 19:32:32 +00:00
|
|
|
(delete-region (progn (skip-chars-backward " \t") (point))
|
2000-05-19 15:37:41 +00:00
|
|
|
(progn (skip-chars-forward " \t") (point)))
|
2000-05-17 19:32:32 +00:00
|
|
|
(if soft (insert-and-inherit ?\n) (newline 1))
|
|
|
|
(if fill-prefix
|
|
|
|
(progn
|
|
|
|
(indent-to-left-margin)
|
|
|
|
(insert-and-inherit fill-prefix))
|
2000-05-19 15:37:41 +00:00
|
|
|
|
|
|
|
;; If necessary check whether we're inside a comment.
|
|
|
|
(unless (or comment-multi-line compos (null comment-start))
|
2000-05-17 19:32:32 +00:00
|
|
|
(save-excursion
|
|
|
|
(backward-char)
|
2000-05-19 15:37:41 +00:00
|
|
|
(setq compos (comment-beginning))
|
|
|
|
(setq comin (point))))
|
|
|
|
|
|
|
|
;; If we're not inside a comment, just try to indent.
|
|
|
|
(if (not compos) (indent-according-to-mode)
|
|
|
|
(let* ((comment-column
|
|
|
|
;; The continuation indentation should be somewhere between
|
|
|
|
;; the current line's indentation (plus 2 for good measure)
|
|
|
|
;; and the current comment's indentation, with a preference
|
|
|
|
;; for comment-column.
|
|
|
|
(save-excursion
|
|
|
|
(goto-char compos)
|
|
|
|
(min (current-column) (max comment-column
|
|
|
|
(+ 2 (current-indentation))))))
|
|
|
|
(comstart (buffer-substring compos comin))
|
|
|
|
(normalp
|
|
|
|
(string-match (regexp-quote (comment-string-strip
|
|
|
|
comment-start t t))
|
|
|
|
comstart))
|
|
|
|
(comment-end
|
|
|
|
(if normalp comment-end
|
|
|
|
;; The comment starter is not the normal comment-start
|
|
|
|
;; so we can't just use comment-end.
|
|
|
|
(save-excursion
|
|
|
|
(goto-char compos)
|
|
|
|
(if (not (comment-forward)) comment-end
|
|
|
|
(comment-string-strip
|
|
|
|
(buffer-substring
|
|
|
|
(save-excursion (comment-enter-backward) (point))
|
|
|
|
(point))
|
|
|
|
nil t)))))
|
|
|
|
(comment-start comstart)
|
|
|
|
;; Force comment-continue to be recreated from comment-start.
|
|
|
|
(comment-continue nil))
|
|
|
|
(insert-and-inherit ?\n)
|
|
|
|
(forward-char -1)
|
|
|
|
(comment-indent (cadr (assoc comment-style comment-styles)))
|
|
|
|
(save-excursion
|
|
|
|
(let ((pt (point)))
|
|
|
|
(end-of-line)
|
|
|
|
(let ((comend (buffer-substring pt (point))))
|
|
|
|
;; The 1+ is to make sure we delete the \n inserted above.
|
|
|
|
(delete-region pt (1+ (point)))
|
|
|
|
(beginning-of-line)
|
|
|
|
(backward-char)
|
|
|
|
(insert comend)
|
|
|
|
(forward-char))))))))))
|
1999-11-30 16:20:55 +00:00
|
|
|
|
1999-11-28 18:51:06 +00:00
|
|
|
(provide 'newcomment)
|
|
|
|
|
|
|
|
;;; newcomment.el ends here
|