changeset 78909:545574ac320d

(comment-choose-indent): New function extracted from comment-indent. Improve the alignment algorithm. (comment-indent): Use it.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Fri, 28 Sep 2007 01:59:40 +0000
parents 7543ecde159e
children ba829cdc5870
files lisp/ChangeLog lisp/newcomment.el
diffstat 2 files changed, 78 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/ChangeLog	Thu Sep 27 22:10:53 2007 +0000
+++ b/lisp/ChangeLog	Fri Sep 28 01:59:40 2007 +0000
@@ -1,3 +1,9 @@
+2007-09-28  Stefan Monnier  <monnier@iro.umontreal.ca>
+
+	* newcomment.el (comment-choose-indent): New function extracted
+	from comment-indent.  Improve the alignment algorithm.
+	(comment-indent): Use it.
+
 2007-09-27  Juanma Barranquero  <lekktu@gmail.com>
 
 	* emacs-lisp/eldoc.el (eldoc-message-commands-table-size)
@@ -8,7 +14,7 @@
 
 2007-09-26  Eli Zaretskii  <eliz@gnu.org>
 
-	* menu-bar.el (menu-bar-search-documentation-menu): Renamed from
+	* menu-bar.el (menu-bar-search-documentation-menu): Rename from
 	menu-bar-apropos-menu.  All users changed.
 	(menu-bar-help-menu): Change menu symbols to better match the text
 	displayed by the menu.
@@ -31,8 +37,8 @@
 
 2007-09-24  Ville Skytt,Ad(B  <scop@xemacs.org>  (tiny change)
 
-	* net/tramp-vc.el (tramp-vc-user-login-name): Use
-	`vc-find-version' instead of `process-file' to check whether we
+	* net/tramp-vc.el (tramp-vc-user-login-name):
+	Use `vc-find-version' instead of `process-file' to check whether we
 	have a new enough vc that doesn't need the defadvice.
 
 2007-09-23  Dan Nicolaescu  <dann@ics.uci.edu>
@@ -75,16 +81,15 @@
 
 2007-09-22  Glenn Morris  <rgm@gnu.org>
 
-	* eshell/esh-mode.el (eshell-output-filter-functions): Add
-	eshell-postoutput-scroll-to-bottom.
+	* eshell/esh-mode.el (eshell-output-filter-functions):
+	Add eshell-postoutput-scroll-to-bottom.
 
 2007-09-21  Dan Nicolaescu  <dann@ics.uci.edu>
 
-	* files.el (file-name-sans-versions): Allow - and a-z in version
-	names.
-
-	* log-view.el (log-view-mode-map, log-view-mode-menu): Bind
-	log-view-annotate-version.
+	* files.el (file-name-sans-versions): Allow - and a-z in version names.
+
+	* log-view.el (log-view-mode-map, log-view-mode-menu):
+	Bind log-view-annotate-version.
 	(log-view-beginning-of-defun, log-view-end-of-defun)
 	(log-view-annotate-version): New functions.
 	(log-view-mode): Use log-view-beginning-of-defun and
@@ -92,7 +97,7 @@
 
 2007-09-21  Stefan Monnier  <monnier@iro.umontreal.ca>
 
-	* emacs-lisp/easy-mmode.el (define-minor-mode): Fix staging brain damage.
+	* emacs-lisp/easy-mmode.el (define-minor-mode): Fix staging.
 
 2007-09-21  Kevin Ryde  <user42@zip.com.au>
 
--- a/lisp/newcomment.el	Thu Sep 27 22:10:53 2007 +0000
+++ b/lisp/newcomment.el	Fri Sep 28 01:59:40 2007 +0000
@@ -97,10 +97,10 @@
 ;;;###autoload
 (defcustom comment-column 32
   "Column to indent right-margin comments to.
-Each mode establishes a different default value for this variable; you
+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'."
+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)
@@ -194,7 +194,7 @@
     (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.
@@ -208,7 +208,8 @@
   "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))
+	    `(choice ,@(mapcar (lambda (s) `(const ,(car s)))
+			       comment-styles))
 	  'symbol)
   :group 'comment)
 
@@ -516,6 +517,58 @@
 	      (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.
@@ -569,38 +622,9 @@
       (if (not indent)
 	  ;; comment-indent-function refuses: delegate to line-indent.
 	  (indent-according-to-mode)
-	;; If the comment is at the left of code, adjust the indentation.
+	;; If the comment is at the right of code, adjust the indentation.
 	(unless (save-excursion (skip-chars-backward " \t") (bolp))
-	  ;; Avoid moving comments past the fill-column.
-	  (let ((max (+ (current-column)
-			(- (or comment-fill-column fill-column)
-			   (save-excursion (end-of-line) (current-column))))))
-	    (if (<= max indent)
-		(setq indent max)	;Don't move past the fill column.
-	      ;; We can choose anywhere between indent..max.
-	      ;; Let's try to align to a comment on the previous line.
-	      (let ((other nil)
-		    (min (max indent
-			      (save-excursion (skip-chars-backward " \t")
-					      (1+ (current-column))))))
-		(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.
-		    (setq indent other)
-		  ;; Let's try to align to a comment on the next line, then.
-		  (let ((other nil))
-		    (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.
-			(setq indent other))))))))
+          (setq indent (comment-choose-indent indent)))
 	;; Update INDENT to leave at least one space
 	;; after other nonwhite text on the line.
 	(save-excursion
@@ -1020,7 +1044,8 @@
 	 (lines (nth 2 style))
 	 (block (nth 1 style))
 	 (multi (nth 0 style)))
-    ;; we use `chars' instead of `syntax' because `\n' might be
+
+    ;; 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)