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