Mercurial > emacs
annotate lisp/hilit-chg.el @ 31452:aafc28756ab2
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Thu, 07 Sep 2000 11:41:18 +0000 |
parents | b309b17a6025 |
children | 64d8d90d180d |
rev | line source |
---|---|
22957 | 1 ;;; hilit-chg.el --- minor mode displaying buffer changes with special face |
2 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
3 ;; Copyright (C) 1998, 2000 Free Software Foundation, Inc. |
22957 | 4 |
23614 | 5 ;; Author: Richard Sharman <rsharman@pobox.com> |
22957 | 6 ;; Keywords: faces |
7 | |
23101 | 8 ;; This file is part of GNU Emacs. |
9 | |
22957 | 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 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
47 ;; `highlight-changes-rotate-faces'. This rotates through a series |
22957 | 48 ;; of different faces, so you can distinguish "new" changes from "older" |
23046 | 49 ;; changes. You can customize these "rotated" faces in two ways. You can |
22957 | 50 ;; either explicitly define each face by customizing |
51 ;; `highlight-changes-face-list'. If, however, the faces differ from | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
52 ;; `highlight-changes-face' only in the foreground color, you can simply set |
22957 | 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 ;; | |
23289 | 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). | |
22957 | 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 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
96 ;; Explicit vs. Implicit |
22957 | 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 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
123 ;; on Highlight Changes mode in all "suitable" existing buffers and will turn |
22957 | 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 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
130 ;; an example, if the value is `buffer-file-name' then all buffers |
22957 | 131 ;; who are visiting files are suitable, but others (like dired |
132 ;; buffers) are not; | |
30924 | 133 ;; * a list -- then the buffer is suitable iff its mode is in the |
134 ;; list, except if the first element is `not', in which case the test | |
22957 | 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 | |
23289 | 150 ;; highlight-compare-with-file |
22957 | 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? | |
23289 | 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. | |
22957 | 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. | |
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 | |
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 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
190 ;; - renamed to Highlight Changes mode. |
22957 | 191 |
192 | |
193 ;;; Code: | |
194 | |
195 (require 'wid-edit) | |
196 | |
197 ;; ====================== Customization ======================= | |
198 (defgroup highlight-changes nil | |
199 "Highlight Changes mode." | |
24554
9a9a307e9a57
(highlight-changes): Add defgroup :version.
Dave Love <fx@gnu.org>
parents:
23614
diff
changeset
|
200 :version "20.4" |
22957 | 201 :group 'faces) |
202 | |
203 | |
204 ;; Face information: How the changes appear. | |
205 | |
206 ;; Defaults for face: red foreground, no change to background, | |
207 ;; and underlined if a change is because of a deletion. | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
208 ;; Note: underlining is helpful in that it shows up changes in white space. |
22957 | 209 ;; However, having it set for non-delete changes can be annoying because all |
210 ;; indentation on inserts gets underlined (which can look pretty ugly!). | |
211 | |
212 (defface highlight-changes-face | |
213 '((((class color)) (:foreground "red" )) | |
214 (t (:inverse-video t))) | |
215 "Face used for highlighting changes." | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
216 :group 'highlight-changes) |
22957 | 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." | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
223 :group 'highlight-changes) |
22957 | 224 |
225 | |
226 | |
227 ;; A (not very good) default list of colours to rotate through. | |
228 ;; | |
229 (defcustom highlight-changes-colours | |
230 (if (eq (frame-parameter nil 'background-mode) 'light) | |
231 ;; defaults for light background: | |
232 '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue") | |
233 ;; defaults for dark background: | |
234 '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid")) | |
235 "*Colours used by `highlight-changes-rotate-faces'. | |
236 The newest rotated change will be displayed in the first element of this list, | |
237 the next older will be in the second element etc. | |
238 | |
239 This list is used if `highlight-changes-face-list' is nil, otherwise that | |
240 variable overrides this list. If you only care about foreground | |
241 colours then use this, if you want fancier faces then set | |
242 `highlight-changes-face-list'." | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
243 :type '(repeat color) |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
244 :group 'highlight-changes) |
22957 | 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)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
256 :group 'highlight-changes) |
22957 | 257 |
258 (defcustom highlight-changes-global-initial-state 'passive | |
259 "*What state `global-highlight-changes' should start in. | |
260 This is used if `global-highlight-changes' is called with no argument. | |
261 This variable must be set to either `active' or `passive'" | |
262 :type '(choice (const :tag "Active" active) | |
263 (const :tag "Passive" passive)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
264 :group 'highlight-changes) |
22957 | 265 |
266 ;; The strings displayed in the mode-line for the minor mode: | |
267 (defcustom highlight-changes-active-string nil | |
268 "*The string used when Highlight Changes mode is in the active state. | |
269 This should be set to nil if no indication is desired, or to | |
270 a string with a leading space." | |
271 :type '(choice string | |
272 (const :tag "None" nil)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
273 :group 'highlight-changes) |
22957 | 274 |
275 (defcustom highlight-changes-passive-string " Chg" | |
276 "*The string used when Highlight Changes mode is in the passive state. | |
277 This should be set to nil if no indication is desired, or to | |
278 a string with a leading space." | |
279 :type '(choice string | |
280 (const :tag "None" nil)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
281 :group 'highlight-changes) |
22957 | 282 |
283 (defcustom highlight-changes-global-modes t | |
284 "*Determine whether a buffer is suitable for global Highlight Changes mode. | |
285 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
286 A function means that function is called: if it returns non-nil, the |
22957 | 287 buffer is suitable. |
288 | |
289 A list is a list of modes for which it is suitable, or a list whose | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
290 first element is `not' followed by modes which are not suitable. |
22957 | 291 |
292 t means the buffer is suitable if its name does not begin with ` ' nor | |
293 `*' and the buffer has a filename. | |
294 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
295 A value of nil means no buffers are suitable for `global-highlight-changes' |
22957 | 296 (effectively disabling the mode). |
297 | |
298 Examples: | |
299 (c-mode c++-mode) | |
300 means that Highlight Changes mode is turned on for buffers in C and C++ | |
301 modes only." | |
302 :type '(choice | |
303 (const :tag "all non-special buffers visiting files" t) | |
304 (set :menu-tag "specific modes" :tag "modes" | |
305 :value (not) | |
306 (const :tag "All except these" not) | |
307 (repeat :tag "Modes" :inline t (symbol :tag "mode"))) | |
308 (function :menu-tag "determined by function" | |
309 :value buffer-file-name) | |
310 (const :tag "none" nil) | |
311 ) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
312 :group 'highlight-changes) |
22957 | 313 |
314 (defvar global-highlight-changes nil) | |
315 | |
316 (defcustom highlight-changes-global-changes-existing-buffers nil | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
317 "*If non-nil, toggling global Highlight Changes mode affects existing buffers. |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
318 Normally, `global-highlight-changes' affects only new buffers (to be |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
319 created). However, if `highlight-changes-global-changes-existing-buffers' |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
320 is non-nil, then turning on `global-highlight-changes' will turn on |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
321 Highlight Changes mode in suitable buffers, and turning the mode off will |
22957 | 322 remove it from existing buffers." |
323 :type 'boolean | |
324 :group 'highlight-changes) | |
325 | |
326 (defun hilit-chg-cust-fix-changes-face-list (w wc &optional event) | |
327 ;; When customization function `highlight-changes-face-list' inserts a new | |
328 ;; face it uses the default face. We don't want the user to modify this | |
329 ;; face, so we rename the faces in the list on an insert. The rename is | |
330 ;; actually done by copying the faces so user-defined faces still remain | |
331 ;; in the same order. | |
332 ;; The notifying the parent is needed because without it changes to the | |
333 ;; faces are saved but not to the actual list itself. | |
334 (let ((old-list (widget-value w))) | |
335 (if (member 'default old-list) | |
336 (let | |
337 ((p (reverse old-list)) | |
338 (n (length old-list)) | |
339 new-name old-name | |
340 (new-list nil) | |
341 ) | |
342 (while p | |
343 (setq old-name (car p)) | |
344 (setq new-name (intern (format "highlight-changes-face-%d" n))) | |
345 (if (eq old-name new-name) | |
346 nil | |
347 ;; A new face has been inserted: we don't want to modify the | |
348 ;; default face so copy it. Better, though, (I think) is to | |
349 ;; make a new face have the same attributes as | |
350 ;; highlight-changes-face . | |
351 (if (eq old-name 'default) | |
352 (copy-face 'highlight-changes-face new-name) | |
353 (copy-face old-name new-name) | |
354 )) | |
355 (setq new-list (append (list new-name) new-list)) | |
356 (setq n (1- n)) | |
357 (setq p (cdr p))) | |
358 (if (equal new-list (widget-value w)) | |
359 nil ;; (message "notify: no change!") | |
360 (widget-value-set w new-list) | |
361 (widget-setup) | |
362 ) | |
363 ) | |
364 ;; (message "notify: no default here!") | |
365 )) | |
366 (let ((parent (widget-get w :parent))) | |
367 (when parent | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
368 (widget-apply parent :notify w event)))) |
22957 | 369 |
370 | |
371 (defcustom highlight-changes-face-list nil | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
372 "*A list of faces used when rotating changes. |
22957 | 373 Normally the variable is initialized to nil and the list is created from |
374 `highlight-changes-colours' when needed. However, you can set this variable | |
375 to any list of faces. You will have to do this if you want faces which | |
376 don't just differ from `highlight-changes-face' by the foreground colour. | |
377 Otherwise, this list will be constructed when needed from | |
378 `highlight-changes-colours'." | |
379 :type '(choice | |
380 (repeat | |
381 :notify hilit-chg-cust-fix-changes-face-list | |
382 face ) | |
383 (const :tag "Derive from highlight-changes-colours" nil) | |
384 ) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
385 :group 'highlight-changes) |
22957 | 386 |
387 ;; ======================================================================== | |
388 | |
389 ;; These shouldn't be changed! | |
390 | |
25533
121060238dc8
(highlight-changes-mode): Add autoload cookie for variable.
Dave Love <fx@gnu.org>
parents:
24554
diff
changeset
|
391 ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'. |
121060238dc8
(highlight-changes-mode): Add autoload cookie for variable.
Dave Love <fx@gnu.org>
parents:
24554
diff
changeset
|
392 ;;;###autoload |
22957 | 393 (defvar highlight-changes-mode nil) |
394 (defvar hilit-chg-list nil) | |
395 (defvar hilit-chg-string " ??") | |
396 (or (assq 'highlight-changes-mode minor-mode-alist) | |
397 (setq minor-mode-alist | |
398 (cons '(highlight-changes-mode hilit-chg-string) minor-mode-alist) | |
399 )) | |
400 (make-variable-buffer-local 'highlight-changes-mode) | |
401 (make-variable-buffer-local 'hilit-chg-string) | |
402 | |
403 | |
404 | |
405 (eval-and-compile | |
23289 | 406 ;; For highlight-compare-with-file |
22957 | 407 (defvar ediff-number-of-differences) |
408 (autoload 'ediff-setup "ediff") | |
409 (autoload 'ediff-with-current-buffer "ediff") | |
410 (autoload 'ediff-really-quit "ediff") | |
411 (autoload 'ediff-make-fine-diffs "ediff") | |
412 (autoload 'ediff-get-fine-diff-vector "ediff") | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
413 (autoload 'ediff-get-difference "ediff")) |
22957 | 414 |
415 | |
416 | |
417 ;;; Functions... | |
418 | |
419 (defun hilit-chg-map-changes (func &optional start-position end-position) | |
420 "Call function FUNC for each region used by Highlight Changes mode." | |
421 ;; if start-position is nil, (point-min) is used | |
422 ;; if end-position is nil, (point-max) is used | |
423 ;; FUNC is called with 3 params: property start stop | |
424 (let ((start (or start-position (point-min))) | |
425 (limit (or end-position (point-max))) | |
426 prop end) | |
427 (while (and start (< start limit)) | |
428 (setq prop (get-text-property start 'hilit-chg)) | |
429 (setq end (text-property-not-all start limit 'hilit-chg prop)) | |
430 (if prop | |
431 (funcall func prop start (or end limit))) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
432 (setq start end)))) |
22957 | 433 |
434 | |
435 (defun hilit-chg-display-changes (&optional beg end) | |
436 "Display face information for Highlight Changes mode. | |
437 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
438 An overlay containing a change face is added from the information |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
439 in the text property of type `hilit-chg'. |
22957 | 440 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
441 This is the opposite of `hilit-chg-hide-changes'." |
22957 | 442 (hilit-chg-map-changes 'hilit-chg-make-ov beg end)) |
443 | |
444 | |
445 (defun hilit-chg-make-ov (prop start end) | |
446 ;; for the region make change overlays corresponding to | |
447 ;; the text property 'hilit-chg | |
448 (let ((ov (make-overlay start end)) | |
449 face) | |
450 (or prop | |
451 (error "hilit-chg-make-ov: prop is nil")) | |
452 (if (eq prop 'hilit-chg-delete) | |
453 (setq face 'highlight-changes-delete-face) | |
454 (setq face (nth 1 (member prop hilit-chg-list)))) | |
455 (if face | |
456 (progn | |
457 ;; We must mark the face, that is the purpose of the overlay | |
458 (overlay-put ov 'face face) | |
459 ;; I don't think we need to set evaporate since we should | |
460 ;; be controlling them! | |
461 (overlay-put ov 'evaporate t) | |
462 ;; We set the change property so we can tell this is one | |
463 ;; of our overlays (so we don't delete someone else's). | |
464 (overlay-put ov 'hilit-chg t) | |
465 ) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
466 (error "hilit-chg-make-ov: no face for prop: %s" prop)))) |
22957 | 467 |
468 (defun hilit-chg-hide-changes (&optional beg end) | |
469 "Remove face information for Highlight Changes mode. | |
470 | |
471 The overlay containing the face is removed, but the text property | |
472 containing the change information is retained. | |
473 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
474 This is the opposite of `hilit-chg-display-changes'." |
22957 | 475 (let ((start (or beg (point-min))) |
476 (limit (or end (point-max))) | |
477 p ov) | |
478 (setq p (overlays-in start limit)) | |
479 (while p | |
480 ;; don't delete the overlay if it isn't ours! | |
481 (if (overlay-get (car p) 'hilit-chg) | |
482 (delete-overlay (car p))) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
483 (setq p (cdr p))))) |
22957 | 484 |
485 (defun hilit-chg-fixup (beg end) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
486 "Fix change overlays in region between BEG and END. |
22957 | 487 |
488 Ensure the overlays agree with the changes as determined from | |
489 the text properties of type `hilit-chg' ." | |
490 ;; Remove or alter overlays in region beg..end | |
491 (let (p ov ov-start ov-end | |
492 props q) | |
493 (setq p (overlays-in beg end)) | |
494 ;; temp for debugging: | |
495 ;; (or (eq highlight-changes-mode 'active) | |
496 ;; (error "hilit-chg-fixup called but Highlight Changes mode not active")) | |
497 (while p | |
498 (setq ov (car p)) | |
499 (setq ov-start (overlay-start ov)) | |
500 (setq ov-end (overlay-end ov)) | |
501 (if (< ov-start beg) | |
502 (progn | |
503 (move-overlay ov ov-start beg) | |
504 (if (> ov-end end) | |
505 (progn | |
506 (setq props (overlay-properties ov)) | |
507 (setq ov (make-overlay end ov-end)) | |
508 (while props | |
509 (overlay-put ov (car props)(car (cdr props))) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
510 (setq props (cdr (cdr props))))))) |
22957 | 511 (if (> ov-end end) |
512 (move-overlay ov end ov-end) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
513 (delete-overlay ov))) |
22957 | 514 (setq p (cdr p))) |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
515 (hilit-chg-display-changes beg end))) |
22957 | 516 |
517 ;;;###autoload | |
518 (defun highlight-changes-remove-highlight (beg end) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
519 "Remove the change face from the region between BEG and END. |
22957 | 520 This allows you to manually remove highlighting from uninteresting changes." |
521 (interactive "r") | |
522 (let ((after-change-functions nil)) | |
523 (remove-text-properties beg end '(hilit-chg nil)) | |
524 (hilit-chg-fixup beg end))) | |
525 | |
526 (defun hilit-chg-set-face-on-change (beg end leng-before | |
527 &optional no-proerty-change) | |
528 "Record changes and optionally display them in a distinctive face. | |
529 `hilit-chg-set' adds this function to the `after-change-functions' hook." | |
530 ;; | |
531 ;; This function is called by the `after-change-functions' hook, which | |
532 ;; is how we are notified when text is changed. | |
23289 | 533 ;; It is also called from `highlight-compare-with-file'. |
22957 | 534 ;; |
535 ;; We do NOT want to simply do this if this is an undo command, because | |
536 ;; otherwise an undone change shows up as changed. While the properties | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
537 ;; are automatically restored by undo, we must fix up the overlay. |
22957 | 538 (save-match-data |
539 (let ((beg-decr 1) (end-incr 1) | |
540 (type 'hilit-chg) | |
541 old) | |
542 (if undo-in-progress | |
543 (if (eq highlight-changes-mode 'active) | |
544 (hilit-chg-fixup beg end)) | |
545 (if (and (= beg end) (> leng-before 0)) | |
546 ;; deletion | |
547 (progn | |
548 ;; The eolp and bolp tests are a kludge! But they prevent | |
549 ;; rather nasty looking displays when deleting text at the end | |
550 ;; of line, such as normal corrections as one is typing and | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
551 ;; immediately makes a correction, and when deleting first |
22957 | 552 ;; character of a line. |
553 ;;; (if (= leng-before 1) | |
554 ;;; (if (eolp) | |
555 ;;; (setq beg-decr 0 end-incr 0) | |
556 ;;; (if (bolp) | |
557 ;;; (setq beg-decr 0)))) | |
558 ;;; (setq beg (max (- beg beg-decr) (point-min))) | |
559 (setq end (min (+ end end-incr) (point-max))) | |
560 (setq type 'hilit-chg-delete)) | |
561 ;; Not a deletion. | |
562 ;; Most of the time the following is not necessary, but | |
563 ;; if the current text was marked as a deletion then | |
564 ;; the old overlay is still in effect, so if we add some | |
565 ;; text then remove the deletion marking, but set it to | |
566 ;; changed otherwise its highlighting disappears. | |
567 (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete) | |
568 (progn | |
569 (remove-text-properties end (+ end 1) '(hilit-chg nil)) | |
570 (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg) | |
571 (if (eq highlight-changes-mode 'active) | |
572 (hilit-chg-fixup beg (+ end 1)))))) | |
573 (unless no-proerty-change | |
574 (put-text-property beg end 'hilit-chg type)) | |
575 (if (or (eq highlight-changes-mode 'active) no-proerty-change) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
576 (hilit-chg-make-ov type beg end)))))) |
22957 | 577 |
578 (defun hilit-chg-set (value) | |
579 "Turn on Highlight Changes mode for this buffer." | |
580 (setq highlight-changes-mode value) | |
581 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t) | |
582 (hilit-chg-make-list) | |
583 (if (eq highlight-changes-mode 'active) | |
584 (progn | |
585 (setq hilit-chg-string highlight-changes-active-string) | |
586 (or buffer-read-only | |
587 (hilit-chg-display-changes))) | |
588 ;; mode is passive | |
589 (setq hilit-chg-string highlight-changes-passive-string) | |
590 (or buffer-read-only | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
591 (hilit-chg-hide-changes))) |
22957 | 592 (force-mode-line-update) |
593 (make-local-hook 'after-change-functions) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
594 (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t)) |
22957 | 595 |
596 (defun hilit-chg-clear () | |
597 "Remove Highlight Changes mode for this buffer. | |
598 This removes all saved change information." | |
599 (if buffer-read-only | |
600 ;; We print the buffer name because this function could be called | |
601 ;; on many buffers from `global-highlight-changes'. | |
602 (message "Cannot remove highlighting from read-only mode buffer %s" | |
603 (buffer-name)) | |
604 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t) | |
605 (let ((after-change-functions nil)) | |
606 (hilit-chg-hide-changes) | |
607 (hilit-chg-map-changes | |
608 '(lambda (prop start stop) | |
609 (remove-text-properties start stop '(hilit-chg nil)))) | |
610 ) | |
611 (setq highlight-changes-mode nil) | |
612 (force-mode-line-update) | |
613 ;; If we type: C-u -1 M-x highlight-changes-mode | |
614 ;; we want to turn it off, but hilit-chg-post-command-hook | |
615 ;; runs and that turns it back on! | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
616 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook))) |
22957 | 617 |
618 ;;;###autoload | |
619 (defun highlight-changes-mode (&optional arg) | |
620 "Toggle (or initially set) Highlight Changes mode. | |
621 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
622 Without an argument: |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
623 If Highlight Changes mode is not enabled, then enable it (in either active |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
624 or passive state as determined by the variable |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
625 `highlight-changes-initial-state'); otherwise, toggle between active |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
626 and passive state. |
22957 | 627 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
628 With an argument ARG: |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
629 If ARG is positive, set state to active; |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
630 If ARG is zero, set state to passive; |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
631 If ARG is negative, disable Highlight Changes mode completely. |
22957 | 632 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
633 Active state - means changes are shown in a distinctive face. |
22957 | 634 Passive state - means changes are kept and new ones recorded but are |
635 not displayed in a different face. | |
636 | |
637 Functions: | |
638 \\[highlight-changes-next-change] - move point to beginning of next change | |
639 \\[highlight-changes-previous-change] - move to beginning of previous change | |
23289 | 640 \\[highlight-compare-with-file] - mark text as changed by comparing this |
641 buffer with the contents of a file | |
22957 | 642 \\[highlight-changes-remove-highlight] - remove the change face from the region |
643 \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes \ | |
644 through | |
645 various faces. | |
646 | |
647 Hook variables: | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
648 `highlight-changes-enable-hook' - when enabling Highlight Changes mode. |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
649 `highlight-changes-toggle-hook' - when entering active or passive state |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
650 `highlight-changes-disable-hook' - when turning off Highlight Changes mode." |
22957 | 651 (interactive "P") |
29204
d38a98cddcec
(highlight-changes-mode): Ask about color or
Eli Zaretskii <eliz@gnu.org>
parents:
25533
diff
changeset
|
652 (if (or (display-color-p) |
d38a98cddcec
(highlight-changes-mode): Ask about color or
Eli Zaretskii <eliz@gnu.org>
parents:
25533
diff
changeset
|
653 (and (fboundp 'x-display-grayscale-p) (x-display-grayscale-p))) |
22957 | 654 (let ((was-on highlight-changes-mode) |
655 (new-highlight-changes-mode | |
656 (cond | |
657 ((null arg) | |
658 ;; no arg => toggle (or set to active initially) | |
659 (if highlight-changes-mode | |
660 (if (eq highlight-changes-mode 'active) 'passive 'active) | |
661 highlight-changes-initial-state)) | |
662 ;; an argument is given | |
663 ((eq arg 'active) | |
664 'active) | |
665 ((eq arg 'passive) | |
666 'passive) | |
667 ((> (prefix-numeric-value arg) 0) | |
668 'active) | |
669 ((< (prefix-numeric-value arg) 0) | |
670 nil) | |
671 (t | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
672 'passive)))) |
22957 | 673 (if new-highlight-changes-mode |
674 ;; mode is turned on -- but may be passive | |
675 (progn | |
676 (hilit-chg-set new-highlight-changes-mode) | |
677 (or was-on | |
678 ;; run highlight-changes-enable-hook once | |
679 (run-hooks 'highlight-changes-enable-hook)) | |
680 (run-hooks 'highlight-changes-toggle-hook)) | |
681 ;; mode is turned off | |
682 (run-hooks 'highlight-changes-disable-hook) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
683 (hilit-chg-clear))) |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
684 (message "Highlight Changes mode requires color or grayscale display"))) |
22957 | 685 |
686 ;;;###autoload | |
687 (defun highlight-changes-next-change () | |
688 "Move to the beginning of the next change, if in Highlight Changes mode." | |
689 (interactive) | |
690 (if highlight-changes-mode | |
691 (let ((start (point)) | |
692 prop) | |
693 (setq prop (get-text-property (point) 'hilit-chg)) | |
694 (if prop | |
695 ;; we are in a change | |
696 (setq start (next-single-property-change (point) 'hilit-chg))) | |
697 (if start | |
698 (setq start (next-single-property-change start 'hilit-chg))) | |
699 (if start | |
700 (goto-char start) | |
701 (message "no next change"))) | |
702 (message "This buffer is not in Highlight Changes mode."))) | |
703 | |
704 | |
705 ;;;###autoload | |
706 (defun highlight-changes-previous-change () | |
707 "Move to the beginning of the previous change, if in Highlight Changes mode." | |
708 (interactive) | |
709 (if highlight-changes-mode | |
710 (let ( (start (point)) (prop nil) ) | |
711 (or (bobp) | |
712 (setq prop (get-text-property (1- (point)) 'hilit-chg))) | |
713 (if prop | |
714 ;; we are in a change | |
715 (setq start (previous-single-property-change (point) 'hilit-chg))) | |
716 (if start | |
717 (setq start (previous-single-property-change start 'hilit-chg))) | |
718 ;; special handling for the case where (point-min) is a change | |
719 (if start | |
720 (setq start (or (previous-single-property-change start 'hilit-chg) | |
721 (if (get-text-property (point-min) 'hilit-chg) | |
722 (point-min))))) | |
723 (if start | |
724 (goto-char start) | |
725 (message "no previous change"))) | |
726 (message "This buffer is not in Highlight Changes mode."))) | |
727 | |
728 ;; ======================================================================== | |
729 | |
730 (defun hilit-chg-make-list (&optional force) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
731 "Construct `hilit-chg-list' and `highlight-changes-face-list'." |
22957 | 732 ;; Constructs highlight-changes-face-list if necessary, |
733 ;; and hilit-chg-list always: | |
734 ;; Maybe this should always be called when rotating a face | |
735 ;; so we pick up any changes? | |
736 (if (or (null highlight-changes-face-list) ; Don't do it if it | |
737 force) ; already exists unless FORCE non-nil. | |
738 (let ((p highlight-changes-colours) | |
739 (n 1) name) | |
740 (setq highlight-changes-face-list nil) | |
741 (while p | |
742 (setq name (intern (format "highlight-changes-face-%d" n))) | |
743 (copy-face 'highlight-changes-face name) | |
744 (set-face-foreground name (car p)) | |
745 (setq highlight-changes-face-list | |
746 (append highlight-changes-face-list (list name))) | |
747 (setq p (cdr p)) | |
748 (setq n (1+ n))))) | |
749 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes-face)) | |
750 (let ((p highlight-changes-face-list) | |
751 (n 1) | |
752 last-category last-face) | |
753 (while p | |
754 (setq last-category (intern (format "change-%d" n))) | |
755 ;; (setq last-face (intern (format "highlight-changes-face-%d" n))) | |
756 (setq last-face (car p)) | |
757 (setq hilit-chg-list | |
758 (append hilit-chg-list | |
759 (list last-category last-face))) | |
760 (setq p (cdr p)) | |
761 (setq n (1+ n))) | |
762 (setq hilit-chg-list | |
763 (append hilit-chg-list | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
764 (list last-category last-face))))) |
22957 | 765 |
766 (defun hilit-chg-bump-change (prop start end) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
767 "Increment (age) the Highlight Changes mode text property." |
22957 | 768 (let ( new-prop ) |
769 (if (eq prop 'hilit-chg-delete) | |
770 (setq new-prop (nth 2 hilit-chg-list)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
771 (setq new-prop (nth 2 (member prop hilit-chg-list)))) |
22957 | 772 (if prop |
773 (put-text-property start end 'hilit-chg new-prop) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
774 (message "%d-%d unknown property %s not changed" start end prop)))) |
22957 | 775 |
776 ;;;###autoload | |
777 (defun highlight-changes-rotate-faces () | |
778 "Rotate the faces used by Highlight Changes mode. | |
779 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
780 Current changes are displayed in the face described by the first element |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
781 of `highlight-changes-face-list', one level older changes are shown in |
22957 | 782 face described by the second element, and so on. Very old changes remain |
783 shown in the last face in the list. | |
784 | |
785 You can automatically rotate colours when the buffer is saved | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
786 by adding the following to `local-write-file-hooks', by evaling it in the |
22957 | 787 buffer to be saved): |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
788 |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
789 \(add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)" |
22957 | 790 (interactive) |
791 ;; If not in active mode do nothing but don't complain because this | |
792 ;; may be bound to a hook. | |
793 (if (eq highlight-changes-mode 'active) | |
794 (let ((after-change-functions nil)) | |
795 ;; ensure hilit-chg-list is made and up to date | |
796 (hilit-chg-make-list) | |
797 ;; remove our existing overlays | |
798 (hilit-chg-hide-changes) | |
799 ;; for each change text property, increment it | |
800 (hilit-chg-map-changes 'hilit-chg-bump-change) | |
801 ;; and display them all if active | |
802 (if (eq highlight-changes-mode 'active) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
803 (hilit-chg-display-changes)))) |
22957 | 804 ;; This always returns nil so it is safe to use in |
805 ;; local-write-file-hook | |
806 nil) | |
807 | |
808 ;; ======================================================================== | |
809 ;; Comparing with an existing file. | |
810 ;; This uses ediff to find the differences. | |
811 | |
812 ;;;###autoload | |
23288
17dd9ecf84f1
(highlight-compare-with-file): Renamed from
Karl Heuer <kwzh@gnu.org>
parents:
23101
diff
changeset
|
813 (defun highlight-compare-with-file (file-b) |
17dd9ecf84f1
(highlight-compare-with-file): Renamed from
Karl Heuer <kwzh@gnu.org>
parents:
23101
diff
changeset
|
814 "Compare this buffer with a file, and highlight differences. |
22957 | 815 |
816 The current buffer must be an unmodified buffer visiting a file, | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
817 and must not be read-only. |
22957 | 818 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
819 If the buffer has a backup filename, it is used as the default when |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
820 this function is called interactively. |
22957 | 821 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
822 If the current buffer is visiting the file being compared against, it |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
823 also will have its differences highlighted. Otherwise, the file is |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
824 read in temporarily but the buffer is deleted. |
22957 | 825 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
826 If the buffer is read-only, differences will be highlighted but no property |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
827 changes are made, so \\[highlight-changes-next-change] and |
22957 | 828 \\[highlight-changes-previous-change] will not work." |
829 (interactive (list | |
830 (read-file-name | |
831 "File to compare with? " ;; prompt | |
832 "" ;; directory | |
833 nil ;; default | |
834 'yes ;; must exist | |
835 (let ((f (make-backup-file-name | |
836 (or (buffer-file-name (current-buffer)) | |
837 (error "no file for this buffer"))))) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
838 (if (file-exists-p f) f ""))))) |
22957 | 839 |
840 (let* ((buf-a (current-buffer)) | |
841 (buf-a-read-only buffer-read-only) | |
842 (orig-pos (point)) | |
843 (file-a (buffer-file-name)) | |
844 (existing-buf (get-file-buffer file-b)) | |
845 (buf-b (or existing-buf | |
846 (find-file-noselect file-b))) | |
847 (buf-b-read-only (with-current-buffer buf-b buffer-read-only)) | |
848 xy xx yy p q | |
849 a-start a-end len-a | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
850 b-start b-end len-b) |
22957 | 851 |
852 ;; We use the fact that the buffer is not marked modified at the | |
853 ;; end where we clear its modified status | |
854 (if (buffer-modified-p buf-a) | |
855 (if (y-or-n-p (format "OK to save %s? " file-a)) | |
856 (save-buffer buf-a) | |
857 (error "Buffer must be saved before comparing with a file."))) | |
858 (if (and existing-buf (buffer-modified-p buf-b)) | |
859 (if (y-or-n-p (format "OK to save %s? " file-b)) | |
860 (save-buffer buf-b) | |
861 (error "Cannot compare with a file in an unsaved buffer."))) | |
862 (highlight-changes-mode 'active) | |
863 (if existing-buf (with-current-buffer buf-b | |
864 (highlight-changes-mode 'active))) | |
865 (save-window-excursion | |
866 (setq xy (hilit-chg-get-diff-info buf-a file-a buf-b file-b))) | |
867 (setq xx (car xy)) | |
868 (setq p xx) | |
869 (setq yy (car (cdr xy))) | |
870 (setq q yy) | |
871 (hilit-chg-make-list) | |
872 (while p | |
873 (setq a-start (nth 0 (car p))) | |
874 (setq a-end (nth 1 (car p))) | |
875 (setq b-start (nth 0 (car q))) | |
876 (setq b-end (nth 1 (car q))) | |
877 (setq len-a (- a-end a-start)) | |
878 (setq len-b (- b-end b-start)) | |
879 (set-buffer buf-a) | |
880 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only) | |
881 (set-buffer-modified-p nil) | |
882 (goto-char orig-pos) | |
883 (if existing-buf | |
884 (with-current-buffer buf-b | |
885 (hilit-chg-set-face-on-change b-start b-end len-a | |
886 buf-b-read-only ) | |
887 )) | |
888 (setq p (cdr p)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
889 (setq q (cdr q))) |
22957 | 890 (if existing-buf |
891 (set-buffer-modified-p nil) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
892 (kill-buffer buf-b)))) |
22957 | 893 |
894 | |
895 (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b) | |
896 (let ((e nil) x y) ;; e is set by function hilit-chg-get-diff-list-hk | |
897 (ediff-setup buf-a file-a buf-b file-b | |
898 nil nil ; buf-c file-C | |
899 'hilit-chg-get-diff-list-hk | |
900 (list (cons 'ediff-job-name 'something)) | |
901 ) | |
902 (ediff-with-current-buffer e (ediff-really-quit nil)) | |
903 (list x y))) | |
904 | |
905 | |
906 (defun hilit-chg-get-diff-list-hk () | |
907 ;; x and y are dynamically bound by hilit-chg-get-diff-info | |
908 ;; which calls this function as a hook | |
909 (defvar x) ;; placate the byte-compiler | |
910 (defvar y) | |
911 (setq e (current-buffer)) | |
912 (let ((n 0) extent p va vb a b) | |
913 (setq x nil y nil) ;; x and y are bound by hilit-chg-get-diff-info | |
914 (while (< n ediff-number-of-differences) | |
915 (ediff-make-fine-diffs n) | |
916 (setq va (ediff-get-fine-diff-vector n 'A)) | |
917 ;; va is a vector if there are fine differences | |
918 (if va | |
919 (setq a (append va nil)) | |
920 ;; if not, get the unrefined difference | |
921 (setq va (ediff-get-difference n 'A)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
922 (setq a (list (elt va 0)))) |
22957 | 923 ;; a list a list |
924 (setq p a) | |
925 (while p | |
926 (setq extent (list (overlay-start (car p)) | |
927 (overlay-end (car p)))) | |
928 (setq p (cdr p)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
929 (setq x (append x (list extent) )));; while p |
22957 | 930 ;; |
931 (setq vb (ediff-get-fine-diff-vector n 'B)) | |
932 ;; vb is a vector | |
933 (if vb | |
934 (setq b (append vb nil)) | |
935 ;; if not, get the unrefined difference | |
936 (setq vb (ediff-get-difference n 'B)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
937 (setq b (list (elt vb 0)))) |
22957 | 938 ;; b list a list |
939 (setq p b) | |
940 (while p | |
941 (setq extent (list (overlay-start (car p)) | |
942 (overlay-end (car p)))) | |
943 (setq p (cdr p)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
944 (setq y (append y (list extent) ))) |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
945 (setq n (1+ n)));; while |
22957 | 946 ;; ediff-quit doesn't work here. |
947 ;; No point in returning a value, since this is a hook function. | |
948 )) | |
949 | |
950 ;; ======================= automatic stuff ============== | |
951 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
952 ;; Global Highlight Changes mode is modeled after Global Font-lock mode. |
22957 | 953 ;; Three hooks are used to gain control. When Global Changes Mode is |
954 ;; enabled, `find-file-hooks' and `change-major-mode-hook' are set. | |
955 ;; `find-file-hooks' is called when visiting a file, the new mode is | |
956 ;; known at this time. | |
957 ;; `change-major-mode-hook' is called when a buffer is changing mode. | |
958 ;; This could be because of finding a file in which case | |
959 ;; `find-file-hooks' has already been called and has done its work. | |
960 ;; However, it also catches the case where a new mode is being set by | |
961 ;; the user. However, it is called from `kill-all-variables' and at | |
962 ;; this time the mode is the old mode, which is not what we want. | |
963 ;; So, our function temporarily sets `post-command-hook' which will | |
964 ;; be called after the buffer has been completely set up (with the new | |
965 ;; mode). It then removes the `post-command-hook'. | |
966 ;; One other wrinkle - every M-x command runs the `change-major-mode-hook' | |
967 ;; so we ignore this by examining the buffer name. | |
968 | |
969 | |
970 (defun hilit-chg-major-mode-hook () | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
971 (add-hook 'post-command-hook 'hilit-chg-post-command-hook)) |
22957 | 972 |
973 (defun hilit-chg-post-command-hook () | |
974 ;; This is called after changing a major mode, but also after each | |
975 ;; M-x command, in which case the current buffer is a minibuffer. | |
976 ;; In that case, do not act on it here, but don't turn it off | |
977 ;; either, we will get called here again soon-after. | |
978 ;; Also, don't enable it for other special buffers. | |
979 (if (string-match "^[ *]" (buffer-name)) | |
980 nil ;; (message "ignoring this post-command-hook") | |
981 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook) | |
982 ;; The following check isn't necessary, since | |
983 ;; hilit-chg-turn-on-maybe makes this check too. | |
984 (or highlight-changes-mode ;; don't turn it on if it already is | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
985 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state)))) |
22957 | 986 |
987 (defun hilit-chg-check-global () | |
988 ;; This is called from the find file hook. | |
989 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state)) | |
990 | |
991 | |
992 ;;;###autoload | |
993 (defun global-highlight-changes (&optional arg) | |
994 "Turn on or off global Highlight Changes mode. | |
995 | |
996 When called interactively: | |
997 - if no prefix, toggle global Highlight Changes mode on or off | |
998 - if called with a positive prefix (or just C-u) turn it on in active mode | |
999 - if called with a zero prefix turn it on in passive mode | |
1000 - if called with a negative prefix turn it off | |
1001 | |
1002 When called from a program: | |
1003 - if ARG is nil or omitted, turn it off | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1004 - if ARG is `active', turn it on in active mode |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1005 - if ARG is `passive', turn it on in passive mode |
22957 | 1006 - otherwise just turn it on |
1007 | |
1008 When global Highlight Changes mode is enabled, Highlight Changes mode is turned | |
1009 on for future \"suitable\" buffers (and for \"suitable\" existing buffers if | |
1010 variable `highlight-changes-global-changes-existing-buffers' is non-nil). | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1011 \"Suitability\" is determined by variable `highlight-changes-global-modes'." |
22957 | 1012 |
1013 (interactive | |
1014 (list | |
1015 (cond | |
1016 ((null current-prefix-arg) | |
1017 ;; no arg => toggle it on/off | |
1018 (setq global-highlight-changes (not global-highlight-changes))) | |
1019 ;; positive interactive arg - turn it on as active | |
1020 ((> (prefix-numeric-value current-prefix-arg) 0) | |
1021 (setq global-highlight-changes t) | |
1022 'active) | |
1023 ;; zero interactive arg - turn it on as passive | |
1024 ((= (prefix-numeric-value current-prefix-arg) 0) | |
1025 (setq global-highlight-changes t) | |
1026 'passive) | |
1027 ;; negative interactive arg - turn it off | |
1028 (t | |
1029 (setq global-highlight-changes nil) | |
1030 nil)))) | |
1031 | |
1032 (if arg | |
1033 (progn | |
1034 (if (eq arg 'active) | |
1035 (setq highlight-changes-global-initial-state 'active) | |
1036 (if (eq arg 'passive) | |
1037 (setq highlight-changes-global-initial-state 'passive))) | |
1038 (setq global-highlight-changes t) | |
1039 (message "turning ON Global Highlight Changes mode in %s state" | |
1040 highlight-changes-global-initial-state) | |
1041 (add-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook) | |
1042 (add-hook 'find-file-hooks 'hilit-chg-check-global) | |
1043 (if highlight-changes-global-changes-existing-buffers | |
1044 (hilit-chg-update-all-buffers | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1045 highlight-changes-global-initial-state))) |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1046 |
22957 | 1047 (message "turning OFF global Highlight Changes mode") |
1048 (remove-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook) | |
1049 (remove-hook 'find-file-hooks 'hilit-chg-check-global) | |
1050 (remove-hook 'post-command-hook | |
1051 'hilit-chg-post-command-hook) | |
1052 (remove-hook 'find-file-hooks 'hilit-chg-check-global) | |
1053 (if highlight-changes-global-changes-existing-buffers | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1054 (hilit-chg-update-all-buffers nil)))) |
22957 | 1055 |
1056 | |
1057 (defun hilit-chg-turn-on-maybe (value) | |
1058 "Turn on Highlight Changes mode if it is appropriate for this buffer. | |
1059 | |
1060 A buffer is appropriate for Highlight Changes mode if all these are true: | |
1061 - the buffer is not a special buffer (one whose name begins with | |
1062 `*' or ` ') | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1063 - the buffer's mode is suitable as per variable |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1064 `highlight-changes-global-modes' |
22957 | 1065 - Highlight Changes mode is not already on for this buffer. |
1066 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1067 This function is called from `hilit-chg-update-all-buffers' or |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1068 from `global-highlight-changes' when turning on global Highlight Changes mode." |
22957 | 1069 (or highlight-changes-mode ; do nothing if already on |
1070 (if | |
1071 (cond | |
1072 ((null highlight-changes-global-modes) | |
1073 nil) | |
1074 ((functionp highlight-changes-global-modes) | |
1075 (funcall highlight-changes-global-modes)) | |
1076 ((listp highlight-changes-global-modes) | |
1077 (if (eq (car-safe highlight-changes-global-modes) 'not) | |
1078 (not (memq major-mode (cdr highlight-changes-global-modes))) | |
1079 (memq major-mode highlight-changes-global-modes))) | |
1080 (t | |
1081 (and | |
1082 (not (string-match "^[ *]" (buffer-name))) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1083 (buffer-file-name)))) |
22957 | 1084 (progn |
1085 (hilit-chg-set value) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1086 (run-hooks 'highlight-changes-enable-hook))))) |
22957 | 1087 |
1088 | |
1089 (defun hilit-chg-turn-off-maybe () | |
1090 (if highlight-changes-mode | |
1091 (progn | |
1092 (run-hooks 'highlight-changes-disable-hook) | |
1093 (hilit-chg-clear)))) | |
1094 | |
1095 | |
1096 (defun hilit-chg-update-all-buffers (value) | |
1097 (mapcar | |
1098 (function (lambda (buffer) | |
1099 (with-current-buffer buffer | |
1100 (if value | |
1101 (hilit-chg-turn-on-maybe value) | |
1102 (hilit-chg-turn-off-maybe)) | |
1103 ))) | |
1104 (buffer-list))) | |
1105 | |
1106 ;; ===================== debug ================== | |
1107 ;; For debug & test use: | |
1108 ;; | |
1109 ;; (defun hilit-chg-debug-show (&optional beg end) | |
1110 ;; (interactive) | |
1111 ;; (message "--- hilit-chg-debug-show ---") | |
1112 ;; (hilit-chg-map-changes '(lambda (prop start end) | |
1113 ;; (message "%d-%d: %s" start end prop) | |
1114 ;; ) | |
1115 ;; beg end | |
1116 ;; )) | |
1117 ;; | |
1118 ;; ================== end of debug =============== | |
1119 | |
1120 (provide 'hilit-chg) | |
22958
cd50dfa70bb2
Delete the undo-in-progress compatibility code.
Richard M. Stallman <rms@gnu.org>
parents:
22957
diff
changeset
|
1121 |
22957 | 1122 ;;; hilit-chg.el ends here |