Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / hilit-chg.el
blob9d4d2d8b383d5366e8ca87040188b647a1a517dd
1 ;;; hilit-chg.el --- minor mode displaying buffer changes with special face
3 ;; Copyright (C) 1998, 2000-2018 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 <https://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 (defcustom highlight-changes-colors
208 (if (eq (frame-parameter nil 'background-mode) 'light)
209 ;; defaults for light background:
210 '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue")
211 ;; defaults for dark background:
212 '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid"))
213 "Colors used by `highlight-changes-rotate-faces'.
214 The newest rotated change will be displayed in the first element of this list,
215 the next older will be in the second element etc.
217 This list is used if `highlight-changes-face-list' is nil, otherwise that
218 variable overrides this list. If you only care about foreground
219 colors then use this, if you want fancier faces then set
220 `highlight-changes-face-list'."
221 :type '(repeat color)
222 :group 'highlight-changes)
224 ;; When you invoke highlight-changes-mode, should highlight-changes-visible-mode
225 ;; be on or off?
227 (define-obsolete-variable-alias 'highlight-changes-initial-state
228 'highlight-changes-visibility-initial-state "23.1")
230 (defcustom highlight-changes-visibility-initial-state t
231 "Controls whether changes are initially visible in Highlight Changes mode.
233 This controls the initial value of `highlight-changes-visible-mode'.
234 When a buffer is in Highlight Changes mode the function
235 `highlight-changes-visible-mode' is used to toggle the mode on or off."
236 :type 'boolean
237 :group 'highlight-changes)
239 ;; highlight-changes-global-initial-state has been removed
243 ;; These are the strings displayed in the mode-line for the minor mode:
244 (define-obsolete-variable-alias 'highlight-changes-active-string
245 'highlight-changes-visible-string "23.1")
247 (defcustom highlight-changes-visible-string " +Chg"
248 "The string used when in Highlight Changes mode and changes are visible.
249 This should be set to nil if no indication is desired, or to
250 a string with a leading space."
251 :type '(choice string
252 (const :tag "None" nil))
253 :group 'highlight-changes)
255 (define-obsolete-variable-alias 'highlight-changes-passive-string
256 'highlight-changes-invisible-string "23.1")
258 (defcustom highlight-changes-invisible-string " -Chg"
259 "The string used when in Highlight Changes mode and changes are hidden.
260 This should be set to nil if no indication is desired, or to
261 a string with a leading space."
262 :type '(choice string
263 (const :tag "None" nil))
264 :group 'highlight-changes)
266 (defcustom highlight-changes-global-modes t
267 "Determine whether a buffer is suitable for global Highlight Changes mode.
269 A function means call that function to decide: if it returns non-nil,
270 the buffer is suitable.
272 A list means the elements are major modes suitable for Highlight
273 Changes mode, or a list whose first element is `not' followed by major
274 modes which are not suitable.
276 A value of t means the buffer is suitable if it is visiting a file and
277 its name does not begin with ` ' or `*'.
279 A value of nil means no buffers are suitable for `global-highlight-changes-mode'
280 \(effectively disabling the mode).
282 Example:
283 (c-mode c++-mode)
284 means that Highlight Changes mode is turned on for buffers in C and C++
285 modes only."
286 :type '(choice
287 (const :tag "all non-special buffers visiting files" t)
288 (set :menu-tag "specific modes" :tag "modes"
289 :value (not)
290 (const :tag "All except these" not)
291 (repeat :tag "Modes" :inline t (symbol :tag "mode")))
292 (function :menu-tag "determined by function"
293 :value buffer-file-name)
294 (const :tag "none" nil)
296 :group 'highlight-changes)
298 (defcustom highlight-changes-global-changes-existing-buffers nil
299 "If non-nil, toggling global Highlight Changes mode affects existing buffers.
300 Normally, `global-highlight-changes-mode' affects only new buffers (to be
301 created). However, if `highlight-changes-global-changes-existing-buffers'
302 is non-nil, then turning on `global-highlight-changes-mode' will turn on
303 Highlight Changes mode in suitable buffers, and turning the mode off will
304 remove it from existing buffers."
305 :type 'boolean
306 :group 'highlight-changes)
308 ;; These are for internal use.
310 (defvar hilit-chg-list nil)
311 (defvar hilit-chg-string " ??")
313 (make-variable-buffer-local 'hilit-chg-string)
317 ;;; Functions...
319 ;;;###autoload
320 (define-minor-mode highlight-changes-mode
321 "Toggle highlighting changes in this buffer (Highlight Changes mode).
322 With a prefix argument ARG, enable Highlight Changes mode if ARG
323 is positive, and disable it otherwise. If called from Lisp,
324 enable the mode if ARG is omitted or nil.
326 When Highlight Changes is enabled, changes are marked with a text
327 property. Normally they are displayed in a distinctive face, but
328 command \\[highlight-changes-visible-mode] can be used to toggle
329 this on and off.
331 Other functions for buffers in this mode include:
332 \\[highlight-changes-next-change] - move point to beginning of next change
333 \\[highlight-changes-previous-change] - move to beginning of previous change
334 \\[highlight-changes-remove-highlight] - remove the change face from the region
335 \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes
336 through various faces.
337 \\[highlight-compare-with-file] - mark text as changed by comparing this
338 buffer with the contents of a file
339 \\[highlight-compare-buffers] highlights differences between two buffers."
340 nil ;; init-value
341 hilit-chg-string ;; lighter
342 nil ;; keymap
343 (if (or (display-color-p)
344 (and (fboundp 'x-display-grayscale-p) (x-display-grayscale-p)))
345 (progn
346 (if (and (eq this-command 'global-highlight-changes-mode)
347 (not highlight-changes-global-changes-existing-buffers))
348 ;; The global mode has toggled the value of the mode variable,
349 ;; but not other changes have been mode, so we are safe
350 ;; to retoggle it.
351 (setq highlight-changes-mode (not highlight-changes-mode)))
352 (if highlight-changes-mode
353 ;; it is being turned on
354 (hilit-chg-set)
355 ;; mode is turned off
356 (hilit-chg-clear)))
357 (message "Highlight Changes mode requires color or grayscale display")))
360 ;;;###autoload
361 (define-minor-mode highlight-changes-visible-mode
362 "Toggle visibility of highlighting due to Highlight Changes mode.
363 With a prefix argument ARG, enable Highlight Changes Visible mode
364 if ARG is positive, and disable it otherwise. If called from
365 Lisp, enable the mode if ARG is omitted or nil.
367 Highlight Changes Visible mode only has an effect when Highlight
368 Changes mode is on. When enabled, the changed text is displayed
369 in a distinctive face.
371 The default value can be customized with variable
372 `highlight-changes-visibility-initial-state'.
374 This command does not itself set Highlight Changes mode."
376 t ;; init-value
377 nil ;; lighter
378 nil ;; keymap
380 (hilit-chg-update)
384 (defun hilit-chg-cust-fix-changes-face-list (w _wc &optional event)
385 ;; When customization function `highlight-changes-face-list' inserts a new
386 ;; face it uses the default face. We don't want the user to modify this
387 ;; face, so we rename the faces in the list on an insert. The rename is
388 ;; actually done by copying the faces so user-defined faces still remain
389 ;; in the same order.
390 ;; The notifying the parent is needed because without it changes to the
391 ;; faces are saved but not to the actual list itself.
392 (let ((old-list (widget-value w)))
393 (if (member 'default old-list)
394 (let
395 ((p (reverse old-list))
396 (n (length old-list))
397 new-name old-name
398 (new-list nil)
400 (while p
401 (setq old-name (car p))
402 (setq new-name (intern (format "highlight-changes-%d" n)))
403 (if (eq old-name new-name)
405 ;; A new face has been inserted: we don't want to modify the
406 ;; default face so copy it. Better, though, (I think) is to
407 ;; make a new face have the same attributes as
408 ;; the `highlight-changes' face.
409 (if (eq old-name 'default)
410 (copy-face 'highlight-changes new-name)
411 (copy-face old-name new-name)
413 (setq new-list (append (list new-name) new-list))
414 (setq n (1- n))
415 (setq p (cdr p)))
416 (if (equal new-list (widget-value w))
417 nil ;; (message "notify: no change!")
418 (widget-value-set w new-list)
419 (widget-setup)
422 ;; (message "notify: no default here!")
424 (let ((parent (widget-get w :parent)))
425 (when parent
426 (widget-apply parent :notify w event))))
429 (defcustom highlight-changes-face-list nil
430 "A list of faces used when rotating changes.
431 Normally the variable is initialized to nil and the list is created from
432 `highlight-changes-colors' when needed. However, you can set this variable
433 to any list of faces. You will have to do this if you want faces which
434 don't just differ from the `highlight-changes' face by the foreground color.
435 Otherwise, this list will be constructed when needed from
436 `highlight-changes-colors'."
437 :type '(choice
438 (repeat
439 :notify hilit-chg-cust-fix-changes-face-list
440 face )
441 (const :tag "Derive from highlight-changes-colors" nil)
443 :group 'highlight-changes)
446 (defun hilit-chg-map-changes (func &optional start-position end-position)
447 "Call function FUNC for each region used by Highlight Changes mode.
448 If START-POSITION is nil, (point-min) is used.
449 If END-POSITION is nil, (point-max) is used.
450 FUNC is called with three params: PROPERTY START STOP."
451 (let ((start (or start-position (point-min)))
452 (limit (or end-position (point-max)))
453 prop end)
454 (while (and start (< start limit))
455 (setq prop (get-text-property start 'hilit-chg))
456 (setq end (text-property-not-all start limit 'hilit-chg prop))
457 (if prop
458 (funcall func prop start (or end limit)))
459 (setq start end))))
462 (defun hilit-chg-display-changes (&optional beg end)
463 "Display face information for Highlight Changes mode.
465 An overlay from BEG to END containing a change face is added
466 from the information in the text property of type `hilit-chg'.
468 This is the opposite of `hilit-chg-hide-changes'."
469 (hilit-chg-map-changes 'hilit-chg-make-ov beg end))
472 (defun hilit-chg-make-ov (prop start end)
473 (or prop
474 (error "hilit-chg-make-ov: prop is nil"))
475 ;; For the region create overlays with a distinctive face
476 ;; and the text property 'hilit-chg.
477 (let ((ov (make-overlay start end))
478 (face (if (eq prop 'hilit-chg-delete)
479 'highlight-changes-delete
480 (nth 1 (member prop hilit-chg-list)))))
481 (if face
482 (progn
483 ;; We must mark the face, that is the purpose of the overlay.
484 (overlay-put ov 'face face)
485 ;; I don't think we need to set evaporate since we should
486 ;; be controlling them!
487 (overlay-put ov 'evaporate t)
488 ;; We set the change property so we can tell this is one
489 ;; of our overlays (so we don't delete someone else's).
490 (overlay-put ov 'hilit-chg t)
492 (error "hilit-chg-make-ov: no face for prop: %s" prop))))
494 (defun hilit-chg-hide-changes (&optional beg end)
495 "Remove face information for Highlight Changes mode.
497 The overlay containing the face is removed, but the text property
498 containing the change information is retained.
500 This is the opposite of `hilit-chg-display-changes'."
501 (let ((start (or beg (point-min)))
502 (limit (or end (point-max))))
503 (dolist (p (overlays-in start limit))
504 ;; don't delete the overlay if it isn't ours!
505 (if (overlay-get p 'hilit-chg)
506 (delete-overlay p)))))
509 (defun hilit-chg-fixup (beg end)
510 "Fix change overlays in region between BEG and END.
512 Ensure the overlays agree with the changes as determined from
513 the text properties of type `hilit-chg'."
514 ;; Remove or alter overlays in region beg..end
515 (remove-overlays beg end 'hilit-chg t)
516 (hilit-chg-display-changes beg end))
518 ;;;###autoload
519 (defun highlight-changes-remove-highlight (beg end)
520 "Remove the change face from the region between BEG and END.
521 This allows you to manually remove highlighting from uninteresting changes."
522 (interactive "r")
523 (with-silent-modifications
524 (remove-text-properties beg end '(hilit-chg nil))
525 (hilit-chg-fixup beg end)))
527 (defun hilit-chg-set-face-on-change (beg end leng-before
528 &optional no-property-change)
529 "Record changes and optionally display them in a distinctive face.
530 `hilit-chg-set' adds this function to the `after-change-functions' hook."
532 ;; This function is called by the `after-change-functions' hook, which
533 ;; is how we are notified when text is changed.
534 ;; It is also called from `highlight-compare-with-file'.
536 ;; We do NOT want to simply do this if this is an undo command, because
537 ;; otherwise an undone change shows up as changed. While the properties
538 ;; are automatically restored by undo, we must fix up the overlay.
539 (save-match-data
540 (let (;;(beg-decr 1)
541 (end-incr 1)
542 (type 'hilit-chg))
543 (if undo-in-progress
544 (if (and highlight-changes-mode
545 highlight-changes-visible-mode)
546 (hilit-chg-fixup beg end))
547 (with-silent-modifications
548 (if (and (= beg end) (> leng-before 0))
549 ;; deletion
550 (progn
551 ;; The eolp and bolp tests are a kludge! But they prevent
552 ;; rather nasty looking displays when deleting text at the end
553 ;; of line, such as normal corrections as one is typing and
554 ;; immediately makes a correction, and when deleting first
555 ;; character of a line.
556 ;; (if (= leng-before 1)
557 ;; (if (eolp)
558 ;; (setq beg-decr 0 end-incr 0)
559 ;; (if (bolp)
560 ;; (setq beg-decr 0))))
561 ;; (setq beg (max (- beg beg-decr) (point-min)))
562 (setq end (min (+ end end-incr) (point-max)))
563 (setq type 'hilit-chg-delete))
564 ;; Not a deletion.
565 ;; Most of the time the following is not necessary, but
566 ;; if the current text was marked as a deletion then
567 ;; the old overlay is still in effect. So if the user adds some
568 ;; text where she earlier deleted text, we have to remove the
569 ;; deletion marking, and replace it explicitly with a `changed'
570 ;; marking, otherwise its highlighting would disappear.
571 (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete)
572 (save-restriction
573 (widen)
574 (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg)
575 (if highlight-changes-visible-mode
576 (hilit-chg-fixup end (+ end 1))))))
577 (unless no-property-change
578 (put-text-property beg end 'hilit-chg type))
579 (if (or highlight-changes-visible-mode no-property-change)
580 (hilit-chg-make-ov type beg end)))))))
582 (defun hilit-chg-update ()
583 "Update a buffer's highlight changes when visibility changed."
584 (if highlight-changes-visible-mode
585 ;; changes are visible
586 (progn
587 (setq hilit-chg-string highlight-changes-visible-string)
588 (or buffer-read-only
589 (hilit-chg-display-changes)))
590 ;; changes are invisible
591 (setq hilit-chg-string highlight-changes-invisible-string)
592 (or buffer-read-only
593 (hilit-chg-hide-changes))))
595 (defun hilit-chg-set ()
596 "Turn on Highlight Changes mode for this buffer."
597 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
598 (hilit-chg-make-list)
599 (setq highlight-changes-mode t)
600 (setq highlight-changes-visible-mode highlight-changes-visibility-initial-state)
601 (hilit-chg-update)
602 (force-mode-line-update)
603 (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t))
605 (defun hilit-chg-clear ()
606 "Remove Highlight Changes mode for this buffer.
607 This removes all saved change information."
608 (if buffer-read-only
609 ;; We print the buffer name because this function could be called
610 ;; on many buffers from `global-highlight-changes-mode'.
611 (message "Cannot remove highlighting from read-only mode buffer %s"
612 (buffer-name))
613 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
614 (with-silent-modifications
615 (hilit-chg-hide-changes)
616 (hilit-chg-map-changes
617 (lambda (_prop start stop)
618 (remove-text-properties start stop '(hilit-chg nil)))))
619 (setq highlight-changes-mode nil)
620 (force-mode-line-update)))
623 ;;;###autoload
624 (defun highlight-changes-next-change ()
625 "Move to the beginning of the next change, if in Highlight Changes mode."
626 (interactive)
627 (if highlight-changes-mode
628 (let ((start (point))
629 prop)
630 (setq prop (get-text-property (point) 'hilit-chg))
631 (if prop
632 ;; we are in a change
633 (setq start (next-single-property-change (point) 'hilit-chg)))
634 (if start
635 (setq start (next-single-property-change start 'hilit-chg)))
636 (if start
637 (goto-char start)
638 (message "no next change")))
639 (message "This buffer is not in Highlight Changes mode.")))
642 ;;;###autoload
643 (defun highlight-changes-previous-change ()
644 "Move to the beginning of the previous change, if in Highlight Changes mode."
645 (interactive)
646 (if highlight-changes-mode
647 (let ( (start (point)) (prop nil) )
648 (or (bobp)
649 (setq prop (get-text-property (1- (point)) 'hilit-chg)))
650 (if prop
651 ;; we are in a change
652 (setq start (previous-single-property-change (point) 'hilit-chg)))
653 (if start
654 (setq start (previous-single-property-change start 'hilit-chg)))
655 ;; special handling for the case where (point-min) is a change
656 (if start
657 (setq start (or (previous-single-property-change start 'hilit-chg)
658 (if (get-text-property (point-min) 'hilit-chg)
659 (point-min)))))
660 (if start
661 (goto-char start)
662 (message "no previous change")))
663 (message "This buffer is not in Highlight Changes mode.")))
665 ;; ========================================================================
667 (defun hilit-chg-make-list (&optional force)
668 "Construct `hilit-chg-list' and `highlight-changes-face-list'."
669 ;; Constructs highlight-changes-face-list if necessary,
670 ;; and hilit-chg-list always:
671 ;; Maybe this should always be called when rotating a face
672 ;; so we pick up any changes?
673 (if (or (null highlight-changes-face-list) ; Don't do it if it
674 force) ; already exists unless FORCE non-nil.
675 (let ((p highlight-changes-colors)
676 (n 1) name)
677 (setq highlight-changes-face-list nil)
678 (while p
679 (setq name (intern (format "highlight-changes-%d" n)))
680 (copy-face 'highlight-changes name)
681 (set-face-foreground name (car p))
682 (setq highlight-changes-face-list
683 (append highlight-changes-face-list (list name)))
684 (setq p (cdr p))
685 (setq n (1+ n)))))
686 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes))
687 (let ((p highlight-changes-face-list)
688 (n 1)
689 last-category last-face)
690 (while p
691 (setq last-category (intern (format "change-%d" n)))
692 ;; (setq last-face (intern (format "highlight-changes-%d" n)))
693 (setq last-face (car p))
694 (setq hilit-chg-list
695 (append hilit-chg-list
696 (list last-category last-face)))
697 (setq p (cdr p))
698 (setq n (1+ n)))
699 (setq hilit-chg-list
700 (append hilit-chg-list
701 (list last-category last-face)))))
703 (defun hilit-chg-bump-change (prop start end)
704 "Increment (age) the Highlight Changes mode text property."
705 (let ( new-prop )
706 (if (eq prop 'hilit-chg-delete)
707 (setq new-prop (nth 2 hilit-chg-list))
708 (setq new-prop (nth 2 (member prop hilit-chg-list))))
709 (if prop
710 (put-text-property start end 'hilit-chg new-prop)
711 (message "%d-%d unknown property %s not changed" start end prop))))
713 ;;;###autoload
714 (defun highlight-changes-rotate-faces ()
715 "Rotate the faces if in Highlight Changes mode and the changes are visible.
717 Current changes are displayed in the face described by the first element
718 of `highlight-changes-face-list', one level older changes are shown in
719 face described by the second element, and so on. Very old changes remain
720 shown in the last face in the list.
722 You can automatically rotate colors when the buffer is saved by adding
723 this function to `write-file-functions' as a buffer-local value. To do
724 this, eval the following in the buffer to be saved:
726 (add-hook \\='write-file-functions \\='highlight-changes-rotate-faces nil t)"
727 (interactive)
728 (when (and highlight-changes-mode highlight-changes-visible-mode)
729 (let ((modified (buffer-modified-p))
730 (inhibit-modification-hooks t))
731 ;; The `modified' related code tries to combine two goals: (1) Record the
732 ;; rotation in `buffer-undo-list' and (2) avoid setting the modified flag
733 ;; of the current buffer due to the rotation. We do this by inserting (in
734 ;; `buffer-undo-list') entries restoring buffer-modified-p to nil before
735 ;; and after the entry for the rotation.
736 ;; FIXME: this is no good: we need to test the `modified' state at the
737 ;; time of the undo, not at the time of the "do", otherwise the undo
738 ;; may erroneously clear the modified flag. --Stef
739 ;; (unless modified
740 ;; ;; Install the "before" entry.
741 ;; (push '(apply restore-buffer-modified-p nil) buffer-undo-list))
742 (unwind-protect
743 (progn
744 ;; ensure hilit-chg-list is made and up to date
745 (hilit-chg-make-list)
746 ;; remove our existing overlays
747 (hilit-chg-hide-changes)
748 ;; for each change text property, increment it
749 (hilit-chg-map-changes 'hilit-chg-bump-change)
750 ;; and display them
751 (hilit-chg-display-changes))
752 (unless modified
753 ;; Install the "after" entry. FIXME: See above.
754 ;; (push '(apply restore-buffer-modified-p nil) buffer-undo-list)
756 (restore-buffer-modified-p nil)))))
757 ;; This always returns nil so it is safe to use in write-file-functions
758 nil)
760 ;; ========================================================================
761 ;; Comparing buffers/files
762 ;; These use ediff to find the differences.
764 (defun highlight-markup-buffers
765 (buf-a file-a buf-b file-b &optional markup-a-only)
766 "Get differences between two buffers and set highlight changes.
767 Both buffers are done unless optional parameter MARKUP-A-ONLY
768 is non-nil."
769 (eval-and-compile
770 (require 'ediff-util))
771 (save-window-excursion
772 (let* (change-info
773 change-a change-b
774 a-start a-end len-a
775 b-start b-end len-b
776 (bufa-modified (buffer-modified-p buf-a))
777 (bufb-modified (and (not (eq buf-a buf-b)) (buffer-modified-p buf-b)))
778 (buf-a-read-only (with-current-buffer buf-a buffer-read-only))
779 (buf-b-read-only (with-current-buffer buf-b buffer-read-only))
780 temp-a temp-b)
781 (if (and file-a bufa-modified)
782 (if (y-or-n-p (format "Save buffer %s? " buf-a))
783 (with-current-buffer buf-a
784 (save-buffer)
785 (setq bufa-modified (buffer-modified-p buf-a)))
786 (setq file-a nil)))
787 (or file-a
788 (setq temp-a (setq file-a (ediff-make-temp-file buf-a nil))))
790 (if (and file-b bufb-modified)
791 (if (y-or-n-p (format "Save buffer %s? " buf-b))
792 (with-current-buffer buf-b
793 (save-buffer)
794 (setq bufb-modified (buffer-modified-p buf-b)))
795 (setq file-b nil)))
796 (or file-b
797 (setq temp-b (setq file-b (ediff-make-temp-file buf-b nil))))
798 (set-buffer buf-a)
799 (highlight-changes-mode 1)
800 (or markup-a-only (with-current-buffer buf-b
801 (highlight-changes-mode 1)))
802 (setq change-info (hilit-chg-get-diff-info buf-a file-a buf-b file-b))
805 (setq change-a (car change-info))
806 (setq change-b (car (cdr change-info)))
808 (hilit-chg-make-list)
809 (while change-a
810 (setq a-start (nth 0 (car change-a)))
811 (setq a-end (nth 1 (car change-a)))
812 (setq b-start (nth 0 (car change-b)))
813 (setq b-end (nth 1 (car change-b)))
814 (setq len-a (- a-end a-start))
815 (setq len-b (- b-end b-start))
816 (set-buffer buf-a)
817 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
818 (or markup-a-only
819 (with-current-buffer buf-b
820 (hilit-chg-set-face-on-change b-start b-end len-a
821 buf-b-read-only)
823 (setq change-a (cdr change-a))
824 (setq change-b (cdr change-b)))
825 (or bufa-modified
826 (with-current-buffer buf-a (set-buffer-modified-p nil)))
827 (or bufb-modified
828 (with-current-buffer buf-b (set-buffer-modified-p nil)))
829 (if temp-a
830 (delete-file temp-a))
831 (if temp-b
832 (delete-file temp-b)))
835 ;;;###autoload
836 (defun highlight-compare-buffers (buf-a buf-b)
837 "Compare two buffers and highlight the differences.
839 The default is the current buffer and the one in the next window.
841 If either buffer is modified and is visiting a file, you are prompted
842 to save the file.
844 Unless the buffer is unmodified and visiting a file, the buffer is
845 written to a temporary file for comparison.
847 If a buffer is read-only, differences will be highlighted but no property
848 changes are made, so \\[highlight-changes-next-change] and
849 \\[highlight-changes-previous-change] will not work."
850 (interactive
851 (list
852 (get-buffer (read-buffer "buffer-a " (current-buffer) t))
853 (get-buffer
854 (read-buffer "buffer-b "
855 (window-buffer (next-window)) t))))
856 (let ((file-a (buffer-file-name buf-a))
857 (file-b (buffer-file-name buf-b)))
858 (highlight-markup-buffers buf-a file-a buf-b file-b)
861 ;;;###autoload
862 (defun highlight-compare-with-file (file-b)
863 "Compare this buffer with a file, and highlight differences.
865 If the buffer has a backup filename, it is used as the default when
866 this function is called interactively.
868 If the current buffer is visiting the file being compared against, it
869 also will have its differences highlighted. Otherwise, the file is
870 read in temporarily but the buffer is deleted.
872 If the buffer is read-only, differences will be highlighted but no property
873 changes are made, so \\[highlight-changes-next-change] and
874 \\[highlight-changes-previous-change] will not work."
875 (interactive
876 (let ((file buffer-file-name)
877 (file-name nil)
878 (file-dir nil))
879 (and file
880 (setq file-name (file-name-nondirectory file)
881 file-dir (file-name-directory file)))
882 (setq file-name (make-backup-file-name file-name))
883 (unless (file-exists-p file-name)
884 (setq file-name nil))
885 (list (read-file-name
886 "Find to compare with: " ;; prompt
887 file-dir ;; directory
888 nil ;; default
889 nil ;; existing
890 file-name) ;; initial
892 (let* ((buf-a (current-buffer))
893 (file-a (buffer-file-name))
894 (existing-buf (get-file-buffer file-b))
895 (buf-b (or existing-buf
896 (find-file-noselect file-b))))
897 (highlight-markup-buffers buf-a file-a buf-b file-b (not existing-buf))
898 (unless existing-buf
899 (kill-buffer buf-b))
903 (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b)
904 ;; hilit-e,x,y are set by function hilit-chg-get-diff-list-hk.
905 (let (hilit-e hilit-x hilit-y)
906 (ediff-setup buf-a file-a buf-b file-b
907 nil nil ; buf-c file-C
908 '(hilit-chg-get-diff-list-hk)
909 (list (cons 'ediff-job-name 'something))
911 (ediff-with-current-buffer hilit-e (ediff-really-quit nil))
912 (list hilit-x hilit-y)))
915 (defun hilit-chg-get-diff-list-hk ()
916 ;; hilit-e/x/y are dynamically bound by hilit-chg-get-diff-info
917 ;; which calls this function as a hook.
918 (defvar hilit-x) ; placate the byte-compiler
919 (defvar hilit-y)
920 (defvar hilit-e)
921 (setq hilit-e (current-buffer))
922 (let ((n 0) extent p va vb a b)
923 (setq hilit-x nil hilit-y nil)
924 (while (< n ediff-number-of-differences)
925 (ediff-make-fine-diffs n)
926 (setq va (ediff-get-fine-diff-vector n 'A))
927 ;; va is a vector if there are fine differences
928 (if va
929 (setq a (append va nil))
930 ;; if not, get the unrefined difference
931 (setq va (ediff-get-difference n 'A))
932 (setq a (list (elt va 0))))
933 ;; a list a list
934 (setq p a)
935 (while p
936 (setq extent (list (overlay-start (car p))
937 (overlay-end (car p))))
938 (setq p (cdr p))
939 (setq hilit-x (append hilit-x (list extent) )));; while p
941 (setq vb (ediff-get-fine-diff-vector n 'B))
942 ;; vb is a vector
943 (if vb
944 (setq b (append vb nil))
945 ;; if not, get the unrefined difference
946 (setq vb (ediff-get-difference n 'B))
947 (setq b (list (elt vb 0))))
948 ;; b list a list
949 (setq p b)
950 (while p
951 (setq extent (list (overlay-start (car p))
952 (overlay-end (car p))))
953 (setq p (cdr p))
954 (setq hilit-y (append hilit-y (list extent) )))
955 (setq n (1+ n)));; while
956 ;; ediff-quit doesn't work here.
957 ;; No point in returning a value, since this is a hook function.
960 ;; ======================= global-highlight-changes-mode ==============
962 ;;;###autoload
963 (define-globalized-minor-mode global-highlight-changes-mode
964 highlight-changes-mode highlight-changes-mode-turn-on)
966 (define-obsolete-function-alias
967 'global-highlight-changes
968 'global-highlight-changes-mode "23.1")
970 (defun highlight-changes-mode-turn-on ()
971 "See if Highlight Changes mode should be turned on for this buffer.
972 This is called when `global-highlight-changes-mode' is turned on."
973 (or highlight-changes-mode ; do nothing if already on
975 (cond
976 ((null highlight-changes-global-modes)
977 nil)
978 ((functionp highlight-changes-global-modes)
979 (funcall highlight-changes-global-modes))
980 ((listp highlight-changes-global-modes)
981 (if (eq (car-safe highlight-changes-global-modes) 'not)
982 (not (memq major-mode (cdr highlight-changes-global-modes)))
983 (memq major-mode highlight-changes-global-modes)))
985 (and
986 (not (string-match "^[ *]" (buffer-name)))
987 (buffer-file-name))))
988 (highlight-changes-mode 1))
992 ;;;; Desktop support.
994 ;; Called by `desktop-create-buffer' to restore `highlight-changes-mode'.
995 (defun hilit-chg-desktop-restore (desktop-buffer-locals)
996 (highlight-changes-mode
997 (or (cdr (assq 'highlight-changes-mode desktop-buffer-locals)) 1)))
999 (add-to-list 'desktop-minor-mode-handlers
1000 '(highlight-changes-mode . hilit-chg-desktop-restore))
1002 (add-to-list 'desktop-locals-to-save 'highlight-changes-mode)
1004 ;; ===================== debug ==================
1005 ;; For debug & test use:
1007 ;; (defun hilit-chg-debug-show (&optional beg end)
1008 ;; (interactive)
1009 ;; (message "--- hilit-chg-debug-show ---")
1010 ;; (hilit-chg-map-changes (lambda (prop start end)
1011 ;; (message "%d-%d: %s" start end prop))
1012 ;; beg end
1013 ;; ))
1015 ;; ================== end of debug ===============
1017 (defun hilit-chg-unload-function ()
1018 "Unload the Highlight Changes library."
1019 (global-hi-lock-mode -1)
1020 ;; continue standard unloading
1021 nil)
1023 (provide 'hilit-chg)
1025 ;;; hilit-chg.el ends here