unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#385: [PATCH] comment-indent doesn't respect comment-indent-function
@ 2008-06-11 17:11 Christopher J. Madsen
  2008-06-11 18:04 ` Stefan Monnier
  2016-02-29  4:59 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 8+ messages in thread
From: Christopher J. Madsen @ 2008-06-11 17:11 UTC (permalink / raw)
  To: bug-gnu-emacs

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  --------------------








^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-07-06  2:58 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-11 17:11 bug#385: [PATCH] comment-indent doesn't respect comment-indent-function Christopher J. Madsen
2008-06-11 18:04 ` Stefan Monnier
2008-06-11 18:59   ` Christopher J. Madsen
2008-06-13 16:47     ` bug#385: [PATCH] comment-indent doesn't respect Stefan Monnier
2017-06-14  4:33   ` bug#385: [PATCH] comment-indent doesn't respect comment-indent-function npostavs
2017-07-06  2:58     ` npostavs
2016-02-29  4:59 ` Lars Ingebrigtsen
2016-03-03  5:03   ` Christopher J. Madsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).