(font-lock-keywords): Fix doc for multiline matches.
[emacs.git] / lisp / hilit-chg.el
blobc7a0586aa44362f145ec30ff91f36c1e362e09b7
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>
6 ;; Keywords: faces
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)
13 ;; any later version.
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.
26 ;;; Commentary:
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
39 ;; distraction.
40 ;;
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
65 ;; of the file).
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
72 ;; is turned off.
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'.)
79 ;;
81 ;;
82 ;; Example usage:
83 ;; (defun my-highlight-changes-enable-hook ()
84 ;; (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
85 ;; )
86 ;;
87 ;; (defun my-highlight-changes-disable-hook ()
88 ;; (remove-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
89 ;; )
90 ;;
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 ;; Explciit 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
102 ;; follows.
104 ;; 1. Most modes have a major-hook, typically called MODE-hook. You
105 ;; can use `add-hook' to call `highlight-changes-mode'.
107 ;; Example:
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
113 ;; (lambda ()
114 ;; (highlight-changes-mode 'passive)))
116 ;; However, this cannot be done for Fundamental mode for there is no
117 ;; such hook.
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
132 ;; buffers) are not;
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.
158 ;;; Bugs:
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.)
176 ;;; History:
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.
186 ;; July 98
187 ;; - global mode and various stuff added
188 ;; - Changed to use overlays
189 ;; August 98
190 ;; - renmaed to Highlight Changes mode.
193 ;;; Code:
195 (require 'wid-edit)
197 ;; ====================== Customization =======================
198 (defgroup highlight-changes nil
199 "Highlight Changes mode."
200 :version "20.4"
201 :group 'faces)
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
294 buffer is suitable.
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).
305 Examples:
306 (c-mode c++-mode)
307 means that Highlight Changes mode is turned on for buffers in C and C++
308 modes only."
309 :type '(choice
310 (const :tag "all non-special buffers visiting files" t)
311 (set :menu-tag "specific modes" :tag "modes"
312 :value (not)
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."
332 :type 'boolean
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)
345 (let
346 ((p (reverse old-list))
347 (n (length old-list))
348 new-name old-name
349 (new-list nil)
351 (while p
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))
365 (setq n (1- n))
366 (setq p (cdr p)))
367 (if (equal new-list (widget-value w))
368 nil ;; (message "notify: no change!")
369 (widget-value-set w new-list)
370 (widget-setup)
373 ;; (message "notify: no default here!")
375 (let ((parent (widget-get w :parent)))
376 (when 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'."
389 :type '(choice
390 (repeat
391 :notify hilit-chg-cust-fix-changes-face-list
392 face )
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'.
403 ;;;###autoload
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)
416 (eval-and-compile
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")
429 ;;; Functions...
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)))
438 prop end)
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))
442 (if prop
443 (funcall func prop start (or end limit)))
444 (setq start end)
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))
462 face)
463 (or prop
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))))
468 (if face
469 (progn
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)))
492 p ov)
493 (setq p (overlays-in start limit))
494 (while p
495 ;; don't delete the overlay if it isn't ours!
496 (if (overlay-get (car p) 'hilit-chg)
497 (delete-overlay (car p)))
498 (setq p (cdr 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
508 props q)
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"))
513 (while p
514 (setq ov (car p))
515 (setq ov-start (overlay-start ov))
516 (setq ov-end (overlay-end ov))
517 (if (< ov-start beg)
518 (progn
519 (move-overlay ov ov-start beg)
520 (if (> ov-end end)
521 (progn
522 (setq props (overlay-properties ov))
523 (setq ov (make-overlay end ov-end))
524 (while props
525 (overlay-put ov (car props)(car (cdr props)))
526 (setq props (cdr (cdr props))))
530 (if (> ov-end end)
531 (move-overlay ov end ov-end)
532 (delete-overlay ov)
534 (setq p (cdr p)))
535 (hilit-chg-display-changes beg end)
543 ;;;###autoload
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."
547 (interactive "r")
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.
564 (save-match-data
565 (let ((beg-decr 1) (end-incr 1)
566 (type 'hilit-chg)
567 old)
568 (if undo-in-progress
569 (if (eq highlight-changes-mode 'active)
570 (hilit-chg-fixup beg end))
571 (if (and (= beg end) (> leng-before 0))
572 ;; deletion
573 (progn
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)
580 ;;; (if (eolp)
581 ;;; (setq beg-decr 0 end-incr 0)
582 ;;; (if (bolp)
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))
587 ;; Not a deletion.
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)
594 (progn
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))
603 ))))
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)
615 (progn
616 (setq hilit-chg-string highlight-changes-active-string)
617 (or buffer-read-only
618 (hilit-chg-display-changes)))
619 ;; mode is passive
620 (setq hilit-chg-string highlight-changes-passive-string)
621 (or buffer-read-only
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."
632 (if buffer-read-only
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"
636 (buffer-name))
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)
652 ;;;###autoload
653 (defun highlight-changes-mode (&optional arg)
654 "Toggle (or initially set) Highlight Changes mode.
656 Without an argument,
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.
661 With an argument,
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.
670 Functions:
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 \
677 through
678 various faces.
681 Hook variables:
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.
686 (interactive "P")
687 (if window-system
688 (let ((was-on highlight-changes-mode)
689 (new-highlight-changes-mode
690 (cond
691 ((null arg)
692 ;; no arg => toggle (or set to active initially)
693 (if highlight-changes-mode
694 (if (eq highlight-changes-mode 'active) 'passive 'active)
695 highlight-changes-initial-state))
696 ;; an argument is given
697 ((eq arg 'active)
698 'active)
699 ((eq arg 'passive)
700 'passive)
701 ((> (prefix-numeric-value arg) 0)
702 'active)
703 ((< (prefix-numeric-value arg) 0)
704 nil)
706 'passive)
708 (if new-highlight-changes-mode
709 ;; mode is turned on -- but may be passive
710 (progn
711 (hilit-chg-set new-highlight-changes-mode)
712 (or was-on
713 ;; run highlight-changes-enable-hook once
714 (run-hooks 'highlight-changes-enable-hook))
715 (run-hooks 'highlight-changes-toggle-hook))
716 ;; mode is turned off
717 (run-hooks 'highlight-changes-disable-hook)
718 (hilit-chg-clear))
720 (message "Highlight Changes mode only works when using a window system"))
725 ;;;###autoload
726 (defun highlight-changes-next-change ()
727 "Move to the beginning of the next change, if in Highlight Changes mode."
728 (interactive)
729 (if highlight-changes-mode
730 (let ((start (point))
731 prop)
732 (setq prop (get-text-property (point) 'hilit-chg))
733 (if prop
734 ;; we are in a change
735 (setq start (next-single-property-change (point) 'hilit-chg)))
736 (if start
737 (setq start (next-single-property-change start 'hilit-chg)))
738 (if start
739 (goto-char start)
740 (message "no next change")))
741 (message "This buffer is not in Highlight Changes mode.")))
744 ;;;###autoload
745 (defun highlight-changes-previous-change ()
746 "Move to the beginning of the previous change, if in Highlight Changes mode."
747 (interactive)
748 (if highlight-changes-mode
749 (let ( (start (point)) (prop nil) )
750 (or (bobp)
751 (setq prop (get-text-property (1- (point)) 'hilit-chg)))
752 (if prop
753 ;; we are in a change
754 (setq start (previous-single-property-change (point) 'hilit-chg)))
755 (if start
756 (setq start (previous-single-property-change start 'hilit-chg)))
757 ;; special handling for the case where (point-min) is a change
758 (if start
759 (setq start (or (previous-single-property-change start 'hilit-chg)
760 (if (get-text-property (point-min) 'hilit-chg)
761 (point-min)))))
762 (if start
763 (goto-char start)
764 (message "no previous change")))
765 (message "This buffer is not in Highlight Changes mode.")))
768 ;; ========================================================================
771 (defun hilit-chg-make-list (&optional force)
772 "Construct hilit-chg-list and highlight-changes-face-list."
773 ;; Constructs highlight-changes-face-list if necessary,
774 ;; and hilit-chg-list always:
775 ;; Maybe this should always be called when rotating a face
776 ;; so we pick up any changes?
777 (if (or (null highlight-changes-face-list) ; Don't do it if it
778 force) ; already exists unless FORCE non-nil.
779 (let ((p highlight-changes-colours)
780 (n 1) name)
781 (setq highlight-changes-face-list nil)
782 (while p
783 (setq name (intern (format "highlight-changes-face-%d" n)))
784 (copy-face 'highlight-changes-face name)
785 (set-face-foreground name (car p))
786 (setq highlight-changes-face-list
787 (append highlight-changes-face-list (list name)))
788 (setq p (cdr p))
789 (setq n (1+ n)))))
790 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes-face))
791 (let ((p highlight-changes-face-list)
792 (n 1)
793 last-category last-face)
794 (while p
795 (setq last-category (intern (format "change-%d" n)))
796 ;; (setq last-face (intern (format "highlight-changes-face-%d" n)))
797 (setq last-face (car p))
798 (setq hilit-chg-list
799 (append hilit-chg-list
800 (list last-category last-face)))
801 (setq p (cdr p))
802 (setq n (1+ n)))
803 (setq hilit-chg-list
804 (append hilit-chg-list
805 (list last-category last-face)))
809 (defun hilit-chg-bump-change (prop start end)
810 "Increment (age) the Highlight Changes mode text property of type change."
811 (let ( new-prop )
812 (if (eq prop 'hilit-chg-delete)
813 (setq new-prop (nth 2 hilit-chg-list))
814 (setq new-prop (nth 2 (member prop hilit-chg-list)))
816 (if prop
817 (put-text-property start end 'hilit-chg new-prop)
818 (message "%d-%d unknown property %s not changed" start end prop)
822 ;;;###autoload
823 (defun highlight-changes-rotate-faces ()
824 "Rotate the faces used by Highlight Changes mode.
826 Current changes will be display in the face described by the first element
827 of highlight-changes-face-list, those (older) changes will be shown in the
828 face described by the second element, and so on. Very old changes remain
829 shown in the last face in the list.
831 You can automatically rotate colours when the buffer is saved
832 by adding this to local-write-file-hooks, by evaling (in the
833 buffer to be saved):
834 (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
836 (interactive)
837 ;; If not in active mode do nothing but don't complain because this
838 ;; may be bound to a hook.
839 (if (eq highlight-changes-mode 'active)
840 (let ((after-change-functions nil))
841 ;; ensure hilit-chg-list is made and up to date
842 (hilit-chg-make-list)
843 ;; remove our existing overlays
844 (hilit-chg-hide-changes)
845 ;; for each change text property, increment it
846 (hilit-chg-map-changes 'hilit-chg-bump-change)
847 ;; and display them all if active
848 (if (eq highlight-changes-mode 'active)
849 (hilit-chg-display-changes))
851 ;; This always returns nil so it is safe to use in
852 ;; local-write-file-hook
853 nil)
856 ;; ========================================================================
857 ;; Comparing with an existing file.
858 ;; This uses ediff to find the differences.
860 ;;;###autoload
861 (defun highlight-compare-with-file (file-b)
862 "Compare this buffer with a file, and highlight differences.
864 The current buffer must be an unmodified buffer visiting a file,
865 and not in read-only mode.
867 If the backup filename exists, it is used as the default
868 when called interactively.
870 If a buffer is visiting the file being compared against, it also will
871 have its differences highlighted. Otherwise, the file is read in
872 temporarily but the buffer is deleted.
874 If a buffer is read-only, differences will be highlighted but no property
875 changes made, so \\[highlight-changes-next-change] and
876 \\[highlight-changes-previous-change] will not work."
877 (interactive (list
878 (read-file-name
879 "File to compare with? " ;; prompt
880 "" ;; directory
881 nil ;; default
882 'yes ;; must exist
883 (let ((f (make-backup-file-name
884 (or (buffer-file-name (current-buffer))
885 (error "no file for this buffer")))))
886 (if (file-exists-p f) f ""))
889 (let* ((buf-a (current-buffer))
890 (buf-a-read-only buffer-read-only)
891 (orig-pos (point))
892 (file-a (buffer-file-name))
893 (existing-buf (get-file-buffer file-b))
894 (buf-b (or existing-buf
895 (find-file-noselect file-b)))
896 (buf-b-read-only (with-current-buffer buf-b buffer-read-only))
897 xy xx yy p q
898 a-start a-end len-a
899 b-start b-end len-b
902 ;; We use the fact that the buffer is not marked modified at the
903 ;; end where we clear its modified status
904 (if (buffer-modified-p buf-a)
905 (if (y-or-n-p (format "OK to save %s? " file-a))
906 (save-buffer buf-a)
907 (error "Buffer must be saved before comparing with a file.")))
908 (if (and existing-buf (buffer-modified-p buf-b))
909 (if (y-or-n-p (format "OK to save %s? " file-b))
910 (save-buffer buf-b)
911 (error "Cannot compare with a file in an unsaved buffer.")))
912 (highlight-changes-mode 'active)
913 (if existing-buf (with-current-buffer buf-b
914 (highlight-changes-mode 'active)))
915 (save-window-excursion
916 (setq xy (hilit-chg-get-diff-info buf-a file-a buf-b file-b)))
917 (setq xx (car xy))
918 (setq p xx)
919 (setq yy (car (cdr xy)))
920 (setq q yy)
921 (hilit-chg-make-list)
922 (while p
923 (setq a-start (nth 0 (car p)))
924 (setq a-end (nth 1 (car p)))
925 (setq b-start (nth 0 (car q)))
926 (setq b-end (nth 1 (car q)))
927 (setq len-a (- a-end a-start))
928 (setq len-b (- b-end b-start))
929 (set-buffer buf-a)
930 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
931 (set-buffer-modified-p nil)
932 (goto-char orig-pos)
933 (if existing-buf
934 (with-current-buffer buf-b
935 (hilit-chg-set-face-on-change b-start b-end len-a
936 buf-b-read-only )
938 (setq p (cdr p))
939 (setq q (cdr q))
941 (if existing-buf
942 (set-buffer-modified-p nil)
943 (kill-buffer buf-b))
947 (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b)
948 (let ((e nil) x y) ;; e is set by function hilit-chg-get-diff-list-hk
949 (ediff-setup buf-a file-a buf-b file-b
950 nil nil ; buf-c file-C
951 'hilit-chg-get-diff-list-hk
952 (list (cons 'ediff-job-name 'something))
954 (ediff-with-current-buffer e (ediff-really-quit nil))
955 (list x y)))
958 (defun hilit-chg-get-diff-list-hk ()
959 ;; x and y are dynamically bound by hilit-chg-get-diff-info
960 ;; which calls this function as a hook
961 (defvar x) ;; placate the byte-compiler
962 (defvar y)
963 (setq e (current-buffer))
964 (let ((n 0) extent p va vb a b)
965 (setq x nil y nil) ;; x and y are bound by hilit-chg-get-diff-info
966 (while (< n ediff-number-of-differences)
967 (ediff-make-fine-diffs n)
968 (setq va (ediff-get-fine-diff-vector n 'A))
969 ;; va is a vector if there are fine differences
970 (if va
971 (setq a (append va nil))
972 ;; if not, get the unrefined difference
973 (setq va (ediff-get-difference n 'A))
974 (setq a (list (elt va 0)))
976 ;; a list a list
977 (setq p a)
978 (while p
979 (setq extent (list (overlay-start (car p))
980 (overlay-end (car p))))
981 (setq p (cdr p))
982 (setq x (append x (list extent) ))
983 );; while p
985 (setq vb (ediff-get-fine-diff-vector n 'B))
986 ;; vb is a vector
987 (if vb
988 (setq b (append vb nil))
989 ;; if not, get the unrefined difference
990 (setq vb (ediff-get-difference n 'B))
991 (setq b (list (elt vb 0)))
993 ;; b list a list
994 (setq p b)
995 (while p
996 (setq extent (list (overlay-start (car p))
997 (overlay-end (car p))))
998 (setq p (cdr p))
999 (setq y (append y (list extent) ))
1000 );; while p
1002 (setq n (1+ n))
1003 );; while
1004 ;; ediff-quit doesn't work here.
1005 ;; No point in returning a value, since this is a hook function.
1008 ;; ======================= automatic stuff ==============
1010 ;; Global Highlight Changes mode is modelled after Global Font-lock mode.
1011 ;; Three hooks are used to gain control. When Global Changes Mode is
1012 ;; enabled, `find-file-hooks' and `change-major-mode-hook' are set.
1013 ;; `find-file-hooks' is called when visiting a file, the new mode is
1014 ;; known at this time.
1015 ;; `change-major-mode-hook' is called when a buffer is changing mode.
1016 ;; This could be because of finding a file in which case
1017 ;; `find-file-hooks' has already been called and has done its work.
1018 ;; However, it also catches the case where a new mode is being set by
1019 ;; the user. However, it is called from `kill-all-variables' and at
1020 ;; this time the mode is the old mode, which is not what we want.
1021 ;; So, our function temporarily sets `post-command-hook' which will
1022 ;; be called after the buffer has been completely set up (with the new
1023 ;; mode). It then removes the `post-command-hook'.
1024 ;; One other wrinkle - every M-x command runs the `change-major-mode-hook'
1025 ;; so we ignore this by examining the buffer name.
1028 (defun hilit-chg-major-mode-hook ()
1029 (add-hook 'post-command-hook 'hilit-chg-post-command-hook)
1032 (defun hilit-chg-post-command-hook ()
1033 ;; This is called after changing a major mode, but also after each
1034 ;; M-x command, in which case the current buffer is a minibuffer.
1035 ;; In that case, do not act on it here, but don't turn it off
1036 ;; either, we will get called here again soon-after.
1037 ;; Also, don't enable it for other special buffers.
1038 (if (string-match "^[ *]" (buffer-name))
1039 nil ;; (message "ignoring this post-command-hook")
1040 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook)
1041 ;; The following check isn't necessary, since
1042 ;; hilit-chg-turn-on-maybe makes this check too.
1043 (or highlight-changes-mode ;; don't turn it on if it already is
1044 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state))
1047 (defun hilit-chg-check-global ()
1048 ;; This is called from the find file hook.
1049 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state))
1053 ;;;###autoload
1054 (defun global-highlight-changes (&optional arg)
1055 "Turn on or off global Highlight Changes mode.
1057 When called interactively:
1058 - if no prefix, toggle global Highlight Changes mode on or off
1059 - if called with a positive prefix (or just C-u) turn it on in active mode
1060 - if called with a zero prefix turn it on in passive mode
1061 - if called with a negative prefix turn it off
1063 When called from a program:
1064 - if ARG is nil or omitted, turn it off
1065 - if ARG is 'active, turn it on in active mode
1066 - if ARG is 'passive, turn it on in passive mode
1067 - otherwise just turn it on
1069 When global Highlight Changes mode is enabled, Highlight Changes mode is turned
1070 on for future \"suitable\" buffers (and for \"suitable\" existing buffers if
1071 variable `highlight-changes-global-changes-existing-buffers' is non-nil).
1072 \"Suitablity\" is determined by variable `highlight-changes-global-modes'."
1074 (interactive
1075 (list
1076 (cond
1077 ((null current-prefix-arg)
1078 ;; no arg => toggle it on/off
1079 (setq global-highlight-changes (not global-highlight-changes)))
1080 ;; positive interactive arg - turn it on as active
1081 ((> (prefix-numeric-value current-prefix-arg) 0)
1082 (setq global-highlight-changes t)
1083 'active)
1084 ;; zero interactive arg - turn it on as passive
1085 ((= (prefix-numeric-value current-prefix-arg) 0)
1086 (setq global-highlight-changes t)
1087 'passive)
1088 ;; negative interactive arg - turn it off
1090 (setq global-highlight-changes nil)
1091 nil))))
1093 (if arg
1094 (progn
1095 (if (eq arg 'active)
1096 (setq highlight-changes-global-initial-state 'active)
1097 (if (eq arg 'passive)
1098 (setq highlight-changes-global-initial-state 'passive)))
1099 (setq global-highlight-changes t)
1100 (message "turning ON Global Highlight Changes mode in %s state"
1101 highlight-changes-global-initial-state)
1102 (add-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook)
1103 (add-hook 'find-file-hooks 'hilit-chg-check-global)
1104 (if highlight-changes-global-changes-existing-buffers
1105 (hilit-chg-update-all-buffers
1106 highlight-changes-global-initial-state))
1108 (message "turning OFF global Highlight Changes mode")
1109 (remove-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook)
1110 (remove-hook 'find-file-hooks 'hilit-chg-check-global)
1111 (remove-hook 'post-command-hook
1112 'hilit-chg-post-command-hook)
1113 (remove-hook 'find-file-hooks 'hilit-chg-check-global)
1114 (if highlight-changes-global-changes-existing-buffers
1115 (hilit-chg-update-all-buffers nil))
1123 (defun hilit-chg-turn-on-maybe (value)
1124 "Turn on Highlight Changes mode if it is appropriate for this buffer.
1126 A buffer is appropriate for Highlight Changes mode if all these are true:
1127 - the buffer is not a special buffer (one whose name begins with
1128 `*' or ` ')
1129 - the buffer's mode is suitable as per variable highlight-changes-global-modes
1130 - Highlight Changes mode is not already on for this buffer.
1132 This function is called from hilit-chg-update-all-buffers
1133 from `global-highlight-changes' when turning on global Highlight Changes mode.
1135 (or highlight-changes-mode ; do nothing if already on
1137 (cond
1138 ((null highlight-changes-global-modes)
1139 nil)
1140 ((functionp highlight-changes-global-modes)
1141 (funcall highlight-changes-global-modes))
1142 ((listp highlight-changes-global-modes)
1143 (if (eq (car-safe highlight-changes-global-modes) 'not)
1144 (not (memq major-mode (cdr highlight-changes-global-modes)))
1145 (memq major-mode highlight-changes-global-modes)))
1147 (and
1148 (not (string-match "^[ *]" (buffer-name)))
1149 (buffer-file-name))
1151 (progn
1152 (hilit-chg-set value)
1153 (run-hooks 'highlight-changes-enable-hook)))
1157 (defun hilit-chg-turn-off-maybe ()
1158 (if highlight-changes-mode
1159 (progn
1160 (run-hooks 'highlight-changes-disable-hook)
1161 (hilit-chg-clear))))
1165 (defun hilit-chg-update-all-buffers (value)
1166 (mapcar
1167 (function (lambda (buffer)
1168 (with-current-buffer buffer
1169 (if value
1170 (hilit-chg-turn-on-maybe value)
1171 (hilit-chg-turn-off-maybe))
1173 (buffer-list)))
1175 ;; ===================== debug ==================
1176 ;; For debug & test use:
1178 ;; (defun hilit-chg-debug-show (&optional beg end)
1179 ;; (interactive)
1180 ;; (message "--- hilit-chg-debug-show ---")
1181 ;; (hilit-chg-map-changes '(lambda (prop start end)
1182 ;; (message "%d-%d: %s" start end prop)
1183 ;; )
1184 ;; beg end
1185 ;; ))
1187 ;; ================== end of debug ===============
1190 (provide 'hilit-chg)
1192 ;;; hilit-chg.el ends here