Add arch taglines
[bpt/emacs.git] / lisp / hilit-chg.el
1 ;;; hilit-chg.el --- minor mode displaying buffer changes with special face
2
3 ;; Copyright (C) 1998, 2000 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 ;; GNU Emacs 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 ;; GNU Emacs 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 ;;; 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
46 ;; `highlight-changes-rotate-faces'. This rotates through a series
47 ;; of different faces, so you can distinguish "new" changes from "older"
48 ;; changes. You can customize these "rotated" faces in two ways. You can
49 ;; either explicitly define each face by customizing
50 ;; `highlight-changes-face-list'. If, however, the faces differ from
51 ;; `highlight-changes-face' only in the foreground color, you can simply set
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 ;;
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).
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
95 ;; Explicit vs. Implicit
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
122 ;; on Highlight Changes mode in all "suitable" existing buffers and will turn
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
129 ;; an example, if the value is `buffer-file-name' then all buffers
130 ;; who are visiting files are suitable, but others (like dired
131 ;; buffers) are not;
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
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
149 ;; highlight-compare-with-file
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?
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.
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.
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
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
189 ;; - renamed to Highlight Changes mode.
190
191
192 ;;; Code:
193
194 (require 'wid-edit)
195
196 ;; ====================== Customization =======================
197 (defgroup highlight-changes nil
198 "Highlight Changes mode."
199 :version "20.4"
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.
207 ;; Note: underlining is helpful in that it shows up changes in white space.
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."
215 :group 'highlight-changes)
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."
222 :group 'highlight-changes)
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'.
235 The newest rotated change will be displayed in the first element of this list,
236 the next older will be in the second element etc.
237
238 This list is used if `highlight-changes-face-list' is nil, otherwise that
239 variable overrides this list. If you only care about foreground
240 colours then use this, if you want fancier faces then set
241 `highlight-changes-face-list'."
242 :type '(repeat color)
243 :group 'highlight-changes)
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.
251 This is used when `highlight-changes' is called with no argument.
252 This variable must be set to one of the symbols `active' or `passive'."
253 :type '(choice (const :tag "Active" active)
254 (const :tag "Passive" passive))
255 :group 'highlight-changes)
256
257 (defcustom highlight-changes-global-initial-state 'passive
258 "*What state `global-highlight-changes' should start in.
259 This is used if `global-highlight-changes' is called with no argument.
260 This variable must be set to either `active' or `passive'"
261 :type '(choice (const :tag "Active" active)
262 (const :tag "Passive" passive))
263 :group 'highlight-changes)
264
265 ;; The strings displayed in the mode-line for the minor mode:
266 (defcustom highlight-changes-active-string " +Chg"
267 "*The string used when Highlight Changes mode is in the active state.
268 This should be set to nil if no indication is desired, or to
269 a string with a leading space."
270 :type '(choice string
271 (const :tag "None" nil))
272 :group 'highlight-changes)
273
274 (defcustom highlight-changes-passive-string " -Chg"
275 "*The string used when Highlight Changes mode is in the passive state.
276 This should be set to nil if no indication is desired, or to
277 a string with a leading space."
278 :type '(choice string
279 (const :tag "None" nil))
280 :group 'highlight-changes)
281
282 (defcustom highlight-changes-global-modes t
283 "*Determine whether a buffer is suitable for global Highlight Changes mode.
284
285 A function means call that function to decide: if it returns non-nil,
286 the buffer is suitable.
287
288 A list means the elements are major modes suitable for Highlight
289 Changes mode, or a list whose first element is `not' followed by major
290 modes which are not suitable.
291
292 t means the buffer is suitable if it is visiting a file and its name
293 does not begin with ` ' or `*'.
294
295 A value of nil means no buffers are suitable for `global-highlight-changes'
296 \(effectively disabling the mode).
297
298 Examples:
299 (c-mode c++-mode)
300 means that Highlight Changes mode is turned on for buffers in C and C++
301 modes only."
302 :type '(choice
303 (const :tag "all non-special buffers visiting files" t)
304 (set :menu-tag "specific modes" :tag "modes"
305 :value (not)
306 (const :tag "All except these" not)
307 (repeat :tag "Modes" :inline t (symbol :tag "mode")))
308 (function :menu-tag "determined by function"
309 :value buffer-file-name)
310 (const :tag "none" nil)
311 )
312 :group 'highlight-changes)
313
314 (defvar global-highlight-changes nil)
315
316 (defcustom highlight-changes-global-changes-existing-buffers nil
317 "*If non-nil, toggling global Highlight Changes mode affects existing buffers.
318 Normally, `global-highlight-changes' affects only new buffers (to be
319 created). However, if `highlight-changes-global-changes-existing-buffers'
320 is non-nil, then turning on `global-highlight-changes' will turn on
321 Highlight Changes mode in suitable buffers, and turning the mode off will
322 remove it from existing buffers."
323 :type 'boolean
324 :group 'highlight-changes)
325
326 (defun hilit-chg-cust-fix-changes-face-list (w wc &optional event)
327 ;; When customization function `highlight-changes-face-list' inserts a new
328 ;; face it uses the default face. We don't want the user to modify this
329 ;; face, so we rename the faces in the list on an insert. The rename is
330 ;; actually done by copying the faces so user-defined faces still remain
331 ;; in the same order.
332 ;; The notifying the parent is needed because without it changes to the
333 ;; faces are saved but not to the actual list itself.
334 (let ((old-list (widget-value w)))
335 (if (member 'default old-list)
336 (let
337 ((p (reverse old-list))
338 (n (length old-list))
339 new-name old-name
340 (new-list nil)
341 )
342 (while p
343 (setq old-name (car p))
344 (setq new-name (intern (format "highlight-changes-face-%d" n)))
345 (if (eq old-name new-name)
346 nil
347 ;; A new face has been inserted: we don't want to modify the
348 ;; default face so copy it. Better, though, (I think) is to
349 ;; make a new face have the same attributes as
350 ;; highlight-changes-face .
351 (if (eq old-name 'default)
352 (copy-face 'highlight-changes-face new-name)
353 (copy-face old-name new-name)
354 ))
355 (setq new-list (append (list new-name) new-list))
356 (setq n (1- n))
357 (setq p (cdr p)))
358 (if (equal new-list (widget-value w))
359 nil ;; (message "notify: no change!")
360 (widget-value-set w new-list)
361 (widget-setup)
362 )
363 )
364 ;; (message "notify: no default here!")
365 ))
366 (let ((parent (widget-get w :parent)))
367 (when parent
368 (widget-apply parent :notify w event))))
369
370
371 (defcustom highlight-changes-face-list nil
372 "*A list of faces used when rotating changes.
373 Normally the variable is initialized to nil and the list is created from
374 `highlight-changes-colours' when needed. However, you can set this variable
375 to any list of faces. You will have to do this if you want faces which
376 don't just differ from `highlight-changes-face' by the foreground colour.
377 Otherwise, this list will be constructed when needed from
378 `highlight-changes-colours'."
379 :type '(choice
380 (repeat
381 :notify hilit-chg-cust-fix-changes-face-list
382 face )
383 (const :tag "Derive from highlight-changes-colours" nil)
384 )
385 :group 'highlight-changes)
386
387 ;; ========================================================================
388
389 ;; These shouldn't be changed!
390
391 ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
392 ;;;###autoload
393 (defvar highlight-changes-mode nil)
394 (defvar hilit-chg-list nil)
395 (defvar hilit-chg-string " ??")
396 (or (assq 'highlight-changes-mode minor-mode-alist)
397 (setq minor-mode-alist
398 (cons '(highlight-changes-mode hilit-chg-string) minor-mode-alist)
399 ))
400 (make-variable-buffer-local 'highlight-changes-mode)
401 (make-variable-buffer-local 'hilit-chg-string)
402
403
404
405 (eval-and-compile
406 ;; For highlight-compare-with-file
407 (defvar ediff-number-of-differences)
408 (autoload 'ediff-setup "ediff")
409 (autoload 'ediff-with-current-buffer "ediff")
410 (autoload 'ediff-really-quit "ediff")
411 (autoload 'ediff-make-fine-diffs "ediff")
412 (autoload 'ediff-get-fine-diff-vector "ediff")
413 (autoload 'ediff-get-difference "ediff"))
414
415
416
417 ;;; Functions...
418
419 (defun hilit-chg-map-changes (func &optional start-position end-position)
420 "Call function FUNC for each region used by Highlight Changes mode."
421 ;; if start-position is nil, (point-min) is used
422 ;; if end-position is nil, (point-max) is used
423 ;; FUNC is called with 3 params: property start stop
424 (let ((start (or start-position (point-min)))
425 (limit (or end-position (point-max)))
426 prop end)
427 (while (and start (< start limit))
428 (setq prop (get-text-property start 'hilit-chg))
429 (setq end (text-property-not-all start limit 'hilit-chg prop))
430 (if prop
431 (funcall func prop start (or end limit)))
432 (setq start end))))
433
434
435 (defun hilit-chg-display-changes (&optional beg end)
436 "Display face information for Highlight Changes mode.
437
438 An overlay containing a change face is added from the information
439 in the text property of type `hilit-chg'.
440
441 This is the opposite of `hilit-chg-hide-changes'."
442 (hilit-chg-map-changes 'hilit-chg-make-ov beg end))
443
444
445 (defun hilit-chg-make-ov (prop start end)
446 (or prop
447 (error "hilit-chg-make-ov: prop is nil"))
448 ;; for the region make change overlays corresponding to
449 ;; the text property 'hilit-chg
450 (let ((ov (make-overlay start end))
451 face)
452 (if (eq prop 'hilit-chg-delete)
453 (setq face 'highlight-changes-delete-face)
454 (setq face (nth 1 (member prop hilit-chg-list))))
455 (if face
456 (progn
457 ;; We must mark the face, that is the purpose of the overlay
458 (overlay-put ov 'face face)
459 ;; I don't think we need to set evaporate since we should
460 ;; be controlling them!
461 (overlay-put ov 'evaporate t)
462 ;; We set the change property so we can tell this is one
463 ;; of our overlays (so we don't delete someone else's).
464 (overlay-put ov 'hilit-chg t)
465 )
466 (error "hilit-chg-make-ov: no face for prop: %s" prop))))
467
468 (defun hilit-chg-hide-changes (&optional beg end)
469 "Remove face information for Highlight Changes mode.
470
471 The overlay containing the face is removed, but the text property
472 containing the change information is retained.
473
474 This is the opposite of `hilit-chg-display-changes'."
475 (let ((start (or beg (point-min)))
476 (limit (or end (point-max)))
477 p ov)
478 (setq p (overlays-in start limit))
479 (while p
480 ;; don't delete the overlay if it isn't ours!
481 (if (overlay-get (car p) 'hilit-chg)
482 (delete-overlay (car p)))
483 (setq p (cdr p)))))
484
485 (defun hilit-chg-fixup (beg end)
486 "Fix change overlays in region between BEG and END.
487
488 Ensure the overlays agree with the changes as determined from
489 the text properties of type `hilit-chg' ."
490 ;; Remove or alter overlays in region beg..end
491 (let (ov-start ov-end props q)
492 ;; temp for debugging:
493 ;; (or (eq highlight-changes-mode 'active)
494 ;; (error "hilit-chg-fixup called but Highlight Changes mode not active"))
495 (dolist (ov (overlays-in beg end))
496 ;; Don't alter overlays that are not ours.
497 (when (overlay-get ov 'hilit-chg)
498 (let ((ov-start (overlay-start ov))
499 (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)))
509 (setq props (cdr (cdr props)))))))
510 (if (> ov-end end)
511 (move-overlay ov end ov-end)
512 (delete-overlay ov))))))
513 (hilit-chg-display-changes beg end)))
514
515 ;;;###autoload
516 (defun highlight-changes-remove-highlight (beg end)
517 "Remove the change face from the region between BEG and END.
518 This allows you to manually remove highlighting from uninteresting changes."
519 (interactive "r")
520 (let ((after-change-functions nil))
521 (remove-text-properties beg end '(hilit-chg nil))
522 (hilit-chg-fixup beg end)))
523
524 (defun hilit-chg-set-face-on-change (beg end leng-before
525 &optional no-property-change)
526 "Record changes and optionally display them in a distinctive face.
527 `hilit-chg-set' adds this function to the `after-change-functions' hook."
528 ;;
529 ;; This function is called by the `after-change-functions' hook, which
530 ;; is how we are notified when text is changed.
531 ;; It is also called from `highlight-compare-with-file'.
532 ;;
533 ;; We do NOT want to simply do this if this is an undo command, because
534 ;; otherwise an undone change shows up as changed. While the properties
535 ;; are automatically restored by undo, we must fix up the overlay.
536 (save-match-data
537 (let ((beg-decr 1) (end-incr 1)
538 (type 'hilit-chg)
539 old)
540 (if undo-in-progress
541 (if (eq highlight-changes-mode 'active)
542 (hilit-chg-fixup beg end))
543 (if (and (= beg end) (> leng-before 0))
544 ;; deletion
545 (progn
546 ;; The eolp and bolp tests are a kludge! But they prevent
547 ;; rather nasty looking displays when deleting text at the end
548 ;; of line, such as normal corrections as one is typing and
549 ;; immediately makes a correction, and when deleting first
550 ;; character of a line.
551 ;;; (if (= leng-before 1)
552 ;;; (if (eolp)
553 ;;; (setq beg-decr 0 end-incr 0)
554 ;;; (if (bolp)
555 ;;; (setq beg-decr 0))))
556 ;;; (setq beg (max (- beg beg-decr) (point-min)))
557 (setq end (min (+ end end-incr) (point-max)))
558 (setq type 'hilit-chg-delete))
559 ;; Not a deletion.
560 ;; Most of the time the following is not necessary, but
561 ;; if the current text was marked as a deletion then
562 ;; the old overlay is still in effect, so if we add some
563 ;; text then remove the deletion marking, but set it to
564 ;; changed otherwise its highlighting disappears.
565 (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete)
566 (progn
567 (remove-text-properties end (+ end 1) '(hilit-chg nil))
568 (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg)
569 (if (eq highlight-changes-mode 'active)
570 (hilit-chg-fixup beg (+ end 1))))))
571 (unless no-property-change
572 (put-text-property beg end 'hilit-chg type))
573 (if (or (eq highlight-changes-mode 'active) no-property-change)
574 (hilit-chg-make-ov type beg end))))))
575
576 (defun hilit-chg-set (value)
577 "Turn on Highlight Changes mode for this buffer."
578 (setq highlight-changes-mode value)
579 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
580 (hilit-chg-make-list)
581 (if (eq highlight-changes-mode 'active)
582 (progn
583 (setq hilit-chg-string highlight-changes-active-string)
584 (or buffer-read-only
585 (hilit-chg-display-changes)))
586 ;; mode is passive
587 (setq hilit-chg-string highlight-changes-passive-string)
588 (or buffer-read-only
589 (hilit-chg-hide-changes)))
590 (force-mode-line-update)
591 (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t))
592
593 (defun hilit-chg-clear ()
594 "Remove Highlight Changes mode for this buffer.
595 This removes all saved change information."
596 (if buffer-read-only
597 ;; We print the buffer name because this function could be called
598 ;; on many buffers from `global-highlight-changes'.
599 (message "Cannot remove highlighting from read-only mode buffer %s"
600 (buffer-name))
601 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
602 (let ((after-change-functions nil))
603 (hilit-chg-hide-changes)
604 (hilit-chg-map-changes
605 '(lambda (prop start stop)
606 (remove-text-properties start stop '(hilit-chg nil))))
607 )
608 (setq highlight-changes-mode nil)
609 (force-mode-line-update)
610 ;; If we type: C-u -1 M-x highlight-changes-mode
611 ;; we want to turn it off, but hilit-chg-post-command-hook
612 ;; runs and that turns it back on!
613 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook)))
614
615 ;;;###autoload
616 (defun highlight-changes-mode (&optional arg)
617 "Toggle (or initially set) Highlight Changes mode.
618
619 Without an argument:
620 If Highlight Changes mode is not enabled, then enable it (in either active
621 or passive state as determined by the variable
622 `highlight-changes-initial-state'); otherwise, toggle between active
623 and passive state.
624
625 With an argument ARG:
626 If ARG is positive, set state to active;
627 If ARG is zero, set state to passive;
628 If ARG is negative, disable Highlight Changes mode completely.
629
630 Active state - means changes are shown in a distinctive face.
631 Passive state - means changes are kept and new ones recorded but are
632 not displayed in a different face.
633
634 Functions:
635 \\[highlight-changes-next-change] - move point to beginning of next change
636 \\[highlight-changes-previous-change] - move to beginning of previous change
637 \\[highlight-compare-with-file] - mark text as changed by comparing this
638 buffer with the contents of a file
639 \\[highlight-changes-remove-highlight] - remove the change face from the region
640 \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes \
641 through
642 various faces.
643
644 Hook variables:
645 `highlight-changes-enable-hook' - when enabling Highlight Changes mode.
646 `highlight-changes-toggle-hook' - when entering active or passive state
647 `highlight-changes-disable-hook' - when turning off Highlight Changes mode."
648 (interactive "P")
649 (if (or (display-color-p)
650 (and (fboundp 'x-display-grayscale-p) (x-display-grayscale-p)))
651 (let ((was-on highlight-changes-mode)
652 (new-highlight-changes-mode
653 (cond
654 ((null arg)
655 ;; no arg => toggle (or set to active initially)
656 (if highlight-changes-mode
657 (if (eq highlight-changes-mode 'active) 'passive 'active)
658 highlight-changes-initial-state))
659 ;; an argument is given
660 ((eq arg 'active)
661 'active)
662 ((eq arg 'passive)
663 'passive)
664 ((> (prefix-numeric-value arg) 0)
665 'active)
666 ((< (prefix-numeric-value arg) 0)
667 nil)
668 (t
669 'passive))))
670 (if new-highlight-changes-mode
671 ;; mode is turned on -- but may be passive
672 (progn
673 (hilit-chg-set new-highlight-changes-mode)
674 (or was-on
675 ;; run highlight-changes-enable-hook once
676 (run-hooks 'highlight-changes-enable-hook))
677 (run-hooks 'highlight-changes-toggle-hook))
678 ;; mode is turned off
679 (run-hooks 'highlight-changes-disable-hook)
680 (hilit-chg-clear)))
681 (message "Highlight Changes mode requires color or grayscale display")))
682
683 ;;;###autoload
684 (defun highlight-changes-next-change ()
685 "Move to the beginning of the next change, if in Highlight Changes mode."
686 (interactive)
687 (if highlight-changes-mode
688 (let ((start (point))
689 prop)
690 (setq prop (get-text-property (point) 'hilit-chg))
691 (if prop
692 ;; we are in a change
693 (setq start (next-single-property-change (point) 'hilit-chg)))
694 (if start
695 (setq start (next-single-property-change start 'hilit-chg)))
696 (if start
697 (goto-char start)
698 (message "no next change")))
699 (message "This buffer is not in Highlight Changes mode.")))
700
701
702 ;;;###autoload
703 (defun highlight-changes-previous-change ()
704 "Move to the beginning of the previous change, if in Highlight Changes mode."
705 (interactive)
706 (if highlight-changes-mode
707 (let ( (start (point)) (prop nil) )
708 (or (bobp)
709 (setq prop (get-text-property (1- (point)) 'hilit-chg)))
710 (if prop
711 ;; we are in a change
712 (setq start (previous-single-property-change (point) 'hilit-chg)))
713 (if start
714 (setq start (previous-single-property-change start 'hilit-chg)))
715 ;; special handling for the case where (point-min) is a change
716 (if start
717 (setq start (or (previous-single-property-change start 'hilit-chg)
718 (if (get-text-property (point-min) 'hilit-chg)
719 (point-min)))))
720 (if start
721 (goto-char start)
722 (message "no previous change")))
723 (message "This buffer is not in Highlight Changes mode.")))
724
725 ;; ========================================================================
726
727 (defun hilit-chg-make-list (&optional force)
728 "Construct `hilit-chg-list' and `highlight-changes-face-list'."
729 ;; Constructs highlight-changes-face-list if necessary,
730 ;; and hilit-chg-list always:
731 ;; Maybe this should always be called when rotating a face
732 ;; so we pick up any changes?
733 (if (or (null highlight-changes-face-list) ; Don't do it if it
734 force) ; already exists unless FORCE non-nil.
735 (let ((p highlight-changes-colours)
736 (n 1) name)
737 (setq highlight-changes-face-list nil)
738 (while p
739 (setq name (intern (format "highlight-changes-face-%d" n)))
740 (copy-face 'highlight-changes-face name)
741 (set-face-foreground name (car p))
742 (setq highlight-changes-face-list
743 (append highlight-changes-face-list (list name)))
744 (setq p (cdr p))
745 (setq n (1+ n)))))
746 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes-face))
747 (let ((p highlight-changes-face-list)
748 (n 1)
749 last-category last-face)
750 (while p
751 (setq last-category (intern (format "change-%d" n)))
752 ;; (setq last-face (intern (format "highlight-changes-face-%d" n)))
753 (setq last-face (car p))
754 (setq hilit-chg-list
755 (append hilit-chg-list
756 (list last-category last-face)))
757 (setq p (cdr p))
758 (setq n (1+ n)))
759 (setq hilit-chg-list
760 (append hilit-chg-list
761 (list last-category last-face)))))
762
763 (defun hilit-chg-bump-change (prop start end)
764 "Increment (age) the Highlight Changes mode text property."
765 (let ( new-prop )
766 (if (eq prop 'hilit-chg-delete)
767 (setq new-prop (nth 2 hilit-chg-list))
768 (setq new-prop (nth 2 (member prop hilit-chg-list))))
769 (if prop
770 (put-text-property start end 'hilit-chg new-prop)
771 (message "%d-%d unknown property %s not changed" start end prop))))
772
773 ;;;###autoload
774 (defun highlight-changes-rotate-faces ()
775 "Rotate the faces used by Highlight Changes mode.
776
777 Current changes are displayed in the face described by the first element
778 of `highlight-changes-face-list', one level older changes are shown in
779 face described by the second element, and so on. Very old changes remain
780 shown in the last face in the list.
781
782 You can automatically rotate colours when the buffer is saved
783 by adding the following to `local-write-file-hooks', by evaling it in the
784 buffer to be saved):
785
786 \(add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)"
787 (interactive)
788 ;; If not in active mode do nothing but don't complain because this
789 ;; may be bound to a hook.
790 (if (eq highlight-changes-mode 'active)
791 (let ((after-change-functions nil))
792 ;; ensure hilit-chg-list is made and up to date
793 (hilit-chg-make-list)
794 ;; remove our existing overlays
795 (hilit-chg-hide-changes)
796 ;; for each change text property, increment it
797 (hilit-chg-map-changes 'hilit-chg-bump-change)
798 ;; and display them all if active
799 (if (eq highlight-changes-mode 'active)
800 (hilit-chg-display-changes))))
801 ;; This always returns nil so it is safe to use in
802 ;; local-write-file-hook
803 nil)
804
805 ;; ========================================================================
806 ;; Comparing with an existing file.
807 ;; This uses ediff to find the differences.
808
809 ;;;###autoload
810 (defun highlight-compare-with-file (file-b)
811 "Compare this buffer with a file, and highlight differences.
812
813 The current buffer must be an unmodified buffer visiting a file,
814 and must not be read-only.
815
816 If the buffer has a backup filename, it is used as the default when
817 this function is called interactively.
818
819 If the current buffer is visiting the file being compared against, it
820 also will have its differences highlighted. Otherwise, the file is
821 read in temporarily but the buffer is deleted.
822
823 If the buffer is read-only, differences will be highlighted but no property
824 changes are made, so \\[highlight-changes-next-change] and
825 \\[highlight-changes-previous-change] will not work."
826 (interactive (list
827 (read-file-name
828 "File to compare with? " ;; prompt
829 "" ;; directory
830 nil ;; default
831 'yes ;; must exist
832 (let ((f (make-backup-file-name
833 (or (buffer-file-name (current-buffer))
834 (error "no file for this buffer")))))
835 (if (file-exists-p f) f "")))))
836
837 (let* ((buf-a (current-buffer))
838 (buf-a-read-only buffer-read-only)
839 (orig-pos (point))
840 (file-a (buffer-file-name))
841 (existing-buf (get-file-buffer file-b))
842 (buf-b (or existing-buf
843 (find-file-noselect file-b)))
844 (buf-b-read-only (with-current-buffer buf-b buffer-read-only))
845 xy xx yy p q
846 a-start a-end len-a
847 b-start b-end len-b)
848
849 ;; We use the fact that the buffer is not marked modified at the
850 ;; end where we clear its modified status
851 (if (buffer-modified-p buf-a)
852 (if (y-or-n-p (format "OK to save %s? " file-a))
853 (save-buffer buf-a)
854 (error "Buffer must be saved before comparing with a file")))
855 (if (and existing-buf (buffer-modified-p buf-b))
856 (if (y-or-n-p (format "OK to save %s? " file-b))
857 (save-buffer buf-b)
858 (error "Cannot compare with a file in an unsaved buffer")))
859 (highlight-changes-mode 'active)
860 (if existing-buf (with-current-buffer buf-b
861 (highlight-changes-mode 'active)))
862 (save-window-excursion
863 (setq xy (hilit-chg-get-diff-info buf-a file-a buf-b file-b)))
864 (setq xx (car xy))
865 (setq p xx)
866 (setq yy (car (cdr xy)))
867 (setq q yy)
868 (hilit-chg-make-list)
869 (while p
870 (setq a-start (nth 0 (car p)))
871 (setq a-end (nth 1 (car p)))
872 (setq b-start (nth 0 (car q)))
873 (setq b-end (nth 1 (car q)))
874 (setq len-a (- a-end a-start))
875 (setq len-b (- b-end b-start))
876 (set-buffer buf-a)
877 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
878 (set-buffer-modified-p nil)
879 (goto-char orig-pos)
880 (if existing-buf
881 (with-current-buffer buf-b
882 (hilit-chg-set-face-on-change b-start b-end len-a
883 buf-b-read-only )
884 ))
885 (setq p (cdr p))
886 (setq q (cdr q)))
887 (if existing-buf
888 (set-buffer-modified-p nil)
889 (kill-buffer buf-b))))
890
891
892 (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b)
893 (let ((e nil) x y) ;; e is set by function hilit-chg-get-diff-list-hk
894 (ediff-setup buf-a file-a buf-b file-b
895 nil nil ; buf-c file-C
896 'hilit-chg-get-diff-list-hk
897 (list (cons 'ediff-job-name 'something))
898 )
899 (ediff-with-current-buffer e (ediff-really-quit nil))
900 (list x y)))
901
902
903 (defun hilit-chg-get-diff-list-hk ()
904 ;; x and y are dynamically bound by hilit-chg-get-diff-info
905 ;; which calls this function as a hook
906 (defvar x) ;; placate the byte-compiler
907 (defvar y)
908 (setq e (current-buffer))
909 (let ((n 0) extent p va vb a b)
910 (setq x nil y nil) ;; x and y are bound by hilit-chg-get-diff-info
911 (while (< n ediff-number-of-differences)
912 (ediff-make-fine-diffs n)
913 (setq va (ediff-get-fine-diff-vector n 'A))
914 ;; va is a vector if there are fine differences
915 (if va
916 (setq a (append va nil))
917 ;; if not, get the unrefined difference
918 (setq va (ediff-get-difference n 'A))
919 (setq a (list (elt va 0))))
920 ;; a list a list
921 (setq p a)
922 (while p
923 (setq extent (list (overlay-start (car p))
924 (overlay-end (car p))))
925 (setq p (cdr p))
926 (setq x (append x (list extent) )));; while p
927 ;;
928 (setq vb (ediff-get-fine-diff-vector n 'B))
929 ;; vb is a vector
930 (if vb
931 (setq b (append vb nil))
932 ;; if not, get the unrefined difference
933 (setq vb (ediff-get-difference n 'B))
934 (setq b (list (elt vb 0))))
935 ;; b list a list
936 (setq p b)
937 (while p
938 (setq extent (list (overlay-start (car p))
939 (overlay-end (car p))))
940 (setq p (cdr p))
941 (setq y (append y (list extent) )))
942 (setq n (1+ n)));; while
943 ;; ediff-quit doesn't work here.
944 ;; No point in returning a value, since this is a hook function.
945 ))
946
947 ;; ======================= automatic stuff ==============
948
949 ;; Global Highlight Changes mode is modeled after Global Font-lock mode.
950 ;; Three hooks are used to gain control. When Global Changes Mode is
951 ;; enabled, `find-file-hooks' and `change-major-mode-hook' are set.
952 ;; `find-file-hooks' is called when visiting a file, the new mode is
953 ;; known at this time.
954 ;; `change-major-mode-hook' is called when a buffer is changing mode.
955 ;; This could be because of finding a file in which case
956 ;; `find-file-hooks' has already been called and has done its work.
957 ;; However, it also catches the case where a new mode is being set by
958 ;; the user. However, it is called from `kill-all-variables' and at
959 ;; this time the mode is the old mode, which is not what we want.
960 ;; So, our function temporarily sets `post-command-hook' which will
961 ;; be called after the buffer has been completely set up (with the new
962 ;; mode). It then removes the `post-command-hook'.
963 ;; One other wrinkle - every M-x command runs the `change-major-mode-hook'
964 ;; so we ignore this by examining the buffer name.
965
966
967 (defun hilit-chg-major-mode-hook ()
968 (add-hook 'post-command-hook 'hilit-chg-post-command-hook))
969
970 (defun hilit-chg-post-command-hook ()
971 ;; This is called after changing a major mode, but also after each
972 ;; M-x command, in which case the current buffer is a minibuffer.
973 ;; In that case, do not act on it here, but don't turn it off
974 ;; either, we will get called here again soon-after.
975 ;; Also, don't enable it for other special buffers.
976 (if (string-match "^[ *]" (buffer-name))
977 nil ;; (message "ignoring this post-command-hook")
978 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook)
979 ;; The following check isn't necessary, since
980 ;; hilit-chg-turn-on-maybe makes this check too.
981 (or highlight-changes-mode ;; don't turn it on if it already is
982 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state))))
983
984 (defun hilit-chg-check-global ()
985 ;; This is called from the find file hook.
986 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state))
987
988
989 ;;;###autoload
990 (defun global-highlight-changes (&optional arg)
991 "Turn on or off global Highlight Changes mode.
992
993 When called interactively:
994 - if no prefix, toggle global Highlight Changes mode on or off
995 - if called with a positive prefix (or just C-u) turn it on in active mode
996 - if called with a zero prefix turn it on in passive mode
997 - if called with a negative prefix turn it off
998
999 When called from a program:
1000 - if ARG is nil or omitted, turn it off
1001 - if ARG is `active', turn it on in active mode
1002 - if ARG is `passive', turn it on in passive mode
1003 - otherwise just turn it on
1004
1005 When global Highlight Changes mode is enabled, Highlight Changes mode is turned
1006 on for future \"suitable\" buffers (and for \"suitable\" existing buffers if
1007 variable `highlight-changes-global-changes-existing-buffers' is non-nil).
1008 \"Suitability\" is determined by variable `highlight-changes-global-modes'."
1009
1010 (interactive
1011 (list
1012 (cond
1013 ((null current-prefix-arg)
1014 ;; no arg => toggle it on/off
1015 (setq global-highlight-changes (not global-highlight-changes)))
1016 ;; positive interactive arg - turn it on as active
1017 ((> (prefix-numeric-value current-prefix-arg) 0)
1018 (setq global-highlight-changes t)
1019 'active)
1020 ;; zero interactive arg - turn it on as passive
1021 ((= (prefix-numeric-value current-prefix-arg) 0)
1022 (setq global-highlight-changes t)
1023 'passive)
1024 ;; negative interactive arg - turn it off
1025 (t
1026 (setq global-highlight-changes nil)
1027 nil))))
1028
1029 (if arg
1030 (progn
1031 (if (eq arg 'active)
1032 (setq highlight-changes-global-initial-state 'active)
1033 (if (eq arg 'passive)
1034 (setq highlight-changes-global-initial-state 'passive)))
1035 (setq global-highlight-changes t)
1036 (message "Turning ON Global Highlight Changes mode in %s state"
1037 highlight-changes-global-initial-state)
1038 (add-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook)
1039 (add-hook 'find-file-hooks 'hilit-chg-check-global)
1040 (if highlight-changes-global-changes-existing-buffers
1041 (hilit-chg-update-all-buffers
1042 highlight-changes-global-initial-state)))
1043
1044 (message "Turning OFF global Highlight Changes mode")
1045 (remove-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook)
1046 (remove-hook 'find-file-hooks 'hilit-chg-check-global)
1047 (remove-hook 'post-command-hook
1048 'hilit-chg-post-command-hook)
1049 (remove-hook 'find-file-hooks 'hilit-chg-check-global)
1050 (if highlight-changes-global-changes-existing-buffers
1051 (hilit-chg-update-all-buffers nil))))
1052
1053
1054 (defun hilit-chg-turn-on-maybe (value)
1055 "Turn on Highlight Changes mode if it is appropriate for this buffer.
1056
1057 A buffer is appropriate for Highlight Changes mode if all these are true:
1058 - the buffer is not a special buffer (one whose name begins with
1059 `*' or ` ')
1060 - the buffer's mode is suitable as per variable
1061 `highlight-changes-global-modes'
1062 - Highlight Changes mode is not already on for this buffer.
1063
1064 This function is called from `hilit-chg-update-all-buffers' or
1065 from `global-highlight-changes' when turning on global Highlight Changes mode."
1066 (or highlight-changes-mode ; do nothing if already on
1067 (if
1068 (cond
1069 ((null highlight-changes-global-modes)
1070 nil)
1071 ((functionp highlight-changes-global-modes)
1072 (funcall highlight-changes-global-modes))
1073 ((listp highlight-changes-global-modes)
1074 (if (eq (car-safe highlight-changes-global-modes) 'not)
1075 (not (memq major-mode (cdr highlight-changes-global-modes)))
1076 (memq major-mode highlight-changes-global-modes)))
1077 (t
1078 (and
1079 (not (string-match "^[ *]" (buffer-name)))
1080 (buffer-file-name))))
1081 (progn
1082 (hilit-chg-set value)
1083 (run-hooks 'highlight-changes-enable-hook)))))
1084
1085
1086 (defun hilit-chg-turn-off-maybe ()
1087 (if highlight-changes-mode
1088 (progn
1089 (run-hooks 'highlight-changes-disable-hook)
1090 (hilit-chg-clear))))
1091
1092
1093 (defun hilit-chg-update-all-buffers (value)
1094 (mapcar
1095 (function (lambda (buffer)
1096 (with-current-buffer buffer
1097 (if value
1098 (hilit-chg-turn-on-maybe value)
1099 (hilit-chg-turn-off-maybe))
1100 )))
1101 (buffer-list)))
1102
1103 ;; ===================== debug ==================
1104 ;; For debug & test use:
1105 ;;
1106 ;; (defun hilit-chg-debug-show (&optional beg end)
1107 ;; (interactive)
1108 ;; (message "--- hilit-chg-debug-show ---")
1109 ;; (hilit-chg-map-changes '(lambda (prop start end)
1110 ;; (message "%d-%d: %s" start end prop)
1111 ;; )
1112 ;; beg end
1113 ;; ))
1114 ;;
1115 ;; ================== end of debug ===============
1116
1117 (provide 'hilit-chg)
1118
1119 ;;; arch-tag: de00301d-5bad-44da-aa82-e0e010b0c463
1120 ;;; hilit-chg.el ends here