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