2009-05-26

tracking changes

It's sometimes nice to see the changes you've made to a file. If the file is under version control, you can use the 'diff'-features of the version control system of course; or you can use diff-buffer-with-file to compare your buffer with the version on disk. That obviously only. works when you haven't saved the file yet.

Anyway, a bit easier, straighforward way may be to use highlight-changes-mode. With that mode, emacs can give a special color to parts of the text that you have changed.

;; higlight changes in documents
(global-highlight-changes-mode t)
(setq highlight-changes-visibility-initial-state nil); initially hide

The last line tells me that the changes should not be visible unless I want to see them.

I defined a key binding (F6) so I can easily toggle between visible/invisible changes:

;; toggle visibility
(global-set-key (kbd "<f6>")      'highlight-changes-visible-mode) ;; changes
;; remove the change-highlight in region
(global-set-key (kbd "S-<f6>")    'highlight-changes-remove-highlight)

With this last keybinding S-<f6> (Shift-F6), I can remove the change-indication of the current region (selection). Here are some other useful keybindings to quickly jump between various changes:

;; alt-pgup/pgdown jump to the previous/next change

;; if you're not already using it for something else...
(global-set-key (kbd "<M-prior>") 'highlight-changes-next-change)
(global-set-key (kbd "<M-next>")  'highlight-changes-previous-change)

Another interesting thing you can do is M-x highlight-compare-with-file.

The only remaining problem with highlight-changes-mode is that the default colors are, well, hideous. But of course, that can easily be fixed by changing the faces:

(set-face-foreground 'highlight-changes nil)
(set-face-background 'highlight-changes "#382f2f")
(set-face-foreground 'highlight-changes-delete nil)
(set-face-background 'highlight-changes-delete "#916868")

Or adding to your color-scheme:

(highlight-changes ((t (:foreground nil :background "#382f2f"))))
(highlight-changes-delete ((t (:foreground nil :background "#916868")))) 

Now, with these color changes, the foreground stays the same, only the background changes a bit. I am using a dark theme, you might want to change the colors to fit in with your theme.

There are some more features - for example, to rotate through changes of different age. For such things I prefer to use a version control system, but you might want to check it out.

Tracking changes can be quite useful. And, unlike some word-processing software, emacs does not hide your highly embarrassing modifications somewhere in your document…

12 comments:

michal said...

nice. may i know what color scheme were you using taking that screenshot above? could you share with it?

zzkt said...

Thanks for the tip. One small correction, the line (global-highlight-changes-mode t) should read (global-highlight-changes t)

djcb said...

@zzkt: well, in my emacs 23, 'global-highlight-changes-mode' is the preferred one. 'global-highlight-changes' is an alias for backward compatibility.

So, indeed for emacs <= 23.1, you might need 'global-highlight-changes' instead.

Anonymous said...

Sorry a beginner question..where do you put this code?

djcb said...

@Anonymous: ignore that last part about 'adding to your color-scheme'

all the other things should go to your ~/.emacs (~/.emacs means 'the file called .emacs, which is located in your homedir)

Also see: http://emacs-fu.blogspot.com/2008/12/emacs.html

Anonymous said...

Thanks, and if you are using xemacs then, do you put these in init.el?

djcb said...

@Anonymous: haven't used Xemacs in ages, but I think it would be: ~/.xemacs/init.el

dave said...

Should the second

(set-face-foreground 'highlight-changes nil)

have been

(set-face-foreground 'highlight-changes-delete nil)
?

My .emacs is almost a mirror of the emacs-fu postings :)

djcb said...

@dave:good catch,eagle-eyed. copy-paste... i won't update right now, to avoid spamming planet.emacsen.org...

djcb said...

updated; fixed the mistake that dave found, and some small other things.

ssislands said...

Hi, just found this after a long trek of Googling. My only remaining issue is I want the changes still being highlighted even after I save and reopen my file, do you have any ideas? Thanks a lot.

Anonymous said...

thank you.. it was a great help to find how the highlight-changes-mode can be controlled .