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