1 ;;; hilit-chg.el --- minor mode displaying buffer changes with special face
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
5 ;; Author: Richard Sharman <rsharman@pobox.com>
8 ;; This file is part of GNU Emacs.
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
28 ;; A minor mode: "Highlight Changes mode".
31 ;; Highlight Changes mode has 2 submodes: active and passive.
32 ;; When active, changes to the buffer are displayed in a different face.
33 ;; When passive, any existing displayed changes are saved and new ones
34 ;; recorded but are not displayed differently.
35 ;; Why active and passive? Having the changes visible can be handy when you
36 ;; want the information but very distracting otherwise. So, you can keep
37 ;; Highlight Changes mode in passive state while you make your changes, toggle
38 ;; it on to active mode to see them, then toggle it back off to avoid
41 ;; When active, changes are displayed in `highlight-changes-face'. When
42 ;; text is deleted, the following character is displayed in
43 ;; `highlight-changes-delete-face' face.
46 ;; You can "age" different sets of changes by using
47 ;; `highlight-changes-rotate-faces'. This rotates different through a series
48 ;; of different faces, so you can distinguish "new" changes from "older"
49 ;; changes. You can customize these "rotated" faces in two ways. You can
50 ;; either explicitly define each face by customizing
51 ;; `highlight-changes-face-list'. If, however, the faces differ from
52 ;; `highlight-changes-face' only in the foreground colour, you can simply set
53 ;; `highlight-changes-colours'. If `highlight-changes-face-list' is nil when
54 ;; the faces are required they will be constructed from
55 ;; `highlight-changes-colours'.
58 ;; When a Highlight Changes mode is on (either active or passive) you can go
59 ;; to the next or previous change with `highlight-changes-next-change' and
60 ;; `highlight-changes-previous-change'.
63 ;; You can also use the command highlight-compare-with-file to show changes
64 ;; in this file compared with another file (typically the previous version
68 ;; There are currently three hooks run by `highlight-changes-mode':
69 ;; `highlight-changes-enable-hook' - is run when Highlight Changes mode
70 ;; is initially enabled for a buffer.
71 ;; `highlight-changes-disable-hook' - is run when Highlight Changes mode
73 ;; `highlight-changes-toggle-hook' - is run each time `highlight-changes-mode'
74 ;; is called. Typically this is when
75 ;; toggling between active and passive
76 ;; modes. The variable
77 ;; `highlight-changes-mode' contains the new
78 ;; state (`active' or `passive'.)
83 ;; (defun my-highlight-changes-enable-hook ()
84 ;; (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
87 ;; (defun my-highlight-changes-disable-hook ()
88 ;; (remove-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
91 ;; (add-hook 'highlight-changes-enable-hook 'my-highlight-changes-enable-hook)
92 ;; (add-hook 'highlight-changes-disable-hook
93 ;; 'my-highlight-changes-disable-hook)
96 ;; Explcit vs. Implicit
99 ;; Normally, Highlight Changes mode is turned on explicitly in a buffer.
101 ;; If you prefer to have it automatically invoked you can do it as
104 ;; 1. Most modes have a major-hook, typically called MODE-hook. You
105 ;; can use `add-hook' to call `highlight-changes-mode'.
108 ;; (add-hook 'c-mode-hook 'highlight-changes-mode)
110 ;; If you want to make it start up in passive mode (regardless of the
111 ;; setting of highlight-changes-initial-state):
112 ;; (add-hook 'emacs-lisp-mode-hook
114 ;; (highlight-changes-mode 'passive)))
116 ;; However, this cannot be done for Fundamental mode for there is no
119 ;; 2. You can use the function `global-highlight-changes'
121 ;; This function, which is fashioned after the way `global-font-lock' works,
122 ;; toggles on or off global Highlight Changes mode. When activated, it turns
123 ;; on Highlight Changes mode in all "suitable" existings buffers and will turn
124 ;; it on in new "suitable" buffers to be created.
126 ;; A buffer's "suitability" is determined by variable
127 ;; `highlight-changes-global-modes', as follows. If the variable is
128 ;; * nil -- then no buffers are suitable;
129 ;; * a function -- this function is called and the result is used. As
130 ;; an example, if the value is 'buffer-file-name then all buffers
131 ;; who are visiting files are suitable, but others (like dired
133 ;; * a list -- then if the buufer is suitable iff its mode is in the
134 ;; list, exccept if the first element is nil in which case the test
135 ;; is reversed (i.e. it is a list of unsuitable modes).
136 ;; * Otherwise, the buffer is suitable if its name does not begin with
137 ;; ` ' or `*' and if `buffer-file-name' returns true.
142 ;; Possible bindings:
143 ;; (global-set-key '[C-right] 'highlight-changes-next-change)
144 ;; (global-set-key '[C-left] 'highlight-changes-previous-change)
146 ;; Other interactive functions (which could be bound if desired):
147 ;; highlight-changes-mode
148 ;; highlight-changes-remove-highlight
149 ;; highlight-changes-rotate-faces
150 ;; highlight-compare-with-file
153 ;; You can automatically rotate faces when the buffer is saved;
154 ;; see function `highlight-changes-rotate-faces' for how to do this.
160 ;; - the next-change and previous-change functions are too literal;
161 ;; they should find the next "real" change, in other words treat
162 ;; consecutive changes as one.
165 ;;; To do (maybe), notes, ...
167 ;; - having different faces for deletion and non-deletion: is it
168 ;; really worth the hassle?
169 ;; - should have better hooks: when should they be run?
170 ;; - highlight-compare-with-file should allow RCS files - e.g. nice to be
171 ;; able to say show changes compared with version 2.1.
172 ;; - Maybe we should have compare-with-buffer as well. (When I tried
173 ;; a while back I ran into a problem with ediff-buffers-internal.)
178 ;; R Sharman (rsharman@magma.ca) Feb 1998:
179 ;; - initial release as change-mode.
180 ;; Jari Aalto <jari.aalto@ntc.nokia.com> Mar 1998
181 ;; - fixes for byte compile errors
182 ;; - use eval-and-compile for autoload
183 ;; Marijn Ros <J.M.Ros@fys.ruu.nl> Mar 98
184 ;; - suggested turning it on by default
185 ;; Eric Ludlam <zappo@gnu.org> Suggested using overlays.
187 ;; - global mode and various stuff added
188 ;; - Changed to use overlays
190 ;; - renmaed to Highlight Changes mode.
197 ;; ====================== Customization =======================
198 (defgroup highlight-changes nil
199 "Highlight Changes mode."
204 ;; Face information: How the changes appear.
206 ;; Defaults for face: red foreground, no change to background,
207 ;; and underlined if a change is because of a deletion.
208 ;; Note: underlining is helpful in that is shows up changes in white space.
209 ;; However, having it set for non-delete changes can be annoying because all
210 ;; indentation on inserts gets underlined (which can look pretty ugly!).
212 (defface highlight-changes-face
213 '((((class color
)) (:foreground
"red" ))
214 (t (:inverse-video t
)))
215 "Face used for highlighting changes."
216 :group
'highlight-changes
219 ;; This looks pretty ugly, actually. Maybe the underline should be removed.
220 (defface highlight-changes-delete-face
221 '((((class color
)) (:foreground
"red" :underline t
))
222 (t (:inverse-video t
)))
223 "Face used for highlighting deletions."
224 :group
'highlight-changes
229 ;; A (not very good) default list of colours to rotate through.
231 (defcustom highlight-changes-colours
232 (if (eq (frame-parameter nil
'background-mode
) 'light
)
233 ;; defaults for light background:
234 '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue")
235 ;; defaults for dark background:
236 '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid"))
237 "*Colours used by `highlight-changes-rotate-faces'.
238 The newest rotated change will be displayed in the first element of this list,
239 the next older will be in the second element etc.
241 This list is used if `highlight-changes-face-list' is nil, otherwise that
242 variable overrides this list. If you only care about foreground
243 colours then use this, if you want fancier faces then set
244 `highlight-changes-face-list'."
245 :type
'(repeat color
)
246 :group
'highlight-changes
250 ;; If you invoke highlight-changes-mode with no argument, should it start in
251 ;; active or passive mode?
253 (defcustom highlight-changes-initial-state
'active
254 "*What state (active or passive) `highlight-changes' should start in.
255 This is used when `highlight-changes' is called with no argument.
256 This variable must be set to one of the symbols `active' or `passive'."
257 :type
'(choice (const :tag
"Active" active
)
258 (const :tag
"Passive" passive
))
259 :group
'highlight-changes
262 (defcustom highlight-changes-global-initial-state
'passive
263 "*What state `global-highlight-changes' should start in.
264 This is used if `global-highlight-changes' is called with no argument.
265 This variable must be set to either `active' or `passive'"
266 :type
'(choice (const :tag
"Active" active
)
267 (const :tag
"Passive" passive
))
268 :group
'highlight-changes
271 ;; The strings displayed in the mode-line for the minor mode:
272 (defcustom highlight-changes-active-string nil
273 "*The string used when Highlight Changes mode is in the active state.
274 This should be set to nil if no indication is desired, or to
275 a string with a leading space."
276 :type
'(choice string
277 (const :tag
"None" nil
))
278 :group
'highlight-changes
281 (defcustom highlight-changes-passive-string
" Chg"
282 "*The string used when Highlight Changes mode is in the passive state.
283 This should be set to nil if no indication is desired, or to
284 a string with a leading space."
285 :type
'(choice string
286 (const :tag
"None" nil
))
287 :group
'highlight-changes
290 (defcustom highlight-changes-global-modes t
291 "*Determine whether a buffer is suitable for global Highlight Changes mode.
293 A function means that function is called: if it returns non-nil the
296 A list is a list of modes for which it is suitable, or a list whose
297 first element is 'not followed by modes which are not suitable.
299 t means the buffer is suitable if its name does not begin with ` ' nor
300 `*' and the buffer has a filename.
302 nil means no buffers are suitable for `global-highlight-changes'
303 (effectively disabling the mode).
307 means that Highlight Changes mode is turned on for buffers in C and C++
310 (const :tag
"all non-special buffers visiting files" t
)
311 (set :menu-tag
"specific modes" :tag
"modes"
313 (const :tag
"All except these" not
)
314 (repeat :tag
"Modes" :inline t
(symbol :tag
"mode")))
315 (function :menu-tag
"determined by function"
316 :value buffer-file-name
)
317 (const :tag
"none" nil
)
319 :group
'highlight-changes
323 (defvar global-highlight-changes nil
)
325 (defcustom highlight-changes-global-changes-existing-buffers nil
326 "*If non-nil toggling global Highlight Changes mode affects existing buffers.
327 Normally, `global-highlight-changes' means affects only new buffers (to be
328 created). However, if highlight-changes-global-changes-existing-buffers
329 is non-nil then turning on `global-highlight-changes' will turn on
330 highlight-changes-mode in suitable buffers and turning the mode off will
331 remove it from existing buffers."
333 :group
'highlight-changes
)
335 (defun hilit-chg-cust-fix-changes-face-list (w wc
&optional event
)
336 ;; When customization function `highlight-changes-face-list' inserts a new
337 ;; face it uses the default face. We don't want the user to modify this
338 ;; face, so we rename the faces in the list on an insert. The rename is
339 ;; actually done by copying the faces so user-defined faces still remain
340 ;; in the same order.
341 ;; The notifying the parent is needed because without it changes to the
342 ;; faces are saved but not to the actual list itself.
343 (let ((old-list (widget-value w
)))
344 (if (member 'default old-list
)
346 ((p (reverse old-list
))
347 (n (length old-list
))
352 (setq old-name
(car p
))
353 (setq new-name
(intern (format "highlight-changes-face-%d" n
)))
354 (if (eq old-name new-name
)
356 ;; A new face has been inserted: we don't want to modify the
357 ;; default face so copy it. Better, though, (I think) is to
358 ;; make a new face have the same attributes as
359 ;; highlight-changes-face .
360 (if (eq old-name
'default
)
361 (copy-face 'highlight-changes-face new-name
)
362 (copy-face old-name new-name
)
364 (setq new-list
(append (list new-name
) new-list
))
367 (if (equal new-list
(widget-value w
))
368 nil
;; (message "notify: no change!")
369 (widget-value-set w new-list
)
373 ;; (message "notify: no default here!")
375 (let ((parent (widget-get w
:parent
)))
377 (widget-apply parent
:notify w event
)))
381 (defcustom highlight-changes-face-list nil
382 "*A list of faces used when rotatating changes.
383 Normally the variable is initialized to nil and the list is created from
384 `highlight-changes-colours' when needed. However, you can set this variable
385 to any list of faces. You will have to do this if you want faces which
386 don't just differ from `highlight-changes-face' by the foreground colour.
387 Otherwise, this list will be constructed when needed from
388 `highlight-changes-colours'."
391 :notify hilit-chg-cust-fix-changes-face-list
393 (const :tag
"Derive from highlight-changes-colours" nil
)
395 :group
'highlight-changes
398 ;; ========================================================================
400 ;; These shouldn't be changed!
402 ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
404 (defvar highlight-changes-mode nil
)
405 (defvar hilit-chg-list nil
)
406 (defvar hilit-chg-string
" ??")
407 (or (assq 'highlight-changes-mode minor-mode-alist
)
408 (setq minor-mode-alist
409 (cons '(highlight-changes-mode hilit-chg-string
) minor-mode-alist
)
411 (make-variable-buffer-local 'highlight-changes-mode
)
412 (make-variable-buffer-local 'hilit-chg-string
)
417 ;; For highlight-compare-with-file
418 (defvar ediff-number-of-differences
)
419 (autoload 'ediff-setup
"ediff")
420 (autoload 'ediff-with-current-buffer
"ediff")
421 (autoload 'ediff-really-quit
"ediff")
422 (autoload 'ediff-make-fine-diffs
"ediff")
423 (autoload 'ediff-get-fine-diff-vector
"ediff")
424 (autoload 'ediff-get-difference
"ediff")
431 (defun hilit-chg-map-changes (func &optional start-position end-position
)
432 "Call function FUNC for each region used by Highlight Changes mode."
433 ;; if start-position is nil, (point-min) is used
434 ;; if end-position is nil, (point-max) is used
435 ;; FUNC is called with 3 params: property start stop
436 (let ((start (or start-position
(point-min)))
437 (limit (or end-position
(point-max)))
439 (while (and start
(< start limit
))
440 (setq prop
(get-text-property start
'hilit-chg
))
441 (setq end
(text-property-not-all start limit
'hilit-chg prop
))
443 (funcall func prop start
(or end limit
)))
448 (defun hilit-chg-display-changes (&optional beg end
)
449 "Display face information for Highlight Changes mode.
451 An overlay containing a change face is added, from the information
452 in the text property of type change.
454 This is the opposite of hilit-chg-hide-changes."
455 (hilit-chg-map-changes 'hilit-chg-make-ov beg end
))
458 (defun hilit-chg-make-ov (prop start end
)
459 ;; for the region make change overlays corresponding to
460 ;; the text property 'hilit-chg
461 (let ((ov (make-overlay start end
))
464 (error "hilit-chg-make-ov: prop is nil"))
465 (if (eq prop
'hilit-chg-delete
)
466 (setq face
'highlight-changes-delete-face
)
467 (setq face
(nth 1 (member prop hilit-chg-list
))))
470 ;; We must mark the face, that is the purpose of the overlay
471 (overlay-put ov
'face face
)
472 ;; I don't think we need to set evaporate since we should
473 ;; be controlling them!
474 (overlay-put ov
'evaporate t
)
475 ;; We set the change property so we can tell this is one
476 ;; of our overlays (so we don't delete someone else's).
477 (overlay-put ov
'hilit-chg t
)
479 (error "hilit-chg-make-ov: no face for prop: %s" prop
)
483 (defun hilit-chg-hide-changes (&optional beg end
)
484 "Remove face information for Highlight Changes mode.
486 The overlay containing the face is removed, but the text property
487 containing the change information is retained.
489 This is the opposite of hilit-chg-display-changes."
490 (let ((start (or beg
(point-min)))
491 (limit (or end
(point-max)))
493 (setq p
(overlays-in start limit
))
495 ;; don't delete the overlay if it isn't ours!
496 (if (overlay-get (car p
) 'hilit-chg
)
497 (delete-overlay (car p
)))
501 (defun hilit-chg-fixup (beg end
)
502 "Fix change overlays in region beg .. end.
504 Ensure the overlays agree with the changes as determined from
505 the text properties of type `hilit-chg' ."
506 ;; Remove or alter overlays in region beg..end
507 (let (p ov ov-start ov-end
509 (setq p
(overlays-in beg end
))
510 ;; temp for debugging:
511 ;; (or (eq highlight-changes-mode 'active)
512 ;; (error "hilit-chg-fixup called but Highlight Changes mode not active"))
515 (setq ov-start
(overlay-start ov
))
516 (setq ov-end
(overlay-end ov
))
519 (move-overlay ov ov-start beg
)
522 (setq props
(overlay-properties ov
))
523 (setq ov
(make-overlay end ov-end
))
525 (overlay-put ov
(car props
)(car (cdr props
)))
526 (setq props
(cdr (cdr props
))))
531 (move-overlay ov end ov-end
)
535 (hilit-chg-display-changes beg end
)
544 (defun highlight-changes-remove-highlight (beg end
)
545 "Remove the change face from the region.
546 This allows you to manually remove highlighting from uninteresting changes."
548 (let ((after-change-functions nil
))
549 (remove-text-properties beg end
'(hilit-chg nil
))
550 (hilit-chg-fixup beg end
)))
552 (defun hilit-chg-set-face-on-change (beg end leng-before
553 &optional no-proerty-change
)
554 "Record changes and optionally display them in a distinctive face.
555 `hilit-chg-set' adds this function to the `after-change-functions' hook."
557 ;; This function is called by the `after-change-functions' hook, which
558 ;; is how we are notified when text is changed.
559 ;; It is also called from `highlight-compare-with-file'.
561 ;; We do NOT want to simply do this if this is an undo command, because
562 ;; otherwise an undone change shows up as changed. While the properties
563 ;; are automatically restored by undo, we must fixup the overlay.
565 (let ((beg-decr 1) (end-incr 1)
569 (if (eq highlight-changes-mode
'active
)
570 (hilit-chg-fixup beg end
))
571 (if (and (= beg end
) (> leng-before
0))
574 ;; The eolp and bolp tests are a kludge! But they prevent
575 ;; rather nasty looking displays when deleting text at the end
576 ;; of line, such as normal corrections as one is typing and
577 ;; immediately makes a corrections, and when deleting first
578 ;; character of a line.
579 ;;; (if (= leng-before 1)
581 ;;; (setq beg-decr 0 end-incr 0)
583 ;;; (setq beg-decr 0))))
584 ;;; (setq beg (max (- beg beg-decr) (point-min)))
585 (setq end
(min (+ end end-incr
) (point-max)))
586 (setq type
'hilit-chg-delete
))
588 ;; Most of the time the following is not necessary, but
589 ;; if the current text was marked as a deletion then
590 ;; the old overlay is still in effect, so if we add some
591 ;; text then remove the deletion marking, but set it to
592 ;; changed otherwise its highlighting disappears.
593 (if (eq (get-text-property end
'hilit-chg
) 'hilit-chg-delete
)
595 (remove-text-properties end
(+ end
1) '(hilit-chg nil
))
596 (put-text-property end
(+ end
1) 'hilit-chg
'hilit-chg
)
597 (if (eq highlight-changes-mode
'active
)
598 (hilit-chg-fixup beg
(+ end
1))))))
599 (unless no-proerty-change
600 (put-text-property beg end
'hilit-chg type
))
601 (if (or (eq highlight-changes-mode
'active
) no-proerty-change
)
602 (hilit-chg-make-ov type beg end
))
609 (defun hilit-chg-set (value)
610 "Turn on Highlight Changes mode for this buffer."
611 (setq highlight-changes-mode value
)
612 (remove-hook 'after-change-functions
'hilit-chg-set-face-on-change t
)
613 (hilit-chg-make-list)
614 (if (eq highlight-changes-mode
'active
)
616 (setq hilit-chg-string highlight-changes-active-string
)
618 (hilit-chg-display-changes)))
620 (setq hilit-chg-string highlight-changes-passive-string
)
622 (hilit-chg-hide-changes))
624 (force-mode-line-update)
625 (make-local-hook 'after-change-functions
)
626 (add-hook 'after-change-functions
'hilit-chg-set-face-on-change nil t
)
629 (defun hilit-chg-clear ()
630 "Remove Highlight Changes mode for this buffer.
631 This removes all saved change information."
633 ;; We print the buffer name because this function could be called
634 ;; on many buffers from `global-highlight-changes'.
635 (message "Cannot remove highlighting from read-only mode buffer %s"
637 (remove-hook 'after-change-functions
'hilit-chg-set-face-on-change t
)
638 (let ((after-change-functions nil
))
639 (hilit-chg-hide-changes)
640 (hilit-chg-map-changes
641 '(lambda (prop start stop
)
642 (remove-text-properties start stop
'(hilit-chg nil
))))
644 (setq highlight-changes-mode nil
)
645 (force-mode-line-update)
646 ;; If we type: C-u -1 M-x highlight-changes-mode
647 ;; we want to turn it off, but hilit-chg-post-command-hook
648 ;; runs and that turns it back on!
649 (remove-hook 'post-command-hook
'hilit-chg-post-command-hook
)
653 (defun highlight-changes-mode (&optional arg
)
654 "Toggle (or initially set) Highlight Changes mode.
657 if Highlight Changes mode is not enabled, then enable it (to either active
658 or passive as determined by variable highlight-changes-initial-state);
659 otherwise, toggle between active and passive states.
662 if just C-u or a positive argument, set state to active;
663 with a zero argument, set state to passive;
664 with a negative argument, disable Highlight Changes mode completely.
666 Active state - means changes are shown in a distinctive face.
667 Passive state - means changes are kept and new ones recorded but are
668 not displayed in a different face.
671 \\[highlight-changes-next-change] - move point to beginning of next change
672 \\[highlight-changes-previous-change] - move to beginning of previous change
673 \\[highlight-compare-with-file] - mark text as changed by comparing this
674 buffer with the contents of a file
675 \\[highlight-changes-remove-highlight] - remove the change face from the region
676 \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes \
682 highlight-changes-enable-hook - when Highlight Changes mode enabled.
683 highlight-changes-toggle-hook - when entering active or passive state
684 highlight-changes-disable-hook - when turning off Highlight Changes mode.
687 (if (or (display-color-p)
688 (and (fboundp 'x-display-grayscale-p
) (x-display-grayscale-p)))
689 (let ((was-on highlight-changes-mode
)
690 (new-highlight-changes-mode
693 ;; no arg => toggle (or set to active initially)
694 (if highlight-changes-mode
695 (if (eq highlight-changes-mode
'active
) 'passive
'active
)
696 highlight-changes-initial-state
))
697 ;; an argument is given
702 ((> (prefix-numeric-value arg
) 0)
704 ((< (prefix-numeric-value arg
) 0)
709 (if new-highlight-changes-mode
710 ;; mode is turned on -- but may be passive
712 (hilit-chg-set new-highlight-changes-mode
)
714 ;; run highlight-changes-enable-hook once
715 (run-hooks 'highlight-changes-enable-hook
))
716 (run-hooks 'highlight-changes-toggle-hook
))
717 ;; mode is turned off
718 (run-hooks 'highlight-changes-disable-hook
)
721 (message "Highlight Changes mode requires color or grayscale display"))
727 (defun highlight-changes-next-change ()
728 "Move to the beginning of the next change, if in Highlight Changes mode."
730 (if highlight-changes-mode
731 (let ((start (point))
733 (setq prop
(get-text-property (point) 'hilit-chg
))
735 ;; we are in a change
736 (setq start
(next-single-property-change (point) 'hilit-chg
)))
738 (setq start
(next-single-property-change start
'hilit-chg
)))
741 (message "no next change")))
742 (message "This buffer is not in Highlight Changes mode.")))
746 (defun highlight-changes-previous-change ()
747 "Move to the beginning of the previous change, if in Highlight Changes mode."
749 (if highlight-changes-mode
750 (let ( (start (point)) (prop nil
) )
752 (setq prop
(get-text-property (1- (point)) 'hilit-chg
)))
754 ;; we are in a change
755 (setq start
(previous-single-property-change (point) 'hilit-chg
)))
757 (setq start
(previous-single-property-change start
'hilit-chg
)))
758 ;; special handling for the case where (point-min) is a change
760 (setq start
(or (previous-single-property-change start
'hilit-chg
)
761 (if (get-text-property (point-min) 'hilit-chg
)
765 (message "no previous change")))
766 (message "This buffer is not in Highlight Changes mode.")))
769 ;; ========================================================================
772 (defun hilit-chg-make-list (&optional force
)
773 "Construct hilit-chg-list and highlight-changes-face-list."
774 ;; Constructs highlight-changes-face-list if necessary,
775 ;; and hilit-chg-list always:
776 ;; Maybe this should always be called when rotating a face
777 ;; so we pick up any changes?
778 (if (or (null highlight-changes-face-list
) ; Don't do it if it
779 force
) ; already exists unless FORCE non-nil.
780 (let ((p highlight-changes-colours
)
782 (setq highlight-changes-face-list nil
)
784 (setq name
(intern (format "highlight-changes-face-%d" n
)))
785 (copy-face 'highlight-changes-face name
)
786 (set-face-foreground name
(car p
))
787 (setq highlight-changes-face-list
788 (append highlight-changes-face-list
(list name
)))
791 (setq hilit-chg-list
(list 'hilit-chg
'highlight-changes-face
))
792 (let ((p highlight-changes-face-list
)
794 last-category last-face
)
796 (setq last-category
(intern (format "change-%d" n
)))
797 ;; (setq last-face (intern (format "highlight-changes-face-%d" n)))
798 (setq last-face
(car p
))
800 (append hilit-chg-list
801 (list last-category last-face
)))
805 (append hilit-chg-list
806 (list last-category last-face
)))
810 (defun hilit-chg-bump-change (prop start end
)
811 "Increment (age) the Highlight Changes mode text property of type change."
813 (if (eq prop
'hilit-chg-delete
)
814 (setq new-prop
(nth 2 hilit-chg-list
))
815 (setq new-prop
(nth 2 (member prop hilit-chg-list
)))
818 (put-text-property start end
'hilit-chg new-prop
)
819 (message "%d-%d unknown property %s not changed" start end prop
)
824 (defun highlight-changes-rotate-faces ()
825 "Rotate the faces used by Highlight Changes mode.
827 Current changes will be display in the face described by the first element
828 of highlight-changes-face-list, those (older) changes will be shown in the
829 face described by the second element, and so on. Very old changes remain
830 shown in the last face in the list.
832 You can automatically rotate colours when the buffer is saved
833 by adding this to local-write-file-hooks, by evaling (in the
835 (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
838 ;; If not in active mode do nothing but don't complain because this
839 ;; may be bound to a hook.
840 (if (eq highlight-changes-mode
'active
)
841 (let ((after-change-functions nil
))
842 ;; ensure hilit-chg-list is made and up to date
843 (hilit-chg-make-list)
844 ;; remove our existing overlays
845 (hilit-chg-hide-changes)
846 ;; for each change text property, increment it
847 (hilit-chg-map-changes 'hilit-chg-bump-change
)
848 ;; and display them all if active
849 (if (eq highlight-changes-mode
'active
)
850 (hilit-chg-display-changes))
852 ;; This always returns nil so it is safe to use in
853 ;; local-write-file-hook
857 ;; ========================================================================
858 ;; Comparing with an existing file.
859 ;; This uses ediff to find the differences.
862 (defun highlight-compare-with-file (file-b)
863 "Compare this buffer with a file, and highlight differences.
865 The current buffer must be an unmodified buffer visiting a file,
866 and not in read-only mode.
868 If the backup filename exists, it is used as the default
869 when called interactively.
871 If a buffer is visiting the file being compared against, it also will
872 have its differences highlighted. Otherwise, the file is read in
873 temporarily but the buffer is deleted.
875 If a buffer is read-only, differences will be highlighted but no property
876 changes made, so \\[highlight-changes-next-change] and
877 \\[highlight-changes-previous-change] will not work."
880 "File to compare with? " ;; prompt
884 (let ((f (make-backup-file-name
885 (or (buffer-file-name (current-buffer))
886 (error "no file for this buffer")))))
887 (if (file-exists-p f
) f
""))
890 (let* ((buf-a (current-buffer))
891 (buf-a-read-only buffer-read-only
)
893 (file-a (buffer-file-name))
894 (existing-buf (get-file-buffer file-b
))
895 (buf-b (or existing-buf
896 (find-file-noselect file-b
)))
897 (buf-b-read-only (with-current-buffer buf-b buffer-read-only
))
903 ;; We use the fact that the buffer is not marked modified at the
904 ;; end where we clear its modified status
905 (if (buffer-modified-p buf-a
)
906 (if (y-or-n-p (format "OK to save %s? " file-a
))
908 (error "Buffer must be saved before comparing with a file.")))
909 (if (and existing-buf
(buffer-modified-p buf-b
))
910 (if (y-or-n-p (format "OK to save %s? " file-b
))
912 (error "Cannot compare with a file in an unsaved buffer.")))
913 (highlight-changes-mode 'active
)
914 (if existing-buf
(with-current-buffer buf-b
915 (highlight-changes-mode 'active
)))
916 (save-window-excursion
917 (setq xy
(hilit-chg-get-diff-info buf-a file-a buf-b file-b
)))
920 (setq yy
(car (cdr xy
)))
922 (hilit-chg-make-list)
924 (setq a-start
(nth 0 (car p
)))
925 (setq a-end
(nth 1 (car p
)))
926 (setq b-start
(nth 0 (car q
)))
927 (setq b-end
(nth 1 (car q
)))
928 (setq len-a
(- a-end a-start
))
929 (setq len-b
(- b-end b-start
))
931 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only
)
932 (set-buffer-modified-p nil
)
935 (with-current-buffer buf-b
936 (hilit-chg-set-face-on-change b-start b-end len-a
943 (set-buffer-modified-p nil
)
948 (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b
)
949 (let ((e nil
) x y
) ;; e is set by function hilit-chg-get-diff-list-hk
950 (ediff-setup buf-a file-a buf-b file-b
951 nil nil
; buf-c file-C
952 'hilit-chg-get-diff-list-hk
953 (list (cons 'ediff-job-name
'something
))
955 (ediff-with-current-buffer e
(ediff-really-quit nil
))
959 (defun hilit-chg-get-diff-list-hk ()
960 ;; x and y are dynamically bound by hilit-chg-get-diff-info
961 ;; which calls this function as a hook
962 (defvar x
) ;; placate the byte-compiler
964 (setq e
(current-buffer))
965 (let ((n 0) extent p va vb a b
)
966 (setq x nil y nil
) ;; x and y are bound by hilit-chg-get-diff-info
967 (while (< n ediff-number-of-differences
)
968 (ediff-make-fine-diffs n
)
969 (setq va
(ediff-get-fine-diff-vector n
'A
))
970 ;; va is a vector if there are fine differences
972 (setq a
(append va nil
))
973 ;; if not, get the unrefined difference
974 (setq va
(ediff-get-difference n
'A
))
975 (setq a
(list (elt va
0)))
980 (setq extent
(list (overlay-start (car p
))
981 (overlay-end (car p
))))
983 (setq x
(append x
(list extent
) ))
986 (setq vb
(ediff-get-fine-diff-vector n
'B
))
989 (setq b
(append vb nil
))
990 ;; if not, get the unrefined difference
991 (setq vb
(ediff-get-difference n
'B
))
992 (setq b
(list (elt vb
0)))
997 (setq extent
(list (overlay-start (car p
))
998 (overlay-end (car p
))))
1000 (setq y
(append y
(list extent
) ))
1005 ;; ediff-quit doesn't work here.
1006 ;; No point in returning a value, since this is a hook function.
1009 ;; ======================= automatic stuff ==============
1011 ;; Global Highlight Changes mode is modelled after Global Font-lock mode.
1012 ;; Three hooks are used to gain control. When Global Changes Mode is
1013 ;; enabled, `find-file-hooks' and `change-major-mode-hook' are set.
1014 ;; `find-file-hooks' is called when visiting a file, the new mode is
1015 ;; known at this time.
1016 ;; `change-major-mode-hook' is called when a buffer is changing mode.
1017 ;; This could be because of finding a file in which case
1018 ;; `find-file-hooks' has already been called and has done its work.
1019 ;; However, it also catches the case where a new mode is being set by
1020 ;; the user. However, it is called from `kill-all-variables' and at
1021 ;; this time the mode is the old mode, which is not what we want.
1022 ;; So, our function temporarily sets `post-command-hook' which will
1023 ;; be called after the buffer has been completely set up (with the new
1024 ;; mode). It then removes the `post-command-hook'.
1025 ;; One other wrinkle - every M-x command runs the `change-major-mode-hook'
1026 ;; so we ignore this by examining the buffer name.
1029 (defun hilit-chg-major-mode-hook ()
1030 (add-hook 'post-command-hook
'hilit-chg-post-command-hook
)
1033 (defun hilit-chg-post-command-hook ()
1034 ;; This is called after changing a major mode, but also after each
1035 ;; M-x command, in which case the current buffer is a minibuffer.
1036 ;; In that case, do not act on it here, but don't turn it off
1037 ;; either, we will get called here again soon-after.
1038 ;; Also, don't enable it for other special buffers.
1039 (if (string-match "^[ *]" (buffer-name))
1040 nil
;; (message "ignoring this post-command-hook")
1041 (remove-hook 'post-command-hook
'hilit-chg-post-command-hook
)
1042 ;; The following check isn't necessary, since
1043 ;; hilit-chg-turn-on-maybe makes this check too.
1044 (or highlight-changes-mode
;; don't turn it on if it already is
1045 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state
))
1048 (defun hilit-chg-check-global ()
1049 ;; This is called from the find file hook.
1050 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state
))
1055 (defun global-highlight-changes (&optional arg
)
1056 "Turn on or off global Highlight Changes mode.
1058 When called interactively:
1059 - if no prefix, toggle global Highlight Changes mode on or off
1060 - if called with a positive prefix (or just C-u) turn it on in active mode
1061 - if called with a zero prefix turn it on in passive mode
1062 - if called with a negative prefix turn it off
1064 When called from a program:
1065 - if ARG is nil or omitted, turn it off
1066 - if ARG is 'active, turn it on in active mode
1067 - if ARG is 'passive, turn it on in passive mode
1068 - otherwise just turn it on
1070 When global Highlight Changes mode is enabled, Highlight Changes mode is turned
1071 on for future \"suitable\" buffers (and for \"suitable\" existing buffers if
1072 variable `highlight-changes-global-changes-existing-buffers' is non-nil).
1073 \"Suitablity\" is determined by variable `highlight-changes-global-modes'."
1078 ((null current-prefix-arg
)
1079 ;; no arg => toggle it on/off
1080 (setq global-highlight-changes
(not global-highlight-changes
)))
1081 ;; positive interactive arg - turn it on as active
1082 ((> (prefix-numeric-value current-prefix-arg
) 0)
1083 (setq global-highlight-changes t
)
1085 ;; zero interactive arg - turn it on as passive
1086 ((= (prefix-numeric-value current-prefix-arg
) 0)
1087 (setq global-highlight-changes t
)
1089 ;; negative interactive arg - turn it off
1091 (setq global-highlight-changes nil
)
1096 (if (eq arg
'active
)
1097 (setq highlight-changes-global-initial-state
'active
)
1098 (if (eq arg
'passive
)
1099 (setq highlight-changes-global-initial-state
'passive
)))
1100 (setq global-highlight-changes t
)
1101 (message "turning ON Global Highlight Changes mode in %s state"
1102 highlight-changes-global-initial-state
)
1103 (add-hook 'hilit-chg-major-mode-hook
'hilit-chg-major-mode-hook
)
1104 (add-hook 'find-file-hooks
'hilit-chg-check-global
)
1105 (if highlight-changes-global-changes-existing-buffers
1106 (hilit-chg-update-all-buffers
1107 highlight-changes-global-initial-state
))
1109 (message "turning OFF global Highlight Changes mode")
1110 (remove-hook 'hilit-chg-major-mode-hook
'hilit-chg-major-mode-hook
)
1111 (remove-hook 'find-file-hooks
'hilit-chg-check-global
)
1112 (remove-hook 'post-command-hook
1113 'hilit-chg-post-command-hook
)
1114 (remove-hook 'find-file-hooks
'hilit-chg-check-global
)
1115 (if highlight-changes-global-changes-existing-buffers
1116 (hilit-chg-update-all-buffers nil
))
1124 (defun hilit-chg-turn-on-maybe (value)
1125 "Turn on Highlight Changes mode if it is appropriate for this buffer.
1127 A buffer is appropriate for Highlight Changes mode if all these are true:
1128 - the buffer is not a special buffer (one whose name begins with
1130 - the buffer's mode is suitable as per variable highlight-changes-global-modes
1131 - Highlight Changes mode is not already on for this buffer.
1133 This function is called from hilit-chg-update-all-buffers
1134 from `global-highlight-changes' when turning on global Highlight Changes mode.
1136 (or highlight-changes-mode
; do nothing if already on
1139 ((null highlight-changes-global-modes
)
1141 ((functionp highlight-changes-global-modes
)
1142 (funcall highlight-changes-global-modes
))
1143 ((listp highlight-changes-global-modes
)
1144 (if (eq (car-safe highlight-changes-global-modes
) 'not
)
1145 (not (memq major-mode
(cdr highlight-changes-global-modes
)))
1146 (memq major-mode highlight-changes-global-modes
)))
1149 (not (string-match "^[ *]" (buffer-name)))
1153 (hilit-chg-set value
)
1154 (run-hooks 'highlight-changes-enable-hook
)))
1158 (defun hilit-chg-turn-off-maybe ()
1159 (if highlight-changes-mode
1161 (run-hooks 'highlight-changes-disable-hook
)
1162 (hilit-chg-clear))))
1166 (defun hilit-chg-update-all-buffers (value)
1168 (function (lambda (buffer)
1169 (with-current-buffer buffer
1171 (hilit-chg-turn-on-maybe value
)
1172 (hilit-chg-turn-off-maybe))
1176 ;; ===================== debug ==================
1177 ;; For debug & test use:
1179 ;; (defun hilit-chg-debug-show (&optional beg end)
1181 ;; (message "--- hilit-chg-debug-show ---")
1182 ;; (hilit-chg-map-changes '(lambda (prop start end)
1183 ;; (message "%d-%d: %s" start end prop)
1188 ;; ================== end of debug ===============
1191 (provide 'hilit-chg
)
1193 ;;; hilit-chg.el ends here