eliminate use of inefficient match-data
[emacs.git] / lisp / hilit-chg.el
blob11898c13f009ba86af1e7ea5970cb77b7dae7e1d
1 ;;; hilit-chg.el --- minor mode displaying buffer changes with special face
3 ;; Copyright (C) 1998, 2000, 2005 Free Software Foundation, Inc.
5 ;; Author: Richard Sharman <rsharman@pobox.com>
6 ;; Keywords: faces
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;; A minor mode: "Highlight Changes mode".
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.
40 ;; When active, changes are displayed in the `highlight-changes' face.
41 ;; When text is deleted, the following character is displayed in the
42 ;; `highlight-changes-delete' face.
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 ;; the `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'.
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'.
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). The command highlight-compare-buffers can be used to
65 ;; compare two buffers.
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'.)
82 ;; Example usage:
83 ;; (defun my-highlight-changes-enable-hook ()
84 ;; (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
85 ;; )
87 ;; (defun my-highlight-changes-disable-hook ()
88 ;; (remove-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
89 ;; )
91 ;; (add-hook 'highlight-changes-enable-hook 'my-highlight-changes-enable-hook)
92 ;; (add-hook 'highlight-changes-disable-hook
93 ;; 'my-highlight-changes-disable-hook)
96 ;; Explicit vs. Implicit
99 ;; Normally, Highlight Changes mode is turned on explicitly in a buffer.
101 ;; If you prefer to have it automatically invoked you can do it as
102 ;; follows.
104 ;; 1. Most modes have a major-hook, typically called MODE-hook. You
105 ;; can use `add-hook' to call `highlight-changes-mode'.
107 ;; Example:
108 ;; (add-hook 'c-mode-hook 'highlight-changes-mode)
110 ;; If you want to make it start up in passive mode (regardless of the
111 ;; setting of highlight-changes-initial-state):
112 ;; (add-hook 'emacs-lisp-mode-hook
113 ;; (lambda ()
114 ;; (highlight-changes-mode 'passive)))
116 ;; However, this cannot be done for Fundamental mode for there is no
117 ;; such hook.
119 ;; 2. You can use the function `global-highlight-changes'
121 ;; This function, which is fashioned after the way `global-font-lock' works,
122 ;; toggles on or off global Highlight Changes mode. When activated, it turns
123 ;; on Highlight Changes mode in all "suitable" existing buffers and will turn
124 ;; it on in new "suitable" buffers to be created.
126 ;; A buffer's "suitability" is determined by variable
127 ;; `highlight-changes-global-modes', as follows. If the variable is
128 ;; * nil -- then no buffers are suitable;
129 ;; * a function -- this function is called and the result is used. As
130 ;; an example, if the value is `buffer-file-name' then all buffers
131 ;; who are visiting files are suitable, but others (like dired
132 ;; buffers) are not;
133 ;; * a list -- then the buffer is suitable iff its mode is in the
134 ;; list, except if the first element is `not', in which case the test
135 ;; is reversed (i.e. it is a list of unsuitable modes).
136 ;; * Otherwise, the buffer is suitable if its name does not begin with
137 ;; ` ' or `*' and if `buffer-file-name' returns true.
142 ;; Possible bindings:
143 ;; (global-set-key '[C-right] 'highlight-changes-next-change)
144 ;; (global-set-key '[C-left] 'highlight-changes-previous-change)
146 ;; Other interactive functions (which could be bound if desired):
147 ;; highlight-changes-mode
148 ;; highlight-changes-remove-highlight
149 ;; highlight-changes-rotate-faces
150 ;; highlight-compare-with-file
151 ;; highlight-compare-buffers
154 ;; You can automatically rotate faces when the buffer is saved;
155 ;; see function `highlight-changes-rotate-faces' for how to do this.
159 ;;; Bugs:
161 ;; - the next-change and previous-change functions are too literal;
162 ;; they should find the next "real" change, in other words treat
163 ;; consecutive changes as one.
166 ;;; To do (maybe), notes, ...
168 ;; - having different faces for deletion and non-deletion: is it
169 ;; really worth the hassle?
170 ;; - should have better hooks: when should they be run?
171 ;; - highlight-compare-with-file should allow RCS files - e.g. nice to be
172 ;; able to say show changes compared with version 2.1.
173 ;; - Maybe we should have compare-with-buffer as well. (When I tried
174 ;; a while back I ran into a problem with ediff-buffers-internal.)
177 ;;; History:
179 ;; R Sharman (rsharman@pobox.com) Feb 1998:
180 ;; - initial release as change-mode.
181 ;; Jari Aalto <jari.aalto@ntc.nokia.com> Mar 1998
182 ;; - fixes for byte compile errors
183 ;; - use eval-and-compile for autoload
184 ;; Marijn Ros <J.M.Ros@fys.ruu.nl> Mar 98
185 ;; - suggested turning it on by default
186 ;; Eric Ludlam <zappo@gnu.org> Suggested using overlays.
187 ;; July 98
188 ;; - global mode and various stuff added
189 ;; - Changed to use overlays
190 ;; August 98
191 ;; - renamed to Highlight Changes mode.
192 ;; Dec 2003
193 ;; - Use require for ediff stuff
194 ;; - Added highlight-compare-buffers
196 ;;; Code:
198 (require 'wid-edit)
200 ;; ====================== Customization =======================
201 (defgroup highlight-changes nil
202 "Highlight Changes mode."
203 :version "20.4"
204 :group 'faces)
207 ;; Face information: How the changes appear.
209 ;; Defaults for face: red foreground, no change to background,
210 ;; and underlined if a change is because of a deletion.
211 ;; Note: underlining is helpful in that it shows up changes in white space.
212 ;; However, having it set for non-delete changes can be annoying because all
213 ;; indentation on inserts gets underlined (which can look pretty ugly!).
215 (defface highlight-changes
216 '((((min-colors 88) (class color)) (:foreground "red1" ))
217 (((class color)) (:foreground "red" ))
218 (t (:inverse-video t)))
219 "Face used for highlighting changes."
220 :group 'highlight-changes)
221 ;; backward-compatibility alias
222 (put 'highlight-changes-face 'face-alias 'highlight-changes)
224 ;; This looks pretty ugly, actually. Maybe the underline should be removed.
225 (defface highlight-changes-delete
226 '((((min-colors 88) (class color)) (:foreground "red1" :underline t))
227 (((class color)) (:foreground "red" :underline t))
228 (t (:inverse-video t)))
229 "Face used for highlighting deletions."
230 :group 'highlight-changes)
231 ;; backward-compatibility alias
232 (put 'highlight-changes-delete-face 'face-alias 'highlight-changes-delete)
236 ;; A (not very good) default list of colours to rotate through.
238 (defcustom highlight-changes-colours
239 (if (eq (frame-parameter nil 'background-mode) 'light)
240 ;; defaults for light background:
241 '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue")
242 ;; defaults for dark background:
243 '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid"))
244 "*Colors used by `highlight-changes-rotate-faces'.
245 The newest rotated change will be displayed in the first element of this list,
246 the next older will be in the second element etc.
248 This list is used if `highlight-changes-face-list' is nil, otherwise that
249 variable overrides this list. If you only care about foreground
250 colors then use this, if you want fancier faces then set
251 `highlight-changes-face-list'."
252 :type '(repeat color)
253 :group 'highlight-changes)
256 ;; If you invoke highlight-changes-mode with no argument, should it start in
257 ;; active or passive mode?
259 (defcustom highlight-changes-initial-state 'active
260 "*What state (active or passive) `highlight-changes' should start in.
261 This is used when `highlight-changes' is called with no argument.
262 This variable must be set to one of the symbols `active' or `passive'."
263 :type '(choice (const :tag "Active" active)
264 (const :tag "Passive" passive))
265 :group 'highlight-changes)
267 (defcustom highlight-changes-global-initial-state 'passive
268 "*What state `global-highlight-changes' should start in.
269 This is used if `global-highlight-changes' is called with no argument.
270 This variable must be set to either `active' or `passive'"
271 :type '(choice (const :tag "Active" active)
272 (const :tag "Passive" passive))
273 :group 'highlight-changes)
275 ;; The strings displayed in the mode-line for the minor mode:
276 (defcustom highlight-changes-active-string " +Chg"
277 "*The string used when Highlight Changes mode is in the active state.
278 This should be set to nil if no indication is desired, or to
279 a string with a leading space."
280 :type '(choice string
281 (const :tag "None" nil))
282 :group 'highlight-changes)
284 (defcustom highlight-changes-passive-string " -Chg"
285 "*The string used when Highlight Changes mode is in the passive state.
286 This should be set to nil if no indication is desired, or to
287 a string with a leading space."
288 :type '(choice string
289 (const :tag "None" nil))
290 :group 'highlight-changes)
292 (defcustom highlight-changes-global-modes t
293 "*Determine whether a buffer is suitable for global Highlight Changes mode.
295 A function means call that function to decide: if it returns non-nil,
296 the buffer is suitable.
298 A list means the elements are major modes suitable for Highlight
299 Changes mode, or a list whose first element is `not' followed by major
300 modes which are not suitable.
302 t means the buffer is suitable if it is visiting a file and its name
303 does not begin with ` ' or `*'.
305 A value of nil means no buffers are suitable for `global-highlight-changes'
306 \(effectively disabling the mode).
308 Examples:
309 (c-mode c++-mode)
310 means that Highlight Changes mode is turned on for buffers in C and C++
311 modes only."
312 :type '(choice
313 (const :tag "all non-special buffers visiting files" t)
314 (set :menu-tag "specific modes" :tag "modes"
315 :value (not)
316 (const :tag "All except these" not)
317 (repeat :tag "Modes" :inline t (symbol :tag "mode")))
318 (function :menu-tag "determined by function"
319 :value buffer-file-name)
320 (const :tag "none" nil)
322 :group 'highlight-changes)
324 (defvar global-highlight-changes nil)
326 (defcustom highlight-changes-global-changes-existing-buffers nil
327 "*If non-nil, toggling global Highlight Changes mode affects existing buffers.
328 Normally, `global-highlight-changes' affects only new buffers (to be
329 created). However, if `highlight-changes-global-changes-existing-buffers'
330 is non-nil, then turning on `global-highlight-changes' will turn on
331 Highlight Changes mode in suitable buffers, and turning the mode off will
332 remove it from existing buffers."
333 :type 'boolean
334 :group 'highlight-changes)
336 (defun hilit-chg-cust-fix-changes-face-list (w wc &optional event)
337 ;; When customization function `highlight-changes-face-list' inserts a new
338 ;; face it uses the default face. We don't want the user to modify this
339 ;; face, so we rename the faces in the list on an insert. The rename is
340 ;; actually done by copying the faces so user-defined faces still remain
341 ;; in the same order.
342 ;; The notifying the parent is needed because without it changes to the
343 ;; faces are saved but not to the actual list itself.
344 (let ((old-list (widget-value w)))
345 (if (member 'default old-list)
346 (let
347 ((p (reverse old-list))
348 (n (length old-list))
349 new-name old-name
350 (new-list nil)
352 (while p
353 (setq old-name (car p))
354 (setq new-name (intern (format "highlight-changes-%d" n)))
355 (if (eq old-name new-name)
357 ;; A new face has been inserted: we don't want to modify the
358 ;; default face so copy it. Better, though, (I think) is to
359 ;; make a new face have the same attributes as
360 ;; the `highlight-changes' face.
361 (if (eq old-name 'default)
362 (copy-face 'highlight-changes new-name)
363 (copy-face old-name new-name)
365 (setq new-list (append (list new-name) new-list))
366 (setq n (1- n))
367 (setq p (cdr p)))
368 (if (equal new-list (widget-value w))
369 nil ;; (message "notify: no change!")
370 (widget-value-set w new-list)
371 (widget-setup)
374 ;; (message "notify: no default here!")
376 (let ((parent (widget-get w :parent)))
377 (when parent
378 (widget-apply parent :notify w event))))
381 (defcustom highlight-changes-face-list nil
382 "*A list of faces used when rotating 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 the `highlight-changes' face by the foreground color.
387 Otherwise, this list will be constructed when needed from
388 `highlight-changes-colours'."
389 :type '(choice
390 (repeat
391 :notify hilit-chg-cust-fix-changes-face-list
392 face )
393 (const :tag "Derive from highlight-changes-colours" nil)
395 :group 'highlight-changes)
397 ;; ========================================================================
399 ;; These shouldn't be changed!
401 (defvar highlight-changes-mode nil)
402 (defvar hilit-chg-list nil)
403 (defvar hilit-chg-string " ??")
404 (or (assq 'highlight-changes-mode minor-mode-alist)
405 (setq minor-mode-alist
406 (cons '(highlight-changes-mode hilit-chg-string) minor-mode-alist)
408 (make-variable-buffer-local 'highlight-changes-mode)
409 (make-variable-buffer-local 'hilit-chg-string)
412 (require 'ediff-init)
413 (require 'ediff-util)
416 ;;; Functions...
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)))
431 (setq start end))))
434 (defun hilit-chg-display-changes (&optional beg end)
435 "Display face information for Highlight Changes mode.
437 An overlay containing a change face is added from the information
438 in the text property of type `hilit-chg'.
440 This is the opposite of `hilit-chg-hide-changes'."
441 (hilit-chg-map-changes 'hilit-chg-make-ov beg end))
444 (defun hilit-chg-make-ov (prop start end)
445 (or prop
446 (error "hilit-chg-make-ov: prop is nil"))
447 ;; for the region make change overlays corresponding to
448 ;; the text property 'hilit-chg
449 (let ((ov (make-overlay start end))
450 face)
451 (if (eq prop 'hilit-chg-delete)
452 (setq face 'highlight-changes-delete)
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)
465 (error "hilit-chg-make-ov: no face for prop: %s" prop))))
467 (defun hilit-chg-hide-changes (&optional beg end)
468 "Remove face information for Highlight Changes mode.
470 The overlay containing the face is removed, but the text property
471 containing the change information is retained.
473 This is the opposite of `hilit-chg-display-changes'."
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)))
482 (setq p (cdr p)))))
484 (defun hilit-chg-fixup (beg end)
485 "Fix change overlays in region between BEG and END.
487 Ensure the overlays agree with the changes as determined from
488 the text properties of type `hilit-chg' ."
489 ;; Remove or alter overlays in region beg..end
490 (let (ov-start ov-end props q)
491 ;; temp for debugging:
492 ;; (or (eq highlight-changes-mode 'active)
493 ;; (error "hilit-chg-fixup called but Highlight Changes mode not active"))
494 (dolist (ov (overlays-in beg end))
495 ;; Don't alter overlays that are not ours.
496 (when (overlay-get ov 'hilit-chg)
497 (let ((ov-start (overlay-start ov))
498 (ov-end (overlay-end ov)))
499 (if (< ov-start beg)
500 (progn
501 (move-overlay ov ov-start beg)
502 (if (> ov-end end)
503 (progn
504 (setq props (overlay-properties ov))
505 (setq ov (make-overlay end ov-end))
506 (while props
507 (overlay-put ov (car props)(car (cdr props)))
508 (setq props (cdr (cdr props)))))))
509 (if (> ov-end end)
510 (move-overlay ov end ov-end)
511 (delete-overlay ov))))))
512 (hilit-chg-display-changes beg end)))
514 ;;;###autoload
515 (defun highlight-changes-remove-highlight (beg end)
516 "Remove the change face from the region between BEG and END.
517 This allows you to manually remove highlighting from uninteresting changes."
518 (interactive "r")
519 (let ((after-change-functions nil))
520 (remove-text-properties beg end '(hilit-chg nil))
521 (hilit-chg-fixup beg end)))
523 (defun hilit-chg-set-face-on-change (beg end leng-before
524 &optional no-property-change)
525 "Record changes and optionally display them in a distinctive face.
526 `hilit-chg-set' adds this function to the `after-change-functions' hook."
528 ;; This function is called by the `after-change-functions' hook, which
529 ;; is how we are notified when text is changed.
530 ;; It is also called from `highlight-compare-with-file'.
532 ;; We do NOT want to simply do this if this is an undo command, because
533 ;; otherwise an undone change shows up as changed. While the properties
534 ;; are automatically restored by undo, we must fix up the overlay.
535 (save-match-data
536 (let ((beg-decr 1) (end-incr 1)
537 (type 'hilit-chg)
538 old)
539 (if undo-in-progress
540 (if (eq highlight-changes-mode 'active)
541 (hilit-chg-fixup beg end))
542 (if (and (= beg end) (> leng-before 0))
543 ;; deletion
544 (progn
545 ;; The eolp and bolp tests are a kludge! But they prevent
546 ;; rather nasty looking displays when deleting text at the end
547 ;; of line, such as normal corrections as one is typing and
548 ;; immediately makes a correction, and when deleting first
549 ;; character of a line.
550 ;;; (if (= leng-before 1)
551 ;;; (if (eolp)
552 ;;; (setq beg-decr 0 end-incr 0)
553 ;;; (if (bolp)
554 ;;; (setq beg-decr 0))))
555 ;;; (setq beg (max (- beg beg-decr) (point-min)))
556 (setq end (min (+ end end-incr) (point-max)))
557 (setq type 'hilit-chg-delete))
558 ;; Not a deletion.
559 ;; Most of the time the following is not necessary, but
560 ;; if the current text was marked as a deletion then
561 ;; the old overlay is still in effect, so if we add some
562 ;; text then remove the deletion marking, but set it to
563 ;; changed otherwise its highlighting disappears.
564 (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete)
565 (progn
566 (remove-text-properties end (+ end 1) '(hilit-chg nil))
567 (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg)
568 (if (eq highlight-changes-mode 'active)
569 (hilit-chg-fixup beg (+ end 1))))))
570 (unless no-property-change
571 (put-text-property beg end 'hilit-chg type))
572 (if (or (eq highlight-changes-mode 'active) no-property-change)
573 (hilit-chg-make-ov type beg end))))))
575 (defun hilit-chg-set (value)
576 "Turn on Highlight Changes mode for this buffer."
577 (setq highlight-changes-mode value)
578 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
579 (hilit-chg-make-list)
580 (if (eq highlight-changes-mode 'active)
581 (progn
582 (setq hilit-chg-string highlight-changes-active-string)
583 (or buffer-read-only
584 (hilit-chg-display-changes)))
585 ;; mode is passive
586 (setq hilit-chg-string highlight-changes-passive-string)
587 (or buffer-read-only
588 (hilit-chg-hide-changes)))
589 (force-mode-line-update)
590 (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t))
592 (defun hilit-chg-clear ()
593 "Remove Highlight Changes mode for this buffer.
594 This removes all saved change information."
595 (if buffer-read-only
596 ;; We print the buffer name because this function could be called
597 ;; on many buffers from `global-highlight-changes'.
598 (message "Cannot remove highlighting from read-only mode buffer %s"
599 (buffer-name))
600 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
601 (let ((after-change-functions nil))
602 (hilit-chg-hide-changes)
603 (hilit-chg-map-changes
604 '(lambda (prop start stop)
605 (remove-text-properties start stop '(hilit-chg nil))))
607 (setq highlight-changes-mode nil)
608 (force-mode-line-update)
609 ;; If we type: C-u -1 M-x highlight-changes-mode
610 ;; we want to turn it off, but hilit-chg-post-command-hook
611 ;; runs and that turns it back on!
612 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook)))
614 ;;;###autoload
615 (defun highlight-changes-mode (&optional arg)
616 "Toggle (or initially set) Highlight Changes mode.
618 Without an argument:
619 If Highlight Changes mode is not enabled, then enable it (in either active
620 or passive state as determined by the variable
621 `highlight-changes-initial-state'); otherwise, toggle between active
622 and passive state.
624 With an argument ARG:
625 If ARG is positive, set state to active;
626 If ARG is zero, set state to passive;
627 If ARG is negative, disable Highlight Changes mode completely.
629 Active state - means changes are shown in a distinctive face.
630 Passive state - means changes are kept and new ones recorded but are
631 not displayed in a different face.
633 Functions:
634 \\[highlight-changes-next-change] - move point to beginning of next change
635 \\[highlight-changes-previous-change] - move to beginning of previous change
636 \\[highlight-compare-with-file] - mark text as changed by comparing this
637 buffer with the contents of a file
638 \\[highlight-changes-remove-highlight] - remove the change face from the region
639 \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes \
640 through
641 various faces.
643 Hook variables:
644 `highlight-changes-enable-hook' - when enabling Highlight Changes mode.
645 `highlight-changes-toggle-hook' - when entering active or passive state
646 `highlight-changes-disable-hook' - when turning off Highlight Changes mode."
647 (interactive "P")
648 (if (or (display-color-p)
649 (and (fboundp 'x-display-grayscale-p) (x-display-grayscale-p)))
650 (let ((was-on highlight-changes-mode)
651 (new-highlight-changes-mode
652 (cond
653 ((null arg)
654 ;; no arg => toggle (or set to active initially)
655 (if highlight-changes-mode
656 (if (eq highlight-changes-mode 'active) 'passive 'active)
657 highlight-changes-initial-state))
658 ;; an argument is given
659 ((eq arg 'active)
660 'active)
661 ((eq arg 'passive)
662 'passive)
663 ((> (prefix-numeric-value arg) 0)
664 'active)
665 ((< (prefix-numeric-value arg) 0)
666 nil)
668 'passive))))
669 (if new-highlight-changes-mode
670 ;; mode is turned on -- but may be passive
671 (progn
672 (hilit-chg-set new-highlight-changes-mode)
673 (or was-on
674 ;; run highlight-changes-enable-hook once
675 (run-hooks 'highlight-changes-enable-hook))
676 (run-hooks 'highlight-changes-toggle-hook))
677 ;; mode is turned off
678 (run-hooks 'highlight-changes-disable-hook)
679 (hilit-chg-clear)))
680 (message "Highlight Changes mode requires color or grayscale display")))
682 ;;;###autoload
683 (defun highlight-changes-next-change ()
684 "Move to the beginning of the next change, if in Highlight Changes mode."
685 (interactive)
686 (if highlight-changes-mode
687 (let ((start (point))
688 prop)
689 (setq prop (get-text-property (point) 'hilit-chg))
690 (if prop
691 ;; we are in a change
692 (setq start (next-single-property-change (point) 'hilit-chg)))
693 (if start
694 (setq start (next-single-property-change start 'hilit-chg)))
695 (if start
696 (goto-char start)
697 (message "no next change")))
698 (message "This buffer is not in Highlight Changes mode.")))
701 ;;;###autoload
702 (defun highlight-changes-previous-change ()
703 "Move to the beginning of the previous change, if in Highlight Changes mode."
704 (interactive)
705 (if highlight-changes-mode
706 (let ( (start (point)) (prop nil) )
707 (or (bobp)
708 (setq prop (get-text-property (1- (point)) 'hilit-chg)))
709 (if prop
710 ;; we are in a change
711 (setq start (previous-single-property-change (point) 'hilit-chg)))
712 (if start
713 (setq start (previous-single-property-change start 'hilit-chg)))
714 ;; special handling for the case where (point-min) is a change
715 (if start
716 (setq start (or (previous-single-property-change start 'hilit-chg)
717 (if (get-text-property (point-min) 'hilit-chg)
718 (point-min)))))
719 (if start
720 (goto-char start)
721 (message "no previous change")))
722 (message "This buffer is not in Highlight Changes mode.")))
724 ;; ========================================================================
726 (defun hilit-chg-make-list (&optional force)
727 "Construct `hilit-chg-list' and `highlight-changes-face-list'."
728 ;; Constructs highlight-changes-face-list if necessary,
729 ;; and hilit-chg-list always:
730 ;; Maybe this should always be called when rotating a face
731 ;; so we pick up any changes?
732 (if (or (null highlight-changes-face-list) ; Don't do it if it
733 force) ; already exists unless FORCE non-nil.
734 (let ((p highlight-changes-colours)
735 (n 1) name)
736 (setq highlight-changes-face-list nil)
737 (while p
738 (setq name (intern (format "highlight-changes-%d" n)))
739 (copy-face 'highlight-changes name)
740 (set-face-foreground name (car p))
741 (setq highlight-changes-face-list
742 (append highlight-changes-face-list (list name)))
743 (setq p (cdr p))
744 (setq n (1+ n)))))
745 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes))
746 (let ((p highlight-changes-face-list)
747 (n 1)
748 last-category last-face)
749 (while p
750 (setq last-category (intern (format "change-%d" n)))
751 ;; (setq last-face (intern (format "highlight-changes-%d" n)))
752 (setq last-face (car p))
753 (setq hilit-chg-list
754 (append hilit-chg-list
755 (list last-category last-face)))
756 (setq p (cdr p))
757 (setq n (1+ n)))
758 (setq hilit-chg-list
759 (append hilit-chg-list
760 (list last-category last-face)))))
762 (defun hilit-chg-bump-change (prop start end)
763 "Increment (age) the Highlight Changes mode text property."
764 (let ( new-prop )
765 (if (eq prop 'hilit-chg-delete)
766 (setq new-prop (nth 2 hilit-chg-list))
767 (setq new-prop (nth 2 (member prop hilit-chg-list))))
768 (if prop
769 (put-text-property start end 'hilit-chg new-prop)
770 (message "%d-%d unknown property %s not changed" start end prop))))
772 ;;;###autoload
773 (defun highlight-changes-rotate-faces ()
774 "Rotate the faces used by Highlight Changes mode.
776 Current changes are displayed in the face described by the first element
777 of `highlight-changes-face-list', one level older changes are shown in
778 face described by the second element, and so on. Very old changes remain
779 shown in the last face in the list.
781 You can automatically rotate colors when the buffer is saved
782 by adding the following to `local-write-file-hooks', by evaling it in the
783 buffer to be saved):
785 \(add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)"
786 (interactive)
787 ;; If not in active mode do nothing but don't complain because this
788 ;; may be bound to a hook.
789 (if (eq highlight-changes-mode 'active)
790 (let ((after-change-functions nil))
791 ;; ensure hilit-chg-list is made and up to date
792 (hilit-chg-make-list)
793 ;; remove our existing overlays
794 (hilit-chg-hide-changes)
795 ;; for each change text property, increment it
796 (hilit-chg-map-changes 'hilit-chg-bump-change)
797 ;; and display them all if active
798 (if (eq highlight-changes-mode 'active)
799 (hilit-chg-display-changes))))
800 ;; This always returns nil so it is safe to use in
801 ;; local-write-file-hook
802 nil)
804 ;; ========================================================================
805 ;; Comparing buffers/files
806 ;; These use ediff to find the differences.
808 (defun highlight-markup-buffers
809 (buf-a file-a buf-b file-b &optional markup-a-only)
810 "Get differences between two buffers and set highlight changes.
811 Both buffers are done unless optional parameter MARKUP-A-ONLY
812 is non-nil."
813 (save-window-excursion
814 (let* (change-info
815 change-a change-b
816 a-start a-end len-a
817 b-start b-end len-b
818 (bufa-modified (buffer-modified-p buf-a))
819 (bufb-modified (buffer-modified-p buf-b))
820 (buf-a-read-only (with-current-buffer buf-a buffer-read-only))
821 (buf-b-read-only (with-current-buffer buf-b buffer-read-only))
822 temp-a temp-b)
823 (if (and file-a bufa-modified)
824 (if (y-or-n-p (format "Save buffer %s? " buf-a))
825 (with-current-buffer buf-a
826 (save-buffer)
827 (setq bufa-modified (buffer-modified-p buf-a)))
828 (setq file-a nil)))
829 (or file-a
830 (setq temp-a (setq file-a (ediff-make-temp-file buf-a nil))))
832 (if (and file-b bufb-modified)
833 (if (y-or-n-p (format "Save buffer %s? " buf-b))
834 (with-current-buffer buf-b
835 (save-buffer)
836 (setq bufb-modified (buffer-modified-p buf-b)))
837 (setq file-b nil)))
838 (or file-b
839 (setq temp-b (setq file-b (ediff-make-temp-file buf-b nil))))
840 (set-buffer buf-a)
841 (highlight-changes-mode 'active)
842 (or markup-a-only (with-current-buffer buf-b
843 (highlight-changes-mode 'active)))
844 (setq change-info (hilit-chg-get-diff-info buf-a file-a buf-b file-b))
847 (setq change-a (car change-info))
848 (setq change-b (car (cdr change-info)))
850 (hilit-chg-make-list)
851 (while change-a
852 (setq a-start (nth 0 (car change-a)))
853 (setq a-end (nth 1 (car change-a)))
854 (setq b-start (nth 0 (car change-b)))
855 (setq b-end (nth 1 (car change-b)))
856 (setq len-a (- a-end a-start))
857 (setq len-b (- b-end b-start))
858 (set-buffer buf-a)
859 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
860 (or markup-a-only
861 (with-current-buffer buf-b
862 (hilit-chg-set-face-on-change b-start b-end len-a
863 buf-b-read-only)
865 (setq change-a (cdr change-a))
866 (setq change-b (cdr change-b)))
867 (or bufa-modified
868 (with-current-buffer buf-a (set-buffer-modified-p nil)))
869 (or bufb-modified
870 (with-current-buffer buf-b (set-buffer-modified-p nil)))
871 (if temp-a
872 (delete-file temp-a))
873 (if temp-b
874 (delete-file temp-b)))
877 ;;;###autoload
878 (defun highlight-compare-buffers (buf-a buf-b)
879 "Compare two buffers and highlight the differences.
881 The default is the current buffer and the one in the next window.
883 If either buffer is modified and is visiting a file, you are prompted
884 to save the file.
886 Unless the buffer is unmodified and visiting a file, the buffer is
887 written to a temporary file for comparison.
889 If a buffer is read-only, differences will be highlighted but no property
890 changes are made, so \\[highlight-changes-next-change] and
891 \\[highlight-changes-previous-change] will not work."
892 (interactive
893 (list
894 (get-buffer (read-buffer "buffer-a " (current-buffer) t))
895 (get-buffer
896 (read-buffer "buffer-b "
897 (window-buffer (next-window (selected-window))) t))))
898 (let ((file-a (buffer-file-name buf-a))
899 (file-b (buffer-file-name buf-b)))
900 (highlight-markup-buffers buf-a file-a buf-b file-b)
903 ;;;###autoload
904 (defun highlight-compare-with-file (file-b)
905 "Compare this buffer with a file, and highlight differences.
907 If the buffer has a backup filename, it is used as the default when
908 this function is called interactively.
910 If the current buffer is visiting the file being compared against, it
911 also will have its differences highlighted. Otherwise, the file is
912 read in temporarily but the buffer is deleted.
914 If the buffer is read-only, differences will be highlighted but no property
915 changes are made, so \\[highlight-changes-next-change] and
916 \\[highlight-changes-previous-change] will not work."
917 (interactive (list
918 (read-file-name
919 "File to compare with? " ;; prompt
920 "" ;; directory
921 nil ;; default
922 'yes ;; must exist
923 (let ((f (buffer-file-name (current-buffer))))
924 (if f
925 (progn
926 (setq f (make-backup-file-name f))
927 (or (file-exists-p f)
928 (setq f nil)))
930 f))))
931 (let* ((buf-a (current-buffer))
932 (file-a (buffer-file-name))
933 (existing-buf (get-file-buffer file-b))
934 (buf-b (or existing-buf
935 (find-file-noselect file-b)))
936 (buf-b-read-only (with-current-buffer buf-b buffer-read-only)))
937 (highlight-markup-buffers buf-a file-a buf-b file-b (not existing-buf))
938 (unless existing-buf
939 (kill-buffer buf-b))
943 (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b)
944 (let ((e nil) x y) ;; e is set by function hilit-chg-get-diff-list-hk
945 (ediff-setup buf-a file-a buf-b file-b
946 nil nil ; buf-c file-C
947 'hilit-chg-get-diff-list-hk
948 (list (cons 'ediff-job-name 'something))
950 (ediff-with-current-buffer e (ediff-really-quit nil))
951 (list x y)))
954 (defun hilit-chg-get-diff-list-hk ()
955 ;; x and y are dynamically bound by hilit-chg-get-diff-info
956 ;; which calls this function as a hook
957 (defvar x) ;; placate the byte-compiler
958 (defvar y)
959 (setq e (current-buffer))
960 (let ((n 0) extent p va vb a b)
961 (setq x nil y nil) ;; x and y are bound by hilit-chg-get-diff-info
962 (while (< n ediff-number-of-differences)
963 (ediff-make-fine-diffs n)
964 (setq va (ediff-get-fine-diff-vector n 'A))
965 ;; va is a vector if there are fine differences
966 (if va
967 (setq a (append va nil))
968 ;; if not, get the unrefined difference
969 (setq va (ediff-get-difference n 'A))
970 (setq a (list (elt va 0))))
971 ;; a list a list
972 (setq p a)
973 (while p
974 (setq extent (list (overlay-start (car p))
975 (overlay-end (car p))))
976 (setq p (cdr p))
977 (setq x (append x (list extent) )));; while p
979 (setq vb (ediff-get-fine-diff-vector n 'B))
980 ;; vb is a vector
981 (if vb
982 (setq b (append vb nil))
983 ;; if not, get the unrefined difference
984 (setq vb (ediff-get-difference n 'B))
985 (setq b (list (elt vb 0))))
986 ;; b list a list
987 (setq p b)
988 (while p
989 (setq extent (list (overlay-start (car p))
990 (overlay-end (car p))))
991 (setq p (cdr p))
992 (setq y (append y (list extent) )))
993 (setq n (1+ n)));; while
994 ;; ediff-quit doesn't work here.
995 ;; No point in returning a value, since this is a hook function.
998 ;; ======================= automatic stuff ==============
1000 ;; Global Highlight Changes mode is modeled after Global Font-lock mode.
1001 ;; Three hooks are used to gain control. When Global Changes Mode is
1002 ;; enabled, `find-file-hook' and `change-major-mode-hook' are set.
1003 ;; `find-file-hook' is called when visiting a file, the new mode is
1004 ;; known at this time.
1005 ;; `change-major-mode-hook' is called when a buffer is changing mode.
1006 ;; This could be because of finding a file in which case
1007 ;; `find-file-hook' has already been called and has done its work.
1008 ;; However, it also catches the case where a new mode is being set by
1009 ;; the user. However, it is called from `kill-all-variables' and at
1010 ;; this time the mode is the old mode, which is not what we want.
1011 ;; So, our function temporarily sets `post-command-hook' which will
1012 ;; be called after the buffer has been completely set up (with the new
1013 ;; mode). It then removes the `post-command-hook'.
1014 ;; One other wrinkle - every M-x command runs the `change-major-mode-hook'
1015 ;; so we ignore this by examining the buffer name.
1018 (defun hilit-chg-major-mode-hook ()
1019 (add-hook 'post-command-hook 'hilit-chg-post-command-hook))
1021 (defun hilit-chg-post-command-hook ()
1022 ;; This is called after changing a major mode, but also after each
1023 ;; M-x command, in which case the current buffer is a minibuffer.
1024 ;; In that case, do not act on it here, but don't turn it off
1025 ;; either, we will get called here again soon-after.
1026 ;; Also, don't enable it for other special buffers.
1027 (if (string-match "^[ *]" (buffer-name))
1028 nil ;; (message "ignoring this post-command-hook")
1029 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook)
1030 ;; The following check isn't necessary, since
1031 ;; hilit-chg-turn-on-maybe makes this check too.
1032 (or highlight-changes-mode ;; don't turn it on if it already is
1033 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state))))
1035 (defun hilit-chg-check-global ()
1036 ;; This is called from the find file hook.
1037 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state))
1040 ;;;###autoload
1041 (defun global-highlight-changes (&optional arg)
1042 "Turn on or off global Highlight Changes mode.
1044 When called interactively:
1045 - if no prefix, toggle global Highlight Changes mode on or off
1046 - if called with a positive prefix (or just C-u) turn it on in active mode
1047 - if called with a zero prefix turn it on in passive mode
1048 - if called with a negative prefix turn it off
1050 When called from a program:
1051 - if ARG is nil or omitted, turn it off
1052 - if ARG is `active', turn it on in active mode
1053 - if ARG is `passive', turn it on in passive mode
1054 - otherwise just turn it on
1056 When global Highlight Changes mode is enabled, Highlight Changes mode is turned
1057 on for future \"suitable\" buffers (and for \"suitable\" existing buffers if
1058 variable `highlight-changes-global-changes-existing-buffers' is non-nil).
1059 \"Suitability\" is determined by variable `highlight-changes-global-modes'."
1061 (interactive
1062 (list
1063 (cond
1064 ((null current-prefix-arg)
1065 ;; no arg => toggle it on/off
1066 (setq global-highlight-changes (not global-highlight-changes)))
1067 ;; positive interactive arg - turn it on as active
1068 ((> (prefix-numeric-value current-prefix-arg) 0)
1069 (setq global-highlight-changes t)
1070 'active)
1071 ;; zero interactive arg - turn it on as passive
1072 ((= (prefix-numeric-value current-prefix-arg) 0)
1073 (setq global-highlight-changes t)
1074 'passive)
1075 ;; negative interactive arg - turn it off
1077 (setq global-highlight-changes nil)
1078 nil))))
1080 (if arg
1081 (progn
1082 (if (eq arg 'active)
1083 (setq highlight-changes-global-initial-state 'active)
1084 (if (eq arg 'passive)
1085 (setq highlight-changes-global-initial-state 'passive)))
1086 (setq global-highlight-changes t)
1087 (message "Turning ON Global Highlight Changes mode in %s state"
1088 highlight-changes-global-initial-state)
1089 ;; FIXME: Not sure what this was intended to do. --Stef
1090 ;; (add-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook)
1091 (add-hook 'find-file-hook 'hilit-chg-check-global)
1092 (if highlight-changes-global-changes-existing-buffers
1093 (hilit-chg-update-all-buffers
1094 highlight-changes-global-initial-state)))
1096 (message "Turning OFF global Highlight Changes mode")
1097 ;; FIXME: Not sure what this was intended to do. --Stef
1098 ;; (remove-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook)
1099 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook)
1100 (remove-hook 'find-file-hook 'hilit-chg-check-global)
1101 (if highlight-changes-global-changes-existing-buffers
1102 (hilit-chg-update-all-buffers nil))))
1105 (defun hilit-chg-turn-on-maybe (value)
1106 "Turn on Highlight Changes mode if it is appropriate for this buffer.
1108 A buffer is appropriate for Highlight Changes mode if all these are true:
1109 - the buffer is not a special buffer (one whose name begins with
1110 `*' or ` ')
1111 - the buffer's mode is suitable as per variable
1112 `highlight-changes-global-modes'
1113 - Highlight Changes mode is not already on for this buffer.
1115 This function is called from `hilit-chg-update-all-buffers' or
1116 from `global-highlight-changes' when turning on global Highlight Changes mode."
1117 (or highlight-changes-mode ; do nothing if already on
1119 (cond
1120 ((null highlight-changes-global-modes)
1121 nil)
1122 ((functionp highlight-changes-global-modes)
1123 (funcall highlight-changes-global-modes))
1124 ((listp highlight-changes-global-modes)
1125 (if (eq (car-safe highlight-changes-global-modes) 'not)
1126 (not (memq major-mode (cdr highlight-changes-global-modes)))
1127 (memq major-mode highlight-changes-global-modes)))
1129 (and
1130 (not (string-match "^[ *]" (buffer-name)))
1131 (buffer-file-name))))
1132 (progn
1133 (hilit-chg-set value)
1134 (run-hooks 'highlight-changes-enable-hook)))))
1137 (defun hilit-chg-turn-off-maybe ()
1138 (if highlight-changes-mode
1139 (progn
1140 (run-hooks 'highlight-changes-disable-hook)
1141 (hilit-chg-clear))))
1144 (defun hilit-chg-update-all-buffers (value)
1145 (mapcar
1146 (function (lambda (buffer)
1147 (with-current-buffer buffer
1148 (if value
1149 (hilit-chg-turn-on-maybe value)
1150 (hilit-chg-turn-off-maybe))
1152 (buffer-list)))
1154 ;; ===================== debug ==================
1155 ;; For debug & test use:
1157 ;; (defun hilit-chg-debug-show (&optional beg end)
1158 ;; (interactive)
1159 ;; (message "--- hilit-chg-debug-show ---")
1160 ;; (hilit-chg-map-changes '(lambda (prop start end)
1161 ;; (message "%d-%d: %s" start end prop)
1162 ;; )
1163 ;; beg end
1164 ;; ))
1166 ;; ================== end of debug ===============
1168 (provide 'hilit-chg)
1170 ;; arch-tag: de00301d-5bad-44da-aa82-e0e010b0c463
1171 ;;; hilit-chg.el ends here