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