1 ;;; compare-w.el --- compare text between windows for Emacs
3 ;; Copyright (C) 1986,1989,1993,1997,2003,2004 Free Software Foundation, Inc.
6 ;; Keywords: convenience files
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
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.
27 ;; This package provides one entry point, compare-windows. It compares
28 ;; text starting from point in two adjacent windows, advancing point
29 ;; until it finds a difference. Option variables permit you to ignore
30 ;; whitespace differences, or case differences, or both.
34 (defgroup compare-w nil
35 "Compare text between windows."
39 (defcustom compare-windows-whitespace
"\\(\\s-\\|\n\\)+"
40 "*Regexp or function that defines whitespace sequences for `compare-windows'.
41 That command optionally ignores changes in whitespace.
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
45 whitespace around (including before) point; it should also advance
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
48 where \\[compare-windows] was originally called; it should not look at
49 any text before that point.
51 If the function returns the same value for both windows, then the
52 whitespace is considered to match, and is skipped."
53 :type
'(choice regexp function
)
56 (defcustom compare-ignore-whitespace nil
57 "*Non-nil means `compare-windows' ignores whitespace."
61 (defcustom compare-ignore-case nil
62 "*Non-nil means `compare-windows' ignores case differences."
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.
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.
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.
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
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
)
93 (defcustom compare-windows-sync-string-size
32
94 "*Size of string from one window that is searched in second window.
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.
100 The default value 32 is good for the most cases."
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.
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
)
114 (defcustom compare-windows-highlight t
115 "*Non-nil means compare-windows highlights the differences."
119 (defface compare-windows-face
120 '((((class color
) (min-colors 88) (background light
))
121 (:background
"paleturquoise"))
122 (((class color
) (min-colors 88) (background dark
))
123 (:background
"paleturquoise4"))
125 (:background
"turquoise3"))
127 "Face for highlighting of compare-windows difference regions."
130 (defvar compare-windows-overlay1 nil
)
131 (defvar compare-windows-overlay2 nil
)
132 (defvar compare-windows-sync-point nil
)
135 (defun compare-windows (ignore-whitespace)
136 "Compare text in current window with text in next window.
137 Compares the text starting at point in each window,
138 moving over text in each one as far as they match.
140 This command pushes the mark in each window
141 at the prior location of point in that window.
142 If both windows display the same buffer,
143 the mark is pushed twice in that buffer:
144 first in the other window, then in the selected window.
146 A prefix arg means reverse the value of variable
147 `compare-ignore-whitespace'. If `compare-ignore-whitespace' is
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
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."
161 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
165 (skip-func (if (if ignore-whitespace
; XOR
166 (not compare-ignore-whitespace
)
167 compare-ignore-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
)))
174 (setq p1
(point) b1
(current-buffer))
175 (setq w2
(next-window (selected-window)))
176 (if (eq w2
(selected-window))
177 (setq w2
(next-window (selected-window) nil
'visible
)))
178 (if (eq w2
(selected-window))
179 (error "No other window"))
180 (setq p2
(window-point w2
)
181 b2
(window-buffer w2
))
183 (setq maxp1
(point-max))
187 (setq maxp2
(point-max)))
190 (while (> progress
0)
191 ;; If both windows have whitespace next to point,
192 ;; optionally skip over it.
195 (let (p1a p2a w1 w2 result1 result2
)
196 (setq result1
(funcall skip-func opoint1
))
200 (setq result2
(funcall skip-func opoint2
))
202 (if (or (stringp compare-windows-whitespace
)
203 (and result1 result2
(eq result1 result2
)))
207 (let ((size (min (- maxp1 p1
) (- maxp2 p2
)))
208 (case-fold-search compare-ignore-case
))
209 (setq progress
(compare-buffer-substrings b2 p2
(+ size p2
)
211 (setq progress
(if (zerop progress
) size
(1- (abs progress
))))
212 (setq p1
(+ p1 progress
) p2
(+ p2 progress
)))
213 ;; Advance point now rather than later, in case we're interrupted.
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
)))))
220 (if (= (point) opoint1
)
223 ;; If points are not advanced (i.e. already on mismatch position),
224 ;; then synchronize points between both windows
226 (setq compare-windows-sync-point nil
)
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)
244 (message "No more matching points"))))))
246 ;; Move forward over whatever might be called whitespace.
247 ;; compare-windows-whitespace is a regexp that matches whitespace.
248 ;; Match it at various starting points before the original point
249 ;; and find the latest point at which a match ends.
250 ;; Don't try starting points before START, though.
251 ;; Value is non-nil if whitespace is found.
252 ;; If there is whitespace before point, but none after,
253 ;; then return t, but don't advance point.
254 (defun compare-windows-skip-whitespace (start)
258 (while (or (and (looking-at compare-windows-whitespace
)
259 (<= end
(match-end 0))
260 ;; This match goes past END, so advance END.
261 (progn (setq end
(match-end 0))
263 (and (/= (point) start
)
264 ;; Consider at least the char before point,
265 ;; unless it is also before START.
267 ;; keep going back until whitespace
268 ;; doesn't extend to or past end
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
)))
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* ((w1 (selected-window))
287 (w2 (next-window w1
))
288 (b2 (window-buffer w2
))
289 (point-max2 (with-current-buffer b2
(point-max)))
290 (op2 (window-point w2
))
292 (region-size compare-windows-sync-string-size
)
293 (string-size compare-windows-sync-string-size
)
294 in-bounds-p s1 p2 p12s p12
)
296 ;; until matching points are found
298 ;; until size exceeds the maximum points of both buffers
299 ;; (bounds below take care to not overdo in each of them)
300 (or (setq in-bounds-p
(< region-size
(max (- (point-max) op1
)
301 (- point-max2 op2
))))
302 ;; until string size becomes smaller than 4
305 ;; make the next search in the double-sized region;
306 ;; on first iteration it is 2*compare-windows-sync-string-size,
307 ;; on last iterations it exceeds both buffers maximum points
308 (setq region-size
(* region-size
2))
309 ;; if region size exceeds the maximum points of both buffers,
310 ;; then start to halve the string size until 4;
311 ;; this helps to find differences near the end of buffers
312 (setq string-size
(/ string-size
2)))
314 (bound1 (- (min (+ op1 region-size
) (point-max)) string-size
))
315 (bound2 (min (+ op2 region-size
) point-max2
)))
317 (setq s1
(buffer-substring-no-properties p1
(+ p1 string-size
)))
318 (setq p2
(with-current-buffer b2
320 (let ((case-fold-search compare-ignore-case
))
321 (search-forward s1 bound2 t
))))
323 (setq p2
(- p2 string-size
))
324 (setq p12s
(cons (list (+ p1 p2
) p1 p2
) p12s
)))
327 ;; use closest matching points (i.e. points with minimal sum)
328 (setq p12
(cdr (assq (apply 'min
(mapcar 'car p12s
)) p12s
)))
329 (goto-char (car p12
))
330 (compare-windows-highlight op1
(car p12
) (current-buffer) w1
331 op2
(cadr p12
) b2 w2
))
332 (setq compare-windows-sync-point
(or (cadr p12
) t
)))
333 ;; else set point in the second window to the pre-calculated value
334 (if (numberp compare-windows-sync-point
)
335 (goto-char compare-windows-sync-point
))
336 (setq compare-windows-sync-point nil
)))
338 ;; Highlight differences
339 (defun compare-windows-highlight (beg1 end1 b1 w1 beg2 end2 b2 w2
)
340 (when compare-windows-highlight
341 (if compare-windows-overlay1
342 (move-overlay compare-windows-overlay1 beg1 end1 b1
)
343 (setq compare-windows-overlay1
(make-overlay beg1 end1 b1
))
344 (overlay-put compare-windows-overlay1
'face
'compare-windows-face
)
345 (overlay-put compare-windows-overlay1
'priority
1))
346 (overlay-put compare-windows-overlay1
'window w1
)
347 (if compare-windows-overlay2
348 (move-overlay compare-windows-overlay2 beg2 end2 b2
)
349 (setq compare-windows-overlay2
(make-overlay beg2 end2 b2
))
350 (overlay-put compare-windows-overlay2
'face
'compare-windows-face
)
351 (overlay-put compare-windows-overlay2
'priority
1))
352 (overlay-put compare-windows-overlay2
'window w2
)
353 ;; Remove highlighting before next command is executed
354 (add-hook 'pre-command-hook
'compare-windows-dehighlight
)))
356 (defun compare-windows-dehighlight ()
357 "Remove highlighting created by `compare-windows-highlight'."
359 (remove-hook 'pre-command-hook
'compare-windows-dehighlight
)
360 (and compare-windows-overlay1
(delete-overlay compare-windows-overlay1
))
361 (and compare-windows-overlay2
(delete-overlay compare-windows-overlay2
)))
365 ;;; arch-tag: 4177aab1-48e6-4a98-b7a1-000ee285de46
366 ;;; compare-w.el ends here