Mercurial > emacs
annotate lisp/hilit-chg.el @ 42847:094039817cba
Move user variables to w32-vars.el and make them defcustoms.
author | Jason Rumney <jasonr@gnu.org> |
---|---|
date | Sun, 20 Jan 2002 15:33:07 +0000 |
parents | d1ec9ab9d09d |
children | 90bce6424b8b |
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' |
41701 | 295 \(effectively disabling the mode). |
22957 | 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) |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
592 (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t)) |
22957 | 593 |
594 (defun hilit-chg-clear () | |
595 "Remove Highlight Changes mode for this buffer. | |
596 This removes all saved change information." | |
597 (if buffer-read-only | |
598 ;; We print the buffer name because this function could be called | |
599 ;; on many buffers from `global-highlight-changes'. | |
600 (message "Cannot remove highlighting from read-only mode buffer %s" | |
601 (buffer-name)) | |
602 (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t) | |
603 (let ((after-change-functions nil)) | |
604 (hilit-chg-hide-changes) | |
605 (hilit-chg-map-changes | |
606 '(lambda (prop start stop) | |
607 (remove-text-properties start stop '(hilit-chg nil)))) | |
608 ) | |
609 (setq highlight-changes-mode nil) | |
610 (force-mode-line-update) | |
611 ;; If we type: C-u -1 M-x highlight-changes-mode | |
612 ;; we want to turn it off, but hilit-chg-post-command-hook | |
613 ;; 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
|
614 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook))) |
22957 | 615 |
616 ;;;###autoload | |
617 (defun highlight-changes-mode (&optional arg) | |
618 "Toggle (or initially set) Highlight Changes mode. | |
619 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
620 Without an argument: |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
621 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
|
622 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
|
623 `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
|
624 and passive state. |
22957 | 625 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
626 With an argument ARG: |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
627 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
|
628 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
|
629 If ARG is negative, disable Highlight Changes mode completely. |
22957 | 630 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
631 Active state - means changes are shown in a distinctive face. |
22957 | 632 Passive state - means changes are kept and new ones recorded but are |
633 not displayed in a different face. | |
634 | |
635 Functions: | |
636 \\[highlight-changes-next-change] - move point to beginning of next change | |
637 \\[highlight-changes-previous-change] - move to beginning of previous change | |
23289 | 638 \\[highlight-compare-with-file] - mark text as changed by comparing this |
639 buffer with the contents of a file | |
22957 | 640 \\[highlight-changes-remove-highlight] - remove the change face from the region |
641 \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes \ | |
642 through | |
643 various faces. | |
644 | |
645 Hook variables: | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
646 `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
|
647 `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
|
648 `highlight-changes-disable-hook' - when turning off Highlight Changes mode." |
22957 | 649 (interactive "P") |
29204
d38a98cddcec
(highlight-changes-mode): Ask about color or
Eli Zaretskii <eliz@gnu.org>
parents:
25533
diff
changeset
|
650 (if (or (display-color-p) |
d38a98cddcec
(highlight-changes-mode): Ask about color or
Eli Zaretskii <eliz@gnu.org>
parents:
25533
diff
changeset
|
651 (and (fboundp 'x-display-grayscale-p) (x-display-grayscale-p))) |
22957 | 652 (let ((was-on highlight-changes-mode) |
653 (new-highlight-changes-mode | |
654 (cond | |
655 ((null arg) | |
656 ;; no arg => toggle (or set to active initially) | |
657 (if highlight-changes-mode | |
658 (if (eq highlight-changes-mode 'active) 'passive 'active) | |
659 highlight-changes-initial-state)) | |
660 ;; an argument is given | |
661 ((eq arg 'active) | |
662 'active) | |
663 ((eq arg 'passive) | |
664 'passive) | |
665 ((> (prefix-numeric-value arg) 0) | |
666 'active) | |
667 ((< (prefix-numeric-value arg) 0) | |
668 nil) | |
669 (t | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
670 'passive)))) |
22957 | 671 (if new-highlight-changes-mode |
672 ;; mode is turned on -- but may be passive | |
673 (progn | |
674 (hilit-chg-set new-highlight-changes-mode) | |
675 (or was-on | |
676 ;; run highlight-changes-enable-hook once | |
677 (run-hooks 'highlight-changes-enable-hook)) | |
678 (run-hooks 'highlight-changes-toggle-hook)) | |
679 ;; mode is turned off | |
680 (run-hooks 'highlight-changes-disable-hook) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
681 (hilit-chg-clear))) |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
682 (message "Highlight Changes mode requires color or grayscale display"))) |
22957 | 683 |
684 ;;;###autoload | |
685 (defun highlight-changes-next-change () | |
686 "Move to the beginning of the next change, if in Highlight Changes mode." | |
687 (interactive) | |
688 (if highlight-changes-mode | |
689 (let ((start (point)) | |
690 prop) | |
691 (setq prop (get-text-property (point) 'hilit-chg)) | |
692 (if prop | |
693 ;; we are in a change | |
694 (setq start (next-single-property-change (point) 'hilit-chg))) | |
695 (if start | |
696 (setq start (next-single-property-change start 'hilit-chg))) | |
697 (if start | |
698 (goto-char start) | |
699 (message "no next change"))) | |
700 (message "This buffer is not in Highlight Changes mode."))) | |
701 | |
702 | |
703 ;;;###autoload | |
704 (defun highlight-changes-previous-change () | |
705 "Move to the beginning of the previous change, if in Highlight Changes mode." | |
706 (interactive) | |
707 (if highlight-changes-mode | |
708 (let ( (start (point)) (prop nil) ) | |
709 (or (bobp) | |
710 (setq prop (get-text-property (1- (point)) 'hilit-chg))) | |
711 (if prop | |
712 ;; we are in a change | |
713 (setq start (previous-single-property-change (point) 'hilit-chg))) | |
714 (if start | |
715 (setq start (previous-single-property-change start 'hilit-chg))) | |
716 ;; special handling for the case where (point-min) is a change | |
717 (if start | |
718 (setq start (or (previous-single-property-change start 'hilit-chg) | |
719 (if (get-text-property (point-min) 'hilit-chg) | |
720 (point-min))))) | |
721 (if start | |
722 (goto-char start) | |
723 (message "no previous change"))) | |
724 (message "This buffer is not in Highlight Changes mode."))) | |
725 | |
726 ;; ======================================================================== | |
727 | |
728 (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
|
729 "Construct `hilit-chg-list' and `highlight-changes-face-list'." |
22957 | 730 ;; Constructs highlight-changes-face-list if necessary, |
731 ;; and hilit-chg-list always: | |
732 ;; Maybe this should always be called when rotating a face | |
733 ;; so we pick up any changes? | |
734 (if (or (null highlight-changes-face-list) ; Don't do it if it | |
735 force) ; already exists unless FORCE non-nil. | |
736 (let ((p highlight-changes-colours) | |
737 (n 1) name) | |
738 (setq highlight-changes-face-list nil) | |
739 (while p | |
740 (setq name (intern (format "highlight-changes-face-%d" n))) | |
741 (copy-face 'highlight-changes-face name) | |
742 (set-face-foreground name (car p)) | |
743 (setq highlight-changes-face-list | |
744 (append highlight-changes-face-list (list name))) | |
745 (setq p (cdr p)) | |
746 (setq n (1+ n))))) | |
747 (setq hilit-chg-list (list 'hilit-chg 'highlight-changes-face)) | |
748 (let ((p highlight-changes-face-list) | |
749 (n 1) | |
750 last-category last-face) | |
751 (while p | |
752 (setq last-category (intern (format "change-%d" n))) | |
753 ;; (setq last-face (intern (format "highlight-changes-face-%d" n))) | |
754 (setq last-face (car p)) | |
755 (setq hilit-chg-list | |
756 (append hilit-chg-list | |
757 (list last-category last-face))) | |
758 (setq p (cdr p)) | |
759 (setq n (1+ n))) | |
760 (setq hilit-chg-list | |
761 (append hilit-chg-list | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
762 (list last-category last-face))))) |
22957 | 763 |
764 (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
|
765 "Increment (age) the Highlight Changes mode text property." |
22957 | 766 (let ( new-prop ) |
767 (if (eq prop 'hilit-chg-delete) | |
768 (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
|
769 (setq new-prop (nth 2 (member prop hilit-chg-list)))) |
22957 | 770 (if prop |
771 (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
|
772 (message "%d-%d unknown property %s not changed" start end prop)))) |
22957 | 773 |
774 ;;;###autoload | |
775 (defun highlight-changes-rotate-faces () | |
776 "Rotate the faces used by Highlight Changes mode. | |
777 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
778 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
|
779 of `highlight-changes-face-list', one level older changes are shown in |
22957 | 780 face described by the second element, and so on. Very old changes remain |
781 shown in the last face in the list. | |
782 | |
783 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
|
784 by adding the following to `local-write-file-hooks', by evaling it in the |
22957 | 785 buffer to be saved): |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
786 |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
787 \(add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)" |
22957 | 788 (interactive) |
789 ;; If not in active mode do nothing but don't complain because this | |
790 ;; may be bound to a hook. | |
791 (if (eq highlight-changes-mode 'active) | |
792 (let ((after-change-functions nil)) | |
793 ;; ensure hilit-chg-list is made and up to date | |
794 (hilit-chg-make-list) | |
795 ;; remove our existing overlays | |
796 (hilit-chg-hide-changes) | |
797 ;; for each change text property, increment it | |
798 (hilit-chg-map-changes 'hilit-chg-bump-change) | |
799 ;; and display them all if active | |
800 (if (eq highlight-changes-mode 'active) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
801 (hilit-chg-display-changes)))) |
22957 | 802 ;; This always returns nil so it is safe to use in |
803 ;; local-write-file-hook | |
804 nil) | |
805 | |
806 ;; ======================================================================== | |
807 ;; Comparing with an existing file. | |
808 ;; This uses ediff to find the differences. | |
809 | |
810 ;;;###autoload | |
23288
17dd9ecf84f1
(highlight-compare-with-file): Renamed from
Karl Heuer <kwzh@gnu.org>
parents:
23101
diff
changeset
|
811 (defun highlight-compare-with-file (file-b) |
17dd9ecf84f1
(highlight-compare-with-file): Renamed from
Karl Heuer <kwzh@gnu.org>
parents:
23101
diff
changeset
|
812 "Compare this buffer with a file, and highlight differences. |
22957 | 813 |
814 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
|
815 and must not be read-only. |
22957 | 816 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
817 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
|
818 this function is called interactively. |
22957 | 819 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
820 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
|
821 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
|
822 read in temporarily but the buffer is deleted. |
22957 | 823 |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
824 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
|
825 changes are made, so \\[highlight-changes-next-change] and |
22957 | 826 \\[highlight-changes-previous-change] will not work." |
827 (interactive (list | |
828 (read-file-name | |
829 "File to compare with? " ;; prompt | |
830 "" ;; directory | |
831 nil ;; default | |
832 'yes ;; must exist | |
833 (let ((f (make-backup-file-name | |
834 (or (buffer-file-name (current-buffer)) | |
835 (error "no file for this buffer"))))) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
836 (if (file-exists-p f) f ""))))) |
22957 | 837 |
838 (let* ((buf-a (current-buffer)) | |
839 (buf-a-read-only buffer-read-only) | |
840 (orig-pos (point)) | |
841 (file-a (buffer-file-name)) | |
842 (existing-buf (get-file-buffer file-b)) | |
843 (buf-b (or existing-buf | |
844 (find-file-noselect file-b))) | |
845 (buf-b-read-only (with-current-buffer buf-b buffer-read-only)) | |
846 xy xx yy p q | |
847 a-start a-end len-a | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
848 b-start b-end len-b) |
22957 | 849 |
850 ;; We use the fact that the buffer is not marked modified at the | |
851 ;; end where we clear its modified status | |
852 (if (buffer-modified-p buf-a) | |
853 (if (y-or-n-p (format "OK to save %s? " file-a)) | |
854 (save-buffer buf-a) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38401
diff
changeset
|
855 (error "Buffer must be saved before comparing with a file"))) |
22957 | 856 (if (and existing-buf (buffer-modified-p buf-b)) |
857 (if (y-or-n-p (format "OK to save %s? " file-b)) | |
858 (save-buffer buf-b) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38401
diff
changeset
|
859 (error "Cannot compare with a file in an unsaved buffer"))) |
22957 | 860 (highlight-changes-mode 'active) |
861 (if existing-buf (with-current-buffer buf-b | |
862 (highlight-changes-mode 'active))) | |
863 (save-window-excursion | |
864 (setq xy (hilit-chg-get-diff-info buf-a file-a buf-b file-b))) | |
865 (setq xx (car xy)) | |
866 (setq p xx) | |
867 (setq yy (car (cdr xy))) | |
868 (setq q yy) | |
869 (hilit-chg-make-list) | |
870 (while p | |
871 (setq a-start (nth 0 (car p))) | |
872 (setq a-end (nth 1 (car p))) | |
873 (setq b-start (nth 0 (car q))) | |
874 (setq b-end (nth 1 (car q))) | |
875 (setq len-a (- a-end a-start)) | |
876 (setq len-b (- b-end b-start)) | |
877 (set-buffer buf-a) | |
878 (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only) | |
879 (set-buffer-modified-p nil) | |
880 (goto-char orig-pos) | |
881 (if existing-buf | |
882 (with-current-buffer buf-b | |
883 (hilit-chg-set-face-on-change b-start b-end len-a | |
884 buf-b-read-only ) | |
885 )) | |
886 (setq p (cdr p)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
887 (setq q (cdr q))) |
22957 | 888 (if existing-buf |
889 (set-buffer-modified-p nil) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
890 (kill-buffer buf-b)))) |
22957 | 891 |
892 | |
893 (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b) | |
894 (let ((e nil) x y) ;; e is set by function hilit-chg-get-diff-list-hk | |
895 (ediff-setup buf-a file-a buf-b file-b | |
896 nil nil ; buf-c file-C | |
897 'hilit-chg-get-diff-list-hk | |
898 (list (cons 'ediff-job-name 'something)) | |
899 ) | |
900 (ediff-with-current-buffer e (ediff-really-quit nil)) | |
901 (list x y))) | |
902 | |
903 | |
904 (defun hilit-chg-get-diff-list-hk () | |
905 ;; x and y are dynamically bound by hilit-chg-get-diff-info | |
906 ;; which calls this function as a hook | |
907 (defvar x) ;; placate the byte-compiler | |
908 (defvar y) | |
909 (setq e (current-buffer)) | |
910 (let ((n 0) extent p va vb a b) | |
911 (setq x nil y nil) ;; x and y are bound by hilit-chg-get-diff-info | |
912 (while (< n ediff-number-of-differences) | |
913 (ediff-make-fine-diffs n) | |
914 (setq va (ediff-get-fine-diff-vector n 'A)) | |
915 ;; va is a vector if there are fine differences | |
916 (if va | |
917 (setq a (append va nil)) | |
918 ;; if not, get the unrefined difference | |
919 (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
|
920 (setq a (list (elt va 0)))) |
22957 | 921 ;; a list a list |
922 (setq p a) | |
923 (while p | |
924 (setq extent (list (overlay-start (car p)) | |
925 (overlay-end (car p)))) | |
926 (setq p (cdr p)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
927 (setq x (append x (list extent) )));; while p |
22957 | 928 ;; |
929 (setq vb (ediff-get-fine-diff-vector n 'B)) | |
930 ;; vb is a vector | |
931 (if vb | |
932 (setq b (append vb nil)) | |
933 ;; if not, get the unrefined difference | |
934 (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
|
935 (setq b (list (elt vb 0)))) |
22957 | 936 ;; b list a list |
937 (setq p b) | |
938 (while p | |
939 (setq extent (list (overlay-start (car p)) | |
940 (overlay-end (car p)))) | |
941 (setq p (cdr p)) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
942 (setq y (append y (list extent) ))) |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
943 (setq n (1+ n)));; while |
22957 | 944 ;; ediff-quit doesn't work here. |
945 ;; No point in returning a value, since this is a hook function. | |
946 )) | |
947 | |
948 ;; ======================= automatic stuff ============== | |
949 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
950 ;; Global Highlight Changes mode is modeled after Global Font-lock mode. |
22957 | 951 ;; Three hooks are used to gain control. When Global Changes Mode is |
952 ;; enabled, `find-file-hooks' and `change-major-mode-hook' are set. | |
953 ;; `find-file-hooks' is called when visiting a file, the new mode is | |
954 ;; known at this time. | |
955 ;; `change-major-mode-hook' is called when a buffer is changing mode. | |
956 ;; This could be because of finding a file in which case | |
957 ;; `find-file-hooks' has already been called and has done its work. | |
958 ;; However, it also catches the case where a new mode is being set by | |
959 ;; the user. However, it is called from `kill-all-variables' and at | |
960 ;; this time the mode is the old mode, which is not what we want. | |
961 ;; So, our function temporarily sets `post-command-hook' which will | |
962 ;; be called after the buffer has been completely set up (with the new | |
963 ;; mode). It then removes the `post-command-hook'. | |
964 ;; One other wrinkle - every M-x command runs the `change-major-mode-hook' | |
965 ;; so we ignore this by examining the buffer name. | |
966 | |
967 | |
968 (defun hilit-chg-major-mode-hook () | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
969 (add-hook 'post-command-hook 'hilit-chg-post-command-hook)) |
22957 | 970 |
971 (defun hilit-chg-post-command-hook () | |
972 ;; This is called after changing a major mode, but also after each | |
973 ;; M-x command, in which case the current buffer is a minibuffer. | |
974 ;; In that case, do not act on it here, but don't turn it off | |
975 ;; either, we will get called here again soon-after. | |
976 ;; Also, don't enable it for other special buffers. | |
977 (if (string-match "^[ *]" (buffer-name)) | |
978 nil ;; (message "ignoring this post-command-hook") | |
979 (remove-hook 'post-command-hook 'hilit-chg-post-command-hook) | |
980 ;; The following check isn't necessary, since | |
981 ;; hilit-chg-turn-on-maybe makes this check too. | |
982 (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
|
983 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state)))) |
22957 | 984 |
985 (defun hilit-chg-check-global () | |
986 ;; This is called from the find file hook. | |
987 (hilit-chg-turn-on-maybe highlight-changes-global-initial-state)) | |
988 | |
989 | |
990 ;;;###autoload | |
991 (defun global-highlight-changes (&optional arg) | |
992 "Turn on or off global Highlight Changes mode. | |
993 | |
994 When called interactively: | |
995 - if no prefix, toggle global Highlight Changes mode on or off | |
996 - if called with a positive prefix (or just C-u) turn it on in active mode | |
997 - if called with a zero prefix turn it on in passive mode | |
998 - if called with a negative prefix turn it off | |
999 | |
1000 When called from a program: | |
1001 - 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
|
1002 - 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
|
1003 - if ARG is `passive', turn it on in passive mode |
22957 | 1004 - otherwise just turn it on |
1005 | |
1006 When global Highlight Changes mode is enabled, Highlight Changes mode is turned | |
1007 on for future \"suitable\" buffers (and for \"suitable\" existing buffers if | |
1008 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
|
1009 \"Suitability\" is determined by variable `highlight-changes-global-modes'." |
22957 | 1010 |
1011 (interactive | |
1012 (list | |
1013 (cond | |
1014 ((null current-prefix-arg) | |
1015 ;; no arg => toggle it on/off | |
1016 (setq global-highlight-changes (not global-highlight-changes))) | |
1017 ;; positive interactive arg - turn it on as active | |
1018 ((> (prefix-numeric-value current-prefix-arg) 0) | |
1019 (setq global-highlight-changes t) | |
1020 'active) | |
1021 ;; zero interactive arg - turn it on as passive | |
1022 ((= (prefix-numeric-value current-prefix-arg) 0) | |
1023 (setq global-highlight-changes t) | |
1024 'passive) | |
1025 ;; negative interactive arg - turn it off | |
1026 (t | |
1027 (setq global-highlight-changes nil) | |
1028 nil)))) | |
1029 | |
1030 (if arg | |
1031 (progn | |
1032 (if (eq arg 'active) | |
1033 (setq highlight-changes-global-initial-state 'active) | |
1034 (if (eq arg 'passive) | |
1035 (setq highlight-changes-global-initial-state 'passive))) | |
1036 (setq global-highlight-changes t) | |
1037 (message "turning ON Global Highlight Changes mode in %s state" | |
1038 highlight-changes-global-initial-state) | |
1039 (add-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook) | |
1040 (add-hook 'find-file-hooks 'hilit-chg-check-global) | |
1041 (if highlight-changes-global-changes-existing-buffers | |
1042 (hilit-chg-update-all-buffers | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1043 highlight-changes-global-initial-state))) |
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1044 |
22957 | 1045 (message "turning OFF global Highlight Changes mode") |
1046 (remove-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook) | |
1047 (remove-hook 'find-file-hooks 'hilit-chg-check-global) | |
1048 (remove-hook 'post-command-hook | |
1049 'hilit-chg-post-command-hook) | |
1050 (remove-hook 'find-file-hooks 'hilit-chg-check-global) | |
1051 (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
|
1052 (hilit-chg-update-all-buffers nil)))) |
22957 | 1053 |
1054 | |
1055 (defun hilit-chg-turn-on-maybe (value) | |
1056 "Turn on Highlight Changes mode if it is appropriate for this buffer. | |
1057 | |
1058 A buffer is appropriate for Highlight Changes mode if all these are true: | |
1059 - the buffer is not a special buffer (one whose name begins with | |
1060 `*' or ` ') | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1061 - 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
|
1062 `highlight-changes-global-modes' |
22957 | 1063 - Highlight Changes mode is not already on for this buffer. |
1064 | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1065 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
|
1066 from `global-highlight-changes' when turning on global Highlight Changes mode." |
22957 | 1067 (or highlight-changes-mode ; do nothing if already on |
1068 (if | |
1069 (cond | |
1070 ((null highlight-changes-global-modes) | |
1071 nil) | |
1072 ((functionp highlight-changes-global-modes) | |
1073 (funcall highlight-changes-global-modes)) | |
1074 ((listp highlight-changes-global-modes) | |
1075 (if (eq (car-safe highlight-changes-global-modes) 'not) | |
1076 (not (memq major-mode (cdr highlight-changes-global-modes))) | |
1077 (memq major-mode highlight-changes-global-modes))) | |
1078 (t | |
1079 (and | |
1080 (not (string-match "^[ *]" (buffer-name))) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1081 (buffer-file-name)))) |
22957 | 1082 (progn |
1083 (hilit-chg-set value) | |
30962
b309b17a6025
General cleanup of doc strings, comments and
Gerd Moellmann <gerd@gnu.org>
parents:
30924
diff
changeset
|
1084 (run-hooks 'highlight-changes-enable-hook))))) |
22957 | 1085 |
1086 | |
1087 (defun hilit-chg-turn-off-maybe () | |
1088 (if highlight-changes-mode | |
1089 (progn | |
1090 (run-hooks 'highlight-changes-disable-hook) | |
1091 (hilit-chg-clear)))) | |
1092 | |
1093 | |
1094 (defun hilit-chg-update-all-buffers (value) | |
1095 (mapcar | |
1096 (function (lambda (buffer) | |
1097 (with-current-buffer buffer | |
1098 (if value | |
1099 (hilit-chg-turn-on-maybe value) | |
1100 (hilit-chg-turn-off-maybe)) | |
1101 ))) | |
1102 (buffer-list))) | |
1103 | |
1104 ;; ===================== debug ================== | |
1105 ;; For debug & test use: | |
1106 ;; | |
1107 ;; (defun hilit-chg-debug-show (&optional beg end) | |
1108 ;; (interactive) | |
1109 ;; (message "--- hilit-chg-debug-show ---") | |
1110 ;; (hilit-chg-map-changes '(lambda (prop start end) | |
1111 ;; (message "%d-%d: %s" start end prop) | |
1112 ;; ) | |
1113 ;; beg end | |
1114 ;; )) | |
1115 ;; | |
1116 ;; ================== end of debug =============== | |
1117 | |
1118 (provide 'hilit-chg) | |
22958
cd50dfa70bb2
Delete the undo-in-progress compatibility code.
Richard M. Stallman <rms@gnu.org>
parents:
22957
diff
changeset
|
1119 |
22957 | 1120 ;;; hilit-chg.el ends here |