]> code.delx.au - gnu-emacs/blobdiff - lisp/newcomment.el
Add 2008 to copyright years.
[gnu-emacs] / lisp / newcomment.el
index 056b9b97119c54bde031bc48adf99cd15401e34f..b5cd7b05dbd29511578215d92be4a96b30c9c053 100644 (file)
@@ -1,16 +1,17 @@
 ;;; newcomment.el --- (un)comment regions of buffers
 
-;; Copyright (C) 1999, 2000  Free Software Foundation Inc.
+;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: code extracted from Emacs-20's simple.el
-;; Maintainer: Stefan Monnier <monnier@cs.yale.edu>
+;; Maintainer: Stefan Monnier <monnier@iro.umontreal.ca>
 ;; Keywords: comment uncomment
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -20,8 +21,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; 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.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -90,36 +91,43 @@ Major modes should set this variable.")
 
 (defcustom comment-fill-column nil
   "Column to use for `comment-indent'.  If nil, use `fill-column' instead."
-  :type '(choice (const nil) integer))
+  :type '(choice (const nil) integer)
+  :group 'comment)
 
 ;;;###autoload
 (defcustom comment-column 32
-  "*Column to indent right-margin comments to.
-Each mode establishes a different default value for this variable; you
+  "Column to indent right-margin comments to.
+Each mode may establish 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
-not to go beyond `comment-fill-column'."
-  :type 'integer)
+Comments might be indented to a different value in order not to go beyond
+`comment-fill-column' or in order to align them with surrounding comments."
+  :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
@@ -129,6 +137,31 @@ the comment's starting delimiter and should return either the desired
 column indentation or nil.
 If nil is returned, indentation is delegated to `indent-according-to-mode'.")
 
+;;;###autoload
+(defvar comment-insert-comment-function nil
+  "Function to insert a comment when a line doesn't contain one.
+The function has no args.
+
+Applicable at least in modes for languages like fixed-format Fortran where
+comments always start in column zero.")
+
+(defvar comment-region-function 'comment-region-default
+  "Function to comment a region.
+Its args are the same as those of `comment-region', but BEG and END are
+guaranteed to be correctly ordered.  It is called within `save-excursion'.
+
+Applicable at least in modes for languages like fixed-format Fortran where
+comments always start in column zero.")
+
+(defvar uncomment-region-function 'uncomment-region-default
+  "Function to uncomment a region.
+Its args are the same as those of `uncomment-region', but BEG and END are
+guaranteed to be correctly ordered.  It is called within `save-excursion'.
+
+Applicable at least in modes for languages like fixed-format Fortran where
+comments always start in column zero.")
+
+;; ?? never set
 (defvar block-comment-start nil)
 (defvar block-comment-end nil)
 
@@ -161,7 +194,7 @@ two semi-colons.")
     (extra-line        . (t nil t t))
     (box       . (nil t t t))
     (box-multi . (t t t t)))
-  "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
+  "Comment region 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.
@@ -172,11 +205,13 @@ 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))
-         'symbol))
+           `(choice ,@(mapcar (lambda (s) `(const ,(car s)))
+                              comment-styles))
+         'symbol)
+  :group 'comment)
 
 ;;;###autoload
 (defcustom comment-padding " "
@@ -186,13 +221,30 @@ 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."
-  :type '(choice string integer (const nil)))
+  :type '(choice string integer (const nil))
+  :group 'comment)
 
 ;;;###autoload
 (defcustom comment-multi-line nil
-  "*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)
+  "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.
+
+It also affects \\[indent-new-comment-line].  However, if you want this
+behavior for explicit filling, you might as well use \\[newline-and-indent]."
+  :type 'boolean
+  :group 'comment)
+
+(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
+terminated by the end of line (i.e. `comment-end' is empty)."
+  :type '(choice (const :tag "Never" nil)
+         (const :tag "Always" t)
+         (const :tag "EOl-terminated" 'eol))
+  :group 'comment)
 
 ;;;;
 ;;;; Helpers
@@ -211,10 +263,16 @@ This is obsolete because you might as well use \\[newline-and-indent]."
 
 ;;;###autoload
 (defun comment-normalize-vars (&optional noerror)
-  (if (not comment-start)
-      (unless noerror
-       (set (make-local-variable 'comment-start)
-            (read-string "No comment syntax is defined.  Use: ")))
+  "Check and setup the variables needed by other commenting functions.
+Functions autoloaded from newcomment.el, being entry points, should call
+this function before any other, so the rest of the code can assume that
+the variables are properly set."
+  (unless (and (not comment-start) noerror)
+    (unless comment-start
+      (let ((cs (read-string "No comment syntax is defined.  Use: ")))
+       (if (zerop (length cs))
+           (error "No comment syntax defined")
+         (set (make-local-variable 'comment-start) cs))))
     ;; comment-use-syntax
     (when (eq comment-use-syntax 'undecided)
       (set (make-local-variable 'comment-use-syntax)
@@ -242,7 +300,7 @@ This is obsolete because you might as well use \\[newline-and-indent]."
                   (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
@@ -305,12 +363,18 @@ If UNP is non-nil, unquote nested comment markers."
 ;;;; Navigation
 ;;;;
 
-;;;###autoload
+(defvar comment-use-global-state nil
+  "Non-nil means that the global syntactic context is used.
+More specifically, it means that `syntax-ppss' is used to find out whether
+point is within a string or not.  Major modes whose syntax is faithfully
+described by the syntax-tables can set this to non-nil so comment markers
+in strings will not confuse Emacs.")
+
 (defun comment-search-forward (limit &optional noerror)
   "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
-and raises an error or returns nil of NOERROR is non-nil."
+and raises an error or returns nil if NOERROR is non-nil."
   (if (not comment-use-syntax)
       (if (re-search-forward comment-start-skip limit noerror)
          (or (match-end 1) (match-beginning 0))
@@ -318,15 +382,19 @@ and raises an error or returns nil of NOERROR is non-nil."
        (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)))
-      (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))))
+          (s (parse-partial-sexp pt (or limit (point-max)) nil nil
+                                 (if comment-use-global-state (syntax-ppss pt))
+                                 t)))
+      (when (and (nth 8 s) (nth 3 s) (not comment-use-global-state))
+       ;; The search ended at eol 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 (or (not (and (nth 8 s) (not (nth 3 s))))
+             ;; Make sure the comment starts after PT.
+             (< (nth 8 s) pt))
          (unless noerror (error "No comment"))
        ;; We found the comment.
        (let ((pos (point))
@@ -344,7 +412,7 @@ and raises an error or returns nil of NOERROR is non-nil."
   "Find a comment start between LIMIT and point.
 Moves point to inside the comment and returns the position of the
 comment-starter.  If no comment is found, moves point to LIMIT
-and raises an error or returns nil of NOERROR is non-nil."
+and raises an error or returns nil if NOERROR is non-nil."
   ;; 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
@@ -370,7 +438,7 @@ and raises an error or returns nil of NOERROR is non-nil."
 (defun comment-beginning ()
   "Find the beginning of the enclosing comment.
 Returns nil if not inside a comment, else moves point and returns
-the same as `comment-search-forward'."
+the same as `comment-search-backward'."
   ;; HACK ATTACK!
   ;; We should really test `in-string-p' but that can be expensive.
   (unless (eq (get-text-property (point) 'face) 'font-lock-string-face)
@@ -382,7 +450,7 @@ the same as `comment-search-forward'."
              (and
               ;; For modes where comment-start and comment-end are the same,
               ;; the search above may have found a `ce' rather than a `cs'.
-              (or (not (looking-at comment-end-skip))
+              (or (if comment-end-skip (not (looking-at comment-end-skip)))
                   ;; Maybe font-lock knows that it's a `cs'?
                   (eq (get-text-property (match-end 0) 'face)
                       'font-lock-comment-face)
@@ -418,19 +486,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
@@ -443,12 +514,64 @@ Point is assumed to be just at the end of a comment."
           (or (match-end 1) (/= (current-column) (current-indentation))))
       0
     (when (or (/= (current-column) (current-indentation))
-             (and (> comment-add 0) (looking-at "\\s<\\S<")))
+             (and (> comment-add 0) (looking-at "\\s<\\(\\S<\\|\\'\\)")))
       comment-column)))
 
+(defun comment-choose-indent (&optional indent)
+  "Choose the indentation to use for a right-hand-side comment.
+The criteria are (in this order):
+- try to keep the comment's text within `comment-fill-column'.
+- try to align with surrounding comments.
+- prefer INDENT (or `comment-column' if nil).
+Point is expected to be at the start of the comment."
+  (unless indent (setq indent comment-column))
+  ;; Avoid moving comments past the fill-column.
+  (let ((max (+ (current-column)
+                (- (or comment-fill-column fill-column)
+                   (save-excursion (end-of-line) (current-column)))))
+        (other nil)
+        (min (save-excursion (skip-chars-backward " \t")
+                             (1+ (current-column)))))
+    ;; Fix up the range.
+    (if (< max min) (setq max min))
+    ;; Don't move past the fill column.
+    (if (<= max indent) (setq indent max))
+    ;; We can choose anywhere between min..max.
+    ;; Let's try to align to a comment on the previous line.
+    (save-excursion
+      (when (and (zerop (forward-line -1))
+                 (setq other (comment-search-forward
+                              (line-end-position) t)))
+        (goto-char other) (setq other (current-column))))
+    (if (and other (<= other max) (>= other min))
+        ;; There is a comment and it's in the range: bingo!
+        other
+      ;; Can't align to a previous comment: let's try to align to comments
+      ;; on the following lines, then.  These have not been re-indented yet,
+      ;; so we can't directly align ourselves with them.  All we do is to try
+      ;; and choose an indentation point with which they will be able to
+      ;; align themselves.
+      (save-excursion
+        (while (and (zerop (forward-line 1))
+                    (setq other (comment-search-forward
+                                 (line-end-position) t)))
+          (goto-char other)
+          (let ((omax (+ (current-column)
+                         (- (or comment-fill-column fill-column)
+                            (save-excursion (end-of-line) (current-column)))))
+                (omin (save-excursion (skip-chars-backward " \t")
+                                      (1+ (current-column)))))
+            (if (and (>= omax min) (<= omin max))
+                (progn (setq min (max omin min))
+                       (setq max (min omax max)))
+              ;; Can't align with this anyway, so exit the loop.
+              (goto-char (point-max))))))
+      ;; Return the closest point to indent within min..max.
+      (max min (min max indent)))))
+
 ;;;###autoload
 (defun comment-indent (&optional continue)
-  "Indent this line's comment to comment column, or insert an empty comment.
+  "Indent this line's comment to `comment-column', or insert an empty comment.
 If CONTINUE is non-nil, use the `comment-continue' markers if any."
   (interactive "*")
   (comment-normalize-vars)
@@ -474,36 +597,44 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
                (forward-char (/ (skip-chars-backward " \t") -2)))
            (setq cpos (point-marker)))
          ;; If none, insert one.
+       (if comment-insert-comment-function
+           (funcall comment-insert-comment-function)
          (save-excursion
-           ;; Some comment-indent-function insist on not moving comments that
-           ;; are in column 0, so we first go to the likely target column.
+           ;; Some `comment-indent-function's insist on not moving
+           ;; comments that are in column 0, so we first go to the
+           ;; likely target column.
            (indent-to comment-column)
-           (setq begpos (point))
            ;; Ensure there's a space before the comment for things
            ;; like sh where it matters (as well as being neater).
-           (unless (eq ?\  (char-syntax (char-before)))
-             (insert ?\ ))
+           (unless (memq (char-before) '(nil ?\n ?\t ?\s))
+             (insert ?\s))
+           (setq begpos (point))
            (insert starter)
            (setq cpos (point-marker))
-           (insert ender)))
+           (insert ender))))
       (goto-char begpos)
       ;; Compute desired indent.
       (setq indent (save-excursion (funcall comment-indent-function)))
+      ;; If `indent' is nil and there's code before the comment, we can't
+      ;; use `indent-according-to-mode', so we default to comment-column.
+      (unless (or indent (save-excursion (skip-chars-backward " \t") (bolp)))
+       (setq indent comment-column))
       (if (not indent)
-         ;; comment-indent-function refuses: delegate to indent.
+         ;; comment-indent-function refuses: delegate to line-indent.
          (indent-according-to-mode)
-       ;; Avoid moving comments past the fill-column.
+       ;; If the comment is at the right of code, adjust the indentation.
        (unless (save-excursion (skip-chars-backward " \t") (bolp))
-         (setq indent
-               (min indent
-                    (+ (current-column)
-                       (- (or comment-fill-column fill-column)
-                          (save-excursion (end-of-line) (current-column)))))))
+          (setq indent (comment-choose-indent indent)))
+       ;; 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))))
 
@@ -518,6 +649,7 @@ With any other arg, set comment column to indentation of the previous comment
   (cond
    ((eq arg '-) (comment-kill nil))
    (arg
+    (comment-normalize-vars)
     (save-excursion
       (beginning-of-line)
       (comment-search-backward)
@@ -534,6 +666,7 @@ With any other arg, set comment column to indentation of the previous comment
   "Kill the comment on this line, if any.
 With prefix ARG, kill comments on that many lines starting with this one."
   (interactive "P")
+  (comment-normalize-vars)
   (dotimes (_ (prefix-numeric-value arg))
     (save-excursion
       (beginning-of-line)
@@ -627,93 +760,109 @@ The numeric prefix ARG can specify a number of chars to remove from the
 comment markers."
   (interactive "*r\nP")
   (comment-normalize-vars)
-  (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
-  (save-excursion
-    (goto-char beg)
-    (setq end (copy-marker end))
-    (let* ((numarg (prefix-numeric-value arg))
-           (ccs comment-continue)
-           (srei (comment-padright ccs 're))
-           (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
-           spt)
-      (while (and (< (point) end)
-                 (setq spt (comment-search-forward end t)))
-       (let ((ipt (point))
-              ;; Find the end of the comment.
-              (ept (progn
-                     (goto-char spt)
-                     (unless (comment-forward)
-                       (error "Can't find the comment end"))
-                     (point)))
-              (box nil)
-              (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 " ")
-           ;; A box-comment starts with a looong comment-start marker.
-           (when (and (or (and (= (- (point) (point-min)) 1)
-                               (setq box-equal t)
-                               (looking-at "=\\{7\\}")
-                               (not (eq (char-before (point-max)) ?\n))
-                               (skip-chars-forward "="))
-                          (> (- (point) (point-min) (length comment-start)) 7))
-                      (> (count-lines (point-min) (point-max)) 2))
-             (setq box t))
-           (when (looking-at (regexp-quote comment-padding))
-             (goto-char (match-end 0)))
-           (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
+  (when (> beg end) (setq beg (prog1 end (setq end beg))))
+  ;; Bind `comment-use-global-state' to nil.  While uncommenting a region
+  ;; (which works a line at a time), a comment can appear to be
+  ;; included in a mult-line string, but it is actually not.
+  (let ((comment-use-global-state nil))
+    (save-excursion
+      (funcall uncomment-region-function beg end arg))))
+
+(defun uncomment-region-default (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
+comment markers."
+  (goto-char beg)
+  (setq end (copy-marker end))
+  (let* ((numarg (prefix-numeric-value arg))
+        (ccs comment-continue)
+        (srei (comment-padright ccs 're))
+        (csre (comment-padright comment-start 're))
+        (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
+        spt)
+    (while (and (< (point) end)
+               (setq spt (comment-search-forward end t)))
+      (let ((ipt (point))
+           ;; Find the end of the comment.
+           (ept (progn
+                  (goto-char spt)
+                  (unless (or (comment-forward)
+                              ;; Allow non-terminated comments.
+                              (eobp))
+                    (error "Can't find the comment end"))
+                  (point)))
+           (box nil)
+           (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 " ")
+         ;; A box-comment starts with a looong comment-start marker.
+         (when (and (or (and (= (- (point) (point-min)) 1)
+                             (setq box-equal t)
+                             (looking-at "=\\{7\\}")
+                             (not (eq (char-before (point-max)) ?\n))
+                             (skip-chars-forward "="))
+                        (> (- (point) (point-min) (length comment-start)) 7))
+                    (> (count-lines (point-min) (point-max)) 2))
+           (setq box t))
+         ;; Skip the padding.  Padding can come from comment-padding and/or
+         ;; from comment-start, so we first check comment-start.
+         (if (or (save-excursion (goto-char (point-min)) (looking-at csre))
+                 (looking-at (regexp-quote comment-padding)))
              (goto-char (match-end 0)))
-           (if (null arg) (delete-region (point-min) (point))
-             (skip-syntax-backward " ")
-             (delete-char (- numarg))
-             (unless (or (bobp)
-                         (save-excursion (goto-char (point-min))
-                                         (looking-at comment-start-skip)))
+         (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
+           (goto-char (match-end 0)))
+         (if (null arg) (delete-region (point-min) (point))
+           (skip-syntax-backward " ")
+           (delete-char (- numarg))
+           (unless (or (bobp)
+                       (save-excursion (goto-char (point-min))
+                                       (looking-at comment-start-skip)))
+             ;; 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.
+         (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
+           (let ((pos (point)))
+             ;; skip `=' but only if there are at least 7.
+             (when (> (skip-chars-backward "=") -7) (goto-char pos))))
+         (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
+           (when (and (bolp) (not (bobp))) (backward-char))
+           (if (null arg) (delete-region (point) (point-max))
+             (skip-syntax-forward " ")
+             (delete-char numarg)
+             (unless (or (eobp) (looking-at comment-end-skip))
                ;; 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.
-           (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
-             (let ((pos (point)))
-               ;; skip `=' but only if there are at least 7.
-               (when (> (skip-chars-backward "=") -7) (goto-char pos))))
-           (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
-             (when (and (bolp) (not (bobp))) (backward-char))
-             (if (null arg) (delete-region (point) (point-max))
-               (skip-syntax-forward " ")
-               (delete-char numarg)
-               (unless (or (eobp) (looking-at comment-end-skip))
-                 ;; If there's something left but it doesn't look like
-                 ;; a comment-end any more, just remove it.
-                 (delete-region (point) (point-max)))))
-
-           ;; 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-*$"))))
-               (goto-char (point-min))
-               (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))
-                 (replace-match "" t t nil (if (match-end 2) 2 1)))))
-           ;; Go to the end for the next comment.
-           (goto-char (point-max)))))
-      (set-marker end nil))))
+               ;; a comment-end any more, just remove it.
+               (delete-region (point) (point-max)))))
+
+         ;; 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-*$"))))
+             (goto-char (point-min))
+             (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))
+               (replace-match "" t t nil (if (match-end 2) 2 1)))))
+         ;; Go to the end for the next comment.
+         (goto-char (point-max))))))
+  (set-marker end nil))
 
 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
   "Make the leading and trailing extra lines.
@@ -759,9 +908,9 @@ Space is added (and then removed) at the beginning for the text's
 indentation to be kept as it was before narrowing."
   (declare (debug t) (indent 2))
   (let ((bindent (make-symbol "bindent")))
-    `(let ((,bindent (save-excursion (goto-char beg) (current-column))))
+    `(let ((,bindent (save-excursion (goto-char ,beg) (current-column))))
        (save-restriction
-        (narrow-to-region beg end)
+        (narrow-to-region ,beg ,end)
         (goto-char (point-min))
         (insert (make-string ,bindent ? ))
         (prog1
@@ -782,20 +931,26 @@ 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)
+                                &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
-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."
+CS and CE are the comment start string and comment end string,
+respectively.  CCS and CCE are the comment continuation strings
+for the start and end of lines, respectively (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."
   ;;(assert (< beg end))
-  (let ((no-empty t))
+  (let ((no-empty (not (or (eq comment-empty-lines t)
+                          (and comment-empty-lines (zerop (length ce)))))))
     ;; Sanitize CE and CCE.
     (if (and (stringp ce) (string= "" ce)) (setq ce nil))
     (if (and (stringp cce) (string= "" cce)) (setq cce nil))
@@ -832,9 +987,13 @@ rather than at left margin."
                (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).
          (setq max-indent
-               (+ max-indent (max (length cs) (length ccs)) tab-width -1))
+               (+ max-indent (max (length cs) (length ccs))
+                   ;; Inserting ccs can change max-indent by (1- tab-width)
+                   ;; but only if there are TABs in the boxed text, of course.
+                   (if (save-excursion (goto-char beg)
+                                       (search-forward "\t" end t))
+                       (1- tab-width) 0)))
          (unless indent (setq min-indent 0))
 
          ;; make the leading and trailing lines if requested
@@ -864,7 +1023,7 @@ rather than at left margin."
 (defun comment-region (beg end &optional arg)
   "Comment or uncomment each line in the region.
 With just \\[universal-argument] prefix arg, uncomment each line in region BEG .. END.
-Numeric prefix arg ARG means use ARG comment characters.
+Numeric prefix ARG means use ARG comment characters.
 If ARG is negative, delete that many comment characters instead.
 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
@@ -875,33 +1034,37 @@ The strings used as comment starts are built from
   (interactive "*r\nP")
   (comment-normalize-vars)
   (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
+  (save-excursion
+    ;; FIXME: maybe we should call uncomment depending on ARG.
+    (funcall comment-region-function beg end arg)))
+
+(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))
         (multi (nth 0 style)))
-    (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
-            lines ;; multi
-            (progn (goto-char beg) (beginning-of-line)
-                   (skip-syntax-forward " ")
-                   (>= (point) beg))
-            (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
-                   (<= (point) end))
-            (or block (not (string= "" comment-end)))
-            (or block (progn (goto-char beg) (search-forward "\n" end t))))))
+
+    ;; 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
+          lines ;; multi
+          (progn (goto-char beg) (beginning-of-line)
+                 (skip-syntax-forward " ")
+                 (>= (point) beg))
+          (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
+                 (<= (point) end))
+          (or block (not (string= "" comment-end)))
+          (or block (progn (goto-char beg) (search-forward "\n" end t)))))
 
     ;; don't add end-markers just because the user asked for `block'
     (unless (or lines (string= "" comment-end)) (setq block nil))
@@ -910,8 +1073,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)))
@@ -921,16 +1083,19 @@ The strings used as comment starts are built from
         (and s (if (string-match comment-end-skip s) s
                  (comment-padright comment-end))))
        (if multi (comment-padright comment-continue numarg))
-       (if multi (comment-padleft (comment-string-reverse comment-continue) numarg))
+       (if multi
+          (comment-padleft (comment-string-reverse comment-continue) numarg))
        block
        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))))
@@ -942,6 +1107,7 @@ end- comment markers additionally to what `comment-add' already specifies."
 in which case call `uncomment-region'.  If a prefix arg is given, it
 is passed on to the respective function."
   (interactive "*r\nP")
+  (comment-normalize-vars)
   (funcall (if (save-excursion ;; check for already commented region
                 (goto-char beg)
                 (comment-forward (point-max))
@@ -957,7 +1123,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)
@@ -966,9 +1133,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))
@@ -977,18 +1143,52 @@ Else, call `comment-indent'."
            (insert (comment-padleft comment-end add)))
          (indent-according-to-mode))))))
 
+;;;###autoload
 (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)
-
-(defun comment-valid-prefix (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 "\\)")
-                fill-prefix)))
+  :type 'boolean
+  :group 'comment)
+
+(defun comment-valid-prefix-p (prefix compos)
+    "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)
@@ -1041,8 +1241,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 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.
@@ -1098,4 +1297,5 @@ unless optional argument SOFT is non-nil."
 
 (provide 'newcomment)
 
+;; arch-tag: 01e3320a-00c8-44ea-a696-8f8e7354c858
 ;;; newcomment.el ends here