1 ;;; hilit-chg.el --- minor mode displaying buffer changes with special face
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Richard Sharman <rsharman@pobox.com>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; A minor mode: "Highlight Changes mode".
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.
37 ;; Two faces are supported: one for changed or inserted text and
38 ;; another for the first character after text has been deleted.
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'.
44 ;; Command highlight-compare-with-file shows changes in this file
45 ;; compared with another file (by default the previous version of the
48 ;; The command highlight-compare-buffers compares two buffers by
49 ;; highlighting their differences.
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'.
62 ;; You can automatically rotate faces when the buffer is saved;
63 ;; see function `highlight-changes-rotate-faces' for how to do this.
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.
72 ;; (defun my-highlight-changes-enable-hook ()
73 ;; (add-hook 'write-file-functions 'highlight-changes-rotate-faces nil t)
76 ;; (defun my-highlight-changes-disable-hook ()
77 ;; (remove-hook 'write-file-functions 'highlight-changes-rotate-faces t)
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)
85 ;; Automatically enabling Highlight Changes mode
88 ;; Normally, Highlight Changes mode is turned on explicitly in a buffer.
90 ;; If you prefer to have it automatically invoked you can do it as
93 ;; 1. Most modes have a major-hook, typically called MODE-hook. You
94 ;; can use `add-hook' to call `highlight-changes-mode'.
97 ;; (add-hook 'c-mode-hook 'highlight-changes-mode)
99 ;; However, this cannot be done for Fundamental mode for there is no
102 ;; 2. You can use the function `global-highlight-changes-mode'
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.
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
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.
122 ;; To enable it for future sessions put this in your ~/.emacs file:
123 ;; (global-highlight-changes-mode t)
126 ;; Possible bindings:
127 ;; (global-set-key '[C-right] 'highlight-changes-next-change)
128 ;; (global-set-key '[C-left] 'highlight-changes-previous-change)
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
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.
146 ;;; To do (maybe), notes, ...
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.
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.
165 ;; - global mode and various stuff added
166 ;; - Changed to use overlays
168 ;; - renamed to Highlight Changes mode.
170 ;; - Use require for ediff stuff
171 ;; - Added highlight-compare-buffers
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
184 ;; ====================== Customization =======================
185 (defgroup highlight-changes nil
186 "Highlight Changes mode."
191 ;; Face information: How the changes appear.
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!).
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
)
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
)
220 ;; A (not very good) default list of colors to rotate through.
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.
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
)
239 (define-obsolete-variable-alias 'highlight-changes-colours
240 'highlight-changes-colors
"22.1")
243 ;; When you invoke highlight-changes-mode, should highlight-changes-visible-mode
246 (define-obsolete-variable-alias 'highlight-changes-initial-state
247 'highlight-changes-visibility-initial-state
)
249 (defcustom highlight-changes-visibility-initial-state t
250 "Controls whether changes are initially visible in Highlight Changes mode.
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."
256 :group
'highlight-changes
)
258 ;; highlight-changes-global-initial-state has been removed
262 ;; These are the strings displayed in the mode-line for the minor mode:
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
)
272 (define-obsolete-variable-alias 'highlight-changes-active-string
273 'highlight-changes-visible-string
"23.1")
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
)
283 (define-obsolete-variable-alias 'highlight-changes-passive-string
284 'highlight-changes-invisible-string
"23.1")
288 (defcustom highlight-changes-global-modes t
289 "Determine whether a buffer is suitable for global Highlight Changes mode.
291 A function means call that function to decide: if it returns non-nil,
292 the buffer is suitable.
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.
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 `*'.
301 A value of nil means no buffers are suitable for `global-highlight-changes-mode'
302 \(effectively disabling the mode).
306 means that Highlight Changes mode is turned on for buffers in C and C++
309 (const :tag
"all non-special buffers visiting files" t
)
310 (set :menu-tag
"specific modes" :tag
"modes"
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
)
318 :group
'highlight-changes
)
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."
328 :group
'highlight-changes
)
330 ;; These are for internal use.
332 (defvar hilit-chg-list nil
)
333 (defvar hilit-chg-string
" ??")
335 (make-variable-buffer-local 'hilit-chg-string
)
342 (define-minor-mode highlight-changes-mode
343 "Toggle Highlight Changes mode.
345 With ARG, turn Highlight Changes mode on if and only if arg is positive.
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
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.
363 `highlight-changes-enable-hook': called when enabling Highlight Changes mode.
364 `highlight-changes-disable-hook': called when disabling Highlight Changes mode."
366 hilit-chg-string
;; lighter
368 (if (or (display-color-p)
369 (and (fboundp 'x-display-grayscale-p
) (x-display-grayscale-p)))
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
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))
382 ;; mode is turned off
384 (message "Highlight Changes mode requires color or grayscale display")))
388 (define-minor-mode highlight-changes-visible-mode
389 "Toggle visiblility of changes when buffer is in Highlight Changes mode.
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.
395 The default value can be customized with variable
396 `highlight-changes-visibility-initial-state'
398 This command does not itself set highlight-changes mode."
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
)
419 ((p (reverse old-list
))
420 (n (length old-list
))
425 (setq old-name
(car p
))
426 (setq new-name
(intern (format "highlight-changes-%d" n
)))
427 (if (eq old-name new-name
)
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
)
437 (setq new-list
(append (list new-name
) new-list
))
440 (if (equal new-list
(widget-value w
))
441 nil
;; (message "notify: no change!")
442 (widget-value-set w new-list
)
446 ;; (message "notify: no default here!")
448 (let ((parent (widget-get w
:parent
)))
450 (widget-apply parent
:notify w event
))))
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'."
463 :notify hilit-chg-cust-fix-changes-face-list
465 (const :tag
"Derive from highlight-changes-colors" nil
)
467 :group
'highlight-changes
)
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)))
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
))
482 (funcall func prop start
(or end limit
)))
486 (defun hilit-chg-display-changes (&optional beg end
)
487 "Display face information for Highlight Changes mode.
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'.
492 This is the opposite of `hilit-chg-hide-changes'."
493 (hilit-chg-map-changes 'hilit-chg-make-ov beg end
))
496 (defun hilit-chg-make-ov (prop start end
)
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
)))))
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
)
516 (error "hilit-chg-make-ov: no face for prop: %s" prop
))))
518 (defun hilit-chg-hide-changes (&optional beg end
)
519 "Remove face information for Highlight Changes mode.
521 The overlay containing the face is removed, but the text property
522 containing the change information is retained.
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
)))))
533 (defun hilit-chg-fixup (beg end
)
534 "Fix change overlays in region between BEG and END.
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
))
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
)
550 ;; So we don't check the file's mtime.
552 buffer-file-truename
)
556 (restore-buffer-modified-p nil
)))))
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."
563 (highlight-save-buffer-state
564 (remove-text-properties beg end
'(hilit-chg nil
))
565 (hilit-chg-fixup beg end
)))
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."
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'.
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.
580 (let ((beg-decr 1) (end-incr 1)
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))
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)
598 ;; (setq beg-decr 0 end-incr 0)
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
))
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
)
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
)))))))
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
625 (setq hilit-chg-string highlight-changes-visible-string
)
627 (hilit-chg-display-changes)))
628 ;; changes are invisible
629 (setq hilit-chg-string highlight-changes-invisible-string
)
631 (hilit-chg-hide-changes))))
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
)
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
))
644 (defun hilit-chg-clear ()
645 "Remove Highlight Changes mode for this buffer.
646 This removes all saved change information."
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"
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)))
663 (defun highlight-changes-next-change ()
664 "Move to the beginning of the next change, if in Highlight Changes mode."
666 (if highlight-changes-mode
667 (let ((start (point))
669 (setq prop
(get-text-property (point) 'hilit-chg
))
671 ;; we are in a change
672 (setq start
(next-single-property-change (point) 'hilit-chg
)))
674 (setq start
(next-single-property-change start
'hilit-chg
)))
677 (message "no next change")))
678 (message "This buffer is not in Highlight Changes mode.")))
682 (defun highlight-changes-previous-change ()
683 "Move to the beginning of the previous change, if in Highlight Changes mode."
685 (if highlight-changes-mode
686 (let ( (start (point)) (prop nil
) )
688 (setq prop
(get-text-property (1- (point)) 'hilit-chg
)))
690 ;; we are in a change
691 (setq start
(previous-single-property-change (point) 'hilit-chg
)))
693 (setq start
(previous-single-property-change start
'hilit-chg
)))
694 ;; special handling for the case where (point-min) is a change
696 (setq start
(or (previous-single-property-change start
'hilit-chg
)
697 (if (get-text-property (point-min) 'hilit-chg
)
701 (message "no previous change")))
702 (message "This buffer is not in Highlight Changes mode.")))
704 ;; ========================================================================
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
)
716 (setq highlight-changes-face-list nil
)
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
)))
725 (setq hilit-chg-list
(list 'hilit-chg
'highlight-changes
))
726 (let ((p highlight-changes-face-list
)
728 last-category last-face
)
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
))
734 (append hilit-chg-list
735 (list last-category last-face
)))
739 (append hilit-chg-list
740 (list last-category last-face
)))))
742 (defun hilit-chg-bump-change (prop start end
)
743 "Increment (age) the Highlight Changes mode text property."
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
))))
749 (put-text-property start end
'hilit-chg new-prop
)
750 (message "%d-%d unknown property %s not changed" start end prop
))))
753 (defun highlight-changes-rotate-faces ()
754 "Rotate the faces if in Highlight Changes mode and the changes are visible.
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.
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:
765 \(add-hook 'write-file-functions 'highlight-changes-rotate-faces nil t)"
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
779 ;; ;; Install the "before" entry.
780 ;; (push '(apply restore-buffer-modified-p nil) buffer-undo-list))
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
)
790 (hilit-chg-display-changes))
792 ;; Install the "after" entry. FIXME: See above.
793 ;; (push '(apply restore-buffer-modified-p nil) buffer-undo-list)
795 (restore-buffer-modified-p nil
)))))
796 ;; This always returns nil so it is safe to use in write-file-functions
799 ;; ========================================================================
800 ;; Comparing buffers/files
801 ;; These use ediff to find the differences.
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
809 (require 'ediff-util
))
810 (save-window-excursion
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
))
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
824 (setq bufa-modified
(buffer-modified-p buf-a
)))
827 (setq temp-a
(setq file-a
(ediff-make-temp-file buf-a nil
))))
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
833 (setq bufb-modified
(buffer-modified-p buf-b
)))
836 (setq temp-b
(setq file-b
(ediff-make-temp-file buf-b nil
))))
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
))
844 (setq change-a
(car change-info
))
845 (setq change-b
(car (cdr change-info
)))
847 (hilit-chg-make-list)
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
))
856 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only
)
858 (with-current-buffer buf-b
859 (hilit-chg-set-face-on-change b-start b-end len-a
862 (setq change-a
(cdr change-a
))
863 (setq change-b
(cdr change-b
)))
865 (with-current-buffer buf-a
(set-buffer-modified-p nil
)))
867 (with-current-buffer buf-b
(set-buffer-modified-p nil
)))
869 (delete-file temp-a
))
871 (delete-file temp-b
)))
875 (defun highlight-compare-buffers (buf-a buf-b
)
876 "Compare two buffers and highlight the differences.
878 The default is the current buffer and the one in the next window.
880 If either buffer is modified and is visiting a file, you are prompted
883 Unless the buffer is unmodified and visiting a file, the buffer is
884 written to a temporary file for comparison.
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."
891 (get-buffer (read-buffer "buffer-a " (current-buffer) t
))
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
)
901 (defun highlight-compare-with-file (file-b)
902 "Compare this buffer with a file, and highlight differences.
904 If the buffer has a backup filename, it is used as the default when
905 this function is called interactively.
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.
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."
915 (let ((file buffer-file-name
)
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
929 file-name
) ;; initial
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
))
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
))
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
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
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))))
974 (setq extent
(list (overlay-start (car p
))
975 (overlay-end (car p
))))
977 (setq x
(append x
(list extent
) )));; while p
979 (setq vb
(ediff-get-fine-diff-vector n
'B
))
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))))
989 (setq extent
(list (overlay-start (car p
))
990 (overlay-end (car 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 ;; ======================= global-highlight-changes-mode ==============
1001 (define-globalized-minor-mode global-highlight-changes-mode
1002 highlight-changes-mode highlight-changes-mode-turn-on
)
1004 (define-obsolete-function-alias
1005 'global-highlight-changes
1006 'global-highlight-changes-mode
"23.1")
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
1014 ((null highlight-changes-global-modes
)
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
)))
1024 (not (string-match "^[ *]" (buffer-name)))
1025 (buffer-file-name))))
1026 (highlight-changes-mode 1))
1030 ;;;; Desktop support.
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)))
1037 (add-to-list 'desktop-minor-mode-handlers
1038 '(highlight-changes-mode . hilit-chg-desktop-restore
))
1040 (add-to-list 'desktop-locals-to-save
'highlight-changes-mode
)
1042 ;; ===================== debug ==================
1043 ;; For debug & test use:
1045 ;; (defun hilit-chg-debug-show (&optional beg end)
1047 ;; (message "--- hilit-chg-debug-show ---")
1048 ;; (hilit-chg-map-changes '(lambda (prop start end)
1049 ;; (message "%d-%d: %s" start end prop)
1054 ;; ================== end of debug ===============
1056 (provide 'hilit-chg
)
1058 ;; arch-tag: de00301d-5bad-44da-aa82-e0e010b0c463
1059 ;;; hilit-chg.el ends here