Format and doc changes to bring closer to xterm.c.
[bpt/emacs.git] / lisp / newcomment.el
index b08d2d2..889f888 100644 (file)
@@ -2,25 +2,28 @@
 
 ;; Copyright (C) 1999-2000  Free Software Foundation Inc.
 
-;; Author: FSF??
+;; Author: code extracted from Emacs-20's simple.el
 ;; Maintainer: Stefan Monnier <monnier@cs.yale.edu>
 ;; Keywords: comment uncomment
 ;; Version: $Name:  $
-;; Revision: $Id: newcomment.el,v 1.9 2000/05/16 22:02:37 monnier Exp $
+;; Revision: $Id: newcomment.el,v 1.14 2000/05/23 20:06:10 monnier Exp $
 
-;; This program is free software; you can redistribute it and/or modify
+;; 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 of the License, or
-;; (at your option) any later version.
-;; 
-;; This program is distributed in the hope that it will be useful,
+;; 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,
 ;; 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.
-;; 
+
 ;; You should have received a copy of the GNU General Public License
-;; along with this program; if not, write to the Free Software
-;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+;; 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.
 
 ;;; Commentary:
 
 ;; - comment-box in TeXinfo generate bogus comments @ccccc@
 ;; - uncomment-region with a numeric argument can render multichar
 ;;   comment markers invalid.
+;; - comment-indent or comment-region when called inside a comment
+;;   will happily break the surrounding comment.
+;; - comment-quote-nested will not (un)quote properly all nested comment
+;;   markers if there are more than just comment-start and comment-end.
+;;   For example, in Pascal where {...*) and (*...} are possible.
 
 ;;; Todo:
 
 ;; - 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.
-;; - (from c-comment-line-break-function): c-ignore-auto-fill so that you can
-;;   auto-fill comments but not code.  This should probably be moved directly
-;;   inside do-auto-fill.
 
 ;;; Code:
 
