[cfe-dev] clang-format emacs integration: window-start dislocation when formatting-before-save

Adam McKee Adam.Matthew.McKee at gmail.com
Thu Jan 1 17:41:29 PST 2015


As I wrote earlier, I've put this in my ~/.emacs to format my C++ buffers
before they're saved:

(add-hook 'c++-mode-hook
    (lambda()
        (add-hook 'before-save-hook 'clang-format-buffer)))

This worked fine when saving individual files with "C-x C-s".  However, I
encountered a problem when using "C-x s" (save-some-buffers) where the
window-start would be moved to the very first line, and the point would be
moved to the left margin on the middle line in the window.  In
clang-format.el, I added some debug messages to show what's happening with
the calls to goto-char() and set-window-start().  What I found is that
there's nothing wrong with those calls in the (save-some-buffers) case, and
they are the exact same as in the case where I save the buffer individually
with "C-x C-s".  Despite (clang-format-buffer) doing apparently the same
thing in both cases, with (save-some-buffers) _something_ is moving
window-start to the start of the file, which is not acceptable.

I don't see a problem with clang-format.el to be fixed, and I don't know
how to proceed with this any more, so here I just wanted to give an account
of my experience with this, in the hope that it could be useful.

What I've done to my clang-format.el for now is to remove the loop at the
end of the function that restores the window-starts of the windows showing
the formatted buffer.  The result of that is still is slightly disorienting
because the line containing the point is centered on the screen (if
possible), but it's far better than having the window-start moved to the
first line.

Clang-formatting before save must be quite a common practice, so is anyone
doing so _not_ seeing the same window-start dislocation problem with
(save-some-buffers)?  If I can duplicate an emacs setup that doesn't have
this problem, that would help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150101/3d34fc6c/attachment.html>


More information about the cfe-dev mailing list