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