-(eval-when-compile (require 'cl))
+;;;###autoload
+(defalias 'indent-for-comment 'comment-indent)
+;;;###autoload
+(defalias 'set-comment-column 'comment-set-column)
+;;;###autoload
+(defalias 'kill-comment 'comment-kill)
+;;;###autoload
+(defalias 'indent-new-comment-line 'comment-indent-new-line)
 
+;;;###autoload
 (defgroup comment nil
   "Indenting and filling of comments."
   :prefix "comment-"
@@ -66,6 +79,7 @@ be used to try to determine whether syntax-tables should be trusted
 to understand comments or not in the given buffer.
 Major modes should set this variable.")
 
+;;;###autoload
 (defcustom comment-column 32
   "*Column to indent right-margin comments to.
 Setting this variable automatically makes it local to the current buffer.
@@ -75,26 +89,26 @@ can set the value for a particular mode using that mode's hook."
   :group 'comment)
 (make-variable-buffer-local 'comment-column)
 
+;;;###autoload
 (defvar comment-start nil
   "*String to insert to start a new comment, or nil if no comment syntax.")
 
+;;;###autoload
 (defvar comment-start-skip nil
   "*Regexp to match the start of a comment plus everything up to its body.
 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
 at the place matched by the close of the first pair.")
 
+;;;###autoload
 (defvar comment-end-skip nil
   "Regexp to match the end of a comment plus everything up to its body.")
 
+;;;###autoload
 (defvar comment-end ""
   "*String to insert to end a new comment.
 Should be an empty string if comments are terminated by end-of-line.")
 
-(defvar comment-indent-hook nil
-  "Obsolete variable for function to compute desired indentation for a comment.
-This function is called with no args with point at the beginning of
-the comment's starting delimiter.")
-
+;;;###autoload
 (defvar comment-indent-function
   (lambda () comment-column)
   "Function to compute desired indentation for a comment.
@@ -131,7 +145,8 @@ two semi-colons.")
     (aligned   . (nil t nil t))
     (multi-line        . (t nil nil t))
     (extra-line        . (t nil t t))
-    (box       . (t t t t)))
+    (box       . (nil t t t))
+    (box-multi . (t t t t)))
   "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
 STYLE should be a mnemonic symbol.
 MULTI specifies that comments are allowed to span multiple lines.
@@ -141,13 +156,17 @@ EXTRA specifies that an extra line should be used before and after the
 INDENT specifies that the `comment-start' markers should not be put at the
   left margin but at the current indentation of the region to comment.")
 
+;;;###autoload
 (defcustom comment-style 'plain
   "*Style to be used for `comment-region'.
 See `comment-styles' for a list of available styles."
   :group 'comment
-  :type `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles)))
+  :type (if (boundp 'comment-styles)
+           `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles))
+         'symbol))
 
-(defcustom comment-padding 1
+;;;###autoload
+(defcustom comment-padding " "
   "Padding string that `comment-region' puts between comment chars and text.
 Can also be an integer which will be automatically turned into a string
 of the corresponding number of spaces.
@@ -155,6 +174,7 @@ of the corresponding number of spaces.
 Extra spacing between the comment characters and the comment text
 makes the comment easier to read.  Default is 1.  nil means 0.")
 
+;;;###autoload
 (defcustom comment-multi-line nil
   "*Non-nil means \\[indent-new-comment-line] should continue same comment
 on new line, with no new terminator or starter.
@@ -201,7 +221,8 @@ This is obsolete because you might as well use \\[newline-and-indent]."
     ;; comment-continue
     (unless (or comment-continue (string= comment-end ""))
       (set (make-local-variable 'comment-continue)
-          (concat " " (substring comment-start 1))))
+          (concat (if (string-match "\\S-\\S-" comment-start) " " "|")
+                  (substring comment-start 1))))
     ;; comment-skip regexps
     (unless comment-start-skip
       (set (make-local-variable 'comment-start-skip)
@@ -261,8 +282,10 @@ 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."
   (if (not comment-use-syntax)
-      (when (re-search-forward comment-start-skip limit noerror)
-       (or (match-end 1) (match-beginning 0)))
+      (if (re-search-forward comment-start-skip limit noerror)
+         (or (match-end 1) (match-beginning 0))
+       (goto-char limit)
+       (unless noerror (error "No comment")))
     (let* ((pt (point))
           ;; Assume (at first) that pt is outside of any string.
           (s (parse-partial-sexp pt (or limit (point-max)) nil nil nil t)))
@@ -276,15 +299,15 @@ and raises an error or returns nil of NOERROR is non-nil."
       (if (not (and (nth 8 s) (not (nth 3 s))))
          (unless noerror (error "No comment"))
        ;; We found the comment.
-       (let ((pt (point))
+       (let ((pos (point))
              (start (nth 8 s))
-             (bol (save-excursion (beginning-of-line) (point)))
+             (bol (line-beginning-position))
              (end nil))
          (while (and (null end) (>= (point) bol))
            (if (looking-at comment-start-skip)
                (setq end (min (or limit (point-max)) (match-end 0)))
              (backward-char)))
-         (goto-char end)
+         (goto-char (or end pos))
          start)))))
 
 (defun comment-search-backward (&optional limit noerror)
@@ -337,10 +360,10 @@ and can use regexps instead of syntax."
     (if comment-use-syntax (forward-comment n)
       (while (> n 0)
        (skip-syntax-forward " ")
-       (if (and (looking-at comment-start-skip)
-                (re-search-forward comment-end-skip nil 'move))
-           (decf n)
-         (setq n -1)))
+       (setq n
+             (if (and (looking-at comment-start-skip)
+                      (re-search-forward comment-end-skip nil 'move))
+                 (1- n) -1)))
       (= n 0))))
 
 (defun comment-enter-backward ()
@@ -367,7 +390,7 @@ Point is assumed to be just at the end of a comment."
 ;;;; Commands
 ;;;;
 
-(defalias 'indent-for-comment 'comment-indent)
+;;;###autoload
 (defun comment-indent (&optional continue)
   "Indent this line's comment to comment column, or insert an empty comment.
 If CONTINUE is non-nil, use the `comment-continuation' markers if any."
@@ -385,15 +408,15 @@ If CONTINUE is non-nil, use the `comment-continuation' markers if any."
      (t (let* ((eolpos (line-end-position))
                cpos indent begpos)
           (beginning-of-line)
-          (when (setq begpos (comment-search-forward eolpos t))
+          (if (not (setq begpos (comment-search-forward eolpos t)))
+             (setq begpos (point))
            (setq cpos (point-marker))
            (goto-char begpos))
-         (setq begpos (point))
           ;; Compute desired indent.
           (if (= (current-column)
-                 (setq indent (if comment-indent-hook
-                                  (funcall comment-indent-hook)
-                                (funcall comment-indent-function))))
+                 (setq indent (funcall (or (and (boundp 'comment-indent-hook)
+                                               comment-indent-hook)
+                                          comment-indent-function))))
               (goto-char begpos)
             ;; If that's different from current, change it.
             (skip-chars-backward " \t")
@@ -401,14 +424,13 @@ If CONTINUE is non-nil, use the `comment-continuation' markers if any."
             (indent-to indent))
           ;; An existing comment?
           (if cpos
-              (progn (goto-char cpos)
-                     (set-marker cpos nil))
+              (progn (goto-char cpos) (set-marker cpos nil))
             ;; No, insert one.
             (insert starter)
             (save-excursion
               (insert ender))))))))
 
-(defalias 'set-comment-column 'comment-set-column)
+;;;###autoload
 (defun comment-set-column (arg)
   "Set the comment column based on point.
 With no ARG, set the comment column to the current column.
@@ -430,7 +452,7 @@ With any other arg, set comment column to indentation of the previous comment
    (t (setq comment-column (current-column))
       (message "Comment column set to %d" comment-column))))
 
-(defalias 'kill-comment 'comment-kill)
+;;;###autoload
 (defun comment-kill (arg)
   "Kill the comment on this line, if any.
 With prefix ARG, kill comments on that many lines starting with this one."
@@ -465,14 +487,22 @@ the string for any N."
          (rpad (concat (substring str (match-end 1)) ;original right padding
                        (substring comment-padding ;additional right padding
                                   (min (- (match-end 0) (match-end 1))
-                                       (length comment-padding))))))
+                                       (length comment-padding)))))
+         ;; We can only duplicate C if the comment-end has multiple chars
+         ;; or if comments can be nested, else the comment-end `}' would
+         ;; be turned into `}}}' where only the first ends the comment
+         ;; and the rest becomes bogus junk.
+         (multi (not (and comment-quote-nested
+                          ;; comment-end is a single char
+                          (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
       (if (not (symbolp n))
-         (concat lpad s (make-string n (aref str (1- (match-end 1)))) rpad)
+         (concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
        ;; construct a regexp that would match anything from just S
        ;; to any possible output of this function for any N.
        (concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
                           lpad "")     ;padding is not required
-               (regexp-quote s) "+"    ;the last char of S might be repeated
+               (regexp-quote s)
+               (when multi "+")        ;the last char of S might be repeated
                (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
                           rpad "")))))) ;padding is not required
 
@@ -513,6 +543,7 @@ If N is `re', a regexp is returned instead, that would match
                (if multi (concat (regexp-quote (string c)) "*"))
                (regexp-quote s))))))
 
+;;;###autoload
 (defun uncomment-region (beg end &optional arg)
   "Uncomment each line in the BEG..END region.
 The numeric prefix ARG can specify a number of chars to remove from the
@@ -543,6 +574,9 @@ comment markers."
            ;; Remove the comment-start.
            (goto-char ipt)
            (skip-syntax-backward " ")
+           ;; Check for special `=' used sometimes in comment-box.
+           (when (and (= (- (point) (point-min)) 1) (looking-at "=\\{7\\}"))
+             (skip-chars-forward "="))
            ;; A box-comment starts with a looong comment-start marker.
            (when (> (- (point) (point-min) (length comment-start)) 7)
              (setq box t))
@@ -556,8 +590,12 @@ comment markers."
 
            ;; Remove the end-comment (and leading padding and such).
            (goto-char (point-max)) (comment-enter-backward)
-           (unless (string-match "\\`\\(\n\\|\\s-\\)*\\'"
-                                 (buffer-substring (point) (point-max)))
+           ;; Check for special `=' used sometimes in comment-box.
+           (when (and (= (- (point-max) (point)) 1) (> (point) 7)
+                      (save-excursion (backward-char 7)
+                                      (looking-at "=\\{7\\}")))
+             (skip-chars-backward "="))
+           (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
              (when (and (bolp) (not (bobp))) (backward-char))
              (if (null arg) (delete-region (point) (point-max))
                (skip-syntax-forward " ")
@@ -588,33 +626,30 @@ comment markers."
 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
   "Make the leading and trailing extra lines.
 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
-  (if block
-      (let* ((s (concat cs "a=m" cce "\n"
-                       (make-string min-indent ? ) ccs))
-            (e (concat cce "\n" (make-string min-indent ? )
-                       ccs "a=m" ce))
-            (_ (assert (string-match "\\s-*\\(a=m\\)\\s-*" s)))
-            (fill (make-string (+ (- max-indent
-                                     min-indent
-                                     (match-beginning 0))
-                                  (- (match-end 0)
-                                     (match-end 1)))
-                               (aref s (match-end 0)))))
+  (let ((eindent 0))
+    (if (not block)
+       ;; Try to match CS and CE's content so they align aesthetically.
+       (progn
+         (setq ce (comment-string-strip ce t t))
+         (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce "\n" cs))
+           (setq eindent
+                 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
+                      0))))
+      ;; box comment
+      (let* ((width (- max-indent min-indent))
+            (s (concat cs "a=m" cce))
+            (e (concat ccs "a=m" ce))
+            (c (if (string-match ".*\\S-\\S-" cs)
+                   (aref cs (1- (match-end 0))) ?=))
+            (_ (string-match "\\s-*a=m\\s-*" s))
+            (fill
+             (make-string (+ width (- (match-end 0)
+                                      (match-beginning 0) (length cs) 3)) c)))
        (setq cs (replace-match fill t t s))
-       (assert (string-match "\\s-*\\(a=m\\)\\s-*" e))
-       (setq ce (replace-match fill t t e)))
-    (when (and ce (string-match "\\`\\s-*\\(.*\\S-\\)\\s-*\\'" ce))
-      (setq ce (match-string 1 ce)))
-    (let* ((c (concat ce "a=m" cs))
-          (indent (if (string-match "\\(.+\\).*a=m\\(.*\\)\\1" c)
-                      (max (+ min-indent
-                              (- (match-end 2) (match-beginning 2))
-                              (- (match-beginning 0)))
-                           0)
-                    min-indent)))
-      (setq ce (concat cce "\n" (make-string indent ? ) (or ce cs)))
-      (setq cs (concat cs "\n" (make-string min-indent ? ) ccs))))
-  (cons cs ce))
+       (string-match "\\s-*a=m\\s-*" e)
+       (setq ce (replace-match fill t t e))))
+    (cons (concat cs "\n" (make-string min-indent ? ) ccs)
+         (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce))))
 
 (def-edebug-spec comment-with-narrowing t)
 (put 'comment-with-narrowing 'lisp-indent-function 2)
@@ -622,34 +657,35 @@ This is used for `extra-line' style (or `box' style if BLOCK is specified)."
   "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."
-  `(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)))
-              (delete-char n)
-              (decf -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))
+  (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)))
                 (delete-char n)
-                (decf -bindent n)))))))))
+                (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)))))))))))
 
 (defun comment-region-internal (beg end cs ce
                                    &optional ccs cce block lines indent)
   "Comment region BEG..END.
-CS and CE are the comment start resp. end string.
-CCS and CCE are the comment continuation strings for the start resp. end
+CS and CE are the comment start resp end string.
+CCS and CCE are the comment continuation strings for the start resp end
 of lines (default to CS and CE).
 BLOCK indicates that end of lines should be marked with either CCE, CE or CS
 \(if CE is empty) and that those markers should be aligned.
@@ -657,7 +693,7 @@ 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))
+  ;;(assert (< beg end))
   (let ((no-empty t))
     ;; Sanitize CE and CCE.
     (if (and (stringp ce) (string= "" ce)) (setq ce nil))
@@ -686,6 +722,7 @@ rather than at left margin."
          (comment-quote-nested comment-start comment-end nil)
 
          ;; Loop over all lines to find the needed indentations.
+         (goto-char (point-min))
          (while
              (progn
                (unless (looking-at "[ \t]*$")
@@ -695,7 +732,8 @@ rather than at left margin."
                (not (or (eobp) (progn (forward-line) nil)))))
          
          ;; Inserting ccs can change max-indent by (1- tab-width).
-         (incf max-indent (+ (max (length cs) (length ccs)) -1 tab-width))
+         (setq max-indent
+               (+ max-indent (max (length cs) (length ccs)) tab-width -1))
          (unless indent (setq min-indent 0))
 
          ;; make the leading and trailing lines if requested
@@ -721,13 +759,15 @@ rather than at left margin."
                (end-of-line)
                (not (or (eobp) (progn (forward-line) nil))))))))))
 
+;;;###autoload
 (defun comment-region (beg end &optional arg)
   "Comment or uncomment each line in the region.
 With just \\[universal-prefix] prefix arg, uncomment each line in region BEG..END.
 Numeric prefix arg ARG means use ARG comment characters.
 If ARG is negative, delete that many comment characters instead.
-Comments are terminated on each line, even for syntax in which newline does
-not end the comment.  Blank lines do not get comments.
+By default, comments start at the left margin, are terminated on each line,
+even for syntax in which newline does not end the comment and blank lines
+do not get comments.  This can be changed with `comment-style'.
 
 The strings used as comment starts are built from
 `comment-start' without trailing spaces and `comment-padding'."
@@ -753,7 +793,7 @@ The strings used as comment starts are built from
       ;; sanitize LINES
       (setq lines
            (and
-            lines multi
+            lines ;; multi
             (progn (goto-char beg) (beginning-of-line)
                    (skip-syntax-forward " ")
                    (>= (point) beg))
@@ -769,8 +809,8 @@ The strings used as comment starts are built from
      ((consp arg) (uncomment-region beg end))
      ((< numarg 0) (uncomment-region beg end (- numarg)))
      (t
-      (if (and (null arg) (= (length comment-start) 1))
-         (setq numarg add) (decf numarg))
+      (setq numarg (if (and (null arg) (= (length comment-start) 1))
+                      add (1- numarg)))
       (comment-region-internal
        beg end
        (let ((s (comment-padright comment-start numarg)))
@@ -790,9 +830,11 @@ The strings used as comment starts are built from
 The numeric prefix ARG specifies how many characters to add to begin- and
 end- comment markers additionally to what `comment-add' already specifies."
   (interactive "*r\np")
-  (let ((comment-style 'box))
+  (let ((comment-style (if (cadr (assoc comment-style comment-styles))
+                          'box-multi 'box)))
     (comment-region beg end (+ comment-add arg))))
 
+;;;###autoload
 (defun comment-dwim (arg)
   "Call the comment command you want (Do What I Mean).
 If the region is active and `transient-mark-mode' is on, call
@@ -830,7 +872,7 @@ This has no effect in modes that do not define a comment syntax."
   :type 'boolean
   :group 'comment)
 
-(defalias 'indent-new-comment-line 'comment-indent-new-line)
+;;;###autoload
 (defun comment-indent-new-line (&optional soft)
   "Break line at point and indent, continuing comment if within one.
 This indents the body of the continued comment
@@ -847,71 +889,118 @@ 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)
-  (let (comcol comstart compt comin)
+  (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).
     (unless (and comment-start
                 comment-auto-fill-only-comments
                 (not (save-excursion
-                       (prog1 (setq compt (comment-beginning))
+                       (prog1 (setq compos (comment-beginning))
                          (setq comin (point))))))
+
+      ;; Now we know we should auto-fill.
       (delete-region (progn (skip-chars-backward " \t") (point))
-                    (progn (skip-chars-forward " \t") (point)))
+                    (progn (skip-chars-forward  " \t") (point)))
       (if soft (insert-and-inherit ?\n) (newline 1))
       (if fill-prefix
          (progn
            (indent-to-left-margin)
            (insert-and-inherit fill-prefix))
-       (unless comment-multi-line
+
+       ;; If necessary check whether we're inside a comment.
+       (unless (or comment-multi-line compos (null comment-start))
          (save-excursion
            (backward-char)
-           (when (and comment-start
-                      (if compt (goto-char comin)
-                        (setq compt (comment-beginning))))
-             ;; The old line has a comment and point was inside the comment.
-             ;; Indent this line like what we found.
-             (setq comstart (buffer-substring compt (point)))
-             (goto-char compt)
-             (setq comcol (current-column)))))
-       (if compt
-           (let* ((comment-column (min comcol comment-column))
-                  (normal-comment
-                   (string-match (regexp-quote (comment-string-strip
-                                                comment-start t t))
-                                 comstart))
-                  ;; Force comment-continue to be recreated from comment-start.
-                  (comment-continue nil) ;(if normal-comment comment-continue)
-                  (comment-end
-                   (if normal-comment comment-end
-                     ;; The comment starter is not the normal comment-start
-                     ;; so we can't just use comment-end.
-                     (save-excursion
-                       (goto-char compt)
-                       (if (not (comment-forward)) comment-end
-                         (comment-string-strip
-                          (buffer-substring
-                           (save-excursion (comment-enter-backward) (point))
-                           (point))
-                          nil t)))))
-                  (comment-start comstart))
-             ;;(if (not (eolp)) (setq comment-end ""))
-             (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)))))
-         (indent-according-to-mode))))))
+           (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))))))))))
 
 (provide 'newcomment)
 
 ;;; Change Log:
 ;; $Log: newcomment.el,v $
+;; Revision 1.14  2000/05/23 20:06:10  monnier
+;; (comment-make-extra-lines): Don't use `assert'.
+;;
+;; Revision 1.13  2000/05/22 04:23:37  monnier
+;; (comment-region-internal): Go back to BEG after quoting
+;; the nested comment markers.
+;;
+;; Revision 1.12  2000/05/21 00:27:31  monnier
+;; (comment-styles): New `box-multi'.
+;; (comment-normalize-vars): Better default for comment-continue to
+;; avoid whitespace-only continuations.
+;; (comment-search-forward): Always move even in the no-syntax case.
+;; (comment-padright): Only obey N if it's only obeyed for padleft.
+;; (comment-make-extra-lines): Better handling of empty continuations.
+;; Use `=' for the filler if comment-start has only one character.
+;; (uncomment-region): Try handling the special `=' filler.
+;; (comment-region): Allow LINES even if MULTI is nil.
+;; (comment-box): Choose box style based on comment-style.
+;;
+;; Revision 1.11  2000/05/19 15:37:41  monnier
+;; Fix license text and author.
+;; Move aliases (indent-for-comment, set-comment-column, kill-comment
+;; and indent-new-comment-line) to the beginning of the file.
+;; Get rid of the last few CLisms.
+;; (comment-forward): Avoid decf.
+;; (comment-make-extra-lines): Comment-out asserts.
+;; (comment-with-narrowing): Properly create uninterned symbol.
+;; (comment-region-internal): Comment-out asserts.  Avoid incf and decf.
+;; (comment-indent-new-line): Fix bug where compt could be set but
+;; not comstart.  Set comment-column more carefully.
+;;
+;; Revision 1.10  2000/05/17 19:32:32  monnier
+;; (comment-beginning): Handle unclosed comment.
+;; (comment-auto-fill-only-comments): New var.
+;; (comment-indent-new-line): Obey comment-auto-fill-only-comments.
+;;   Align with comment-column rather than previous comment if previous
+;;   comment's indentation is greater than comment-column.
+;;
 ;; Revision 1.9  2000/05/16 22:02:37  monnier
 ;; (comment-string-strip): Strip terminating newlines.
 ;; (comment-search-forward): Make LIMIT compulsory.