Mercurial > emacs
comparison lisp/compare-w.el @ 89909:68c22ea6027c
Sync to HEAD
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Fri, 16 Apr 2004 12:51:06 +0000 |
parents | 375f2633d815 |
children | 4c90ffeb71c5 |
comparison
equal
deleted
inserted
replaced
89908:ee1402f7b568 | 89909:68c22ea6027c |
---|---|
1 ;;; compare-w.el --- compare text between windows for Emacs | 1 ;;; compare-w.el --- compare text between windows for Emacs |
2 | 2 |
3 ;; Copyright (C) 1986, 1989, 1993, 1997 Free Software Foundation, Inc. | 3 ;; Copyright (C) 1986, 1989, 1993, 1997, 2003 Free Software Foundation, Inc. |
4 | 4 |
5 ;; Maintainer: FSF | 5 ;; Maintainer: FSF |
6 ;; Keywords: convenience files | 6 ;; Keywords: convenience files |
7 | 7 |
8 ;; This file is part of GNU Emacs. | 8 ;; This file is part of GNU Emacs. |
35 "Compare text between windows." | 35 "Compare text between windows." |
36 :prefix "compare-" | 36 :prefix "compare-" |
37 :group 'tools) | 37 :group 'tools) |
38 | 38 |
39 (defcustom compare-windows-whitespace "\\(\\s-\\|\n\\)+" | 39 (defcustom compare-windows-whitespace "\\(\\s-\\|\n\\)+" |
40 "*Regexp that defines whitespace sequences for \\[compare-windows]. | 40 "*Regexp or function that defines whitespace sequences for `compare-windows'. |
41 That command optionally ignores changes in whitespace. | 41 That command optionally ignores changes in whitespace. |
42 | 42 |
43 The value of `compare-windows-whitespace' is normally a regexp, but it | 43 The value of `compare-windows-whitespace' is normally a regexp, but it |
44 can also be a function. The function's job is to categorize any | 44 can also be a function. The function's job is to categorize any |
45 whitespace around (including before) point; it should also advance | 45 whitespace around (including before) point; it should also advance |
46 past any whitespace. The function is called in each buffer, with | 46 past any whitespace. The function is called in each window, with |
47 point at the current scanning point. It gets one argument, the point | 47 point at the current scanning point. It gets one argument, the point |
48 where `compare-windows' was originally called; it should not look at | 48 where \\[compare-windows] was originally called; it should not look at |
49 any text before that point. | 49 any text before that point. |
50 | 50 |
51 If the function returns the same value for both buffers, then the | 51 If the function returns the same value for both windows, then the |
52 whitespace is considered to match, and is skipped." | 52 whitespace is considered to match, and is skipped." |
53 :type '(choice regexp function) | 53 :type '(choice regexp function) |
54 :group 'compare-w) | 54 :group 'compare-w) |
55 | 55 |
56 (defcustom compare-ignore-whitespace nil | |
57 "*Non-nil means `compare-windows' ignores whitespace." | |
58 :type 'boolean | |
59 :group 'compare-w) | |
60 | |
56 (defcustom compare-ignore-case nil | 61 (defcustom compare-ignore-case nil |
57 "*Non-nil means \\[compare-windows] ignores case differences." | 62 "*Non-nil means `compare-windows' ignores case differences." |
58 :type 'boolean | 63 :type 'boolean |
59 :group 'compare-w) | 64 :group 'compare-w) |
65 | |
66 (defcustom compare-windows-sync 'compare-windows-sync-default-function | |
67 "*Function or regexp that is used to synchronize points in two | |
68 windows if before calling `compare-windows' points are located | |
69 on mismatched positions. | |
70 | |
71 The value of `compare-windows-sync' can be a function. The | |
72 function's job is to advance points in both windows to the next | |
73 matching text. If the value of `compare-windows-sync' is a | |
74 regexp, then points in both windows are advanced to the next | |
75 occurrence of this regexp. | |
76 | |
77 The current default value is the general function | |
78 `compare-windows-sync-default-function' that is able to | |
79 synchronize points by using quadratic algorithm to find the first | |
80 matching 32-character string in two windows. | |
81 | |
82 The other useful values of this variable could be such functions | |
83 as `forward-word', `forward-sentence', `forward-paragraph', or a | |
84 regexp containing some field separator or a newline, depending on | |
85 the nature of the difference units separator. The variable can | |
86 be made buffer-local. | |
87 | |
88 If the value of this variable is `nil', then function `ding' is | |
89 called to beep or flash the screen when points are mismatched." | |
90 :type '(choice regexp function) | |
91 :group 'compare-w) | |
92 | |
93 (defcustom compare-windows-sync-string-size 32 | |
94 "*Size of string from one window that is searched in second window. | |
95 | |
96 Small number makes difference regions more fine-grained, but it | |
97 may fail by finding the wrong match. The bigger number makes | |
98 difference regions more coarse-grained. | |
99 | |
100 The default value 32 is good for the most cases." | |
101 :type 'integer | |
102 :group 'compare-w) | |
103 | |
104 (defcustom compare-windows-recenter nil | |
105 "*List of two values, each of which is used as argument of | |
106 function `recenter' called in each of two windows to place | |
107 matching points side-by-side. | |
108 | |
109 The value `(-1 0)' is useful if windows are split vertically, | |
110 and the value `((4) (4))' for horizontally split windows." | |
111 :type '(list sexp sexp) | |
112 :group 'compare-w) | |
113 | |
114 (defcustom compare-windows-highlight t | |
115 "*Non-nil means compare-windows highlights the differences." | |
116 :type 'boolean | |
117 :group 'compare-w) | |
118 | |
119 (defface compare-windows-face | |
120 '((((type tty pc) (class color)) | |
121 (:background "turquoise3")) | |
122 (((class color) (background light)) | |
123 (:background "paleturquoise")) | |
124 (((class color) (background dark)) | |
125 (:background "paleturquoise4")) | |
126 (t (:underline t))) | |
127 "Face for highlighting of compare-windows difference regions." | |
128 :group 'compare-w) | |
129 | |
130 (defvar compare-windows-overlay1 nil) | |
131 (defvar compare-windows-overlay2 nil) | |
132 (defvar compare-windows-sync-point nil) | |
60 | 133 |
61 ;;;###autoload | 134 ;;;###autoload |
62 (defun compare-windows (ignore-whitespace) | 135 (defun compare-windows (ignore-whitespace) |
63 "Compare text in current window with text in next window. | 136 "Compare text in current window with text in next window. |
64 Compares the text starting at point in each window, | 137 Compares the text starting at point in each window, |
68 at the prior location of point in that window. | 141 at the prior location of point in that window. |
69 If both windows display the same buffer, | 142 If both windows display the same buffer, |
70 the mark is pushed twice in that buffer: | 143 the mark is pushed twice in that buffer: |
71 first in the other window, then in the selected window. | 144 first in the other window, then in the selected window. |
72 | 145 |
73 A prefix arg means ignore changes in whitespace. | 146 A prefix arg means reverse the value of variable |
74 The variable `compare-windows-whitespace' controls how whitespace is skipped. | 147 `compare-ignore-whitespace'. If `compare-ignore-whitespace' is |
75 If `compare-ignore-case' is non-nil, changes in case are also ignored." | 148 nil, then a prefix arg means ignore changes in whitespace. If |
149 `compare-ignore-whitespace' is non-nil, then a prefix arg means | |
150 don't ignore changes in whitespace. The variable | |
151 `compare-windows-whitespace' controls how whitespace is skipped. | |
152 If `compare-ignore-case' is non-nil, changes in case are also | |
153 ignored. | |
154 | |
155 If `compare-windows-sync' is non-nil, then successive calls of | |
156 this command work in interlaced mode: | |
157 on first call it advances points to the next difference, | |
158 on second call it synchronizes points by skipping the difference, | |
159 on third call it again advances points to the next difference and so on." | |
76 (interactive "P") | 160 (interactive "P") |
77 (let* (p1 p2 maxp1 maxp2 b1 b2 w2 | 161 (let* (p1 p2 maxp1 maxp2 b1 b2 w2 |
78 (progress 1) | 162 (progress 1) |
79 (opoint1 (point)) | 163 (opoint1 (point)) |
80 opoint2 | 164 opoint2 |
81 (skip-func (if ignore-whitespace | 165 (skip-func (if (if ignore-whitespace ; XOR |
82 (if (stringp compare-windows-whitespace) | 166 (not compare-ignore-whitespace) |
83 'compare-windows-skip-whitespace | 167 compare-ignore-whitespace) |
84 compare-windows-whitespace)))) | 168 (if (stringp compare-windows-whitespace) |
169 'compare-windows-skip-whitespace | |
170 compare-windows-whitespace))) | |
171 (sync-func (if (stringp compare-windows-sync) | |
172 'compare-windows-sync-regexp | |
173 compare-windows-sync))) | |
85 (setq p1 (point) b1 (current-buffer)) | 174 (setq p1 (point) b1 (current-buffer)) |
86 (setq w2 (next-window (selected-window))) | 175 (setq w2 (next-window (selected-window))) |
87 (if (eq w2 (selected-window)) | 176 (if (eq w2 (selected-window)) |
88 (setq w2 (next-window (selected-window) nil 'visible))) | 177 (setq w2 (next-window (selected-window) nil 'visible))) |
89 (if (eq w2 (selected-window)) | 178 (if (eq w2 (selected-window)) |
97 (push-mark p2 t) | 186 (push-mark p2 t) |
98 (setq maxp2 (point-max))) | 187 (setq maxp2 (point-max))) |
99 (push-mark) | 188 (push-mark) |
100 | 189 |
101 (while (> progress 0) | 190 (while (> progress 0) |
102 ;; If both buffers have whitespace next to point, | 191 ;; If both windows have whitespace next to point, |
103 ;; optionally skip over it. | 192 ;; optionally skip over it. |
104 | |
105 (and skip-func | 193 (and skip-func |
106 (save-excursion | 194 (save-excursion |
107 (let (p1a p2a w1 w2 result1 result2) | 195 (let (p1a p2a w1 w2 result1 result2) |
108 (setq result1 (funcall skip-func opoint1)) | 196 (setq result1 (funcall skip-func opoint1)) |
109 (setq p1a (point)) | 197 (setq p1a (point)) |
122 b1 p1 (+ size p1))) | 210 b1 p1 (+ size p1))) |
123 (setq progress (if (zerop progress) size (1- (abs progress)))) | 211 (setq progress (if (zerop progress) size (1- (abs progress)))) |
124 (setq p1 (+ p1 progress) p2 (+ p2 progress))) | 212 (setq p1 (+ p1 progress) p2 (+ p2 progress))) |
125 ;; Advance point now rather than later, in case we're interrupted. | 213 ;; Advance point now rather than later, in case we're interrupted. |
126 (goto-char p1) | 214 (goto-char p1) |
127 (set-window-point w2 p2)) | 215 (set-window-point w2 p2) |
216 (when compare-windows-recenter | |
217 (recenter (car compare-windows-recenter)) | |
218 (with-selected-window w2 (recenter (cadr compare-windows-recenter))))) | |
128 | 219 |
129 (if (= (point) opoint1) | 220 (if (= (point) opoint1) |
130 (ding)))) | 221 (if (not sync-func) |
222 (ding) | |
223 ;; If points are not advanced (i.e. already on mismatch position), | |
224 ;; then synchronize points between both windows | |
225 (save-excursion | |
226 (setq compare-windows-sync-point nil) | |
227 (funcall sync-func) | |
228 (setq p1 (point)) | |
229 (set-buffer b2) | |
230 (goto-char p2) | |
231 (funcall sync-func) | |
232 (setq p2 (point))) | |
233 (goto-char p1) | |
234 (set-window-point w2 p2) | |
235 (when compare-windows-recenter | |
236 (recenter (car compare-windows-recenter)) | |
237 (with-selected-window w2 (recenter (cadr compare-windows-recenter)))) | |
238 ;; If points are still not synchronized, then ding | |
239 (when (and (= p1 opoint1) (= p2 opoint2)) | |
240 ;; Display error message when current points in two windows | |
241 ;; are unmatched and next matching points can't be found. | |
242 (compare-windows-dehighlight) | |
243 (ding) | |
244 (message "No more matching points")))))) | |
131 | 245 |
132 ;; Move forward over whatever might be called whitespace. | 246 ;; Move forward over whatever might be called whitespace. |
133 ;; compare-windows-whitespace is a regexp that matches whitespace. | 247 ;; compare-windows-whitespace is a regexp that matches whitespace. |
134 ;; Match it at various starting points before the original point | 248 ;; Match it at various starting points before the original point |
135 ;; and find the latest point at which a match ends. | 249 ;; and find the latest point at which a match ends. |
136 ;; Don't try starting points before START, though. | 250 ;; Don't try starting points before START, though. |
137 ;; Value is non-nil if whitespace is found. | 251 ;; Value is non-nil if whitespace is found. |
138 | |
139 ;; If there is whitespace before point, but none after, | 252 ;; If there is whitespace before point, but none after, |
140 ;; then return t, but don't advance point. | 253 ;; then return t, but don't advance point. |
141 (defun compare-windows-skip-whitespace (start) | 254 (defun compare-windows-skip-whitespace (start) |
142 (let ((end (point)) | 255 (let ((end (point)) |
143 (beg (point)) | 256 (beg (point)) |
157 (setq beg (point)) | 270 (setq beg (point)) |
158 (goto-char end) | 271 (goto-char end) |
159 (or (/= beg opoint) | 272 (or (/= beg opoint) |
160 (/= end opoint)))) | 273 (/= end opoint)))) |
161 | 274 |
275 ;; Move forward to the next synchronization regexp. | |
276 (defun compare-windows-sync-regexp () | |
277 (if (stringp compare-windows-sync) | |
278 (re-search-forward compare-windows-sync nil t))) | |
279 | |
280 ;; Function works in two passes: one call on each window. | |
281 ;; On the first call both matching points are computed, | |
282 ;; and one of them is stored in compare-windows-sync-point | |
283 ;; to be used when this function is called on second window. | |
284 (defun compare-windows-sync-default-function () | |
285 (if (not compare-windows-sync-point) | |
286 (let* ((w2 (next-window (selected-window))) | |
287 (b2 (window-buffer w2)) | |
288 (point-max2 (with-current-buffer b2 (point-max))) | |
289 (op2 (window-point w2)) | |
290 (op1 (point)) | |
291 (region-size compare-windows-sync-string-size) | |
292 (string-size compare-windows-sync-string-size) | |
293 in-bounds-p s1 p2 p12s p12) | |
294 (while (and | |
295 ;; until matching points are found | |
296 (not p12s) | |
297 ;; until size exceeds the maximum points of both buffers | |
298 ;; (bounds below take care to not overdo in each of them) | |
299 (or (setq in-bounds-p (< region-size (max (- (point-max) op1) | |
300 (- point-max2 op2)))) | |
301 ;; until string size becomes smaller than 4 | |
302 (> string-size 4))) | |
303 (if in-bounds-p | |
304 ;; make the next search in the double-sized region; | |
305 ;; on first iteration it is 2*compare-windows-sync-string-size, | |
306 ;; on last iterations it exceeds both buffers maximum points | |
307 (setq region-size (* region-size 2)) | |
308 ;; if region size exceeds the maximum points of both buffers, | |
309 ;; then start to halve the string size until 4; | |
310 ;; this helps to find differences near the end of buffers | |
311 (setq string-size (/ string-size 2))) | |
312 (let ((p1 op1) | |
313 (bound1 (- (min (+ op1 region-size) (point-max)) string-size)) | |
314 (bound2 (min (+ op2 region-size) point-max2))) | |
315 (while (< p1 bound1) | |
316 (setq s1 (buffer-substring-no-properties p1 (+ p1 string-size))) | |
317 (setq p2 (with-current-buffer b2 | |
318 (goto-char op2) | |
319 (let ((case-fold-search compare-ignore-case)) | |
320 (search-forward s1 bound2 t)))) | |
321 (when p2 | |
322 (setq p2 (- p2 string-size)) | |
323 (setq p12s (cons (list (+ p1 p2) p1 p2) p12s))) | |
324 (setq p1 (1+ p1))))) | |
325 (when p12s | |
326 ;; use closest matching points (i.e. points with minimal sum) | |
327 (setq p12 (cdr (assq (apply 'min (mapcar 'car p12s)) p12s))) | |
328 (goto-char (car p12)) | |
329 (compare-windows-highlight op1 (car p12) op2 (cadr p12) b2)) | |
330 (setq compare-windows-sync-point (or (cadr p12) t))) | |
331 ;; else set point in the second window to the pre-calculated value | |
332 (if (numberp compare-windows-sync-point) | |
333 (goto-char compare-windows-sync-point)) | |
334 (setq compare-windows-sync-point nil))) | |
335 | |
336 ;; Highlight differences | |
337 (defun compare-windows-highlight (beg1 end1 beg2 end2 buf2) | |
338 (when compare-windows-highlight | |
339 (if compare-windows-overlay1 | |
340 (move-overlay compare-windows-overlay1 beg1 end1 (current-buffer)) | |
341 (setq compare-windows-overlay1 (make-overlay beg1 end1 (current-buffer))) | |
342 (overlay-put compare-windows-overlay1 'face 'compare-windows-face) | |
343 (overlay-put compare-windows-overlay1 'priority 1)) | |
344 (if compare-windows-overlay2 | |
345 (move-overlay compare-windows-overlay2 beg2 end2 buf2) | |
346 (setq compare-windows-overlay2 (make-overlay beg2 end2 buf2)) | |
347 (overlay-put compare-windows-overlay2 'face 'compare-windows-face) | |
348 (overlay-put compare-windows-overlay2 'priority 1)) | |
349 ;; Remove highlighting before next command is executed | |
350 (add-hook 'pre-command-hook 'compare-windows-dehighlight))) | |
351 | |
352 (defun compare-windows-dehighlight () | |
353 "Remove highlighting created by `compare-windows-highlight'." | |
354 (interactive) | |
355 (remove-hook 'pre-command-hook 'compare-windows-dehighlight) | |
356 (and compare-windows-overlay1 (delete-overlay compare-windows-overlay1)) | |
357 (and compare-windows-overlay2 (delete-overlay compare-windows-overlay2))) | |
358 | |
162 (provide 'compare-w) | 359 (provide 'compare-w) |
163 | 360 |
361 ;;; arch-tag: 4177aab1-48e6-4a98-b7a1-000ee285de46 | |
164 ;;; compare-w.el ends here | 362 ;;; compare-w.el ends here |