Subject: [PATCH] comment-indent doesn't respect comment-indent-function

From: "Christopher J. Madsen" <[email-masked]>
Date: 11 Jun 2008 17:11:02 -0000

Please describe exactly what actions triggered the bug
and the precise symptoms of the bug:

It appears that comment-indent changed in 22.1.  It gained some code
to attempt to align the comment with those on surrounding lines.
Unfortunately, this made it impossible to do certain things with
comment-indent-function.

For example, I had a custom indent function that placed comments
immediately after a closing brace.  However, in Emacs 22, I'd see this:

  while (1) {
    while (2) {

    } # end 2 <-- this comment placed correctly
  }   # end 1 <-- this comment was aligned with the previous one

instead of this:

  while (1) {
    while (2) {

    } # end 2
  } # end 1 <-- here's where comment-indent-function placed it

On the other hand, I do like the idea of automatically aligning
comments.  I had code in my custom indent functions to do that, but it
would be nice if I didn't need to handle that in every indent
function.

I think what's needed is a way for comment-indent-function to
distinguish between "Here's where the comment goes, and that's final"
and "I suggest this position, but make it blend in with the
neighborhood".  Ideally, this would be backwards-compatible with older
versions of Emacs.

Here's a patch I came up with to provide that.  If
comment-indent-function sets comment-indent-fixed to non-nil, then the
return value will be used as-is.  Otherwise, it behaves like Emacs
22.2 did.

Perhaps the sense should be reversed, and it should always respect the
value of comment-indent-function unless told it's ok to adjust it.

*** orig/newcomment.el	Fri Mar 07 18:01:12 2008
--- new/newcomment.el	Wed Jun 11 11:13:24 2008
*************** (defvar comment-indent-function 'comment
*** 135,140 ****
--- 135,143 ----
  This function is called with no args with point at the beginning of
  the comment's starting delimiter and should return either the desired
  column indentation or nil.
+ The returned value may be adjusted by `comment-choose-indent'.
+ To prevent that, the function should set `comment-indent-fixed'
+ to a non-nil value.
  If nil is returned, indentation is delegated to `indent-according-to-mode'.")
  
  ;;;###autoload
*************** (defun comment-indent (&optional continu
*** 585,591 ****
      (beginning-of-line)
      (let* ((eolpos (line-end-position))
  	   (begpos (comment-search-forward eolpos t))
! 	   cpos indent)
        ;; An existing comment?
        (if begpos
  	  (progn
--- 588,594 ----
      (beginning-of-line)
      (let* ((eolpos (line-end-position))
  	   (begpos (comment-search-forward eolpos t))
! 	   cpos indent comment-indent-fixed)
        ;; An existing comment?
        (if begpos
  	  (progn
*************** (defun comment-indent (&optional continu
*** 622,636 ****
        (if (not indent)
  	  ;; comment-indent-function refuses: delegate to line-indent.
  	  (indent-according-to-mode)
! 	;; If the comment is at the right of code, adjust the indentation.
! 	(unless (save-excursion (skip-chars-backward " \t") (bolp))
!           (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)
  	  (delete-region (point) (progn (skip-chars-backward " \t") (point)))
--- 625,640 ----
        (if (not indent)
  	  ;; comment-indent-function refuses: delegate to line-indent.
  	  (indent-according-to-mode)
!         (unless comment-indent-fixed
!           ;; If the comment is at the right of code, adjust the indentation.
!           (unless (save-excursion (skip-chars-backward " \t") (bolp))
!             (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)
  	  (delete-region (point) (progn (skip-chars-backward " \t") (point)))

In GNU Emacs 22.2.1 (i386-mingw-nt5.1.2600)
 of 2008-03-26 on RELEASE
Windowing system distributor `Microsoft Corp.', version 5.1.2600
configured using `configure --with-gcc (3.4)'

-- 
Chris Madsen                                           cjm cjmweb.net
  --------------------  http://www.cjmweb.net  --------------------