Update for calendar.el name changes.
[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, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Richard Sharman <rsharman@pobox.com>
7 ;; Keywords: faces
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; A minor mode: "Highlight Changes mode".
29
30 ;; When Highlight Changes mode is enabled changes to the buffer are
31 ;; recorded with a text property. Normally these ranges of text are
32 ;; displayed in a distinctive face. However, sometimes it is
33 ;; desirable to temporarily not see these changes. Instead of
34 ;; disabling Highlight Changes mode (which would remove the text property)
35 ;; use the command highlight-changes-visible-mode.
36
37 ;; Two faces are supported: one for changed or inserted text and
38 ;; another for the first character after text has been deleted.
39
40 ;; When Highlight Changes mode is on (even if changes are not visible)
41 ;; you can go to the next or previous change with
42 ;; `highlight-changes-next-change' or `highlight-changes-previous-change'.
43
44 ;; Command highlight-compare-with-file shows changes in this file
45 ;; compared with another file (by default the previous version of the
46 ;; file).
47 ;;
48 ;; The command highlight-compare-buffers compares two buffers by
49 ;; highlighting their differences.
50
51 ;; You can "age" different sets of changes by using
52 ;; `highlight-changes-rotate-faces'. This rotates through a series
53 ;; of different faces, so you can distinguish "new" changes from "older"
54 ;; changes. You can customize these "rotated" faces in two ways. You can
55 ;; either explicitly define each face by customizing
56 ;; `highlight-changes-face-list'. If, however, the faces differ from
57 ;; `highlight-changes-face' only in the foreground color, you can simply set
58 ;; `highlight-changes-colors'. If `highlight-changes-face-list' is nil when
59 ;; the faces are required they will be constructed from
60 ;; `highlight-changes-colors'.
61
62 ;; You can automatically rotate faces when the buffer is saved;
63 ;; see function `highlight-changes-rotate-faces' for how to do this.
64
65 ;; There are two hooks used by `highlight-changes-mode':
66 ;; `highlight-changes-enable-hook' - is run when Highlight Changes mode
67 ;; is enabled for a buffer.
68 ;; `highlight-changes-disable-hook' - is run when Highlight Changes mode
69 ;; is disabled for a buffer.
70
71 ;; Example usage:
72 ;; (defun my-highlight-changes-enable-hook ()
73 ;; (add-hook 'write-file-functions 'highlight-changes-rotate-faces nil t)
74 ;; )
75 ;;
76 ;; (defun my-highlight-changes-disable-hook ()
77 ;; (remove-hook 'write-file-functions 'highlight-changes-rotate-faces t)
78 ;; )
79 ;;
80 ;; (add-hook 'highlight-changes-enable-hook 'my-highlight-changes-enable-hook)
81 ;; (add-hook 'highlight-changes-disable-hook
82 ;; 'my-highlight-changes-disable-hook)
83
84
85 ;; Automatically enabling Highlight Changes mode
86 ;;
87
88 ;; Normally, Highlight Changes mode is turned on explicitly in a buffer.
89 ;;
90 ;; If you prefer to have it automatically invoked you can do it as
91 ;; follows.
92
93 ;; 1. Most modes have a major-hook, typically called MODE-hook. You
94 ;; can use `add-hook' to call `highlight-changes-mode'.
95
96 ;; Example:
97 ;; (add-hook 'c-mode-hook 'highlight-changes-mode)
98
99 ;; However, this cannot be done for Fundamental mode for there is no
100 ;; such hook.
101
102 ;; 2. You can use the function `global-highlight-changes-mode'
103 ;;
104 ;; This function, which is fashioned after the way `global-font-lock' works,
105 ;; toggles on or off global Highlight Changes mode. When activated, it turns
106 ;; on Highlight Changes mode in all "suitable" existing buffers and will turn
107 ;; it on in new "suitable" buffers to be created.
108
109 ;; A buffer's "suitability" is determined by variable
110 ;; `highlight-changes-global-modes', as follows. If it is
111 ;; * nil -- then no buffers are suitable;
112 ;; * a function -- this function is called and the result is used. As
113 ;; an example, if the value is `buffer-file-name' then all buffers
114 ;; who are visiting files are suitable, but others (like dired
115 ;; buffers) are not;
116 ;; * a list -- then the buffer is suitable if and only if its mode is in the
117 ;; list, except if the first element is `not', in which case the test
118 ;; is reversed (i.e. it is a list of unsuitable modes).
119 ;; * Otherwise, the buffer is suitable if its name does not begin with
120 ;; ` ' or `*' and if `buffer-file-name' returns true.
121
122 ;; To enable it for future sessions put this in your ~/.emacs file:
123 ;; (global-highlight-changes-mode t)
124
125
126 ;; Possible bindings:
127 ;; (global-set-key '[C-right] 'highlight-changes-next-change)
128 ;; (global-set-key '[C-left] 'highlight-changes-previous-change)
129 ;;
130 ;; Other interactive functions (that could be bound if desired):
131 ;; highlight-changes-mode
132 ;; highlight-changes-toggle-visibility
133 ;; highlight-changes-remove-highlight
134 ;; highlight-compare-with-file
135 ;; highlight-compare-buffers
136 ;; highlight-changes-rotate-faces
137
138
139 ;;; Bugs:
140
141 ;; - the next-change and previous-change functions are too literal;
142 ;; they should find the next "real" change, in other words treat
143 ;; consecutive changes as one.
144
145
146 ;;; To do (maybe), notes, ...
147
148 ;; - having different faces for deletion and non-deletion: is it
149 ;; really worth the hassle?
150 ;; - highlight-compare-with-file should allow RCS files - e.g. nice to be
151 ;; able to say show changes compared with version 2.1.
152
153
154 ;;; History:
155
156 ;; R Sharman (rsharman@pobox.com) Feb 1998:
157 ;; - initial release as change-mode.
158 ;; Jari Aalto <jari.aalto@ntc.nokia.com> Mar 1998
159 ;; - fixes for byte compile errors
160 ;; - use eval-and-compile for autoload
161 ;; Marijn Ros <J.M.Ros@fys.ruu.nl> Mar 98
162 ;; - suggested turning it on by default
163 ;; Eric Ludlam <zappo@gnu.org> Suggested using overlays.
164 ;; July 98
165 ;; - global mode and various stuff added
166 ;; - Changed to use overlays
167 ;; August 98
168 ;; - renamed to Highlight Changes mode.
169 ;; Dec 2003
170 ;; - Use require for ediff stuff
171 ;; - Added highlight-compare-buffers
172 ;; Mar 2008
173 ;; - Made highlight-changes-mode like other modes (toggle on/off)
174 ;; - Added new command highlight-changes-visible-mode to replace the
175 ;; previous active/passive aspect of highlight-changes-mode.
176 ;; - Removed highlight-changes-toggle-hook
177 ;; - Put back eval-and-compile inadvertently dropped
178
179
180 ;;; Code:
181
182 (require 'wid-edit)
183
184 ;; ====================== Customization =======================
185 (defgroup highlight-changes nil
186 "Highlight Changes mode."
187 :version "20.4"
188 :group 'faces)
189
190
191 ;; Face information: How the changes appear.
192
193 ;; Defaults for face: red foreground, no change to background,
194 ;; and underlined if a change is because of a deletion.
195 ;; Note: underlining is helpful in that it shows up changes in white space.
196 ;; However, having it set for non-delete changes can be annoying because all
197 ;; indentation on inserts gets underlined (which can look pretty ugly!).
198
199 (defface highlight-changes
200 '((((min-colors 88) (class color)) (:foreground "red1"))
201 (((class color)) (:foreground "red" ))
202 (t (:inverse-video t)))
203 "Face used for highlighting changes."
204 :group 'highlight-changes)
205 ;; backward-compatibility alias
206 (put 'highlight-changes-face 'face-alias 'highlight-changes)
207
208 ;; This looks pretty ugly, actually. Maybe the underline should be removed.
209 (defface highlight-changes-delete
210 '((((min-colors 88) (class color)) (:foreground "red1" :underline t))
211 (((class color)) (:foreground "red" :underline t))
212 (t (:inverse-video t)))
213 "Face used for highlighting deletions."
214 :group 'highlight-changes)
215 ;; backward-compatibility alias
216 (put 'highlight-changes-delete-face 'face-alias 'highlight-changes-delete)
217
218
219
220 ;; A (not very good) default list of colors to rotate through.
221 ;;
222 (defcustom highlight-changes-colors
223 (if (eq (frame-parameter nil 'background-mode) 'light)
224 ;; defaults for light background:
225 '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue")
226 ;; defaults for dark background:
227 '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid"))
228 "Colors used by `highlight-changes-rotate-faces'.
229 The newest rotated change will be displayed in the first element of this list,
230 the next older will be in the second element etc.
231
232 This list is used if `highlight-changes-face-list' is nil, otherwise that
233 variable overrides this list. If you only care about foreground
234 colors then use this, if you want fancier faces then set
235 `highlight-changes-face-list'."
236 :type '(repeat color)
237 :group 'highlight-changes)
238
239 (define-obsolete-variable-alias 'highlight-changes-colours
240 'highlight-changes-colors "22.1")
241
242
243 ;; When you invoke highlight-changes-mode, should highlight-changes-visible-mode
244 ;; be on or off?
245
246 (define-obsolete-variable-alias 'highlight-changes-initial-state
247 'highlight-changes-visibility-initial-state)
248
249 (defcustom highlight-changes-visibility-initial-state t
250 "Controls whether changes are initially visible in Highlight Changes mode.
251
252 This controls the initial value of `highlight-changes-visible-mode'.
253 When a buffer is in Highlight Changes mode the function
254 `highlight-changes-visible-mode' is used to toggle the mode on or off."
255 :type 'boolean
256 :group 'highlight-changes)
257
258 ;; highlight-changes-global-initial-state has been removed
259
260
261
262 ;; These are the strings displayed in the mode-line for the minor mode:
263
264 (defcustom highlight-changes-visible-string " +Chg"
265 "The string used when in Highlight Changes mode and changes are visible.
266 This should be set to nil if no indication is desired, or to
267 a string with a leading space."
268 :type '(choice string
269 (const :tag "None" nil))
270 :group 'highlight-changes)
271
272 (define-obsolete-variable-alias 'highlight-changes-active-string
273 'highlight-changes-visible-string "23.1")
274
275 (defcustom highlight-changes-invisible-string " -Chg"
276 "The string used when in Highlight Changes mode and changes are hidden.
277 This should be set to nil if no indication is desired, or to
278 a string with a leading space."
279 :type '(choice string
280 (const :tag "None" nil))
281 :group 'highlight-changes)
282
283 (define-obsolete-variable-alias 'highlight-changes-passive-string
284 'highlight-changes-invisible-string "23.1")
285
286
287
288 (defcustom highlight-changes-global-modes t
289 "Determine whether a buffer is suitable for global Highlight Changes mode.
290
291 A function means call that function to decide: if it returns non-nil,
292 the buffer is suitable.
293
294 A list means the elements are major modes suitable for Highlight
295 Changes mode, or a list whose first element is `not' followed by major
296 modes which are not suitable.
297
298 A value of t means the buffer is suitable if it is visiting a file and
299 its name does not begin with ` ' or `*'.
300
301 A value of nil means no buffers are suitable for `global-highlight-changes-mode'
302 \(effectively disabling the mode).
303
304 Example:
305 (c-mode c++-mode)
306 means that Highlight Changes mode is turned on for buffers in C and C++
307 modes only."
308 :type '(choice
309 (const :tag "all non-special buffers visiting files" t)
310 (set :menu-tag "specific modes" :tag "modes"
311 :value (not)
312 (const :tag "All except these" not)
313 (repeat :tag "Modes" :inline t (symbol :tag "mode")))
314 (function :menu-tag "determined by function"
315 :value buffer-file-name)
316 (const :tag "none" nil)
317 )
318 :group 'highlight-changes)
319
320 (defcustom highlight-changes-global-changes-existing-buffers nil
321 "If non-nil, toggling global Highlight Changes mode affects existing buffers.
322 Normally, `global-highlight-changes' affects only new buffers (to be
323 created). However, if `highlight-changes-global-changes-existing-buffers'
324 is non-nil, then turning on `global-highlight-changes' will turn on
325 Highlight Changes mode in suitable buffers, and turning the mode off will
326 remove it from existing buffers."
327 :type 'boolean
328 :group 'highlight-changes)
329
330 ;; These are for internal use.
331
332 (defvar hilit-chg-list nil)
333 (defvar hilit-chg-string " ??")
334
335 (make-variable-buffer-local 'hilit-chg-string)
336
337
338
339 ;;; Functions...
340
341 ;;;###autoload
342 (define-minor-mode highlight-changes-mode
343 "Toggle Highlight Changes mode.
344
345 With ARG, turn Highlight Changes mode on if and only if arg is positive.
346
347 In Highlight Changes mode changes are recorded with a text property.
348 Normally they are displayed in a distinctive face, but command
349 \\[highlight-changes-visible-mode] can be used to toggles this
350 on and off.
351
352 Other functions for buffers in this mode include:
353 \\[highlight-changes-next-change] - move point to beginning of next change
354 \\[highlight-changes-previous-change] - move to beginning of previous change
355 \\[highlight-changes-remove-highlight] - remove the change face from the region
356 \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes
357 through various faces.
358 \\[highlight-compare-with-file] - mark text as changed by comparing this
359 buffer with the contents of a file
360 \\[highlight-compare-buffers] highlights differences between two buffers.
361
362 Hook variables:
363 `highlight-changes-enable-hook': called when enabling Highlight Changes mode.
364 `highlight-changes-disable-hook': called when disabling Highlight Changes mode."
365 nil ;; init-value
366 hilit-chg-string ;; lighter
367 nil ;; keymap
368 (if (or (display-color-p)
369 (and (fboundp 'x-display-grayscale-p) (x-display-grayscale-p)))
370 (progn
371 (if (and (eq this-command 'global-highlight-changes-mode)
372 (not highlight-changes-global-changes-existing-buffers))
373 ;; The global mode has toggled the value of the mode variable,
374 ;; but not other changes have been mode, so we are safe
375 ;; to retoggle it.
376 (setq highlight-changes-mode (not highlight-changes-mode)))
377 (if highlight-changes-mode
378 ;; it is being turned on
379 ;; the hook has been moved into hilit-chg-set
380 ;; (run-hooks 'highlight-changes-enable-hook))
381 (hilit-chg-set)
382 ;; mode is turned off
383 (hilit-chg-clear)))
384 (message "Highlight Changes mode requires color or grayscale display")))
385
386
387 ;;;###autoload
388 (define-minor-mode highlight-changes-visible-mode
389 "Toggle visiblility of changes when buffer is in Highlight Changes mode.
390
391 This mode only has an effect when Highlight Changes mode is on.
392 It allows toggling between whether or not the changed text is displayed
393 in a distinctive face.
394
395 The default value can be customized with variable
396 `highlight-changes-visibility-initial-state'
397
398 This command does not itself set highlight-changes mode."
399
400 t ;; init-value
401 nil ;; lighter
402 nil ;; keymap
403
404 (hilit-chg-update)
405 )
406
407
408 (defun hilit-chg-cust-fix-changes-face-list (w wc &optional event)
409 ;; When customization function `highlight-changes-face-list' inserts a new
410 ;; face it uses the default face. We don't want the user to modify this
411 ;; face, so we rename the faces in the list on an insert. The rename is
412 ;; actually done by copying the faces so user-defined faces still remain
413 ;; in the same order.
414 ;; The notifying the parent is needed because without it changes to the
415 ;; faces are saved but not to the actual list itself.
416 (let ((old-list (widget-value w)))
417 (if (member 'default old-list)
418 (let
419 ((p (reverse old-list))
420 (n (length old-list))
421 new-name old-name
422 (new-list nil)
423 )
424 (while p
425 (setq old-name (car p))
426 (setq new-name (intern (format "highlight-changes-%d" n)))
427 (if (eq old-name new-name)
428 nil
429 ;; A new face has been inserted: we don't want to modify the
430 ;; default face so copy it. Better, though, (I think) is to
431 ;; make a new face have the same attributes as
432 ;; the `highlight-changes' face.
433 (if (eq old-name 'default)
434 (copy-face 'highlight-changes new-name)
435 (copy-face old-name new-name)
436 ))
437 (setq new-list (append (list new-name) new-list))
438 (setq n (1- n))
439 (setq p (cdr p)))
440 (if (equal new-list (widget-value w))
441 nil ;; (message "notify: no change!")
442 (widget-value-set w new-list)
443 (widget-setup)
444 )
445 )
446 ;; (message "notify: no default here!")
447 ))
448 (let ((parent (widget-get w :parent)))
449 (when parent
450 (widget-apply parent :notify w event))))
451
452
453 (defcustom highlight-changes-face-list nil
454 "A list of faces used when rotating changes.
455 Normally the variable is initialized to nil and the list is created from
456 `highlight-changes-colors' when needed. However, you can set this variable
457 to any list of faces. You will have to do this if you want faces which
458 don't just differ from the `highlight-changes' face by the foreground color.
459 Otherwise, this list will be constructed when needed from
460 `highlight-changes-colors'."
461 :type '(choice
462 (repeat
463 :notify hilit-chg-cust-fix-changes-face-list
464 face )
465 (const :tag "Derive from highlight-changes-colors" nil)
466 )
467 :group 'highlight-changes)
468
469
470 (defun hilit-chg-map-changes (func &optional start-position end-position)
471 "Call function FUNC for each region used by Highlight Changes mode.
472 If START-POSITION is nil, (point-min) is used.
473 If END-POSITION is nil, (point-max) is used.
474 FUNC is called with 3 params: PROPERTY START STOP."
475 (let ((start (or start-position (point-min)))
476 (limit (or end-position (point-max)))
477 prop end)
478 (while (and start (< start limit))
479 (setq prop (get-text-property start 'hilit-chg))
480 (setq end (text-property-not-all start limit 'hilit-chg prop))
481 (if prop
482 (funcall func prop start (or end limit)))
483 (setq start end))))
484
485
486 (defun hilit-chg-display-changes (&optional beg end)
487 "Display face information for Highlight Changes mode.
488
489 An overlay from BEG to END containing a change face is added from the
490 information in the text property of type `hilit-chg'.
491
492 This is the opposite of `hilit-chg-hide-changes'."
493 (hilit-chg-map-changes 'hilit-chg-make-ov beg end))
494
495
496 (defun hilit-chg-make-ov (prop start end)
497 (or prop
498 (error "hilit-chg-make-ov: prop is nil"))
499 ;; For the region create overlays with a distincive face
500 ;; and the text property 'hilit-chg.
501 (let ((ov (make-overlay start end))
502 (face (if (eq prop 'hilit-chg-delete)
503 'highlight-changes-delete
504 (nth 1 (member prop hilit-chg-list)))))
505 (if face
506 (progn
507 ;; We must mark the face, that is the purpose of the overlay.
508 (overlay-put ov 'face face)
509 ;; I don't think we need to set evaporate since we should
510 ;; be controlling them!
511 (overlay-put ov 'evaporate t)
512 ;; We set the change property so we can tell this is one
513 ;; of our overlays (so we don't delete someone else's).
514 (overlay-put ov 'hilit-chg t)
515 )
516 (error "hilit-chg-make-ov: no face for prop: %s" prop))))
517
518 (defun hilit-chg-hide-changes (&optional beg end)
519 "Remove face information for Highlight Changes mode.
520
521 The overlay containing the face is removed, but the text property
522 containing the change information is retained.
523
524 This is the opposite of `hilit-chg-display-changes'."
525 (let ((start (or beg (point-min)))
526 (limit (or end (point-max))))
527 (dolist (p (overlays-in start limit))
528 ;; don't delete the overlay if it isn't ours!
529 (if (overlay-get p 'hilit-chg)
530 (delete-overlay p)))))
531
532
533 (defun hilit-chg-fixup (beg end)
534 "Fix change overlays in region between BEG and END.
535
536 Ensure the overlays agree with the changes as determined from
537 the text properties of type `hilit-chg'."
538 ;; Remove or alter overlays in region beg..end
539 (remove-overlays beg end 'hilit-chg t)
540 (hilit-chg-display-changes beg end))
541
542 ;; Inspired by font-lock. Something like this should be moved to subr.el.
543 (defmacro highlight-save-buffer-state (&rest body)
544 "Bind variables according to VARLIST and eval BODY restoring buffer state."
545 (declare (indent 0) (debug t))
546 (let ((modified (make-symbol "modified")))
547 `(let* ((,modified (buffer-modified-p))
548 (inhibit-modification-hooks t)
549 deactivate-mark
550 ;; So we don't check the file's mtime.
551 buffer-file-name
552 buffer-file-truename)
553 (progn
554 ,@body)
555 (unless ,modified
556 (restore-buffer-modified-p nil)))))
557
558 ;;;###autoload
559 (defun highlight-changes-remove-highlight (beg end)
560 "Remove the change face from the region between BEG and END.
561 This allows you to manually remove highlighting from uninteresting changes."
562 (interactive "r")
563 (highlight-save-buffer-state
564 (remove-text-properties beg end '(hilit-chg nil))
565 (hilit-chg-fixup beg end)))
566
567 (defun hilit-chg-set-face-on-change (beg end leng-before
568 &optional no-property-change)
569 "Record changes and optionally display them in a distinctive face.
570 `hilit-chg-set' adds this function to the `after-change-functions' hook."
571 ;;
572 ;; This function is called by the `after-change-functions' hook, which
573 ;; is how we are notified when text is changed.
574 ;; It is also called from `highlight-compare-with-file'.
575 ;;
576 ;; We do NOT want to simply do this if this is an undo command, because
577 ;; otherwise an undone change shows up as changed. While the properties
578 ;; are automatically restored by undo, we must fix up the overlay.
579 (save-match-data
580 (let ((beg-decr 1) (end-incr 1)
581 (type 'hilit-chg)
582 old)
583 (if undo-in-progress
584 (if (and highlight-changes-mode
585 highlight-changes-visible-mode)
586 (hilit-chg-fixup beg end))
587 (highlight-save-buffer-state
588 (if (and (= beg end) (> leng-before 0))
589 ;; deletion
590 (progn
591 ;; The eolp and bolp tests are a kludge! But they prevent
592 ;; rather nasty looking displays when deleting text at the end
593 ;; of line, such as normal corrections as one is typing and
594 ;; immediately makes a correction, and when deleting first
595 ;; character of a line.
596 ;; (if (= leng-before 1)
597 ;; (if (eolp)
598 ;; (setq beg-decr 0 end-incr 0)
599 ;; (if (bolp)
600 ;; (setq beg-decr 0))))
601 ;; (setq beg (max (- beg beg-decr) (point-min)))
602 (setq end (min (+ end end-incr) (point-max)))
603 (setq type 'hilit-chg-delete))
604 ;; Not a deletion.
605 ;; Most of the time the following is not necessary, but
606 ;; if the current text was marked as a deletion then
607 ;; the old overlay is still in effect, so if we add some
608 ;; text then remove the deletion marking, but set it to
609 ;; changed otherwise its highlighting disappears.
610 (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete)
611 (progn
612 (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg)
613 (if highlight-changes-visible-mode
614 (hilit-chg-fixup beg (+ end 1))))))
615 (unless no-property-change
616 (put-text-property beg end 'hilit-chg type))
617 (if (or highlight-changes-visible-mode no-property-change)
618 (hilit-chg-make-ov type beg end)))))))
619
620 (defun hilit-chg-update ()
621 "Update a buffer's highlight changes when visibility changed."
622 (if highlight-changes-visible-mode
623 ;; changes are visible
624 (progn
625 (setq hilit-chg-string highlight-changes-visible-string)
626 (or buffer-read-only
627 (hilit-chg-display-changes)))
628 ;; changes are invisible
629 (setq hilit-chg-string highlight-changes-invisible-string)
630 (or buffer-read-only
631 (hilit-chg-hide-changes))))
632
633 (defun hilit-chg-set ()
634 "Turn on Highlight Changes mode for this buffer."
635 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
636 (hilit-chg-make-list)
637 (setq highlight-changes-mode t)
638 (setq highlight-changes-visible-mode highlight-changes-visibility-initial-state)
639 (hilit-chg-update)
640 (force-mode-line-update)
641 (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t)
642 (run-hooks 'highlight-changes-enable-hook))
643
644 (defun hilit-chg-clear ()
645 "Remove Highlight Changes mode for this buffer.
646 This removes all saved change information."
647 (if buffer-read-only
648 ;; We print the buffer name because this function could be called
649 ;; on many buffers from `global-highlight-changes-mode'.
650 (message "Cannot remove highlighting from read-only mode buffer %s"
651 (buffer-name))
652 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
653 (highlight-save-buffer-state
654 (hilit-chg-hide-changes)
655 (hilit-chg-map-changes
656 (lambda (prop start stop)
657 (remove-text-properties start stop '(hilit-chg nil)))))
658 (setq highlight-changes-mode nil)
659 (force-mode-line-update)))
660
661
662 ;;;###autoload
663 (defun highlight-changes-next-change ()
664 "Move to the beginning of the next change, if in Highlight Changes mode."
665 (interactive)
666 (if highlight-changes-mode
667 (let ((start (point))
668 prop)
669 (setq prop (get-text-property (point) 'hilit-chg))
670 (if prop
671 ;; we are in a change
672 (setq start (next-single-property-change (point) 'hilit-chg)))
673 (if start
674 (setq start (next-single-property-change start 'hilit-chg)))
675 (if start
676 (goto-char start)
677 (message "no next change")))
678 (message "This buffer is not in Highlight Changes mode.")))
679
680
681 ;;;###autoload
682 (defun highlight-changes-previous-change ()
683 "Move to the beginning of the previous change, if in Highlight Changes mode."
684 (interactive)
685 (if highlight-changes-mode
686 (let ( (start (point)) (prop nil) )
687 (or (bobp)
688 (setq prop (get-text-property (1- (point)) 'hilit-chg)))
689 (if prop
690 ;; we are in a change
691 (setq start (previous-single-property-change (point) 'hilit-chg)))
692 (if start
693 (setq start (previous-single-property-change start 'hilit-chg)))
694 ;; special handling for the case where (point-min) is a change
695 (if start
696 (setq start (or (previous-single-property-change start 'hilit-chg)
697 (if (get-text-property (point-min) 'hilit-chg)
698 (point-min)))))
699 (if start
700 (goto-char start)
701 (message "no previous change")))
702 (message "This buffer is not in Highlight Changes mode.")))
703
704 ;; ========================================================================
705
706 (defun hilit-chg-make-list (&optional force)
707 "Construct `hilit-chg-list' and `highlight-changes-face-list'."
708 ;; Constructs highlight-changes-face-list if necessary,
709 ;; and hilit-chg-list always:
710 ;; Maybe this should always be called when rotating a face
711 ;; so we pick up any changes?
712 (if (or (null highlight-changes-face-list) ; Don't do it if it
713 force) ; already exists unless FORCE non-nil.
714 (let ((p highlight-changes-colors)
715 (n 1) name)
716 (setq highlight-changes-face-list nil)
717 (while p
718 (setq name (intern (format "highlight-changes-%d" n)))
719 (copy-face 'highlight-changes name)
720 (set-face-foreground name (car p))
721 (setq highlight-changes-face-list
722 (append highlight-changes-face-list (list name)))
723 (setq p (cdr p))
724 (setq n (1+ n)))))
725 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes))
726 (let ((p highlight-changes-face-list)
727 (n 1)
728 last-category last-face)
729 (while p
730 (setq last-category (intern (format "change-%d" n)))
731 ;; (setq last-face (intern (format "highlight-changes-%d" n)))
732 (setq last-face (car p))
733 (setq hilit-chg-list
734 (append hilit-chg-list
735 (list last-category last-face)))
736 (setq p (cdr p))
737 (setq n (1+ n)))
738 (setq hilit-chg-list
739 (append hilit-chg-list
740 (list last-category last-face)))))
741
742 (defun hilit-chg-bump-change (prop start end)
743 "Increment (age) the Highlight Changes mode text property."
744 (let ( new-prop )
745 (if (eq prop 'hilit-chg-delete)
746 (setq new-prop (nth 2 hilit-chg-list))
747 (setq new-prop (nth 2 (member prop hilit-chg-list))))
748 (if prop
749 (put-text-property start end 'hilit-chg new-prop)
750 (message "%d-%d unknown property %s not changed" start end prop))))
751
752 ;;;###autoload
753 (defun highlight-changes-rotate-faces ()
754 "Rotate the faces if in Highlight Changes mode and the changes are visible.
755
756 Current changes are displayed in the face described by the first element
757 of `highlight-changes-face-list', one level older changes are shown in
758 face described by the second element, and so on. Very old changes remain
759 shown in the last face in the list.
760
761 You can automatically rotate colors when the buffer is saved by adding
762 this function to `write-file-functions' as a buffer-local value. To do
763 this, eval the following in the buffer to be saved:
764
765 \(add-hook 'write-file-functions 'highlight-changes-rotate-faces nil t)"
766 (interactive)
767 (when (and highlight-changes-mode highlight-changes-visible-mode)
768 (let ((modified (buffer-modified-p))
769 (inhibit-modification-hooks t))
770 ;; The `modified' related code tries to combine two goals: (1) Record the
771 ;; rotation in `buffer-undo-list' and (2) avoid setting the modified flag
772 ;; of the current buffer due to the rotation. We do this by inserting (in
773 ;; `buffer-undo-list') entries restoring buffer-modified-p to nil before
774 ;; and after the entry for the rotation.
775 ;; FIXME: this is no good: we need to test the `modified' state at the
776 ;; time of the undo, not at the time of the "do", otherwise the undo
777 ;; may erroneously clear the modified flag. --Stef
778 ;; (unless modified
779 ;; ;; Install the "before" entry.
780 ;; (push '(apply restore-buffer-modified-p nil) buffer-undo-list))
781 (unwind-protect
782 (progn
783 ;; ensure hilit-chg-list is made and up to date
784 (hilit-chg-make-list)
785 ;; remove our existing overlays
786 (hilit-chg-hide-changes)
787 ;; for each change text property, increment it
788 (hilit-chg-map-changes 'hilit-chg-bump-change)
789 ;; and display them
790 (hilit-chg-display-changes))
791 (unless modified
792 ;; Install the "after" entry. FIXME: See above.
793 ;; (push '(apply restore-buffer-modified-p nil) buffer-undo-list)
794
795 (restore-buffer-modified-p nil)))))
796 ;; This always returns nil so it is safe to use in write-file-functions
797 nil)
798
799 ;; ========================================================================
800 ;; Comparing buffers/files
801 ;; These use ediff to find the differences.
802
803 (defun highlight-markup-buffers
804 (buf-a file-a buf-b file-b &optional markup-a-only)
805 "Get differences between two buffers and set highlight changes.
806 Both buffers are done unless optional parameter MARKUP-A-ONLY
807 is non-nil."
808 (eval-and-compile
809 (require 'ediff-util))
810 (save-window-excursion
811 (let* (change-info
812 change-a change-b
813 a-start a-end len-a
814 b-start b-end len-b
815 (bufa-modified (buffer-modified-p buf-a))
816 (bufb-modified (buffer-modified-p buf-b))
817 (buf-a-read-only (with-current-buffer buf-a buffer-read-only))
818 (buf-b-read-only (with-current-buffer buf-b buffer-read-only))
819 temp-a temp-b)
820 (if (and file-a bufa-modified)
821 (if (y-or-n-p (format "Save buffer %s? " buf-a))
822 (with-current-buffer buf-a
823 (save-buffer)
824 (setq bufa-modified (buffer-modified-p buf-a)))
825 (setq file-a nil)))
826 (or file-a
827 (setq temp-a (setq file-a (ediff-make-temp-file buf-a nil))))
828
829 (if (and file-b bufb-modified)
830 (if (y-or-n-p (format "Save buffer %s? " buf-b))
831 (with-current-buffer buf-b
832 (save-buffer)
833 (setq bufb-modified (buffer-modified-p buf-b)))
834 (setq file-b nil)))
835 (or file-b
836 (setq temp-b (setq file-b (ediff-make-temp-file buf-b nil))))
837 (set-buffer buf-a)
838 (highlight-changes-mode 1)
839 (or markup-a-only (with-current-buffer buf-b
840 (highlight-changes-mode 1)))
841 (setq change-info (hilit-chg-get-diff-info buf-a file-a buf-b file-b))
842
843
844 (setq change-a (car change-info))
845 (setq change-b (car (cdr change-info)))
846
847 (hilit-chg-make-list)
848 (while change-a
849 (setq a-start (nth 0 (car change-a)))
850 (setq a-end (nth 1 (car change-a)))
851 (setq b-start (nth 0 (car change-b)))
852 (setq b-end (nth 1 (car change-b)))
853 (setq len-a (- a-end a-start))
854 (setq len-b (- b-end b-start))
855 (set-buffer buf-a)
856 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
857 (or markup-a-only
858 (with-current-buffer buf-b
859 (hilit-chg-set-face-on-change b-start b-end len-a
860 buf-b-read-only)
861 ))
862 (setq change-a (cdr change-a))
863 (setq change-b (cdr change-b)))
864 (or bufa-modified
865 (with-current-buffer buf-a (set-buffer-modified-p nil)))
866 (or bufb-modified
867 (with-current-buffer buf-b (set-buffer-modified-p nil)))
868 (if temp-a
869 (delete-file temp-a))
870 (if temp-b
871 (delete-file temp-b)))
872 ))
873
874 ;;;###autoload
875 (defun highlight-compare-buffers (buf-a buf-b)
876 "Compare two buffers and highlight the differences.
877
878 The default is the current buffer and the one in the next window.
879
880 If either buffer is modified and is visiting a file, you are prompted
881 to save the file.
882
883 Unless the buffer is unmodified and visiting a file, the buffer is
884 written to a temporary file for comparison.
885
886 If a buffer is read-only, differences will be highlighted but no property
887 changes are made, so \\[highlight-changes-next-change] and
888 \\[highlight-changes-previous-change] will not work."
889 (interactive
890 (list
891 (get-buffer (read-buffer "buffer-a " (current-buffer) t))
892 (get-buffer
893 (read-buffer "buffer-b "
894 (window-buffer (next-window (selected-window))) t))))
895 (let ((file-a (buffer-file-name buf-a))
896 (file-b (buffer-file-name buf-b)))
897 (highlight-markup-buffers buf-a file-a buf-b file-b)
898 ))
899
900 ;;;###autoload
901 (defun highlight-compare-with-file (file-b)
902 "Compare this buffer with a file, and highlight differences.
903
904 If the buffer has a backup filename, it is used as the default when
905 this function is called interactively.
906
907 If the current buffer is visiting the file being compared against, it
908 also will have its differences highlighted. Otherwise, the file is
909 read in temporarily but the buffer is deleted.
910
911 If the buffer is read-only, differences will be highlighted but no property
912 changes are made, so \\[highlight-changes-next-change] and
913 \\[highlight-changes-previous-change] will not work."
914 (interactive
915 (let ((file buffer-file-name)
916 (file-name nil)
917 (file-dir nil))
918 (and file
919 (setq file-name (file-name-nondirectory file)
920 file-dir (file-name-directory file)))
921 (setq file-name (make-backup-file-name file-name))
922 (unless (file-exists-p file-name)
923 (setq file-name nil))
924 (list (read-file-name
925 "Find to compare with: " ;; prompt
926 file-dir ;; directory
927 nil ;; default
928 nil ;; existing
929 file-name) ;; initial
930 )))
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))
940 ))
941
942
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))
949 )
950 (ediff-with-current-buffer e (ediff-really-quit nil))
951 (list x y)))
952
953
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
978 ;;
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.
996 ))
997
998 ;; ======================= global-highlight-changes-mode ==============
999
1000 ;;;###autoload
1001 (define-globalized-minor-mode global-highlight-changes-mode
1002 highlight-changes-mode highlight-changes-mode-turn-on)
1003
1004 (define-obsolete-function-alias
1005 'global-highlight-changes
1006 'global-highlight-changes-mode "23.1")
1007
1008 (defun highlight-changes-mode-turn-on ()
1009 "See if Highlight Changes mode should be turned on for this buffer.
1010 This is called when `global-highlight-changes-mode' is turned on."
1011 (or highlight-changes-mode ; do nothing if already on
1012 (if
1013 (cond
1014 ((null highlight-changes-global-modes)
1015 nil)
1016 ((functionp highlight-changes-global-modes)
1017 (funcall highlight-changes-global-modes))
1018 ((listp highlight-changes-global-modes)
1019 (if (eq (car-safe highlight-changes-global-modes) 'not)
1020 (not (memq major-mode (cdr highlight-changes-global-modes)))
1021 (memq major-mode highlight-changes-global-modes)))
1022 (t
1023 (and
1024 (not (string-match "^[ *]" (buffer-name)))
1025 (buffer-file-name))))
1026 (highlight-changes-mode 1))
1027 ))
1028
1029
1030 ;;;; Desktop support.
1031
1032 ;; Called by `desktop-create-buffer' to restore `highlight-changes-mode'.
1033 (defun hilit-chg-desktop-restore (desktop-buffer-locals)
1034 (highlight-changes-mode
1035 (or (cdr (assq 'highlight-changes-mode desktop-buffer-locals)) 1)))
1036
1037 (add-to-list 'desktop-minor-mode-handlers
1038 '(highlight-changes-mode . hilit-chg-desktop-restore))
1039
1040 (add-to-list 'desktop-locals-to-save 'highlight-changes-mode)
1041
1042 ;; ===================== debug ==================
1043 ;; For debug & test use:
1044 ;;
1045 ;; (defun hilit-chg-debug-show (&optional beg end)
1046 ;; (interactive)
1047 ;; (message "--- hilit-chg-debug-show ---")
1048 ;; (hilit-chg-map-changes '(lambda (prop start end)
1049 ;; (message "%d-%d: %s" start end prop)
1050 ;; )
1051 ;; beg end
1052 ;; ))
1053 ;;
1054 ;; ================== end of debug ===============
1055
1056 (provide 'hilit-chg)
1057
1058 ;; arch-tag: de00301d-5bad-44da-aa82-e0e010b0c463
1059 ;;; hilit-chg.el ends here