Subject: bug#6141: 23.2; comment-choose-indent does the wrong thing

From: "Drew Adams" <[email-masked]>
Date: Sat, 8 May 2010 09:52:38 -0700

Put the cursor on the first comment line in this defun and hit M-;.  The
comment is moved one space to the right.  It should not be moved at all.
 
(defun foo (zz)
  "..."
  (let (;; (xx  xx)
        ;; (ww  ww)
        (mm  mm))
    toto))
 
`comment-choose-indent' is the problematic code.  It is called with arg
INDENT = 0 in this case
 
The doc string of `comment-choose-indent', which is the problematic
code, even says that the code should try to align with surrounding
comments, and that it should prefer 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."
 
The problem comes from the systematic (1+ (current-column)), which sets
the minimum (variable MIN) to one more than the current column.
 
See also this comment:
 
;; 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.
 
The assumption that the following comment lines have not been correctly
indented is incorrect in this case. `comment-choose-indent' should not
be assuming anything about "re-indenting" - it is for _caller_ functions
to deal with any re-indenting that might take place on other lines than
the current one.  `comment-choose-indent' does not do any
"re-indenting".  It does not even do any indenting.  It simply returns a
computed possible indent value for code that might want to indent or
re-indent.
 
This bug exists since Emacs 21, BTW.  Emacs 20 does the right thing in
this case.

In GNU Emacs 23.2.1 (i386-mingw-nt5.1.2600)
 of 2010-05-08 on G41R2F1
Windowing system distributor `Microsoft Corp.', version 5.1.2600
configured using `configure --with-gcc (3.4) --no-opt --cflags -Ic:/xpm/include'