Tuesday, June 22, 2010

vc-diff tweak

For a while I've noticed that 'vc-diff (C-x v =) would pop up a buffer when there were no differences, and that buffer would just say there weren't any differences. It finally got on my nerves, so I wrote this code which works around that issue.

I don't totally understand the vc code, but parts are set up to run asynchronously, and that is what was tripping up 'vc-diff in my case. The diff would come back as identical, but at the time of the callback, Emacs had already lost the window configuration - so it was forced to pop up a buffer.


(defvar my-vc-diff-pre-diff-window-configuration nil
"place to store window configuration,
because sometimes (usually) the vc-diff does things asynchronously")

(defadvice vc-diff-internal (around
vc-diff-dont-show-diff-buffer-when-no-diff
activate)
"no changes still results in *vc-diff* buffer being shown, try to avoid that"
(setq my-vc-diff-pre-diff-window-configuration (current-window-configuration))
(let* ((res ad-do-it))
(when (or (null res)
(string-match-p "^No changes between"
(with-current-buffer "*vc-diff*"
(buffer-substring-no-properties
(point-min) (point-max)))))
(set-window-configuration my-vc-diff-pre-diff-window-configuration))))

(defadvice vc-diff-finish (after
vc-diff-finish-dont-show-diff-buffer-when-no-diff
activate)
"when buffer says no differences, revert window configuration"
(when (and my-vc-diff-pre-diff-window-configuration
(string-match-p "^No changes between"
(with-current-buffer "*vc-diff*"
(buffer-substring-no-properties
(point-min) (point-max)))))
(set-window-configuration my-vc-diff-pre-diff-window-configuration)))

4 comments:

Paul Hobbs said...

Thanks a lot! This a pretty cool hack. I think I'll try my hand at some advice sometime soon... I love how much power it has!

Vivianne said...

Maybe that is not the right place to ask this, but do you make your .emacs file available anywhere?

a said...

Vivianne, I don't currently, but that's mostly because it has so many dependencies on other packages. It's about 5k lines long...

Vivianne said...

That is definitely a lot of lines!

I was trying to organise and improve my .emacs file today. I went back to 3 .emacs files from other people I had saved but I realised they just weren't good (one had been last modified in 2001!). I remembered you and decided to ask, because I am sure your one would be worth looking into. In any case, I know there is plenty of other .emacs files available, I just need to find the right one :)

Thanks for the reply.