Commit | Line | Data |
---|---|---|
e287d328 RS |
1 | ;;; hilit-chg.el --- minor mode displaying buffer changes with special face |
2 | ||
3 | ;; Copyright (C) 1998 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Richard Sharman <rsharman@magma.ca> | |
6 | ;; Keywords: faces | |
7 | ||
66236b77 KH |
8 | ;; This file is part of GNU Emacs. |
9 | ||
e287d328 RS |
10 | ;; This program 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 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; This program 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. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
25 | ||
26 | ;;; Commentary: | |
27 | ||
28 | ;; A minor mode: "Highlight Changes mode". | |
29 | ;; | |
30 | ||
31 | ;; Highlight Changes mode has 2 submodes: active and passive. | |
32 | ;; When active, changes to the buffer are displayed in a different face. | |
33 | ;; When passive, any existing displayed changes are saved and new ones | |
34 | ;; recorded but are not displayed differently. | |
35 | ;; Why active and passive? Having the changes visible can be handy when you | |
36 | ;; want the information but very distracting otherwise. So, you can keep | |
37 | ;; Highlight Changes mode in passive state while you make your changes, toggle | |
38 | ;; it on to active mode to see them, then toggle it back off to avoid | |
39 | ;; distraction. | |
40 | ;; | |
41 | ;; When active, changes are displayed in `highlight-changes-face'. When | |
42 | ;; text is deleted, the following character is displayed in | |
43 | ;; `highlight-changes-delete-face' face. | |
44 | ;; | |
45 | ;; | |
46 | ;; You can "age" different sets of changes by using | |
47 | ;; `highlight-changes-rotate-faces'. This rotates different through a series | |
48 | ;; of different faces, so you can distinguish "new" changes from "older" | |
00e25b96 | 49 | ;; changes. You can customize these "rotated" faces in two ways. You can |
e287d328 RS |
50 | ;; either explicitly define each face by customizing |
51 | ;; `highlight-changes-face-list'. If, however, the faces differ from | |
52 | ;; `highlight-changes-face' only in the foreground colour, you can simply set | |
53 | ;; `highlight-changes-colours'. If `highlight-changes-face-list' is nil when | |
54 | ;; the faces are required they will be constructed from | |
55 | ;; `highlight-changes-colours'. | |
56 | ;; | |
57 | ;; | |
58 | ;; When a Highlight Changes mode is on (either active or passive) you can go | |
59 | ;; to the next or previous change with `highlight-changes-next-change' and | |
60 | ;; `highlight-changes-previous-change'. | |
61 | ;; | |
62 | ;; | |
99f08df4 KH |
63 | ;; You can also use the command highlight-compare-with-file to show changes |
64 | ;; in this file compared with another file (typically the previous version | |
65 | ;; of the file). | |
e287d328 RS |
66 | ;; |
67 | ;; | |
68 | ;; There are currently three hooks run by `highlight-changes-mode': | |
69 | ;; `highlight-changes-enable-hook' - is run when Highlight Changes mode | |
70 | ;; is initially enabled for a buffer. | |
71 | ;; `highlight-changes-disable-hook' - is run when Highlight Changes mode | |
72 | ;; is turned off. | |
73 | ;; `highlight-changes-toggle-hook' - is run each time `highlight-changes-mode' | |
74 | ;; is called. Typically this is when | |
75 | ;; toggling between active and passive | |
76 | ;; modes. The variable | |
77 | ;; `highlight-changes-mode' contains the new | |
78 | ;; state (`active' or `passive'.) | |
79 | ;; | |
80 | ;; | |
81 | ;; | |
82 | ;; Example usage: | |
83 | ;; (defun my-highlight-changes-enable-hook () | |
84 | ;; (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces) | |
85 | ;; ) | |
86 | ;; | |
87 | ;; (defun my-highlight-changes-disable-hook () | |
88 | ;; (remove-hook 'local-write-file-hooks 'highlight-changes-rotate-faces) | |
89 | ;; ) | |
90 | ;; | |
91 | ;; (add-hook 'highlight-changes-enable-hook 'my-highlight-changes-enable-hook) | |
92 | ;; (add-hook 'highlight-changes-disable-hook | |
93 | ;; 'my-highlight-changes-disable-hook) | |
94 | ||
95 | ||
96 | ;; Explciit vs. Implicit | |
97 | ;; | |
98 | ||
99 | ;; Normally, Highlight Changes mode is turned on explicitly in a buffer. | |
100 | ;; | |
101 | ;; If you prefer to have it automatically invoked you can do it as | |
102 | ;; follows. | |
103 | ;; | |
104 | ;; 1. Most modes have a major-hook, typically called MODE-hook. You | |
105 | ;; can use `add-hook' to call `highlight-changes-mode'. | |
106 | ;; | |
107 | ;; Example: | |
108 | ;; (add-hook 'c-mode-hook 'highlight-changes-mode) | |
109 | ;; | |
110 | ;; If you want to make it start up in passive mode (regardless of the | |
111 | ;; setting of highlight-changes-initial-state): | |
112 | ;; (add-hook 'emacs-lisp-mode-hook | |
113 | ;; (lambda () | |
114 | ;; (highlight-changes-mode 'passive))) | |
115 | ;; | |
116 | ;; However, this cannot be done for Fundamental mode for there is no | |
117 | ;; such hook. | |
118 | ;; | |
119 | ;; 2. You can use the function `global-highlight-changes' | |
120 | ;; | |
121 | ;; This function, which is fashioned after the way `global-font-lock' works, | |
122 | ;; toggles on or off global Highlight Changes mode. When activated, it turns | |
123 | ;; on Highlight Changes mode in all "suitable" existings buffers and will turn | |
124 | ;; it on in new "suitable" buffers to be created. | |
125 | ;; | |
126 | ;; A buffer's "suitability" is determined by variable | |
127 | ;; `highlight-changes-global-modes', as follows. If the variable is | |
128 | ;; * nil -- then no buffers are suitable; | |
129 | ;; * a function -- this function is called and the result is used. As | |
130 | ;; an example, if the value is 'buffer-file-name then all buffers | |
131 | ;; who are visiting files are suitable, but others (like dired | |
132 | ;; buffers) are not; | |
133 | ;; * a list -- then if the buufer is suitable iff its mode is in the | |
134 | ;; list, exccept if the first element is nil in which case the test | |
135 | ;; is reversed (i.e. it is a list of unsuitable modes). | |
136 | ;; * Otherwise, the buffer is suitable if its name does not begin with | |
137 | ;; ` ' or `*' and if `buffer-file-name' returns true. | |
138 | ;; | |
139 | ||
140 | ||
141 | ||
142 | ;; Possible bindings: | |
143 | ;; (global-set-key '[C-right] 'highlight-changes-next-change) | |
144 | ;; (global-set-key '[C-left] 'highlight-changes-previous-change) | |
145 | ;; | |
146 | ;; Other interactive functions (which could be bound if desired): | |
147 | ;; highlight-changes-mode | |
148 | ;; highlight-changes-remove-highlight | |
149 | ;; highlight-changes-rotate-faces | |
99f08df4 | 150 | ;; highlight-compare-with-file |
e287d328 RS |
151 | |
152 | ;; | |
153 | ;; You can automatically rotate faces when the buffer is saved; | |
154 | ;; see function `highlight-changes-rotate-faces' for how to do this. | |
155 | ;; | |
156 | ||
157 | ||
158 | ;;; Bugs: | |
159 | ||
160 | ;; - the next-change and previous-change functions are too literal; | |
161 | ;; they should find the next "real" change, in other words treat | |
162 | ;; consecutive changes as one. | |
163 | ||
164 | ||
165 | ;;; To do (maybe), notes, ... | |
166 | ||
167 | ;; - having different faces for deletion and non-deletion: is it | |
168 | ;; really worth the hassle? | |
169 | ;; - should have better hooks: when should they be run? | |
99f08df4 KH |
170 | ;; - highlight-compare-with-file should allow RCS files - e.g. nice to be |
171 | ;; able to say show changes compared with version 2.1. | |
e287d328 RS |
172 | ;; - Maybe we should have compare-with-buffer as well. (When I tried |
173 | ;; a while back I ran into a problem with ediff-buffers-internal.) | |
174 | ||
175 | ||
176 | ;;; History: | |
177 | ||
178 | ;; R Sharman (rsharman@magma.ca) Feb 1998: | |
179 | ;; - initial release as change-mode. | |
e287d328 RS |
180 | ;; Jari Aalto <jari.aalto@ntc.nokia.com> Mar 1998 |
181 | ;; - fixes for byte compile errors | |
182 | ;; - use eval-and-compile for autoload | |
183 | ;; Marijn Ros <J.M.Ros@fys.ruu.nl> Mar 98 | |
184 | ;; - suggested turning it on by default | |
e287d328 RS |
185 | ;; Eric Ludlam <zappo@gnu.org> Suggested using overlays. |
186 | ;; July 98 | |
187 | ;; - global mode and various stuff added | |
188 | ;; - Changed to use overlays | |
189 | ;; August 98 | |
190 | ;; - renmaed to Highlight Changes mode. | |
191 | ||
192 | ||
193 | ;;; Code: | |
194 | ||
e287d328 RS |
195 | (require 'wid-edit) |
196 | ||
197 | ;; ====================== Customization ======================= | |
198 | (defgroup highlight-changes nil | |
199 | "Highlight Changes mode." | |
200 | :group 'faces) | |
201 | ||
202 | ||
203 | ;; Face information: How the changes appear. | |
204 | ||
205 | ;; Defaults for face: red foreground, no change to background, | |
206 | ;; and underlined if a change is because of a deletion. | |
207 | ;; Note: underlining is helpful in that is shows up changes in white space. | |
208 | ;; However, having it set for non-delete changes can be annoying because all | |
209 | ;; indentation on inserts gets underlined (which can look pretty ugly!). | |
210 | ||
211 | (defface highlight-changes-face | |
212 | '((((class color)) (:foreground "red" )) | |
213 | (t (:inverse-video t))) | |
214 | "Face used for highlighting changes." | |
215 | :group 'highlight-changes | |
216 | ) | |
217 | ||
218 | ;; This looks pretty ugly, actually. Maybe the underline should be removed. | |
219 | (defface highlight-changes-delete-face | |
220 | '((((class color)) (:foreground "red" :underline t)) | |
221 | (t (:inverse-video t))) | |
222 | "Face used for highlighting deletions." | |
223 | :group 'highlight-changes | |
224 | ) | |
225 | ||
226 | ||
227 | ||
228 | ;; A (not very good) default list of colours to rotate through. | |
229 | ;; | |
230 | (defcustom highlight-changes-colours | |
231 | (if (eq (frame-parameter nil 'background-mode) 'light) | |
232 | ;; defaults for light background: | |
233 | '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue") | |
234 | ;; defaults for dark background: | |
235 | '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid")) | |
236 | "*Colours used by `highlight-changes-rotate-faces'. | |
237 | The newest rotated change will be displayed in the first element of this list, | |
238 | the next older will be in the second element etc. | |
239 | ||
240 | This list is used if `highlight-changes-face-list' is nil, otherwise that | |
241 | variable overrides this list. If you only care about foreground | |
242 | colours then use this, if you want fancier faces then set | |
243 | `highlight-changes-face-list'." | |
244 | :type '(repeat color) | |
245 | :group 'highlight-changes | |
246 | ) | |
247 | ||
248 | ||
249 | ;; If you invoke highlight-changes-mode with no argument, should it start in | |
250 | ;; active or passive mode? | |
251 | ;; | |
252 | (defcustom highlight-changes-initial-state 'active | |
253 | "*What state (active or passive) `highlight-changes' should start in. | |
254 | This is used when `highlight-changes' is called with no argument. | |
255 | This variable must be set to one of the symbols `active' or `passive'." | |
256 | :type '(choice (const :tag "Active" active) | |
257 | (const :tag "Passive" passive)) | |
258 | :group 'highlight-changes | |
259 | ) | |
260 | ||
261 | (defcustom highlight-changes-global-initial-state 'passive | |
262 | "*What state `global-highlight-changes' should start in. | |
263 | This is used if `global-highlight-changes' is called with no argument. | |
264 | This variable must be set to either `active' or `passive'" | |
265 | :type '(choice (const :tag "Active" active) | |
266 | (const :tag "Passive" passive)) | |
267 | :group 'highlight-changes | |
268 | ) | |
269 | ||
270 | ;; The strings displayed in the mode-line for the minor mode: | |
271 | (defcustom highlight-changes-active-string nil | |
272 | "*The string used when Highlight Changes mode is in the active state. | |
273 | This should be set to nil if no indication is desired, or to | |
274 | a string with a leading space." | |
275 | :type '(choice string | |
276 | (const :tag "None" nil)) | |
277 | :group 'highlight-changes | |
278 | ) | |
279 | ||
280 | (defcustom highlight-changes-passive-string " Chg" | |
281 | "*The string used when Highlight Changes mode is in the passive state. | |
282 | This should be set to nil if no indication is desired, or to | |
283 | a string with a leading space." | |
284 | :type '(choice string | |
285 | (const :tag "None" nil)) | |
286 | :group 'highlight-changes | |
287 | ) | |
288 | ||
289 | (defcustom highlight-changes-global-modes t | |
290 | "*Determine whether a buffer is suitable for global Highlight Changes mode. | |
291 | ||
292 | A function means that function is called: if it returns non-nil the | |
293 | buffer is suitable. | |
294 | ||
295 | A list is a list of modes for which it is suitable, or a list whose | |
296 | first element is 'not followed by modes which are not suitable. | |
297 | ||
298 | t means the buffer is suitable if its name does not begin with ` ' nor | |
299 | `*' and the buffer has a filename. | |
300 | ||
301 | nil means no buffers are suitable for `global-highlight-changes' | |
302 | (effectively disabling the mode). | |
303 | ||
304 | Examples: | |
305 | (c-mode c++-mode) | |
306 | means that Highlight Changes mode is turned on for buffers in C and C++ | |
307 | modes only." | |
308 | :type '(choice | |
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 | ) | |
318 | :group 'highlight-changes | |
319 | ) | |
320 | ||
321 | ||
322 | (defvar global-highlight-changes nil) | |
323 | ||
324 | (defcustom highlight-changes-global-changes-existing-buffers nil | |
325 | "*If non-nil toggling global Highlight Changes mode affects existing buffers. | |
326 | Normally, `global-highlight-changes' means affects only new buffers (to be | |
327 | created). However, if highlight-changes-global-changes-existing-buffers | |
328 | is non-nil then turning on `global-highlight-changes' will turn on | |
329 | highlight-changes-mode in suitable buffers and turning the mode off will | |
330 | remove it from existing buffers." | |
331 | :type 'boolean | |
332 | :group 'highlight-changes) | |
333 | ||
334 | (defun hilit-chg-cust-fix-changes-face-list (w wc &optional event) | |
335 | ;; When customization function `highlight-changes-face-list' inserts a new | |
336 | ;; face it uses the default face. We don't want the user to modify this | |
337 | ;; face, so we rename the faces in the list on an insert. The rename is | |
338 | ;; actually done by copying the faces so user-defined faces still remain | |
339 | ;; in the same order. | |
340 | ;; The notifying the parent is needed because without it changes to the | |
341 | ;; faces are saved but not to the actual list itself. | |
342 | (let ((old-list (widget-value w))) | |
343 | (if (member 'default old-list) | |
344 | (let | |
345 | ((p (reverse old-list)) | |
346 | (n (length old-list)) | |
347 | new-name old-name | |
348 | (new-list nil) | |
349 | ) | |
350 | (while p | |
351 | (setq old-name (car p)) | |
352 | (setq new-name (intern (format "highlight-changes-face-%d" n))) | |
353 | (if (eq old-name new-name) | |
354 | nil | |
355 | ;; A new face has been inserted: we don't want to modify the | |
356 | ;; default face so copy it. Better, though, (I think) is to | |
357 | ;; make a new face have the same attributes as | |
358 | ;; highlight-changes-face . | |
359 | (if (eq old-name 'default) | |
360 | (copy-face 'highlight-changes-face new-name) | |
361 | (copy-face old-name new-name) | |
362 | )) | |
363 | (setq new-list (append (list new-name) new-list)) | |
364 | (setq n (1- n)) | |
365 | (setq p (cdr p))) | |
366 | (if (equal new-list (widget-value w)) | |
367 | nil ;; (message "notify: no change!") | |
368 | (widget-value-set w new-list) | |
369 | (widget-setup) | |
370 | ) | |
371 | ) | |
372 | ;; (message "notify: no default here!") | |
373 | )) | |
374 | (let ((parent (widget-get w :parent))) | |
375 | (when parent | |
376 | (widget-apply parent :notify w event))) | |
377 | ) | |
378 | ||
379 | ||
380 | (defcustom highlight-changes-face-list nil | |
381 | "*A list of faces used when rotatating changes. | |
382 | Normally the variable is initialized to nil and the list is created from | |
383 | `highlight-changes-colours' when needed. However, you can set this variable | |
384 | to any list of faces. You will have to do this if you want faces which | |
385 | don't just differ from `highlight-changes-face' by the foreground colour. | |
386 | Otherwise, this list will be constructed when needed from | |
387 | `highlight-changes-colours'." | |
388 | :type '(choice | |
389 | (repeat | |
390 | :notify hilit-chg-cust-fix-changes-face-list | |
391 | face ) | |
392 | (const :tag "Derive from highlight-changes-colours" nil) | |
393 | ) | |
394 | :group 'highlight-changes | |
395 | ) | |
396 | ||
397 | ;; ======================================================================== | |
398 | ||
399 | ;; These shouldn't be changed! | |
400 | ||
401 | (defvar highlight-changes-mode nil) | |
402 | (defvar hilit-chg-list nil) | |
403 | (defvar hilit-chg-string " ??") | |
404 | (or (assq 'highlight-changes-mode minor-mode-alist) | |
405 | (setq minor-mode-alist | |
406 | (cons '(highlight-changes-mode hilit-chg-string) minor-mode-alist) | |
407 | )) | |
408 | (make-variable-buffer-local 'highlight-changes-mode) | |
409 | (make-variable-buffer-local 'hilit-chg-string) | |
410 | ||
411 | ||
412 | ||
413 | (eval-and-compile | |
99f08df4 | 414 | ;; For highlight-compare-with-file |
e287d328 RS |
415 | (defvar ediff-number-of-differences) |
416 | (autoload 'ediff-setup "ediff") | |
417 | (autoload 'ediff-with-current-buffer "ediff") | |
418 | (autoload 'ediff-really-quit "ediff") | |
419 | (autoload 'ediff-make-fine-diffs "ediff") | |
420 | (autoload 'ediff-get-fine-diff-vector "ediff") | |
421 | (autoload 'ediff-get-difference "ediff") | |
422 | ) | |
423 | ||
424 | ||
425 | ||
426 | ;;; Functions... | |
427 | ||
428 | (defun hilit-chg-map-changes (func &optional start-position end-position) | |
429 | "Call function FUNC for each region used by Highlight Changes mode." | |
430 | ;; if start-position is nil, (point-min) is used | |
431 | ;; if end-position is nil, (point-max) is used | |
432 | ;; FUNC is called with 3 params: property start stop | |
433 | (let ((start (or start-position (point-min))) | |
434 | (limit (or end-position (point-max))) | |
435 | prop end) | |
436 | (while (and start (< start limit)) | |
437 | (setq prop (get-text-property start 'hilit-chg)) | |
438 | (setq end (text-property-not-all start limit 'hilit-chg prop)) | |
439 | (if prop | |
440 | (funcall func prop start (or end limit))) | |
441 | (setq start end) | |
442 | ))) | |
443 | ||
444 | ||
445 | (defun hilit-chg-display-changes (&optional beg end) | |
446 | "Display face information for Highlight Changes mode. | |
447 | ||
448 | An overlay containing a change face is added, from the information | |
449 | in the text property of type change. | |
450 | ||
451 | This is the opposite of hilit-chg-hide-changes." | |
452 | (hilit-chg-map-changes 'hilit-chg-make-ov beg end)) | |
453 | ||
454 | ||
455 | (defun hilit-chg-make-ov (prop start end) | |
456 | ;; for the region make change overlays corresponding to | |
457 | ;; the text property 'hilit-chg | |
458 | (let ((ov (make-overlay start end)) | |
459 | face) | |
460 | (or prop | |
461 | (error "hilit-chg-make-ov: prop is nil")) | |
462 | (if (eq prop 'hilit-chg-delete) | |
463 | (setq face 'highlight-changes-delete-face) | |
464 | (setq face (nth 1 (member prop hilit-chg-list)))) | |
465 | (if face | |
466 | (progn | |
467 | ;; We must mark the face, that is the purpose of the overlay | |
468 | (overlay-put ov 'face face) | |
469 | ;; I don't think we need to set evaporate since we should | |
470 | ;; be controlling them! | |
471 | (overlay-put ov 'evaporate t) | |
472 | ;; We set the change property so we can tell this is one | |
473 | ;; of our overlays (so we don't delete someone else's). | |
474 | (overlay-put ov 'hilit-chg t) | |
475 | ) | |
476 | (error "hilit-chg-make-ov: no face for prop: %s" prop) | |
477 | ) | |
478 | )) | |
479 | ||
480 | (defun hilit-chg-hide-changes (&optional beg end) | |
481 | "Remove face information for Highlight Changes mode. | |
482 | ||
483 | The overlay containing the face is removed, but the text property | |
484 | containing the change information is retained. | |
485 | ||
486 | This is the opposite of hilit-chg-display-changes." | |
487 | (let ((start (or beg (point-min))) | |
488 | (limit (or end (point-max))) | |
489 | p ov) | |
490 | (setq p (overlays-in start limit)) | |
491 | (while p | |
492 | ;; don't delete the overlay if it isn't ours! | |
493 | (if (overlay-get (car p) 'hilit-chg) | |
494 | (delete-overlay (car p))) | |
495 | (setq p (cdr p)) | |
496 | ))) | |
497 | ||
498 | (defun hilit-chg-fixup (beg end) | |
499 | "Fix change overlays in region beg .. end. | |
500 | ||
501 | Ensure the overlays agree with the changes as determined from | |
502 | the text properties of type `hilit-chg' ." | |
503 | ;; Remove or alter overlays in region beg..end | |
504 | (let (p ov ov-start ov-end | |
505 | props q) | |
506 | (setq p (overlays-in beg end)) | |
507 | ;; temp for debugging: | |
508 | ;; (or (eq highlight-changes-mode 'active) | |
509 | ;; (error "hilit-chg-fixup called but Highlight Changes mode not active")) | |
510 | (while p | |
511 | (setq ov (car p)) | |
512 | (setq ov-start (overlay-start ov)) | |
513 | (setq ov-end (overlay-end ov)) | |
514 | (if (< ov-start beg) | |
515 | (progn | |
516 | (move-overlay ov ov-start beg) | |
517 | (if (> ov-end end) | |
518 | (progn | |
519 | (setq props (overlay-properties ov)) | |
520 | (setq ov (make-overlay end ov-end)) | |
521 | (while props | |
522 | (overlay-put ov (car props)(car (cdr props))) | |
523 | (setq props (cdr (cdr props)))) | |
524 | ) | |
525 | ) | |
526 | ) | |
527 | (if (> ov-end end) | |
528 | (move-overlay ov end ov-end) | |
529 | (delete-overlay ov) | |
530 | )) | |
531 | (setq p (cdr p))) | |
532 | (hilit-chg-display-changes beg end) | |
533 | )) | |
534 | ||
535 | ||
536 | ||
537 | ||
538 | ||
539 | ||
540 | ;;;###autoload | |
541 | (defun highlight-changes-remove-highlight (beg end) | |
542 | "Remove the change face from the region. | |
543 | This allows you to manually remove highlighting from uninteresting changes." | |
544 | (interactive "r") | |
545 | (let ((after-change-functions nil)) | |
546 | (remove-text-properties beg end '(hilit-chg nil)) | |
547 | (hilit-chg-fixup beg end))) | |
548 | ||
549 | (defun hilit-chg-set-face-on-change (beg end leng-before | |
550 | &optional no-proerty-change) | |
551 | "Record changes and optionally display them in a distinctive face. | |
552 | `hilit-chg-set' adds this function to the `after-change-functions' hook." | |
553 | ;; | |
554 | ;; This function is called by the `after-change-functions' hook, which | |
555 | ;; is how we are notified when text is changed. | |
99f08df4 | 556 | ;; It is also called from `highlight-compare-with-file'. |
e287d328 RS |
557 | ;; |
558 | ;; We do NOT want to simply do this if this is an undo command, because | |
559 | ;; otherwise an undone change shows up as changed. While the properties | |
560 | ;; are automatically restored by undo, we must fixup the overlay. | |
561 | (save-match-data | |
562 | (let ((beg-decr 1) (end-incr 1) | |
563 | (type 'hilit-chg) | |
564 | old) | |
565 | (if undo-in-progress | |
566 | (if (eq highlight-changes-mode 'active) | |
567 | (hilit-chg-fixup beg end)) | |
568 | (if (and (= beg end) (> leng-before 0)) | |
569 | ;; deletion | |
570 | (progn | |
571 | ;; The eolp and bolp tests are a kludge! But they prevent | |
572 | ;; rather nasty looking displays when deleting text at the end | |
573 | ;; of line, such as normal corrections as one is typing and | |
574 | ;; immediately makes a corrections, and when deleting first | |
575 | ;; character of a line. | |
576 | ;;; (if (= leng-before 1) | |
577 | ;;; (if (eolp) | |
578 | ;;; (setq beg-decr 0 end-incr 0) | |
579 | ;;; (if (bolp) | |
580 | ;;; (setq beg-decr 0)))) | |
581 | ;;; (setq beg (max (- beg beg-decr) (point-min))) | |
582 | (setq end (min (+ end end-incr) (point-max))) | |
583 | (setq type 'hilit-chg-delete)) | |
584 | ;; Not a deletion. | |
585 | ;; Most of the time the following is not necessary, but | |
586 | ;; if the current text was marked as a deletion then | |
587 | ;; the old overlay is still in effect, so if we add some | |
588 | ;; text then remove the deletion marking, but set it to | |
589 | ;; changed otherwise its highlighting disappears. | |
590 | (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete) | |
591 | (progn | |
592 | (remove-text-properties end (+ end 1) '(hilit-chg nil)) | |
593 | (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg) | |
594 | (if (eq highlight-changes-mode 'active) | |
595 | (hilit-chg-fixup beg (+ end 1)))))) | |
596 | (unless no-proerty-change | |
597 | (put-text-property beg end 'hilit-chg type)) | |
598 | (if (or (eq highlight-changes-mode 'active) no-proerty-change) | |
599 | (hilit-chg-make-ov type beg end)) | |
600 | )))) | |
601 | ||
602 | ||
603 | ||
604 | ||
605 | ||
606 | (defun hilit-chg-set (value) | |
607 | "Turn on Highlight Changes mode for this buffer." | |
608 | (setq highlight-changes-mode value) | |
609 | (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t) | |
610 | (hilit-chg-make-list) | |
611 | (if (eq highlight-changes-mode 'active) | |
612 | (progn | |
613 | (setq hilit-chg-string highlight-changes-active-string) | |
614 | (or buffer-read-only | |
615 | (hilit-chg-display-changes))) | |
616 | ;; mode is passive | |
617 | (setq hilit-chg-string highlight-changes-passive-string) | |
618 | (or buffer-read-only | |
619 | (hilit-chg-hide-changes)) | |
620 | ) | |
621 | (force-mode-line-update) | |
622 | (make-local-hook 'after-change-functions) | |
623 | (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t) | |
624 | ) | |
625 | ||
626 | (defun hilit-chg-clear () | |
627 | "Remove Highlight Changes mode for this buffer. | |
628 | This removes all saved change information." | |
629 | (if buffer-read-only | |
630 | ;; We print the buffer name because this function could be called | |
631 | ;; on many buffers from `global-highlight-changes'. | |
632 | (message "Cannot remove highlighting from read-only mode buffer %s" | |
633 | (buffer-name)) | |
634 | (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t) | |
635 | (let ((after-change-functions nil)) | |
636 | (hilit-chg-hide-changes) | |
637 | (hilit-chg-map-changes | |
638 | '(lambda (prop start stop) | |
639 | (remove-text-properties start stop '(hilit-chg nil)))) | |
640 | ) | |
641 | (setq highlight-changes-mode nil) | |
642 | (force-mode-line-update) | |
643 | ;; If we type: C-u -1 M-x highlight-changes-mode | |
644 | ;; we want to turn it off, but hilit-chg-post-command-hook | |
645 | ;; runs and that turns it back on! | |
646 | (remove-hook 'post-command-hook 'hilit-chg-post-command-hook) | |
647 | )) | |
648 | ||
649 | ;;;###autoload | |
650 | (defun highlight-changes-mode (&optional arg) | |
651 | "Toggle (or initially set) Highlight Changes mode. | |
652 | ||
653 | Without an argument, | |
654 | if Highlight Changes mode is not enabled, then enable it (to either active | |
655 | or passive as determined by variable highlight-changes-initial-state); | |
656 | otherwise, toggle between active and passive states. | |
657 | ||
658 | With an argument, | |
659 | if just C-u or a positive argument, set state to active; | |
660 | with a zero argument, set state to passive; | |
661 | with a negative argument, disable Highlight Changes mode completely. | |
662 | ||
663 | Active state - means changes are shown in a distinctive face. | |
664 | Passive state - means changes are kept and new ones recorded but are | |
665 | not displayed in a different face. | |
666 | ||
667 | Functions: | |
668 | \\[highlight-changes-next-change] - move point to beginning of next change | |
669 | \\[highlight-changes-previous-change] - move to beginning of previous change | |
99f08df4 KH |
670 | \\[highlight-compare-with-file] - mark text as changed by comparing this |
671 | buffer with the contents of a file | |
e287d328 RS |
672 | \\[highlight-changes-remove-highlight] - remove the change face from the region |
673 | \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes \ | |
674 | through | |
675 | various faces. | |
676 | ||
677 | ||
678 | Hook variables: | |
679 | highlight-changes-enable-hook - when Highlight Changes mode enabled. | |
680 | highlight-changes-toggle-hook - when entering active or passive state | |
681 | highlight-changes-disable-hook - when turning off Highlight Changes mode. | |
682 | " | |
683 | (interactive "P") | |
684 | (if window-system | |
685 | (let ((was-on highlight-changes-mode) | |
686 | (new-highlight-changes-mode | |
687 | (cond | |
688 | ((null arg) | |
689 | ;; no arg => toggle (or set to active initially) | |
690 | (if highlight-changes-mode | |
691 | (if (eq highlight-changes-mode 'active) 'passive 'active) | |
692 | highlight-changes-initial-state)) | |
693 | ;; an argument is given | |
694 | ((eq arg 'active) | |
695 | 'active) | |
696 | ((eq arg 'passive) | |
697 | 'passive) | |
698 | ((> (prefix-numeric-value arg) 0) | |
699 | 'active) | |
700 | ((< (prefix-numeric-value arg) 0) | |
701 | nil) | |
702 | (t | |
703 | 'passive) | |
704 | ))) | |
705 | (if new-highlight-changes-mode | |
706 | ;; mode is turned on -- but may be passive | |
707 | (progn | |
708 | (hilit-chg-set new-highlight-changes-mode) | |
709 | (or was-on | |
710 | ;; run highlight-changes-enable-hook once | |
711 | (run-hooks 'highlight-changes-enable-hook)) | |
712 | (run-hooks 'highlight-changes-toggle-hook)) | |
713 | ;; mode is turned off | |
714 | (run-hooks 'highlight-changes-disable-hook) | |
715 | (hilit-chg-clear)) | |
716 | ) | |
717 | (message "Highlight Changes mode only works when using a window system")) | |
718 | ) | |
719 | ||
720 | ||
721 | ||
722 | ;;;###autoload | |
723 | (defun highlight-changes-next-change () | |
724 | "Move to the beginning of the next change, if in Highlight Changes mode." | |
725 | (interactive) | |
726 | (if highlight-changes-mode | |
727 | (let ((start (point)) | |
728 | prop) | |
729 | (setq prop (get-text-property (point) 'hilit-chg)) | |
730 | (if prop | |
731 | ;; we are in a change | |
732 | (setq start (next-single-property-change (point) 'hilit-chg))) | |
733 | (if start | |
734 | (setq start (next-single-property-change start 'hilit-chg))) | |
735 | (if start | |
736 | (goto-char start) | |
737 | (message "no next change"))) | |
738 | (message "This buffer is not in Highlight Changes mode."))) | |
739 | ||
740 | ||
741 | ;;;###autoload | |
742 | (defun highlight-changes-previous-change () | |
743 | "Move to the beginning of the previous change, if in Highlight Changes mode." | |
744 | (interactive) | |
745 | (if highlight-changes-mode | |
746 | (let ( (start (point)) (prop nil) ) | |
747 | (or (bobp) | |
748 | (setq prop (get-text-property (1- (point)) 'hilit-chg))) | |
749 | (if prop | |
750 | ;; we are in a change | |
751 | (setq start (previous-single-property-change (point) 'hilit-chg))) | |
752 | (if start | |
753 | (setq start (previous-single-property-change start 'hilit-chg))) | |
754 | ;; special handling for the case where (point-min) is a change | |
755 | (if start | |
756 | (setq start (or (previous-single-property-change start 'hilit-chg) | |
757 | (if (get-text-property (point-min) 'hilit-chg) | |
758 | (point-min))))) | |
759 | (if start | |
760 | (goto-char start) | |
761 | (message "no previous change"))) | |
762 | (message "This buffer is not in Highlight Changes mode."))) | |
763 | ||
764 | ||
765 | ;; ======================================================================== | |
766 | ||
767 | ||
768 | (defun hilit-chg-make-list (&optional force) | |
769 | "Construct hilit-chg-list and highlight-changes-face-list." | |
770 | ;; Constructs highlight-changes-face-list if necessary, | |
771 | ;; and hilit-chg-list always: | |
772 | ;; Maybe this should always be called when rotating a face | |
773 | ;; so we pick up any changes? | |
774 | (if (or (null highlight-changes-face-list) ; Don't do it if it | |
775 | force) ; already exists unless FORCE non-nil. | |
776 | (let ((p highlight-changes-colours) | |
777 | (n 1) name) | |
778 | (setq highlight-changes-face-list nil) | |
779 | (while p | |
780 | (setq name (intern (format "highlight-changes-face-%d" n))) | |
781 | (copy-face 'highlight-changes-face name) | |
782 | (set-face-foreground name (car p)) | |
783 | (setq highlight-changes-face-list | |
784 | (append highlight-changes-face-list (list name))) | |
785 | (setq p (cdr p)) | |
786 | (setq n (1+ n))))) | |
787 | (setq hilit-chg-list (list 'hilit-chg 'highlight-changes-face)) | |
788 | (let ((p highlight-changes-face-list) | |
789 | (n 1) | |
790 | last-category last-face) | |
791 | (while p | |
792 | (setq last-category (intern (format "change-%d" n))) | |
793 | ;; (setq last-face (intern (format "highlight-changes-face-%d" n))) | |
794 | (setq last-face (car p)) | |
795 | (setq hilit-chg-list | |
796 | (append hilit-chg-list | |
797 | (list last-category last-face))) | |
798 | (setq p (cdr p)) | |
799 | (setq n (1+ n))) | |
800 | (setq hilit-chg-list | |
801 | (append hilit-chg-list | |
802 | (list last-category last-face))) | |
803 | )) | |
804 | ||
805 | ||
806 | (defun hilit-chg-bump-change (prop start end) | |
807 | "Increment (age) the Highlight Changes mode text property of type change." | |
808 | (let ( new-prop ) | |
809 | (if (eq prop 'hilit-chg-delete) | |
810 | (setq new-prop (nth 2 hilit-chg-list)) | |
811 | (setq new-prop (nth 2 (member prop hilit-chg-list))) | |
812 | ) | |
813 | (if prop | |
814 | (put-text-property start end 'hilit-chg new-prop) | |
815 | (message "%d-%d unknown property %s not changed" start end prop) | |
816 | ) | |
817 | )) | |
818 | ||
819 | ;;;###autoload | |
820 | (defun highlight-changes-rotate-faces () | |
821 | "Rotate the faces used by Highlight Changes mode. | |
822 | ||
823 | Current changes will be display in the face described by the first element | |
824 | of highlight-changes-face-list, those (older) changes will be shown in the | |
825 | face described by the second element, and so on. Very old changes remain | |
826 | shown in the last face in the list. | |
827 | ||
828 | You can automatically rotate colours when the buffer is saved | |
829 | by adding this to local-write-file-hooks, by evaling (in the | |
830 | buffer to be saved): | |
831 | (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces) | |
832 | " | |
833 | (interactive) | |
834 | ;; If not in active mode do nothing but don't complain because this | |
835 | ;; may be bound to a hook. | |
836 | (if (eq highlight-changes-mode 'active) | |
837 | (let ((after-change-functions nil)) | |
838 | ;; ensure hilit-chg-list is made and up to date | |
839 | (hilit-chg-make-list) | |
840 | ;; remove our existing overlays | |
841 | (hilit-chg-hide-changes) | |
842 | ;; for each change text property, increment it | |
843 | (hilit-chg-map-changes 'hilit-chg-bump-change) | |
844 | ;; and display them all if active | |
845 | (if (eq highlight-changes-mode 'active) | |
846 | (hilit-chg-display-changes)) | |
847 | )) | |
848 | ;; This always returns nil so it is safe to use in | |
849 | ;; local-write-file-hook | |
850 | nil) | |
851 | ||
852 | ||
853 | ;; ======================================================================== | |
854 | ;; Comparing with an existing file. | |
855 | ;; This uses ediff to find the differences. | |
856 | ||
857 | ;;;###autoload | |
b32e3ef8 KH |
858 | (defun highlight-compare-with-file (file-b) |
859 | "Compare this buffer with a file, and highlight differences. | |
e287d328 RS |
860 | |
861 | The current buffer must be an unmodified buffer visiting a file, | |
862 | and not in read-only mode. | |
863 | ||
864 | If the backup filename exists, it is used as the default | |
865 | when called interactively. | |
866 | ||
867 | If a buffer is visiting the file being compared against, it also will | |
868 | have its differences highlighted. Otherwise, the file is read in | |
869 | temporarily but the buffer is deleted. | |
870 | ||
871 | If a buffer is read-only, differences will be highlighted but no property | |
872 | changes made, so \\[highlight-changes-next-change] and | |
873 | \\[highlight-changes-previous-change] will not work." | |
874 | (interactive (list | |
875 | (read-file-name | |
876 | "File to compare with? " ;; prompt | |
877 | "" ;; directory | |
878 | nil ;; default | |
879 | 'yes ;; must exist | |
880 | (let ((f (make-backup-file-name | |
881 | (or (buffer-file-name (current-buffer)) | |
882 | (error "no file for this buffer"))))) | |
883 | (if (file-exists-p f) f "")) | |
884 | ))) | |
885 | ||
886 | (let* ((buf-a (current-buffer)) | |
887 | (buf-a-read-only buffer-read-only) | |
888 | (orig-pos (point)) | |
889 | (file-a (buffer-file-name)) | |
890 | (existing-buf (get-file-buffer file-b)) | |
891 | (buf-b (or existing-buf | |
892 | (find-file-noselect file-b))) | |
893 | (buf-b-read-only (with-current-buffer buf-b buffer-read-only)) | |
894 | xy xx yy p q | |
895 | a-start a-end len-a | |
896 | b-start b-end len-b | |
897 | ) | |
898 | ||
899 | ;; We use the fact that the buffer is not marked modified at the | |
900 | ;; end where we clear its modified status | |
901 | (if (buffer-modified-p buf-a) | |
902 | (if (y-or-n-p (format "OK to save %s? " file-a)) | |
903 | (save-buffer buf-a) | |
904 | (error "Buffer must be saved before comparing with a file."))) | |
905 | (if (and existing-buf (buffer-modified-p buf-b)) | |
906 | (if (y-or-n-p (format "OK to save %s? " file-b)) | |
907 | (save-buffer buf-b) | |
908 | (error "Cannot compare with a file in an unsaved buffer."))) | |
909 | (highlight-changes-mode 'active) | |
910 | (if existing-buf (with-current-buffer buf-b | |
911 | (highlight-changes-mode 'active))) | |
912 | (save-window-excursion | |
913 | (setq xy (hilit-chg-get-diff-info buf-a file-a buf-b file-b))) | |
914 | (setq xx (car xy)) | |
915 | (setq p xx) | |
916 | (setq yy (car (cdr xy))) | |
917 | (setq q yy) | |
918 | (hilit-chg-make-list) | |
919 | (while p | |
920 | (setq a-start (nth 0 (car p))) | |
921 | (setq a-end (nth 1 (car p))) | |
922 | (setq b-start (nth 0 (car q))) | |
923 | (setq b-end (nth 1 (car q))) | |
924 | (setq len-a (- a-end a-start)) | |
925 | (setq len-b (- b-end b-start)) | |
926 | (set-buffer buf-a) | |
927 | (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only) | |
928 | (set-buffer-modified-p nil) | |
929 | (goto-char orig-pos) | |
930 | (if existing-buf | |
931 | (with-current-buffer buf-b | |
932 | (hilit-chg-set-face-on-change b-start b-end len-a | |
933 | buf-b-read-only ) | |
934 | )) | |
935 | (setq p (cdr p)) | |
936 | (setq q (cdr q)) | |
937 | ) | |
938 | (if existing-buf | |
939 | (set-buffer-modified-p nil) | |
940 | (kill-buffer buf-b)) | |
941 | )) | |
942 | ||
943 | ||
944 | (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b) | |
945 | (let ((e nil) x y) ;; e is set by function hilit-chg-get-diff-list-hk | |
946 | (ediff-setup buf-a file-a buf-b file-b | |
947 | nil nil ; buf-c file-C | |
948 | 'hilit-chg-get-diff-list-hk | |
949 | (list (cons 'ediff-job-name 'something)) | |
950 | ) | |
951 | (ediff-with-current-buffer e (ediff-really-quit nil)) | |
952 | (list x y))) | |
953 | ||
954 | ||
955 | (defun hilit-chg-get-diff-list-hk () | |
956 | ;; x and y are dynamically bound by hilit-chg-get-diff-info | |
957 | ;; which calls this function as a hook | |
958 | (defvar x) ;; placate the byte-compiler | |
959 | (defvar y) | |
960 | (setq e (current-buffer)) | |
961 | (let ((n 0) extent p va vb a b) | |
962 | (setq x nil y nil) ;; x and y are bound by hilit-chg-get-diff-info | |
963 | (while (< n ediff-number-of-differences) | |
964 | (ediff-make-fine-diffs n) | |
965 | (setq va (ediff-get-fine-diff-vector n 'A)) | |
966 | ;; va is a vector if there are fine differences | |
967 | (if va | |
968 | (setq a (append va nil)) | |
969 | ;; if not, get the unrefined difference | |
970 | (setq va (ediff-get-difference n 'A)) | |
971 | (setq a (list (elt va 0))) | |
972 | ) | |
973 | ;; a list a list | |
974 | (setq p a) | |
975 | (while p | |
976 | (setq extent (list (overlay-start (car p)) | |
977 | (overlay-end (car p)))) | |
978 | (setq p (cdr p)) | |
979 | (setq x (append x (list extent) )) | |
980 | );; while p | |
981 | ;; | |
982 | (setq vb (ediff-get-fine-diff-vector n 'B)) | |
983 | ;; vb is a vector | |
984 | (if vb | |
985 | (setq b (append vb nil)) | |
986 | ;; if not, get the unrefined difference | |
987 | (setq vb (ediff-get-difference n 'B)) | |
988 | (setq b (list (elt vb 0))) | |
989 | ) | |
990 | ;; b list a list | |
991 | (setq p b) | |
992 | (while p | |
993 | (setq extent (list (overlay-start (car p)) | |
994 | (overlay-end (car p)))) | |
995 | (setq p (cdr p)) | |
996 | (setq y (append y (list extent) )) | |
997 | );; while p | |
998 | ;; | |
999 | (setq n (1+ n)) | |
1000 | );; while | |
1001 | ;; ediff-quit doesn't work here. | |
1002 | ;; No point in returning a value, since this is a hook function. | |
1003 | )) | |
1004 | ||
1005 | ;; ======================= automatic stuff ============== | |
1006 | ||
1007 | ;; Global Highlight Changes mode is modelled after Global Font-lock mode. | |
1008 | ;; Three hooks are used to gain control. When Global Changes Mode is | |
1009 | ;; enabled, `find-file-hooks' and `change-major-mode-hook' are set. | |
1010 | ;; `find-file-hooks' is called when visiting a file, the new mode is | |
1011 | ;; known at this time. | |
1012 | ;; `change-major-mode-hook' is called when a buffer is changing mode. | |
1013 | ;; This could be because of finding a file in which case | |
1014 | ;; `find-file-hooks' has already been called and has done its work. | |
1015 | ;; However, it also catches the case where a new mode is being set by | |
1016 | ;; the user. However, it is called from `kill-all-variables' and at | |
1017 | ;; this time the mode is the old mode, which is not what we want. | |
1018 | ;; So, our function temporarily sets `post-command-hook' which will | |
1019 | ;; be called after the buffer has been completely set up (with the new | |
1020 | ;; mode). It then removes the `post-command-hook'. | |
1021 | ;; One other wrinkle - every M-x command runs the `change-major-mode-hook' | |
1022 | ;; so we ignore this by examining the buffer name. | |
1023 | ||
1024 | ||
1025 | (defun hilit-chg-major-mode-hook () | |
1026 | (add-hook 'post-command-hook 'hilit-chg-post-command-hook) | |
1027 | ) | |
1028 | ||
1029 | (defun hilit-chg-post-command-hook () | |
1030 | ;; This is called after changing a major mode, but also after each | |
1031 | ;; M-x command, in which case the current buffer is a minibuffer. | |
1032 | ;; In that case, do not act on it here, but don't turn it off | |
1033 | ;; either, we will get called here again soon-after. | |
1034 | ;; Also, don't enable it for other special buffers. | |
1035 | (if (string-match "^[ *]" (buffer-name)) | |
1036 | nil ;; (message "ignoring this post-command-hook") | |
1037 | (remove-hook 'post-command-hook 'hilit-chg-post-command-hook) | |
1038 | ;; The following check isn't necessary, since | |
1039 | ;; hilit-chg-turn-on-maybe makes this check too. | |
1040 | (or highlight-changes-mode ;; don't turn it on if it already is | |
1041 | (hilit-chg-turn-on-maybe highlight-changes-global-initial-state)) | |
1042 | )) | |
1043 | ||
1044 | (defun hilit-chg-check-global () | |
1045 | ;; This is called from the find file hook. | |
1046 | (hilit-chg-turn-on-maybe highlight-changes-global-initial-state)) | |
1047 | ||
1048 | ||
1049 | ||
1050 | ;;;###autoload | |
1051 | (defun global-highlight-changes (&optional arg) | |
1052 | "Turn on or off global Highlight Changes mode. | |
1053 | ||
1054 | When called interactively: | |
1055 | - if no prefix, toggle global Highlight Changes mode on or off | |
1056 | - if called with a positive prefix (or just C-u) turn it on in active mode | |
1057 | - if called with a zero prefix turn it on in passive mode | |
1058 | - if called with a negative prefix turn it off | |
1059 | ||
1060 | When called from a program: | |
1061 | - if ARG is nil or omitted, turn it off | |
1062 | - if ARG is 'active, turn it on in active mode | |
1063 | - if ARG is 'passive, turn it on in passive mode | |
1064 | - otherwise just turn it on | |
1065 | ||
1066 | When global Highlight Changes mode is enabled, Highlight Changes mode is turned | |
1067 | on for future \"suitable\" buffers (and for \"suitable\" existing buffers if | |
1068 | variable `highlight-changes-global-changes-existing-buffers' is non-nil). | |
1069 | \"Suitablity\" is determined by variable `highlight-changes-global-modes'." | |
1070 | ||
1071 | (interactive | |
1072 | (list | |
1073 | (cond | |
1074 | ((null current-prefix-arg) | |
1075 | ;; no arg => toggle it on/off | |
1076 | (setq global-highlight-changes (not global-highlight-changes))) | |
1077 | ;; positive interactive arg - turn it on as active | |
1078 | ((> (prefix-numeric-value current-prefix-arg) 0) | |
1079 | (setq global-highlight-changes t) | |
1080 | 'active) | |
1081 | ;; zero interactive arg - turn it on as passive | |
1082 | ((= (prefix-numeric-value current-prefix-arg) 0) | |
1083 | (setq global-highlight-changes t) | |
1084 | 'passive) | |
1085 | ;; negative interactive arg - turn it off | |
1086 | (t | |
1087 | (setq global-highlight-changes nil) | |
1088 | nil)))) | |
1089 | ||
1090 | (if arg | |
1091 | (progn | |
1092 | (if (eq arg 'active) | |
1093 | (setq highlight-changes-global-initial-state 'active) | |
1094 | (if (eq arg 'passive) | |
1095 | (setq highlight-changes-global-initial-state 'passive))) | |
1096 | (setq global-highlight-changes t) | |
1097 | (message "turning ON Global Highlight Changes mode in %s state" | |
1098 | highlight-changes-global-initial-state) | |
1099 | (add-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook) | |
1100 | (add-hook 'find-file-hooks 'hilit-chg-check-global) | |
1101 | (if highlight-changes-global-changes-existing-buffers | |
1102 | (hilit-chg-update-all-buffers | |
1103 | highlight-changes-global-initial-state)) | |
1104 | ) | |
1105 | (message "turning OFF global Highlight Changes mode") | |
1106 | (remove-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook) | |
1107 | (remove-hook 'find-file-hooks 'hilit-chg-check-global) | |
1108 | (remove-hook 'post-command-hook | |
1109 | 'hilit-chg-post-command-hook) | |
1110 | (remove-hook 'find-file-hooks 'hilit-chg-check-global) | |
1111 | (if highlight-changes-global-changes-existing-buffers | |
1112 | (hilit-chg-update-all-buffers nil)) | |
1113 | ) | |
1114 | ) | |
1115 | ||
1116 | ||
1117 | ||
1118 | ||
1119 | ||
1120 | (defun hilit-chg-turn-on-maybe (value) | |
1121 | "Turn on Highlight Changes mode if it is appropriate for this buffer. | |
1122 | ||
1123 | A buffer is appropriate for Highlight Changes mode if all these are true: | |
1124 | - the buffer is not a special buffer (one whose name begins with | |
1125 | `*' or ` ') | |
1126 | - the buffer's mode is suitable as per variable highlight-changes-global-modes | |
1127 | - Highlight Changes mode is not already on for this buffer. | |
1128 | ||
1129 | This function is called from hilit-chg-update-all-buffers | |
1130 | from `global-highlight-changes' when turning on global Highlight Changes mode. | |
1131 | " | |
1132 | (or highlight-changes-mode ; do nothing if already on | |
1133 | (if | |
1134 | (cond | |
1135 | ((null highlight-changes-global-modes) | |
1136 | nil) | |
1137 | ((functionp highlight-changes-global-modes) | |
1138 | (funcall highlight-changes-global-modes)) | |
1139 | ((listp highlight-changes-global-modes) | |
1140 | (if (eq (car-safe highlight-changes-global-modes) 'not) | |
1141 | (not (memq major-mode (cdr highlight-changes-global-modes))) | |
1142 | (memq major-mode highlight-changes-global-modes))) | |
1143 | (t | |
1144 | (and | |
1145 | (not (string-match "^[ *]" (buffer-name))) | |
1146 | (buffer-file-name)) | |
1147 | )) | |
1148 | (progn | |
1149 | (hilit-chg-set value) | |
1150 | (run-hooks 'highlight-changes-enable-hook))) | |
1151 | )) | |
1152 | ||
1153 | ||
1154 | (defun hilit-chg-turn-off-maybe () | |
1155 | (if highlight-changes-mode | |
1156 | (progn | |
1157 | (run-hooks 'highlight-changes-disable-hook) | |
1158 | (hilit-chg-clear)))) | |
1159 | ||
1160 | ||
1161 | ||
1162 | (defun hilit-chg-update-all-buffers (value) | |
1163 | (mapcar | |
1164 | (function (lambda (buffer) | |
1165 | (with-current-buffer buffer | |
1166 | (if value | |
1167 | (hilit-chg-turn-on-maybe value) | |
1168 | (hilit-chg-turn-off-maybe)) | |
1169 | ))) | |
1170 | (buffer-list))) | |
1171 | ||
1172 | ;; ===================== debug ================== | |
1173 | ;; For debug & test use: | |
1174 | ;; | |
1175 | ;; (defun hilit-chg-debug-show (&optional beg end) | |
1176 | ;; (interactive) | |
1177 | ;; (message "--- hilit-chg-debug-show ---") | |
1178 | ;; (hilit-chg-map-changes '(lambda (prop start end) | |
1179 | ;; (message "%d-%d: %s" start end prop) | |
1180 | ;; ) | |
1181 | ;; beg end | |
1182 | ;; )) | |
1183 | ;; | |
1184 | ;; ================== end of debug =============== | |
1185 | ||
1186 | ||
1187 | (provide 'hilit-chg) | |
e287d328 | 1188 | |
fdbd749a | 1189 | ;;; hilit-chg.el ends here |