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