Some fixes to follow coding conventions.
[bpt/emacs.git] / lisp / hilit-chg.el
CommitLineData
e287d328
RS
1;;; hilit-chg.el --- minor mode displaying buffer changes with special face
2
3ec30bcb 3;; Copyright (C) 1998, 2000 Free Software Foundation, Inc.
e287d328 4
08246f2e 5;; Author: Richard Sharman <rsharman@pobox.com>
e287d328
RS
6;; Keywords: faces
7
66236b77
KH
8;; This file is part of GNU Emacs.
9
2be7dabc 10;; GNU Emacs is free software; you can redistribute it and/or modify
e287d328
RS
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.
14
2be7dabc 15;; GNU Emacs is distributed in the hope that it will be useful,
e287d328
RS
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.
19
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.
24
e287d328
RS
25;;; Commentary:
26
27;; A minor mode: "Highlight Changes mode".
28;;
29
30;; Highlight Changes mode has 2 submodes: active and passive.
31;; When active, changes to the buffer are displayed in a different face.
32;; When passive, any existing displayed changes are saved and new ones
33;; recorded but are not displayed differently.
34;; Why active and passive? Having the changes visible can be handy when you
35;; want the information but very distracting otherwise. So, you can keep
36;; Highlight Changes mode in passive state while you make your changes, toggle
37;; it on to active mode to see them, then toggle it back off to avoid
38;; distraction.
39;;
40;; When active, changes are displayed in `highlight-changes-face'. When
41;; text is deleted, the following character is displayed in
42;; `highlight-changes-delete-face' face.
43;;
44;;
45;; You can "age" different sets of changes by using
3ec30bcb 46;; `highlight-changes-rotate-faces'. This rotates through a series
e287d328 47;; of different faces, so you can distinguish "new" changes from "older"
00e25b96 48;; changes. You can customize these "rotated" faces in two ways. You can
e287d328
RS
49;; either explicitly define each face by customizing
50;; `highlight-changes-face-list'. If, however, the faces differ from
3ec30bcb 51;; `highlight-changes-face' only in the foreground color, you can simply set
e287d328
RS
52;; `highlight-changes-colours'. If `highlight-changes-face-list' is nil when
53;; the faces are required they will be constructed from
54;; `highlight-changes-colours'.
55;;
56;;
57;; When a Highlight Changes mode is on (either active or passive) you can go
58;; to the next or previous change with `highlight-changes-next-change' and
59;; `highlight-changes-previous-change'.
60;;
61;;
99f08df4
KH
62;; You can also use the command highlight-compare-with-file to show changes
63;; in this file compared with another file (typically the previous version
64;; of the file).
e287d328
RS
65;;
66;;
67;; There are currently three hooks run by `highlight-changes-mode':
68;; `highlight-changes-enable-hook' - is run when Highlight Changes mode
69;; is initially enabled for a buffer.
70;; `highlight-changes-disable-hook' - is run when Highlight Changes mode
71;; is turned off.
72;; `highlight-changes-toggle-hook' - is run each time `highlight-changes-mode'
73;; is called. Typically this is when
74;; toggling between active and passive
75;; modes. The variable
76;; `highlight-changes-mode' contains the new
77;; state (`active' or `passive'.)
78;;
79;;
80;;
81;; Example usage:
82;; (defun my-highlight-changes-enable-hook ()
83;; (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
84;; )
85;;
86;; (defun my-highlight-changes-disable-hook ()
87;; (remove-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
88;; )
89;;
90;; (add-hook 'highlight-changes-enable-hook 'my-highlight-changes-enable-hook)
91;; (add-hook 'highlight-changes-disable-hook
92;; 'my-highlight-changes-disable-hook)
93
94
3ec30bcb 95;; Explicit vs. Implicit
e287d328
RS
96;;
97
98;; Normally, Highlight Changes mode is turned on explicitly in a buffer.
99;;
100;; If you prefer to have it automatically invoked you can do it as
101;; follows.
102;;
103;; 1. Most modes have a major-hook, typically called MODE-hook. You
104;; can use `add-hook' to call `highlight-changes-mode'.
105;;
106;; Example:
107;; (add-hook 'c-mode-hook 'highlight-changes-mode)
108;;
109;; If you want to make it start up in passive mode (regardless of the
110;; setting of highlight-changes-initial-state):
111;; (add-hook 'emacs-lisp-mode-hook
112;; (lambda ()
113;; (highlight-changes-mode 'passive)))
114;;
115;; However, this cannot be done for Fundamental mode for there is no
116;; such hook.
117;;
118;; 2. You can use the function `global-highlight-changes'
119;;
120;; This function, which is fashioned after the way `global-font-lock' works,
121;; toggles on or off global Highlight Changes mode. When activated, it turns
3ec30bcb 122;; on Highlight Changes mode in all "suitable" existing buffers and will turn
e287d328
RS
123;; it on in new "suitable" buffers to be created.
124;;
125;; A buffer's "suitability" is determined by variable
126;; `highlight-changes-global-modes', as follows. If the variable is
127;; * nil -- then no buffers are suitable;
128;; * a function -- this function is called and the result is used. As
3ec30bcb 129;; an example, if the value is `buffer-file-name' then all buffers
e287d328
RS
130;; who are visiting files are suitable, but others (like dired
131;; buffers) are not;
72f7b362
GM
132;; * a list -- then the buffer is suitable iff its mode is in the
133;; list, except if the first element is `not', in which case the test
e287d328
RS
134;; is reversed (i.e. it is a list of unsuitable modes).
135;; * Otherwise, the buffer is suitable if its name does not begin with
136;; ` ' or `*' and if `buffer-file-name' returns true.
137;;
138
139
140
141;; Possible bindings:
142;; (global-set-key '[C-right] 'highlight-changes-next-change)
143;; (global-set-key '[C-left] 'highlight-changes-previous-change)
144;;
145;; Other interactive functions (which could be bound if desired):
146;; highlight-changes-mode
147;; highlight-changes-remove-highlight
148;; highlight-changes-rotate-faces
99f08df4 149;; highlight-compare-with-file
e287d328
RS
150
151;;
152;; You can automatically rotate faces when the buffer is saved;
153;; see function `highlight-changes-rotate-faces' for how to do this.
154;;
155
156
157;;; Bugs:
158
159;; - the next-change and previous-change functions are too literal;
160;; they should find the next "real" change, in other words treat
161;; consecutive changes as one.
162
163
164;;; To do (maybe), notes, ...
165
166;; - having different faces for deletion and non-deletion: is it
167;; really worth the hassle?
168;; - should have better hooks: when should they be run?
99f08df4
KH
169;; - highlight-compare-with-file should allow RCS files - e.g. nice to be
170;; able to say show changes compared with version 2.1.
e287d328
RS
171;; - Maybe we should have compare-with-buffer as well. (When I tried
172;; a while back I ran into a problem with ediff-buffers-internal.)
173
174
175;;; History:
176
177;; R Sharman (rsharman@magma.ca) Feb 1998:
178;; - initial release as change-mode.
e287d328
RS
179;; Jari Aalto <jari.aalto@ntc.nokia.com> Mar 1998
180;; - fixes for byte compile errors
181;; - use eval-and-compile for autoload
182;; Marijn Ros <J.M.Ros@fys.ruu.nl> Mar 98
183;; - suggested turning it on by default
e287d328
RS
184;; Eric Ludlam <zappo@gnu.org> Suggested using overlays.
185;; July 98
186;; - global mode and various stuff added
187;; - Changed to use overlays
188;; August 98
3ec30bcb 189;; - renamed to Highlight Changes mode.
e287d328
RS
190
191
192;;; Code:
193
e287d328
RS
194(require 'wid-edit)
195
196;; ====================== Customization =======================
197(defgroup highlight-changes nil
198 "Highlight Changes mode."
a7845785 199 :version "20.4"
e287d328
RS
200 :group 'faces)
201
202
203;; Face information: How the changes appear.
204
205;; Defaults for face: red foreground, no change to background,
206;; and underlined if a change is because of a deletion.
3ec30bcb 207;; Note: underlining is helpful in that it shows up changes in white space.
e287d328
RS
208;; However, having it set for non-delete changes can be annoying because all
209;; indentation on inserts gets underlined (which can look pretty ugly!).
210
211(defface highlight-changes-face
212 '((((class color)) (:foreground "red" ))
213 (t (:inverse-video t)))
214 "Face used for highlighting changes."
3ec30bcb 215 :group 'highlight-changes)
e287d328
RS
216
217;; This looks pretty ugly, actually. Maybe the underline should be removed.
218(defface highlight-changes-delete-face
219 '((((class color)) (:foreground "red" :underline t))
220 (t (:inverse-video t)))
221 "Face used for highlighting deletions."
3ec30bcb 222 :group 'highlight-changes)
e287d328
RS
223
224
225
226;; A (not very good) default list of colours to rotate through.
227;;
228(defcustom highlight-changes-colours
229 (if (eq (frame-parameter nil 'background-mode) 'light)
230 ;; defaults for light background:
231 '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue")
232 ;; defaults for dark background:
233 '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid"))
234 "*Colours used by `highlight-changes-rotate-faces'.
235The newest rotated change will be displayed in the first element of this list,
236the next older will be in the second element etc.
237
238This list is used if `highlight-changes-face-list' is nil, otherwise that
239variable overrides this list. If you only care about foreground
240colours then use this, if you want fancier faces then set
241`highlight-changes-face-list'."
3ec30bcb
GM
242 :type '(repeat color)
243 :group 'highlight-changes)
e287d328
RS
244
245
246;; If you invoke highlight-changes-mode with no argument, should it start in
247;; active or passive mode?
248;;
249(defcustom highlight-changes-initial-state 'active
250 "*What state (active or passive) `highlight-changes' should start in.
251This is used when `highlight-changes' is called with no argument.
252This variable must be set to one of the symbols `active' or `passive'."
253 :type '(choice (const :tag "Active" active)
254 (const :tag "Passive" passive))
3ec30bcb 255 :group 'highlight-changes)
e287d328
RS
256
257(defcustom highlight-changes-global-initial-state 'passive
258 "*What state `global-highlight-changes' should start in.
259This is used if `global-highlight-changes' is called with no argument.
260This variable must be set to either `active' or `passive'"
261 :type '(choice (const :tag "Active" active)
262 (const :tag "Passive" passive))
3ec30bcb 263 :group 'highlight-changes)
e287d328
RS
264
265;; The strings displayed in the mode-line for the minor mode:
266(defcustom highlight-changes-active-string nil
267 "*The string used when Highlight Changes mode is in the active state.
268This should be set to nil if no indication is desired, or to
269a string with a leading space."
270 :type '(choice string
271 (const :tag "None" nil))
3ec30bcb 272 :group 'highlight-changes)
e287d328
RS
273
274(defcustom highlight-changes-passive-string " Chg"
275 "*The string used when Highlight Changes mode is in the passive state.
276This should be set to nil if no indication is desired, or to
277a string with a leading space."
278 :type '(choice string
279 (const :tag "None" nil))
3ec30bcb 280 :group 'highlight-changes)
e287d328
RS
281
282(defcustom highlight-changes-global-modes t
283 "*Determine whether a buffer is suitable for global Highlight Changes mode.
284
3ec30bcb 285A function means that function is called: if it returns non-nil, the
e287d328
RS
286buffer is suitable.
287
288A list is a list of modes for which it is suitable, or a list whose
3ec30bcb 289first element is `not' followed by modes which are not suitable.
e287d328
RS
290
291t means the buffer is suitable if its name does not begin with ` ' nor
292`*' and the buffer has a filename.
293
3ec30bcb 294A value of nil means no buffers are suitable for `global-highlight-changes'
e287d328
RS
295(effectively disabling the mode).
296
297Examples:
298 (c-mode c++-mode)
299means that Highlight Changes mode is turned on for buffers in C and C++
300modes only."
301 :type '(choice
302 (const :tag "all non-special buffers visiting files" t)
303 (set :menu-tag "specific modes" :tag "modes"
304 :value (not)
305 (const :tag "All except these" not)
306 (repeat :tag "Modes" :inline t (symbol :tag "mode")))
307 (function :menu-tag "determined by function"
308 :value buffer-file-name)
309 (const :tag "none" nil)
310 )
3ec30bcb 311 :group 'highlight-changes)
e287d328
RS
312
313(defvar global-highlight-changes nil)
314
315(defcustom highlight-changes-global-changes-existing-buffers nil
3ec30bcb
GM
316 "*If non-nil, toggling global Highlight Changes mode affects existing buffers.
317Normally, `global-highlight-changes' affects only new buffers (to be
318created). However, if `highlight-changes-global-changes-existing-buffers'
319is non-nil, then turning on `global-highlight-changes' will turn on
320Highlight Changes mode in suitable buffers, and turning the mode off will
e287d328
RS
321remove it from existing buffers."
322 :type 'boolean
323 :group 'highlight-changes)
324
325(defun hilit-chg-cust-fix-changes-face-list (w wc &optional event)
326 ;; When customization function `highlight-changes-face-list' inserts a new
327 ;; face it uses the default face. We don't want the user to modify this
328 ;; face, so we rename the faces in the list on an insert. The rename is
329 ;; actually done by copying the faces so user-defined faces still remain
330 ;; in the same order.
331 ;; The notifying the parent is needed because without it changes to the
332 ;; faces are saved but not to the actual list itself.
333 (let ((old-list (widget-value w)))
334 (if (member 'default old-list)
335 (let
336 ((p (reverse old-list))
337 (n (length old-list))
338 new-name old-name
339 (new-list nil)
340 )
341 (while p
342 (setq old-name (car p))
343 (setq new-name (intern (format "highlight-changes-face-%d" n)))
344 (if (eq old-name new-name)
345 nil
346 ;; A new face has been inserted: we don't want to modify the
347 ;; default face so copy it. Better, though, (I think) is to
348 ;; make a new face have the same attributes as
349 ;; highlight-changes-face .
350 (if (eq old-name 'default)
351 (copy-face 'highlight-changes-face new-name)
352 (copy-face old-name new-name)
353 ))
354 (setq new-list (append (list new-name) new-list))
355 (setq n (1- n))
356 (setq p (cdr p)))
357 (if (equal new-list (widget-value w))
358 nil ;; (message "notify: no change!")
359 (widget-value-set w new-list)
360 (widget-setup)
361 )
362 )
363 ;; (message "notify: no default here!")
364 ))
365 (let ((parent (widget-get w :parent)))
366 (when parent
3ec30bcb 367 (widget-apply parent :notify w event))))
e287d328
RS
368
369
370(defcustom highlight-changes-face-list nil
3ec30bcb 371 "*A list of faces used when rotating changes.
e287d328
RS
372Normally the variable is initialized to nil and the list is created from
373`highlight-changes-colours' when needed. However, you can set this variable
374to any list of faces. You will have to do this if you want faces which
375don't just differ from `highlight-changes-face' by the foreground colour.
376Otherwise, this list will be constructed when needed from
377`highlight-changes-colours'."
378 :type '(choice
379 (repeat
380 :notify hilit-chg-cust-fix-changes-face-list
381 face )
382 (const :tag "Derive from highlight-changes-colours" nil)
383 )
3ec30bcb 384 :group 'highlight-changes)
e287d328
RS
385
386;; ========================================================================
387
388;; These shouldn't be changed!
389
aaaecdf3
DL
390;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
391;;;###autoload
e287d328
RS
392(defvar highlight-changes-mode nil)
393(defvar hilit-chg-list nil)
394(defvar hilit-chg-string " ??")
395(or (assq 'highlight-changes-mode minor-mode-alist)
396 (setq minor-mode-alist
397 (cons '(highlight-changes-mode hilit-chg-string) minor-mode-alist)
398 ))
399(make-variable-buffer-local 'highlight-changes-mode)
400(make-variable-buffer-local 'hilit-chg-string)
401
402
403
404(eval-and-compile
99f08df4 405 ;; For highlight-compare-with-file
e287d328
RS
406 (defvar ediff-number-of-differences)
407 (autoload 'ediff-setup "ediff")
408 (autoload 'ediff-with-current-buffer "ediff")
409 (autoload 'ediff-really-quit "ediff")
410 (autoload 'ediff-make-fine-diffs "ediff")
411 (autoload 'ediff-get-fine-diff-vector "ediff")
3ec30bcb 412 (autoload 'ediff-get-difference "ediff"))
e287d328
RS
413
414
415
416;;; Functions...
417
418(defun hilit-chg-map-changes (func &optional start-position end-position)
419 "Call function FUNC for each region used by Highlight Changes mode."
420 ;; if start-position is nil, (point-min) is used
421 ;; if end-position is nil, (point-max) is used
422 ;; FUNC is called with 3 params: property start stop
423 (let ((start (or start-position (point-min)))
424 (limit (or end-position (point-max)))
425 prop end)
426 (while (and start (< start limit))
427 (setq prop (get-text-property start 'hilit-chg))
428 (setq end (text-property-not-all start limit 'hilit-chg prop))
429 (if prop
430 (funcall func prop start (or end limit)))
3ec30bcb 431 (setq start end))))
e287d328
RS
432
433
434(defun hilit-chg-display-changes (&optional beg end)
435 "Display face information for Highlight Changes mode.
436
3ec30bcb
GM
437An overlay containing a change face is added from the information
438in the text property of type `hilit-chg'.
e287d328 439
3ec30bcb 440This is the opposite of `hilit-chg-hide-changes'."
e287d328
RS
441 (hilit-chg-map-changes 'hilit-chg-make-ov beg end))
442
443
444(defun hilit-chg-make-ov (prop start end)
445 ;; for the region make change overlays corresponding to
446 ;; the text property 'hilit-chg
447 (let ((ov (make-overlay start end))
448 face)
449 (or prop
450 (error "hilit-chg-make-ov: prop is nil"))
451 (if (eq prop 'hilit-chg-delete)
452 (setq face 'highlight-changes-delete-face)
453 (setq face (nth 1 (member prop hilit-chg-list))))
454 (if face
455 (progn
456 ;; We must mark the face, that is the purpose of the overlay
457 (overlay-put ov 'face face)
458 ;; I don't think we need to set evaporate since we should
459 ;; be controlling them!
460 (overlay-put ov 'evaporate t)
461 ;; We set the change property so we can tell this is one
462 ;; of our overlays (so we don't delete someone else's).
463 (overlay-put ov 'hilit-chg t)
464 )
3ec30bcb 465 (error "hilit-chg-make-ov: no face for prop: %s" prop))))
e287d328
RS
466
467(defun hilit-chg-hide-changes (&optional beg end)
468 "Remove face information for Highlight Changes mode.
469
470The overlay containing the face is removed, but the text property
471containing the change information is retained.
472
3ec30bcb 473This is the opposite of `hilit-chg-display-changes'."
e287d328
RS
474 (let ((start (or beg (point-min)))
475 (limit (or end (point-max)))
476 p ov)
477 (setq p (overlays-in start limit))
478 (while p
479 ;; don't delete the overlay if it isn't ours!
480 (if (overlay-get (car p) 'hilit-chg)
481 (delete-overlay (car p)))
3ec30bcb 482 (setq p (cdr p)))))
e287d328
RS
483
484(defun hilit-chg-fixup (beg end)
3ec30bcb 485 "Fix change overlays in region between BEG and END.
e287d328
RS
486
487Ensure the overlays agree with the changes as determined from
488the text properties of type `hilit-chg' ."
489 ;; Remove or alter overlays in region beg..end
490 (let (p ov ov-start ov-end
491 props q)
492 (setq p (overlays-in beg end))
493 ;; temp for debugging:
494 ;; (or (eq highlight-changes-mode 'active)
495 ;; (error "hilit-chg-fixup called but Highlight Changes mode not active"))
496 (while p
497 (setq ov (car p))
498 (setq ov-start (overlay-start ov))
499 (setq ov-end (overlay-end ov))
500 (if (< ov-start beg)
501 (progn
502 (move-overlay ov ov-start beg)
503 (if (> ov-end end)
504 (progn
505 (setq props (overlay-properties ov))
506 (setq ov (make-overlay end ov-end))
507 (while props
508 (overlay-put ov (car props)(car (cdr props)))
3ec30bcb 509 (setq props (cdr (cdr props)))))))
e287d328
RS
510 (if (> ov-end end)
511 (move-overlay ov end ov-end)
3ec30bcb 512 (delete-overlay ov)))
e287d328 513 (setq p (cdr p)))
3ec30bcb 514 (hilit-chg-display-changes beg end)))
e287d328
RS
515
516;;;###autoload
517(defun highlight-changes-remove-highlight (beg end)
3ec30bcb 518 "Remove the change face from the region between BEG and END.
e287d328
RS
519This allows you to manually remove highlighting from uninteresting changes."
520 (interactive "r")
521 (let ((after-change-functions nil))
522 (remove-text-properties beg end '(hilit-chg nil))
523 (hilit-chg-fixup beg end)))
524
525(defun hilit-chg-set-face-on-change (beg end leng-before
526 &optional no-proerty-change)
527 "Record changes and optionally display them in a distinctive face.
528`hilit-chg-set' adds this function to the `after-change-functions' hook."
529 ;;
530 ;; This function is called by the `after-change-functions' hook, which
531 ;; is how we are notified when text is changed.
99f08df4 532 ;; It is also called from `highlight-compare-with-file'.
e287d328
RS
533 ;;
534 ;; We do NOT want to simply do this if this is an undo command, because
535 ;; otherwise an undone change shows up as changed. While the properties
3ec30bcb 536 ;; are automatically restored by undo, we must fix up the overlay.
e287d328
RS
537 (save-match-data
538 (let ((beg-decr 1) (end-incr 1)
539 (type 'hilit-chg)
540 old)
541 (if undo-in-progress
542 (if (eq highlight-changes-mode 'active)
543 (hilit-chg-fixup beg end))
544 (if (and (= beg end) (> leng-before 0))
545 ;; deletion
546 (progn
547 ;; The eolp and bolp tests are a kludge! But they prevent
548 ;; rather nasty looking displays when deleting text at the end
549 ;; of line, such as normal corrections as one is typing and
3ec30bcb 550 ;; immediately makes a correction, and when deleting first
e287d328
RS
551 ;; character of a line.
552;;; (if (= leng-before 1)
553;;; (if (eolp)
554;;; (setq beg-decr 0 end-incr 0)
555;;; (if (bolp)
556;;; (setq beg-decr 0))))
557;;; (setq beg (max (- beg beg-decr) (point-min)))
558 (setq end (min (+ end end-incr) (point-max)))
559 (setq type 'hilit-chg-delete))
560 ;; Not a deletion.
561 ;; Most of the time the following is not necessary, but
562 ;; if the current text was marked as a deletion then
563 ;; the old overlay is still in effect, so if we add some
564 ;; text then remove the deletion marking, but set it to
565 ;; changed otherwise its highlighting disappears.
566 (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete)
567 (progn
568 (remove-text-properties end (+ end 1) '(hilit-chg nil))
569 (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg)
570 (if (eq highlight-changes-mode 'active)
571 (hilit-chg-fixup beg (+ end 1))))))
572 (unless no-proerty-change
573 (put-text-property beg end 'hilit-chg type))
574 (if (or (eq highlight-changes-mode 'active) no-proerty-change)
3ec30bcb 575 (hilit-chg-make-ov type beg end))))))
e287d328
RS
576
577(defun hilit-chg-set (value)
578 "Turn on Highlight Changes mode for this buffer."
579 (setq highlight-changes-mode value)
580 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
581 (hilit-chg-make-list)
582 (if (eq highlight-changes-mode 'active)
583 (progn
584 (setq hilit-chg-string highlight-changes-active-string)
585 (or buffer-read-only
586 (hilit-chg-display-changes)))
587 ;; mode is passive
588 (setq hilit-chg-string highlight-changes-passive-string)
589 (or buffer-read-only
3ec30bcb 590 (hilit-chg-hide-changes)))
e287d328
RS
591 (force-mode-line-update)
592 (make-local-hook 'after-change-functions)
3ec30bcb 593 (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t))
e287d328
RS
594
595(defun hilit-chg-clear ()
596 "Remove Highlight Changes mode for this buffer.
597This removes all saved change information."
598 (if buffer-read-only
599 ;; We print the buffer name because this function could be called
600 ;; on many buffers from `global-highlight-changes'.
601 (message "Cannot remove highlighting from read-only mode buffer %s"
602 (buffer-name))
603 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
604 (let ((after-change-functions nil))
605 (hilit-chg-hide-changes)
606 (hilit-chg-map-changes
607 '(lambda (prop start stop)
608 (remove-text-properties start stop '(hilit-chg nil))))
609 )
610 (setq highlight-changes-mode nil)
611 (force-mode-line-update)
612 ;; If we type: C-u -1 M-x highlight-changes-mode
613 ;; we want to turn it off, but hilit-chg-post-command-hook
614 ;; runs and that turns it back on!
3ec30bcb 615 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook)))
e287d328
RS
616
617;;;###autoload
618(defun highlight-changes-mode (&optional arg)
619 "Toggle (or initially set) Highlight Changes mode.
620
3ec30bcb
GM
621Without an argument:
622 If Highlight Changes mode is not enabled, then enable it (in either active
623 or passive state as determined by the variable
624 `highlight-changes-initial-state'); otherwise, toggle between active
625 and passive state.
e287d328 626
3ec30bcb
GM
627With an argument ARG:
628 If ARG is positive, set state to active;
629 If ARG is zero, set state to passive;
630 If ARG is negative, disable Highlight Changes mode completely.
e287d328 631
3ec30bcb 632Active state - means changes are shown in a distinctive face.
e287d328
RS
633Passive state - means changes are kept and new ones recorded but are
634 not displayed in a different face.
635
636Functions:
637\\[highlight-changes-next-change] - move point to beginning of next change
638\\[highlight-changes-previous-change] - move to beginning of previous change
99f08df4
KH
639\\[highlight-compare-with-file] - mark text as changed by comparing this
640 buffer with the contents of a file
e287d328
RS
641\\[highlight-changes-remove-highlight] - remove the change face from the region
642\\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes \
643through
644 various faces.
645
e287d328 646Hook variables:
3ec30bcb
GM
647`highlight-changes-enable-hook' - when enabling Highlight Changes mode.
648`highlight-changes-toggle-hook' - when entering active or passive state
649`highlight-changes-disable-hook' - when turning off Highlight Changes mode."
e287d328 650 (interactive "P")
e7a903e8
EZ
651 (if (or (display-color-p)
652 (and (fboundp 'x-display-grayscale-p) (x-display-grayscale-p)))
e287d328
RS
653 (let ((was-on highlight-changes-mode)
654 (new-highlight-changes-mode
655 (cond
656 ((null arg)
657 ;; no arg => toggle (or set to active initially)
658 (if highlight-changes-mode
659 (if (eq highlight-changes-mode 'active) 'passive 'active)
660 highlight-changes-initial-state))
661 ;; an argument is given
662 ((eq arg 'active)
663 'active)
664 ((eq arg 'passive)
665 'passive)
666 ((> (prefix-numeric-value arg) 0)
667 'active)
668 ((< (prefix-numeric-value arg) 0)
669 nil)
670 (t
3ec30bcb 671 'passive))))
e287d328
RS
672 (if new-highlight-changes-mode
673 ;; mode is turned on -- but may be passive
674 (progn
675 (hilit-chg-set new-highlight-changes-mode)
676 (or was-on
677 ;; run highlight-changes-enable-hook once
678 (run-hooks 'highlight-changes-enable-hook))
679 (run-hooks 'highlight-changes-toggle-hook))
680 ;; mode is turned off
681 (run-hooks 'highlight-changes-disable-hook)
3ec30bcb
GM
682 (hilit-chg-clear)))
683 (message "Highlight Changes mode requires color or grayscale display")))
e287d328
RS
684
685;;;###autoload
686(defun highlight-changes-next-change ()
687 "Move to the beginning of the next change, if in Highlight Changes mode."
688 (interactive)
689 (if highlight-changes-mode
690 (let ((start (point))
691 prop)
692 (setq prop (get-text-property (point) 'hilit-chg))
693 (if prop
694 ;; we are in a change
695 (setq start (next-single-property-change (point) 'hilit-chg)))
696 (if start
697 (setq start (next-single-property-change start 'hilit-chg)))
698 (if start
699 (goto-char start)
700 (message "no next change")))
701 (message "This buffer is not in Highlight Changes mode.")))
702
703
704;;;###autoload
705(defun highlight-changes-previous-change ()
706 "Move to the beginning of the previous change, if in Highlight Changes mode."
707 (interactive)
708 (if highlight-changes-mode
709 (let ( (start (point)) (prop nil) )
710 (or (bobp)
711 (setq prop (get-text-property (1- (point)) 'hilit-chg)))
712 (if prop
713 ;; we are in a change
714 (setq start (previous-single-property-change (point) 'hilit-chg)))
715 (if start
716 (setq start (previous-single-property-change start 'hilit-chg)))
717 ;; special handling for the case where (point-min) is a change
718 (if start
719 (setq start (or (previous-single-property-change start 'hilit-chg)
720 (if (get-text-property (point-min) 'hilit-chg)
721 (point-min)))))
722 (if start
723 (goto-char start)
724 (message "no previous change")))
725 (message "This buffer is not in Highlight Changes mode.")))
726
e287d328
RS
727;; ========================================================================
728
e287d328 729(defun hilit-chg-make-list (&optional force)
3ec30bcb 730 "Construct `hilit-chg-list' and `highlight-changes-face-list'."
e287d328
RS
731 ;; Constructs highlight-changes-face-list if necessary,
732 ;; and hilit-chg-list always:
733 ;; Maybe this should always be called when rotating a face
734 ;; so we pick up any changes?
735 (if (or (null highlight-changes-face-list) ; Don't do it if it
736 force) ; already exists unless FORCE non-nil.
737 (let ((p highlight-changes-colours)
738 (n 1) name)
739 (setq highlight-changes-face-list nil)
740 (while p
741 (setq name (intern (format "highlight-changes-face-%d" n)))
742 (copy-face 'highlight-changes-face name)
743 (set-face-foreground name (car p))
744 (setq highlight-changes-face-list
745 (append highlight-changes-face-list (list name)))
746 (setq p (cdr p))
747 (setq n (1+ n)))))
748 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes-face))
749 (let ((p highlight-changes-face-list)
750 (n 1)
751 last-category last-face)
752 (while p
753 (setq last-category (intern (format "change-%d" n)))
754 ;; (setq last-face (intern (format "highlight-changes-face-%d" n)))
755 (setq last-face (car p))
756 (setq hilit-chg-list
757 (append hilit-chg-list
758 (list last-category last-face)))
759 (setq p (cdr p))
760 (setq n (1+ n)))
761 (setq hilit-chg-list
762 (append hilit-chg-list
3ec30bcb 763 (list last-category last-face)))))
e287d328
RS
764
765(defun hilit-chg-bump-change (prop start end)
3ec30bcb 766 "Increment (age) the Highlight Changes mode text property."
e287d328
RS
767 (let ( new-prop )
768 (if (eq prop 'hilit-chg-delete)
769 (setq new-prop (nth 2 hilit-chg-list))
3ec30bcb 770 (setq new-prop (nth 2 (member prop hilit-chg-list))))
e287d328
RS
771 (if prop
772 (put-text-property start end 'hilit-chg new-prop)
3ec30bcb 773 (message "%d-%d unknown property %s not changed" start end prop))))
e287d328
RS
774
775;;;###autoload
776(defun highlight-changes-rotate-faces ()
777 "Rotate the faces used by Highlight Changes mode.
778
3ec30bcb
GM
779Current changes are displayed in the face described by the first element
780of `highlight-changes-face-list', one level older changes are shown in
e287d328
RS
781face described by the second element, and so on. Very old changes remain
782shown in the last face in the list.
783
784You can automatically rotate colours when the buffer is saved
3ec30bcb 785by adding the following to `local-write-file-hooks', by evaling it in the
e287d328 786buffer to be saved):
3ec30bcb
GM
787
788 \(add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)"
e287d328
RS
789 (interactive)
790 ;; If not in active mode do nothing but don't complain because this
791 ;; may be bound to a hook.
792 (if (eq highlight-changes-mode 'active)
793 (let ((after-change-functions nil))
794 ;; ensure hilit-chg-list is made and up to date
795 (hilit-chg-make-list)
796 ;; remove our existing overlays
797 (hilit-chg-hide-changes)
798 ;; for each change text property, increment it
799 (hilit-chg-map-changes 'hilit-chg-bump-change)
800 ;; and display them all if active
801 (if (eq highlight-changes-mode 'active)
3ec30bcb 802 (hilit-chg-display-changes))))
e287d328
RS
803 ;; This always returns nil so it is safe to use in
804 ;; local-write-file-hook
805 nil)
806
e287d328
RS
807;; ========================================================================
808;; Comparing with an existing file.
809;; This uses ediff to find the differences.
810
811;;;###autoload
b32e3ef8
KH
812(defun highlight-compare-with-file (file-b)
813 "Compare this buffer with a file, and highlight differences.
e287d328
RS
814
815The current buffer must be an unmodified buffer visiting a file,
3ec30bcb 816and must not be read-only.
e287d328 817
3ec30bcb
GM
818If the buffer has a backup filename, it is used as the default when
819this function is called interactively.
e287d328 820
3ec30bcb
GM
821If the current buffer is visiting the file being compared against, it
822also will have its differences highlighted. Otherwise, the file is
823read in temporarily but the buffer is deleted.
e287d328 824
3ec30bcb
GM
825If the buffer is read-only, differences will be highlighted but no property
826changes are made, so \\[highlight-changes-next-change] and
e287d328
RS
827\\[highlight-changes-previous-change] will not work."
828 (interactive (list
829 (read-file-name
830 "File to compare with? " ;; prompt
831 "" ;; directory
832 nil ;; default
833 'yes ;; must exist
834 (let ((f (make-backup-file-name
835 (or (buffer-file-name (current-buffer))
836 (error "no file for this buffer")))))
3ec30bcb 837 (if (file-exists-p f) f "")))))
e287d328
RS
838
839 (let* ((buf-a (current-buffer))
840 (buf-a-read-only buffer-read-only)
841 (orig-pos (point))
842 (file-a (buffer-file-name))
843 (existing-buf (get-file-buffer file-b))
844 (buf-b (or existing-buf
845 (find-file-noselect file-b)))
846 (buf-b-read-only (with-current-buffer buf-b buffer-read-only))
847 xy xx yy p q
848 a-start a-end len-a
3ec30bcb 849 b-start b-end len-b)
e287d328
RS
850
851 ;; We use the fact that the buffer is not marked modified at the
852 ;; end where we clear its modified status
853 (if (buffer-modified-p buf-a)
854 (if (y-or-n-p (format "OK to save %s? " file-a))
855 (save-buffer buf-a)
e8af40ee 856 (error "Buffer must be saved before comparing with a file")))
e287d328
RS
857 (if (and existing-buf (buffer-modified-p buf-b))
858 (if (y-or-n-p (format "OK to save %s? " file-b))
859 (save-buffer buf-b)
e8af40ee 860 (error "Cannot compare with a file in an unsaved buffer")))
e287d328
RS
861 (highlight-changes-mode 'active)
862 (if existing-buf (with-current-buffer buf-b
863 (highlight-changes-mode 'active)))
864 (save-window-excursion
865 (setq xy (hilit-chg-get-diff-info buf-a file-a buf-b file-b)))
866 (setq xx (car xy))
867 (setq p xx)
868 (setq yy (car (cdr xy)))
869 (setq q yy)
870 (hilit-chg-make-list)
871 (while p
872 (setq a-start (nth 0 (car p)))
873 (setq a-end (nth 1 (car p)))
874 (setq b-start (nth 0 (car q)))
875 (setq b-end (nth 1 (car q)))
876 (setq len-a (- a-end a-start))
877 (setq len-b (- b-end b-start))
878 (set-buffer buf-a)
879 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
880 (set-buffer-modified-p nil)
881 (goto-char orig-pos)
882 (if existing-buf
883 (with-current-buffer buf-b
884 (hilit-chg-set-face-on-change b-start b-end len-a
885 buf-b-read-only )
886 ))
887 (setq p (cdr p))
3ec30bcb 888 (setq q (cdr q)))
e287d328
RS
889 (if existing-buf
890 (set-buffer-modified-p nil)
3ec30bcb 891 (kill-buffer buf-b))))
e287d328
RS
892
893
894(defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b)
895 (let ((e nil) x y) ;; e is set by function hilit-chg-get-diff-list-hk
896 (ediff-setup buf-a file-a buf-b file-b
897 nil nil ; buf-c file-C
898 'hilit-chg-get-diff-list-hk
899 (list (cons 'ediff-job-name 'something))
900 )
901 (ediff-with-current-buffer e (ediff-really-quit nil))
902 (list x y)))
903
904
905(defun hilit-chg-get-diff-list-hk ()
906 ;; x and y are dynamically bound by hilit-chg-get-diff-info
907 ;; which calls this function as a hook
908 (defvar x) ;; placate the byte-compiler
909 (defvar y)
910 (setq e (current-buffer))
911 (let ((n 0) extent p va vb a b)
912 (setq x nil y nil) ;; x and y are bound by hilit-chg-get-diff-info
913 (while (< n ediff-number-of-differences)
914 (ediff-make-fine-diffs n)
915 (setq va (ediff-get-fine-diff-vector n 'A))
916 ;; va is a vector if there are fine differences
917 (if va
918 (setq a (append va nil))
919 ;; if not, get the unrefined difference
920 (setq va (ediff-get-difference n 'A))
3ec30bcb 921 (setq a (list (elt va 0))))
e287d328
RS
922 ;; a list a list
923 (setq p a)
924 (while p
925 (setq extent (list (overlay-start (car p))
926 (overlay-end (car p))))
927 (setq p (cdr p))
3ec30bcb 928 (setq x (append x (list extent) )));; while p
e287d328
RS
929 ;;
930 (setq vb (ediff-get-fine-diff-vector n 'B))
931 ;; vb is a vector
932 (if vb
933 (setq b (append vb nil))
934 ;; if not, get the unrefined difference
935 (setq vb (ediff-get-difference n 'B))
3ec30bcb 936 (setq b (list (elt vb 0))))
e287d328
RS
937 ;; b list a list
938 (setq p b)
939 (while p
940 (setq extent (list (overlay-start (car p))
941 (overlay-end (car p))))
942 (setq p (cdr p))
3ec30bcb
GM
943 (setq y (append y (list extent) )))
944 (setq n (1+ n)));; while
e287d328
RS
945 ;; ediff-quit doesn't work here.
946 ;; No point in returning a value, since this is a hook function.
947 ))
948
949;; ======================= automatic stuff ==============
950
3ec30bcb 951;; Global Highlight Changes mode is modeled after Global Font-lock mode.
e287d328
RS
952;; Three hooks are used to gain control. When Global Changes Mode is
953;; enabled, `find-file-hooks' and `change-major-mode-hook' are set.
954;; `find-file-hooks' is called when visiting a file, the new mode is
955;; known at this time.
956;; `change-major-mode-hook' is called when a buffer is changing mode.
957;; This could be because of finding a file in which case
958;; `find-file-hooks' has already been called and has done its work.
959;; However, it also catches the case where a new mode is being set by
960;; the user. However, it is called from `kill-all-variables' and at
961;; this time the mode is the old mode, which is not what we want.
962;; So, our function temporarily sets `post-command-hook' which will
963;; be called after the buffer has been completely set up (with the new
964;; mode). It then removes the `post-command-hook'.
965;; One other wrinkle - every M-x command runs the `change-major-mode-hook'
966;; so we ignore this by examining the buffer name.
967
968
969(defun hilit-chg-major-mode-hook ()
3ec30bcb 970 (add-hook 'post-command-hook 'hilit-chg-post-command-hook))
e287d328
RS
971
972(defun hilit-chg-post-command-hook ()
973 ;; This is called after changing a major mode, but also after each
974 ;; M-x command, in which case the current buffer is a minibuffer.
975 ;; In that case, do not act on it here, but don't turn it off
976 ;; either, we will get called here again soon-after.
977 ;; Also, don't enable it for other special buffers.
978 (if (string-match "^[ *]" (buffer-name))
979 nil ;; (message "ignoring this post-command-hook")
980 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook)
981 ;; The following check isn't necessary, since
982 ;; hilit-chg-turn-on-maybe makes this check too.
983 (or highlight-changes-mode ;; don't turn it on if it already is
3ec30bcb 984 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state))))
e287d328
RS
985
986(defun hilit-chg-check-global ()
987 ;; This is called from the find file hook.
988 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state))
989
990
e287d328
RS
991;;;###autoload
992(defun global-highlight-changes (&optional arg)
993 "Turn on or off global Highlight Changes mode.
994
995When called interactively:
996- if no prefix, toggle global Highlight Changes mode on or off
997- if called with a positive prefix (or just C-u) turn it on in active mode
998- if called with a zero prefix turn it on in passive mode
999- if called with a negative prefix turn it off
1000
1001When called from a program:
1002- if ARG is nil or omitted, turn it off
3ec30bcb
GM
1003- if ARG is `active', turn it on in active mode
1004- if ARG is `passive', turn it on in passive mode
e287d328
RS
1005- otherwise just turn it on
1006
1007When global Highlight Changes mode is enabled, Highlight Changes mode is turned
1008on for future \"suitable\" buffers (and for \"suitable\" existing buffers if
1009variable `highlight-changes-global-changes-existing-buffers' is non-nil).
3ec30bcb 1010\"Suitability\" is determined by variable `highlight-changes-global-modes'."
e287d328
RS
1011
1012 (interactive
1013 (list
1014 (cond
1015 ((null current-prefix-arg)
1016 ;; no arg => toggle it on/off
1017 (setq global-highlight-changes (not global-highlight-changes)))
1018 ;; positive interactive arg - turn it on as active
1019 ((> (prefix-numeric-value current-prefix-arg) 0)
1020 (setq global-highlight-changes t)
1021 'active)
1022 ;; zero interactive arg - turn it on as passive
1023 ((= (prefix-numeric-value current-prefix-arg) 0)
1024 (setq global-highlight-changes t)
1025 'passive)
1026 ;; negative interactive arg - turn it off
1027 (t
1028 (setq global-highlight-changes nil)
1029 nil))))
1030
1031 (if arg
1032 (progn
1033 (if (eq arg 'active)
1034 (setq highlight-changes-global-initial-state 'active)
1035 (if (eq arg 'passive)
1036 (setq highlight-changes-global-initial-state 'passive)))
1037 (setq global-highlight-changes t)
1038 (message "turning ON Global Highlight Changes mode in %s state"
1039 highlight-changes-global-initial-state)
1040 (add-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook)
1041 (add-hook 'find-file-hooks 'hilit-chg-check-global)
1042 (if highlight-changes-global-changes-existing-buffers
1043 (hilit-chg-update-all-buffers
3ec30bcb
GM
1044 highlight-changes-global-initial-state)))
1045
e287d328
RS
1046 (message "turning OFF global Highlight Changes mode")
1047 (remove-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook)
1048 (remove-hook 'find-file-hooks 'hilit-chg-check-global)
1049 (remove-hook 'post-command-hook
1050 'hilit-chg-post-command-hook)
1051 (remove-hook 'find-file-hooks 'hilit-chg-check-global)
1052 (if highlight-changes-global-changes-existing-buffers
3ec30bcb 1053 (hilit-chg-update-all-buffers nil))))
e287d328
RS
1054
1055
1056(defun hilit-chg-turn-on-maybe (value)
1057 "Turn on Highlight Changes mode if it is appropriate for this buffer.
1058
1059A buffer is appropriate for Highlight Changes mode if all these are true:
1060- the buffer is not a special buffer (one whose name begins with
1061 `*' or ` ')
3ec30bcb
GM
1062- the buffer's mode is suitable as per variable
1063 `highlight-changes-global-modes'
e287d328
RS
1064- Highlight Changes mode is not already on for this buffer.
1065
3ec30bcb
GM
1066This function is called from `hilit-chg-update-all-buffers' or
1067from `global-highlight-changes' when turning on global Highlight Changes mode."
e287d328
RS
1068 (or highlight-changes-mode ; do nothing if already on
1069 (if
1070 (cond
1071 ((null highlight-changes-global-modes)
1072 nil)
1073 ((functionp highlight-changes-global-modes)
1074 (funcall highlight-changes-global-modes))
1075 ((listp highlight-changes-global-modes)
1076 (if (eq (car-safe highlight-changes-global-modes) 'not)
1077 (not (memq major-mode (cdr highlight-changes-global-modes)))
1078 (memq major-mode highlight-changes-global-modes)))
1079 (t
1080 (and
1081 (not (string-match "^[ *]" (buffer-name)))
3ec30bcb 1082 (buffer-file-name))))
e287d328
RS
1083 (progn
1084 (hilit-chg-set value)
3ec30bcb 1085 (run-hooks 'highlight-changes-enable-hook)))))
e287d328
RS
1086
1087
1088(defun hilit-chg-turn-off-maybe ()
1089 (if highlight-changes-mode
1090 (progn
1091 (run-hooks 'highlight-changes-disable-hook)
1092 (hilit-chg-clear))))
1093
1094
e287d328
RS
1095(defun hilit-chg-update-all-buffers (value)
1096 (mapcar
1097 (function (lambda (buffer)
1098 (with-current-buffer buffer
1099 (if value
1100 (hilit-chg-turn-on-maybe value)
1101 (hilit-chg-turn-off-maybe))
1102 )))
1103 (buffer-list)))
1104
1105;; ===================== debug ==================
1106;; For debug & test use:
1107;;
1108;; (defun hilit-chg-debug-show (&optional beg end)
1109;; (interactive)
1110;; (message "--- hilit-chg-debug-show ---")
1111;; (hilit-chg-map-changes '(lambda (prop start end)
1112;; (message "%d-%d: %s" start end prop)
1113;; )
1114;; beg end
1115;; ))
1116;;
1117;; ================== end of debug ===============
1118
e287d328 1119(provide 'hilit-chg)
e287d328 1120
fdbd749a 1121;;; hilit-chg.el ends here