]> code.delx.au - gnu-emacs/blobdiff - lisp/newcomment.el
(calendar-dst-check-each-year-flag): Avoid
[gnu-emacs] / lisp / newcomment.el
index 938cd5fe141cc899e4ec381c729ea2ccb32a350f..9d089a2e164045056200bc950e7042eb2033c6f2 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
@@ -104,25 +104,30 @@ not to go beyond `comment-fill-column'."
   :type 'integer
   :group 'comment)
 (make-variable-buffer-local 'comment-column)
+;;;###autoload(put 'comment-column 'safe-local-variable 'integerp)
 
 ;;;###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 +205,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 +225,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.
@@ -233,7 +238,7 @@ behavior for explicit filling, you might as well use \\[newline-and-indent]."
 (defcustom comment-empty-lines nil
   "If nil, `comment-region' does not comment out empty lines.
 If t, it always comments out empty lines.
-if `eol' it only comments out empty lines if comments are
+If `eol' it only comments out empty lines if comments are
 terminated by the end of line (i.e. `comment-end' is empty)."
   :type '(choice (const :tag "Never" nil)
          (const :tag "Always" t)
@@ -294,7 +299,7 @@ the variables are properly set."
                   (substring comment-start 1)))
       ;; Hasn't been necessary yet.
       ;; (unless (string-match comment-start-skip comment-continue)
-      ;;       (kill-local-variable 'comment-continue))
+      ;;       (kill-local-variable 'comment-continue))
       )
     ;; comment-skip regexps
     (unless (and comment-start-skip
@@ -478,19 +483,22 @@ Point is assumed to be just at the end of a comment."
   (if (bolp)
       ;; comment-end = ""
       (progn (backward-char) (skip-syntax-backward " "))
-    (let ((end (point)))
-      (beginning-of-line)
-      (save-restriction
-       (narrow-to-region (point) end)
-       (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 " "))))))
+    (cond
+     ((save-restriction
+        (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
+     ;; closes the comment.
+     ((eobp) (skip-syntax-backward " "))
+     (t
+      ;; else comment-end-skip was 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.
+      (backward-char 2)
+      (skip-chars-backward (string (char-after)))
+      (skip-syntax-backward " ")))))
 
 ;;;;
 ;;;; Commands
@@ -591,11 +599,16 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
                    (if (and other (<= other max) (> other min))
                        ;; There is a comment and it's in the range: bingo.
                        (setq indent other))))))))
+       ;; Update INDENT to leave at least one space
+       ;; after other nonwhite text on the line.
+       (save-excursion
+         (skip-chars-backward " \t")
+         (unless (bolp)
+           (setq indent (max indent (1+ (current-column))))))
+       ;; If that's different from comment's current position, change it.
        (unless (= (current-column) indent)
-         ;; If that's different from current, change it.
          (delete-region (point) (progn (skip-chars-backward " \t") (point)))
-         (indent-to (if (bolp) indent
-                      (max indent (1+ (current-column)))))))
+         (indent-to indent)))
       (goto-char cpos)
       (set-marker cpos nil))))
 
@@ -756,7 +769,7 @@ comment markers."
            (box-equal nil))       ;Whether we might be using `=' for boxes.
        (save-restriction
          (narrow-to-region spt ept)
-               
+
          ;; Remove the comment-start.
          (goto-char ipt)
          (skip-syntax-backward " ")
@@ -785,7 +798,7 @@ comment markers."
              ;; If there's something left but it doesn't look like
              ;; a comment-start any more, just remove it.
              (delete-region (point-min) (point))))
-               
+
          ;; Remove the end-comment (and leading padding and such).
          (goto-char (point-max)) (comment-enter-backward)
          ;; Check for special `=' used sometimes in comment-box.
@@ -892,6 +905,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.
@@ -996,7 +1014,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))
@@ -1029,8 +1046,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)))
@@ -1046,11 +1062,13 @@ The strings used as comment starts are built from
        lines
        (nth 3 style))))))
 
+;;;###autoload
 (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")
+  (comment-normalize-vars)
   (let ((comment-style (if (cadr (assoc comment-style comment-styles))
                           'box-multi 'box)))
     (comment-region beg end (+ comment-add arg))))
@@ -1078,7 +1096,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)
@@ -1087,9 +1106,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))
@@ -1106,12 +1124,44 @@ This has no effect in modes that do not define a comment syntax."
   :group 'comment)
 
 (defun comment-valid-prefix-p (prefix compos)
-  (or
-   ;; Accept any prefix if the current comment is not EOL-terminated.
-   (save-excursion (goto-char compos) (comment-forward) (not (bolp)))
-   ;; Accept any prefix that starts with a comment-start marker.
-   (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)")
-                prefix)))
+    "Check that the adaptive-fill-prefix is consistent with the context.
+PREFIX is the prefix (presumably guessed by `adaptive-fill-mode').
+COMPOS is the position of the beginning of the comment we're in, or nil
+if we're not inside a comment."
+  ;; This consistency checking is mostly needed to workaround the limitation
+  ;; of auto-fill-mode whose paragraph-determination doesn't pay attention
+  ;; to comment boundaries.
+  (if (null compos)
+      ;; We're not inside a comment: the prefix shouldn't match
+      ;; a comment-starter.
+      (not (and comment-start comment-start-skip
+                (string-match comment-start-skip prefix)))
+    (or
+     ;; Accept any prefix if the current comment is not EOL-terminated.
+     (save-excursion (goto-char compos) (comment-forward) (not (bolp)))
+     ;; Accept any prefix that starts with the same comment-start marker
+     ;; as the current one.
+     (when (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)")
+                         prefix)
+       (let ((prefix-com (comment-string-strip (match-string 0 prefix) nil t)))
+         (string-match "\\`[ \t]*" prefix-com)
+         (let* ((prefix-space (match-string 0 prefix-com))
+                (prefix-indent (string-width prefix-space))
+                (prefix-comstart (substring prefix-com (match-end 0))))
+           (save-excursion
+             (goto-char compos)
+             ;; The comstart marker is the same.
+             (and (looking-at (regexp-quote prefix-comstart))
+                  ;; The indentation as well.
+                  (or (= prefix-indent
+                         (- (current-column) (current-left-margin)))
+                      ;; Check the indentation in two different ways, just
+                      ;; to try and avoid most of the potential funny cases.
+                      (equal prefix-space
+                             (buffer-substring (point)
+                                               (progn (move-to-left-margin)
+                                                      (point)))))))))))))
+                    
 
 ;;;###autoload
 (defun comment-indent-new-line (&optional soft)
@@ -1164,8 +1214,7 @@ unless optional argument SOFT is non-nil."
         ;; If there's an adaptive prefix, use it unless we're inside
         ;; a comment and the prefix is not a comment starter.
         ((and fill-prefix
-              (or (not compos)
-                  (comment-valid-prefix-p fill-prefix compos)))
+               (comment-valid-prefix-p fill-prefix compos))
          (indent-to-left-margin)
          (insert-and-inherit fill-prefix))
         ;; If we're not inside a comment, just try to indent.