Is there any editor/IDE that provides a live view of differences (probably using diff
) while I am editing a file. The ideal setting would be when I open a file for editing in such a diff-mode, the file is buffered in two independent panes (but put side by side), so that when I edit the contents of one, the differences of the two are highlighted for me.
1 Answer
There doesn't seem to be an existing Emacs mode to do exactly what you want, but a cursory googling turned up this and this.
From the second one (with mildly fixed formatting):
(defun diff-buffer-against-file (context)
"diff the current [edited] buffer and the file of the same name"
(interactive "P")
(let (($file buffer-file-name)
($tempFile "/tmp/emacs.diff")
($tempBuffer "emacs.diff"))
(delete-other-windows)
(push-mark (point) t)
(generate-new-buffer $tempFile)
(copy-to-buffer $tempBuffer (point-min) (point-max))
(set-buffer $tempBuffer)
(write-file $tempFile)
(shell-command (concat (if context "diff -c " "diff ") $file " " $tempFile))
(kill-buffer $tempFile)
(pop-mark)))
(global-set-key "\C-cd" 'diff-buffer-against-file)
The operation looks a bit too intense to bind to a change hook, but there's nothing preventing you from doing so if you're into that sort of thing.
Edit: Stefan points out that diff-buffer-with-file
exists, and has the behavior you're looking for (it takes a buffer, and diff
s that buffer with its file, showing that output in an unfocused temporary buffer), so you don't even need to define the above. I did try it though, and the naive
(defun diff-current (start end len) (diff-buffer-with-file (current-buffer)))
(add-hook 'after-change-functions 'diff-current)
makes editing too uncomfortable for my tastes. You'll probably want to follow Stefan's suggestion and use a timeout instead of diff
ing immediately on every change.
-
3Emacs already provides
diff-buffer-with-file
, no need to write it by hand any more. It would probably be OK to add such a function to a timer (it wouldn't be refreshed immediately, but soon).– StefanCommented Apr 12, 2013 at 20:50 -
It is nice to see that Emacs also provides a partial solution, which may be hacked as you suggested to a full solution.– dayCommented Apr 13, 2013 at 11:05
:diffupdate
; what's missing fromvimdiff
for you?!vimdiff
: 1. it cannot buffer the same file independently to allow adiff
comparison. Of course, this can be solved by automatically making a temporary copy of the file; 2. Insidevimdiff
, inline editing (inserting or deleting) could not triggerdiff
to recalculate the difference, one has to do it manually by executing:diffupdate
. I want this recalculation to be automated when I exit the INSERT mode and enter the NORMAL mode.au
for event InsertEnter/InsertLeave. you can call diffupdate there.CursorMoved
andCursorMovedI
.