Merge from emacs--devo--0
[bpt/emacs.git] / lisp / compare-w.el
CommitLineData
55535639 1;;; compare-w.el --- compare text between windows for Emacs
c0274f38 2
0d30b337 3;; Copyright (C) 1986, 1989, 1993, 1997, 2002, 2003, 2004,
aaef169d 4;; 2005, 2006 Free Software Foundation, Inc.
745bc783 5
9750e079 6;; Maintainer: FSF
30764597 7;; Keywords: convenience files
9750e079 8
745bc783
JB
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
745bc783
JB
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
745bc783 25
e41b2db1
ER
26;;; Commentary:
27
28;; This package provides one entry point, compare-windows. It compares
29;; text starting from point in two adjacent windows, advancing point
30;; until it finds a difference. Option variables permit you to ignore
31;; whitespace differences, or case differences, or both.
32
e5167999
ER
33;;; Code:
34
4bef9110
SE
35(defgroup compare-w nil
36 "Compare text between windows."
37 :prefix "compare-"
38 :group 'tools)
39
76960e50 40(defcustom compare-windows-whitespace "\\(\\s-\\|\n\\)+"
6166a68e 41 "*Regexp or function that defines whitespace sequences for `compare-windows'.
76960e50 42That command optionally ignores changes in whitespace.
745bc783 43
76960e50
RS
44The value of `compare-windows-whitespace' is normally a regexp, but it
45can also be a function. The function's job is to categorize any
46whitespace around (including before) point; it should also advance
6166a68e 47past any whitespace. The function is called in each window, with
76960e50 48point at the current scanning point. It gets one argument, the point
6166a68e 49where \\[compare-windows] was originally called; it should not look at
76960e50 50any text before that point.
745bc783 51
6166a68e 52If the function returns the same value for both windows, then the
4bef9110
SE
53whitespace is considered to match, and is skipped."
54 :type '(choice regexp function)
55 :group 'compare-w)
745bc783 56
6166a68e
RS
57(defcustom compare-ignore-whitespace nil
58 "*Non-nil means `compare-windows' ignores whitespace."
59 :type 'boolean
1dbe7fe7
JL
60 :group 'compare-w
61 :version "22.1")
6166a68e 62
4bef9110 63(defcustom compare-ignore-case nil
6166a68e
RS
64 "*Non-nil means `compare-windows' ignores case differences."
65 :type 'boolean
66 :group 'compare-w)
67
68(defcustom compare-windows-sync 'compare-windows-sync-default-function
69 "*Function or regexp that is used to synchronize points in two
70windows if before calling `compare-windows' points are located
71on mismatched positions.
72
73The value of `compare-windows-sync' can be a function. The
74function's job is to advance points in both windows to the next
75matching text. If the value of `compare-windows-sync' is a
76regexp, then points in both windows are advanced to the next
77occurrence of this regexp.
78
79The current default value is the general function
80`compare-windows-sync-default-function' that is able to
81synchronize points by using quadratic algorithm to find the first
82matching 32-character string in two windows.
83
84The other useful values of this variable could be such functions
85as `forward-word', `forward-sentence', `forward-paragraph', or a
86regexp containing some field separator or a newline, depending on
87the nature of the difference units separator. The variable can
88be made buffer-local.
89
90If the value of this variable is `nil', then function `ding' is
91called to beep or flash the screen when points are mismatched."
92 :type '(choice regexp function)
1dbe7fe7
JL
93 :group 'compare-w
94 :version "22.1")
6166a68e
RS
95
96(defcustom compare-windows-sync-string-size 32
97 "*Size of string from one window that is searched in second window.
98
99Small number makes difference regions more fine-grained, but it
100may fail by finding the wrong match. The bigger number makes
101difference regions more coarse-grained.
102
103The default value 32 is good for the most cases."
104 :type 'integer
1dbe7fe7
JL
105 :group 'compare-w
106 :version "22.1")
6166a68e
RS
107
108(defcustom compare-windows-recenter nil
109 "*List of two values, each of which is used as argument of
110function `recenter' called in each of two windows to place
111matching points side-by-side.
112
113The value `(-1 0)' is useful if windows are split vertically,
114and the value `((4) (4))' for horizontally split windows."
115 :type '(list sexp sexp)
1dbe7fe7
JL
116 :group 'compare-w
117 :version "22.1")
6166a68e
RS
118
119(defcustom compare-windows-highlight t
9b0f7f31
JL
120 "*Non-nil means compare-windows highlights the differences.
121The value t removes highlighting immediately after invoking a command
122other than `compare-windows'.
123The value `persistent' leaves all highlighted differences. You can clear
124out all highlighting later with the command `compare-windows-dehighlight'."
125 :type '(choice (const :tag "No highlighting" nil)
126 (const :tag "Persistent highlighting" persistent)
127 (other :tag "Highlight until next command" t))
1dbe7fe7
JL
128 :group 'compare-w
129 :version "22.1")
745bc783 130
37af2dd3 131(defface compare-windows
1dbe7fe7 132 '((t :inherit lazy-highlight))
6166a68e 133 "Face for highlighting of compare-windows difference regions."
1dbe7fe7
JL
134 :group 'compare-w
135 :version "22.1")
6166a68e
RS
136
137(defvar compare-windows-overlay1 nil)
138(defvar compare-windows-overlay2 nil)
9b0f7f31
JL
139(defvar compare-windows-overlays1 nil)
140(defvar compare-windows-overlays2 nil)
6166a68e
RS
141(defvar compare-windows-sync-point nil)
142
745bc783
JB
143;;;###autoload
144(defun compare-windows (ignore-whitespace)
145 "Compare text in current window with text in next window.
146Compares the text starting at point in each window,
147moving over text in each one as far as they match.
148
82734236
RS
149This command pushes the mark in each window
150at the prior location of point in that window.
151If both windows display the same buffer,
152the mark is pushed twice in that buffer:
153first in the other window, then in the selected window.
154
6166a68e
RS
155A prefix arg means reverse the value of variable
156`compare-ignore-whitespace'. If `compare-ignore-whitespace' is
157nil, then a prefix arg means ignore changes in whitespace. If
158`compare-ignore-whitespace' is non-nil, then a prefix arg means
159don't ignore changes in whitespace. The variable
160`compare-windows-whitespace' controls how whitespace is skipped.
161If `compare-ignore-case' is non-nil, changes in case are also
162ignored.
163
164If `compare-windows-sync' is non-nil, then successive calls of
165this command work in interlaced mode:
166on first call it advances points to the next difference,
167on second call it synchronizes points by skipping the difference,
168on third call it again advances points to the next difference and so on."
745bc783
JB
169 (interactive "P")
170 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
8c9dbabe 171 (progress 1)
745bc783
JB
172 (opoint1 (point))
173 opoint2
6166a68e
RS
174 (skip-func (if (if ignore-whitespace ; XOR
175 (not compare-ignore-whitespace)
176 compare-ignore-whitespace)
177 (if (stringp compare-windows-whitespace)
178 'compare-windows-skip-whitespace
179 compare-windows-whitespace)))
180 (sync-func (if (stringp compare-windows-sync)
181 'compare-windows-sync-regexp
182 compare-windows-sync)))
745bc783 183 (setq p1 (point) b1 (current-buffer))
0846761d
AS
184 (setq w2 (next-window (selected-window)))
185 (if (eq w2 (selected-window))
186 (setq w2 (next-window (selected-window) nil 'visible)))
745bc783
JB
187 (if (eq w2 (selected-window))
188 (error "No other window"))
189 (setq p2 (window-point w2)
190 b2 (window-buffer w2))
191 (setq opoint2 p2)
192 (setq maxp1 (point-max))
193 (save-excursion
194 (set-buffer b2)
82734236 195 (push-mark p2 t)
745bc783 196 (setq maxp2 (point-max)))
82734236 197 (push-mark)
745bc783 198
8c9dbabe 199 (while (> progress 0)
6166a68e 200 ;; If both windows have whitespace next to point,
745bc783 201 ;; optionally skip over it.
8c9dbabe 202 (and skip-func
745bc783
JB
203 (save-excursion
204 (let (p1a p2a w1 w2 result1 result2)
8c9dbabe 205 (setq result1 (funcall skip-func opoint1))
048ab7d3 206 (setq p1a (point))
745bc783
JB
207 (set-buffer b2)
208 (goto-char p2)
8c9dbabe 209 (setq result2 (funcall skip-func opoint2))
048ab7d3 210 (setq p2a (point))
5ff083af 211 (if (or (stringp compare-windows-whitespace)
5a534851
RS
212 (and result1 result2 (eq result1 result2)))
213 (setq p1 p1a
214 p2 p2a)))))
745bc783 215
8c9dbabe 216 (let ((size (min (- maxp1 p1) (- maxp2 p2)))
d76bfaa2 217 (case-fold-search compare-ignore-case))
8c9dbabe
KH
218 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
219 b1 p1 (+ size p1)))
220 (setq progress (if (zerop progress) size (1- (abs progress))))
221 (setq p1 (+ p1 progress) p2 (+ p2 progress)))
222 ;; Advance point now rather than later, in case we're interrupted.
223 (goto-char p1)
6166a68e
RS
224 (set-window-point w2 p2)
225 (when compare-windows-recenter
226 (recenter (car compare-windows-recenter))
227 (with-selected-window w2 (recenter (cadr compare-windows-recenter)))))
8c9dbabe 228
745bc783 229 (if (= (point) opoint1)
6166a68e
RS
230 (if (not sync-func)
231 (ding)
232 ;; If points are not advanced (i.e. already on mismatch position),
233 ;; then synchronize points between both windows
234 (save-excursion
235 (setq compare-windows-sync-point nil)
236 (funcall sync-func)
237 (setq p1 (point))
238 (set-buffer b2)
239 (goto-char p2)
240 (funcall sync-func)
241 (setq p2 (point)))
242 (goto-char p1)
243 (set-window-point w2 p2)
244 (when compare-windows-recenter
245 (recenter (car compare-windows-recenter))
246 (with-selected-window w2 (recenter (cadr compare-windows-recenter))))
247 ;; If points are still not synchronized, then ding
248 (when (and (= p1 opoint1) (= p2 opoint2))
249 ;; Display error message when current points in two windows
250 ;; are unmatched and next matching points can't be found.
251 (compare-windows-dehighlight)
252 (ding)
253 (message "No more matching points"))))))
49116ac0 254
048ab7d3
RS
255;; Move forward over whatever might be called whitespace.
256;; compare-windows-whitespace is a regexp that matches whitespace.
257;; Match it at various starting points before the original point
258;; and find the latest point at which a match ends.
259;; Don't try starting points before START, though.
260;; Value is non-nil if whitespace is found.
7f5d3541
RS
261;; If there is whitespace before point, but none after,
262;; then return t, but don't advance point.
048ab7d3
RS
263(defun compare-windows-skip-whitespace (start)
264 (let ((end (point))
7f5d3541 265 (beg (point))
048ab7d3 266 (opoint (point)))
5dd1ad8e 267 (while (or (and (looking-at compare-windows-whitespace)
7f5d3541
RS
268 (<= end (match-end 0))
269 ;; This match goes past END, so advance END.
270 (progn (setq end (match-end 0))
5dd1ad8e
RS
271 (> (point) start)))
272 (and (/= (point) start)
273 ;; Consider at least the char before point,
274 ;; unless it is also before START.
275 (= (point) opoint)))
048ab7d3
RS
276 ;; keep going back until whitespace
277 ;; doesn't extend to or past end
278 (forward-char -1))
7f5d3541 279 (setq beg (point))
048ab7d3 280 (goto-char end)
7f5d3541
RS
281 (or (/= beg opoint)
282 (/= end opoint))))
048ab7d3 283
6166a68e
RS
284;; Move forward to the next synchronization regexp.
285(defun compare-windows-sync-regexp ()
286 (if (stringp compare-windows-sync)
287 (re-search-forward compare-windows-sync nil t)))
288
289;; Function works in two passes: one call on each window.
290;; On the first call both matching points are computed,
291;; and one of them is stored in compare-windows-sync-point
292;; to be used when this function is called on second window.
293(defun compare-windows-sync-default-function ()
294 (if (not compare-windows-sync-point)
efbbac29
JL
295 (let* ((w1 (selected-window))
296 (w2 (next-window w1))
6166a68e
RS
297 (b2 (window-buffer w2))
298 (point-max2 (with-current-buffer b2 (point-max)))
299 (op2 (window-point w2))
300 (op1 (point))
301 (region-size compare-windows-sync-string-size)
302 (string-size compare-windows-sync-string-size)
303 in-bounds-p s1 p2 p12s p12)
304 (while (and
305 ;; until matching points are found
306 (not p12s)
307 ;; until size exceeds the maximum points of both buffers
308 ;; (bounds below take care to not overdo in each of them)
309 (or (setq in-bounds-p (< region-size (max (- (point-max) op1)
310 (- point-max2 op2))))
311 ;; until string size becomes smaller than 4
312 (> string-size 4)))
313 (if in-bounds-p
314 ;; make the next search in the double-sized region;
315 ;; on first iteration it is 2*compare-windows-sync-string-size,
316 ;; on last iterations it exceeds both buffers maximum points
317 (setq region-size (* region-size 2))
318 ;; if region size exceeds the maximum points of both buffers,
319 ;; then start to halve the string size until 4;
320 ;; this helps to find differences near the end of buffers
321 (setq string-size (/ string-size 2)))
322 (let ((p1 op1)
323 (bound1 (- (min (+ op1 region-size) (point-max)) string-size))
324 (bound2 (min (+ op2 region-size) point-max2)))
325 (while (< p1 bound1)
326 (setq s1 (buffer-substring-no-properties p1 (+ p1 string-size)))
327 (setq p2 (with-current-buffer b2
328 (goto-char op2)
329 (let ((case-fold-search compare-ignore-case))
330 (search-forward s1 bound2 t))))
331 (when p2
332 (setq p2 (- p2 string-size))
333 (setq p12s (cons (list (+ p1 p2) p1 p2) p12s)))
334 (setq p1 (1+ p1)))))
335 (when p12s
336 ;; use closest matching points (i.e. points with minimal sum)
337 (setq p12 (cdr (assq (apply 'min (mapcar 'car p12s)) p12s)))
338 (goto-char (car p12))
efbbac29
JL
339 (compare-windows-highlight op1 (car p12) (current-buffer) w1
340 op2 (cadr p12) b2 w2))
6166a68e
RS
341 (setq compare-windows-sync-point (or (cadr p12) t)))
342 ;; else set point in the second window to the pre-calculated value
343 (if (numberp compare-windows-sync-point)
344 (goto-char compare-windows-sync-point))
345 (setq compare-windows-sync-point nil)))
346
347;; Highlight differences
efbbac29 348(defun compare-windows-highlight (beg1 end1 b1 w1 beg2 end2 b2 w2)
6166a68e
RS
349 (when compare-windows-highlight
350 (if compare-windows-overlay1
efbbac29
JL
351 (move-overlay compare-windows-overlay1 beg1 end1 b1)
352 (setq compare-windows-overlay1 (make-overlay beg1 end1 b1))
37af2dd3 353 (overlay-put compare-windows-overlay1 'face 'compare-windows)
67019e7f 354 (overlay-put compare-windows-overlay1 'priority 1000))
efbbac29 355 (overlay-put compare-windows-overlay1 'window w1)
6166a68e 356 (if compare-windows-overlay2
efbbac29
JL
357 (move-overlay compare-windows-overlay2 beg2 end2 b2)
358 (setq compare-windows-overlay2 (make-overlay beg2 end2 b2))
37af2dd3 359 (overlay-put compare-windows-overlay2 'face 'compare-windows)
67019e7f 360 (overlay-put compare-windows-overlay2 'priority 1000))
efbbac29 361 (overlay-put compare-windows-overlay2 'window w2)
9b0f7f31
JL
362 (if (not (eq compare-windows-highlight 'persistent))
363 ;; Remove highlighting before next command is executed
364 (add-hook 'pre-command-hook 'compare-windows-dehighlight)
365 (when compare-windows-overlay1
366 (push (copy-overlay compare-windows-overlay1) compare-windows-overlays1)
367 (delete-overlay compare-windows-overlay1))
368 (when compare-windows-overlay2
369 (push (copy-overlay compare-windows-overlay2) compare-windows-overlays2)
370 (delete-overlay compare-windows-overlay2)))))
6166a68e
RS
371
372(defun compare-windows-dehighlight ()
373 "Remove highlighting created by `compare-windows-highlight'."
374 (interactive)
375 (remove-hook 'pre-command-hook 'compare-windows-dehighlight)
9b0f7f31
JL
376 (mapc 'delete-overlay compare-windows-overlays1)
377 (mapc 'delete-overlay compare-windows-overlays2)
6166a68e
RS
378 (and compare-windows-overlay1 (delete-overlay compare-windows-overlay1))
379 (and compare-windows-overlay2 (delete-overlay compare-windows-overlay2)))
380
49116ac0
JB
381(provide 'compare-w)
382
ab5796a9 383;;; arch-tag: 4177aab1-48e6-4a98-b7a1-000ee285de46
c0274f38 384;;; compare-w.el ends here