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