]> code.delx.au - gnu-emacs/blobdiff - lisp/newcomment.el
(add-to-history): New function.
[gnu-emacs] / lisp / newcomment.el
index 7716a70435948e60c53316206390d1a1ab96176b..d5a2cea914afd534453cf613d36c97b8705b46cf 100644 (file)
@@ -1,7 +1,7 @@
 ;;; newcomment.el --- (un)comment regions of buffers
 
 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
-;;   2005 Free Software Foundation, Inc.
+;;   2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: code extracted from Emacs-20's simple.el
 ;; Maintainer: Stefan Monnier <monnier@iro.umontreal.ca>
@@ -96,7 +96,7 @@ Major modes should set this variable.")
 
 ;;;###autoload
 (defcustom comment-column 32
-  "*Column to indent right-margin comments to.
+  "Column to indent right-margin comments to.
 Each mode establishes a different default value for this variable; you
 can set the value for a particular mode using that mode's hook.
 Comments might be indented to a value smaller than this in order
@@ -108,21 +108,25 @@ not to go beyond `comment-fill-column'."
 ;;;###autoload
 (defvar comment-start nil
   "*String to insert to start a new comment, or nil if no comment syntax.")
+;;;###autoload(put 'comment-start 'safe-local-variable 'string-or-null-p)
 
 ;;;###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(put 'comment-start-skip 'safe-local-variable 'string-or-null-p)
 
 ;;;###autoload
 (defvar comment-end-skip nil
   "Regexp to match the end of a comment plus everything up to its body.")
+;;;###autoload(put 'comment-end-skip 'safe-local-variable 'string-or-null-p)
 
 ;;;###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.")
+;;;###autoload(put 'comment-end 'safe-local-variable 'string-or-null-p)
 
 ;;;###autoload
 (defvar comment-indent-function 'comment-indent-default
@@ -200,7 +204,7 @@ INDENT specifies that the `comment-start' markers should not be put at the
 
 ;;;###autoload
 (defcustom comment-style 'plain
-  "*Style to be used for `comment-region'.
+  "Style to be used for `comment-region'.
 See `comment-styles' for a list of available styles."
   :type (if (boundp 'comment-styles)
            `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles))
@@ -220,7 +224,7 @@ makes the comment easier to read.  Default is 1.  nil means 0."
 
 ;;;###autoload
 (defcustom comment-multi-line nil
-  "*Non-nil means `comment-indent-new-line' continues comments.
+  "Non-nil means `comment-indent-new-line' continues comments.
 That is, it inserts no new terminator or starter.
 This affects `auto-fill-mode', which is the main reason to
 customize this variable.
@@ -480,8 +484,8 @@ Point is assumed to be just at the end of a comment."
       (progn (backward-char) (skip-syntax-backward " "))
     (cond
      ((save-restriction
-        (beginning-of-line)
-        (narrow-to-region (point) end)
+        (narrow-to-region (line-beginning-position) (point))
+        (goto-char (point-min))
         (re-search-forward (concat comment-end-skip "\\'") nil t))
       (goto-char (match-beginning 0)))
      ;; comment-end-skip not found.  Maybe we're at EOB which implicitly
@@ -895,6 +899,11 @@ indentation to be kept as it was before narrowing."
                   (delete-char n)
                   (setq ,bindent (- ,bindent n)))))))))))
 
+(defun comment-add (arg)
+  (if (and (null arg) (= (string-match "[ \t]*\\'" comment-start) 1))
+      comment-add
+    (1- (prefix-numeric-value arg))))
+
 (defun comment-region-internal (beg end cs ce
                                 &optional ccs cce block lines indent)
   "Comment region BEG .. END.
@@ -999,7 +1008,6 @@ The strings used as comment starts are built from
 
 (defun comment-region-default (beg end &optional arg)
   (let* ((numarg (prefix-numeric-value arg))
-        (add comment-add)
         (style (cdr (assoc comment-style comment-styles)))
         (lines (nth 2 style))
         (block (nth 1 style))
@@ -1032,8 +1040,7 @@ The strings used as comment starts are built from
      ((consp arg) (uncomment-region beg end))
      ((< numarg 0) (uncomment-region beg end (- numarg)))
      (t
-      (setq numarg (if (and (null arg) (= (length comment-start) 1))
-                      add (1- numarg)))
+      (setq numarg (comment-add arg))
       (comment-region-internal
        beg end
        (let ((s (comment-padright comment-start numarg)))
@@ -1081,7 +1088,8 @@ If the region is active and `transient-mark-mode' is on, call
   case it calls `uncomment-region').
 Else, if the current line is empty, insert a comment and indent it.
 Else if a prefix ARG is specified, call `comment-kill'.
-Else, call `comment-indent'."
+Else, call `comment-indent'.
+You can configure `comment-style' to change the way regions are commented."
   (interactive "*P")
   (comment-normalize-vars)
   (if (and mark-active transient-mark-mode)
@@ -1090,9 +1098,8 @@ Else, call `comment-indent'."
        ;; 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))
-      (let ((add (if arg (prefix-numeric-value arg)
-                  (if (= (length comment-start) 1) comment-add 0))))
-       ;; Some modes insist on keeping column 0 comment in column 0
+      (let ((add (comment-add arg)))
+        ;; Some modes insist on keeping column 0 comment in column 0
        ;; so we need to move away from it before inserting the comment.
        (indent-according-to-mode)
        (insert (comment-padright comment-start add))