*** empty log message ***
[bpt/emacs.git] / lisp / hilit-chg.el
1 ;;; hilit-chg.el --- minor mode displaying buffer changes with special face
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: Richard Sharman <rsharman@pobox.com>
6 ;; Keywords: faces
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
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
25
26 ;;; Commentary:
27
28 ;; A minor mode: "Highlight Changes mode".
29 ;;
30
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.
44 ;;
45 ;;
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'.
56 ;;
57 ;;
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'.
61 ;;
62 ;;
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).
66 ;;
67 ;;
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 ;;
80 ;;
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)
94
95
96 ;; Explcit vs. Implicit
97 ;;
98
99 ;; Normally, Highlight Changes mode is turned on explicitly in a buffer.
100 ;;
101 ;; If you prefer to have it automatically invoked you can do it as
102 ;; follows.
103 ;;
104 ;; 1. Most modes have a major-hook, typically called MODE-hook. You
105 ;; can use `add-hook' to call `highlight-changes-mode'.
106 ;;
107 ;; Example:
108 ;; (add-hook 'c-mode-hook 'highlight-changes-mode)
109 ;;
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)))
115 ;;
116 ;; However, this cannot be done for Fundamental mode for there is no
117 ;; such hook.
118 ;;
119 ;; 2. You can use the function `global-highlight-changes'
120 ;;
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.
125 ;;
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.
138 ;;
139
140
141
142 ;; Possible bindings:
143 ;; (global-set-key '[C-right] 'highlight-changes-next-change)
144 ;; (global-set-key '[C-left] 'highlight-changes-previous-change)
145 ;;
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
151
152 ;;
153 ;; You can automatically rotate faces when the buffer is saved;
154 ;; see function `highlight-changes-rotate-faces' for how to do this.
155 ;;
156
157
158 ;;; Bugs:
159
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.
163
164
165 ;;; To do (maybe), notes, ...
166
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.)
174
175
176 ;;; History:
177
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.
191
192
193 ;;; Code:
194
195 (require 'wid-edit)
196
197 ;; ====================== Customization =======================
198 (defgroup highlight-changes nil
199 "Highlight Changes mode."
200 :version "20.4"
201 :group 'faces)
202
203
204 ;; Face information: How the changes appear.
205
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!).
211
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
217 )
218
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
225 )
226
227
228
229 ;; A (not very good) default list of colours to rotate through.
230 ;;
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.
240
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
247 )
248
249
250 ;; If you invoke highlight-changes-mode with no argument, should it start in
251 ;; active or passive mode?
252 ;;
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
260 )
261
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
269 )
270
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
279 )
280
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
288 )
289
290 (defcustom highlight-changes-global-modes t
291 "*Determine whether a buffer is suitable for global Highlight Changes mode.
292
293 A function means that function is called: if it returns non-nil the
294 buffer is suitable.
295
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.
298
299 t means the buffer is suitable if its name does not begin with ` ' nor
300 `*' and the buffer has a filename.
301
302 nil means no buffers are suitable for `global-highlight-changes'
303 (effectively disabling the mode).
304
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)
318 )
319 :group 'highlight-changes
320 )
321
322
323 (defvar global-highlight-changes nil)
324
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)
334
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)
350 )
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)
355 nil
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)
363 ))
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)
371 )
372 )
373 ;; (message "notify: no default here!")
374 ))
375 (let ((parent (widget-get w :parent)))
376 (when parent
377 (widget-apply parent :notify w event)))
378 )
379
380
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)
394 )
395 :group 'highlight-changes
396 )
397
398 ;; ========================================================================
399
400 ;; These shouldn't be changed!
401
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)
410 ))
411 (make-variable-buffer-local 'highlight-changes-mode)
412 (make-variable-buffer-local 'hilit-chg-string)
413
414
415
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")
425 )
426
427
428
429 ;;; Functions...
430
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)
445 )))
446
447
448 (defun hilit-chg-display-changes (&optional beg end)
449 "Display face information for Highlight Changes mode.
450
451 An overlay containing a change face is added, from the information
452 in the text property of type change.
453
454 This is the opposite of hilit-chg-hide-changes."
455 (hilit-chg-map-changes 'hilit-chg-make-ov beg end))
456
457
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)
478 )
479 (error "hilit-chg-make-ov: no face for prop: %s" prop)
480 )
481 ))
482
483 (defun hilit-chg-hide-changes (&optional beg end)
484 "Remove face information for Highlight Changes mode.
485
486 The overlay containing the face is removed, but the text property
487 containing the change information is retained.
488
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))
499 )))
500
501 (defun hilit-chg-fixup (beg end)
502 "Fix change overlays in region beg .. end.
503
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))))
527 )
528 )
529 )
530 (if (> ov-end end)
531 (move-overlay ov end ov-end)
532 (delete-overlay ov)
533 ))
534 (setq p (cdr p)))
535 (hilit-chg-display-changes beg end)
536 ))
537
538
539
540
541
542
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)))
551
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."
556 ;;
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'.
560 ;;
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 ))))
604
605
606
607
608
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))
623 )
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)
627 )
628
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))))
643 )
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)
650 ))
651
652 ;;;###autoload
653 (defun highlight-changes-mode (&optional arg)
654 "Toggle (or initially set) Highlight Changes mode.
655
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.
660
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.
665
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.
669
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.
679
680
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.
685 "
686 (interactive "P")
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
691 (cond
692 ((null arg)
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
698 ((eq arg 'active)
699 'active)
700 ((eq arg 'passive)
701 'passive)
702 ((> (prefix-numeric-value arg) 0)
703 'active)
704 ((< (prefix-numeric-value arg) 0)
705 nil)
706 (t
707 'passive)
708 )))
709 (if new-highlight-changes-mode
710 ;; mode is turned on -- but may be passive
711 (progn
712 (hilit-chg-set new-highlight-changes-mode)
713 (or was-on
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)
719 (hilit-chg-clear))
720 )
721 (message "Highlight Changes mode requires color or grayscale display"))
722 )
723
724
725
726 ;;;###autoload
727 (defun highlight-changes-next-change ()
728 "Move to the beginning of the next change, if in Highlight Changes mode."
729 (interactive)
730 (if highlight-changes-mode
731 (let ((start (point))
732 prop)
733 (setq prop (get-text-property (point) 'hilit-chg))
734 (if prop
735 ;; we are in a change
736 (setq start (next-single-property-change (point) 'hilit-chg)))
737 (if start
738 (setq start (next-single-property-change start 'hilit-chg)))
739 (if start
740 (goto-char start)
741 (message "no next change")))
742 (message "This buffer is not in Highlight Changes mode.")))
743
744
745 ;;;###autoload
746 (defun highlight-changes-previous-change ()
747 "Move to the beginning of the previous change, if in Highlight Changes mode."
748 (interactive)
749 (if highlight-changes-mode
750 (let ( (start (point)) (prop nil) )
751 (or (bobp)
752 (setq prop (get-text-property (1- (point)) 'hilit-chg)))
753 (if prop
754 ;; we are in a change
755 (setq start (previous-single-property-change (point) 'hilit-chg)))
756 (if start
757 (setq start (previous-single-property-change start 'hilit-chg)))
758 ;; special handling for the case where (point-min) is a change
759 (if start
760 (setq start (or (previous-single-property-change start 'hilit-chg)
761 (if (get-text-property (point-min) 'hilit-chg)
762 (point-min)))))
763 (if start
764 (goto-char start)
765 (message "no previous change")))
766 (message "This buffer is not in Highlight Changes mode.")))
767
768
769 ;; ========================================================================
770
771
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)
781 (n 1) name)
782 (setq highlight-changes-face-list nil)
783 (while p
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)))
789 (setq p (cdr p))
790 (setq n (1+ n)))))
791 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes-face))
792 (let ((p highlight-changes-face-list)
793 (n 1)
794 last-category last-face)
795 (while p
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))
799 (setq hilit-chg-list
800 (append hilit-chg-list
801 (list last-category last-face)))
802 (setq p (cdr p))
803 (setq n (1+ n)))
804 (setq hilit-chg-list
805 (append hilit-chg-list
806 (list last-category last-face)))
807 ))
808
809
810 (defun hilit-chg-bump-change (prop start end)
811 "Increment (age) the Highlight Changes mode text property of type change."
812 (let ( new-prop )
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)))
816 )
817 (if prop
818 (put-text-property start end 'hilit-chg new-prop)
819 (message "%d-%d unknown property %s not changed" start end prop)
820 )
821 ))
822
823 ;;;###autoload
824 (defun highlight-changes-rotate-faces ()
825 "Rotate the faces used by Highlight Changes mode.
826
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.
831
832 You can automatically rotate colours when the buffer is saved
833 by adding this to local-write-file-hooks, by evaling (in the
834 buffer to be saved):
835 (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
836 "
837 (interactive)
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))
851 ))
852 ;; This always returns nil so it is safe to use in
853 ;; local-write-file-hook
854 nil)
855
856
857 ;; ========================================================================
858 ;; Comparing with an existing file.
859 ;; This uses ediff to find the differences.
860
861 ;;;###autoload
862 (defun highlight-compare-with-file (file-b)
863 "Compare this buffer with a file, and highlight differences.
864
865 The current buffer must be an unmodified buffer visiting a file,
866 and not in read-only mode.
867
868 If the backup filename exists, it is used as the default
869 when called interactively.
870
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.
874
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."
878 (interactive (list
879 (read-file-name
880 "File to compare with? " ;; prompt
881 "" ;; directory
882 nil ;; default
883 'yes ;; must exist
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 ""))
888 )))
889
890 (let* ((buf-a (current-buffer))
891 (buf-a-read-only buffer-read-only)
892 (orig-pos (point))
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))
898 xy xx yy p q
899 a-start a-end len-a
900 b-start b-end len-b
901 )
902
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))
907 (save-buffer buf-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))
911 (save-buffer buf-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)))
918 (setq xx (car xy))
919 (setq p xx)
920 (setq yy (car (cdr xy)))
921 (setq q yy)
922 (hilit-chg-make-list)
923 (while p
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))
930 (set-buffer buf-a)
931 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
932 (set-buffer-modified-p nil)
933 (goto-char orig-pos)
934 (if existing-buf
935 (with-current-buffer buf-b
936 (hilit-chg-set-face-on-change b-start b-end len-a
937 buf-b-read-only )
938 ))
939 (setq p (cdr p))
940 (setq q (cdr q))
941 )
942 (if existing-buf
943 (set-buffer-modified-p nil)
944 (kill-buffer buf-b))
945 ))
946
947
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))
954 )
955 (ediff-with-current-buffer e (ediff-really-quit nil))
956 (list x y)))
957
958
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
963 (defvar y)
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
971 (if va
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)))
976 )
977 ;; a list a list
978 (setq p a)
979 (while p
980 (setq extent (list (overlay-start (car p))
981 (overlay-end (car p))))
982 (setq p (cdr p))
983 (setq x (append x (list extent) ))
984 );; while p
985 ;;
986 (setq vb (ediff-get-fine-diff-vector n 'B))
987 ;; vb is a vector
988 (if vb
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)))
993 )
994 ;; b list a list
995 (setq p b)
996 (while p
997 (setq extent (list (overlay-start (car p))
998 (overlay-end (car p))))
999 (setq p (cdr p))
1000 (setq y (append y (list extent) ))
1001 );; while p
1002 ;;
1003 (setq n (1+ n))
1004 );; while
1005 ;; ediff-quit doesn't work here.
1006 ;; No point in returning a value, since this is a hook function.
1007 ))
1008
1009 ;; ======================= automatic stuff ==============
1010
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.
1027
1028
1029 (defun hilit-chg-major-mode-hook ()
1030 (add-hook 'post-command-hook 'hilit-chg-post-command-hook)
1031 )
1032
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))
1046 ))
1047
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))
1051
1052
1053
1054 ;;;###autoload
1055 (defun global-highlight-changes (&optional arg)
1056 "Turn on or off global Highlight Changes mode.
1057
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
1063
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
1069
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'."
1074
1075 (interactive
1076 (list
1077 (cond
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)
1084 'active)
1085 ;; zero interactive arg - turn it on as passive
1086 ((= (prefix-numeric-value current-prefix-arg) 0)
1087 (setq global-highlight-changes t)
1088 'passive)
1089 ;; negative interactive arg - turn it off
1090 (t
1091 (setq global-highlight-changes nil)
1092 nil))))
1093
1094 (if arg
1095 (progn
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))
1108 )
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))
1117 )
1118 )
1119
1120
1121
1122
1123
1124 (defun hilit-chg-turn-on-maybe (value)
1125 "Turn on Highlight Changes mode if it is appropriate for this buffer.
1126
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
1129 `*' or ` ')
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.
1132
1133 This function is called from hilit-chg-update-all-buffers
1134 from `global-highlight-changes' when turning on global Highlight Changes mode.
1135 "
1136 (or highlight-changes-mode ; do nothing if already on
1137 (if
1138 (cond
1139 ((null highlight-changes-global-modes)
1140 nil)
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)))
1147 (t
1148 (and
1149 (not (string-match "^[ *]" (buffer-name)))
1150 (buffer-file-name))
1151 ))
1152 (progn
1153 (hilit-chg-set value)
1154 (run-hooks 'highlight-changes-enable-hook)))
1155 ))
1156
1157
1158 (defun hilit-chg-turn-off-maybe ()
1159 (if highlight-changes-mode
1160 (progn
1161 (run-hooks 'highlight-changes-disable-hook)
1162 (hilit-chg-clear))))
1163
1164
1165
1166 (defun hilit-chg-update-all-buffers (value)
1167 (mapcar
1168 (function (lambda (buffer)
1169 (with-current-buffer buffer
1170 (if value
1171 (hilit-chg-turn-on-maybe value)
1172 (hilit-chg-turn-off-maybe))
1173 )))
1174 (buffer-list)))
1175
1176 ;; ===================== debug ==================
1177 ;; For debug & test use:
1178 ;;
1179 ;; (defun hilit-chg-debug-show (&optional beg end)
1180 ;; (interactive)
1181 ;; (message "--- hilit-chg-debug-show ---")
1182 ;; (hilit-chg-map-changes '(lambda (prop start end)
1183 ;; (message "%d-%d: %s" start end prop)
1184 ;; )
1185 ;; beg end
1186 ;; ))
1187 ;;
1188 ;; ================== end of debug ===============
1189
1190
1191 (provide 'hilit-chg)
1192
1193 ;;; hilit-chg.el ends here