Make ‘text-quoting-style’ a plain defvar
[emacs.git] / lisp / vc / compare-w.el
blobcec164469846174b00a617099c47d3c14324b4c3
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
154 'compare-windows-get-recent-window
155 "Function that provides the window to compare with."
156 :type '(choice
157 (function-item :tag "Most recently used window"
158 compare-windows-get-recent-window)
159 (function-item :tag "Next window"
160 compare-windows-get-next-window)
161 (function :tag "Your function"))
162 :group 'compare-windows
163 :version "25.1")
165 (defun compare-windows-get-recent-window ()
166 "Return the most recently used window.
167 First try to get the most recently used window on a visible frame,
168 then try to get a window on an iconified frame, and finally
169 consider all existing frames."
170 (or (get-mru-window 'visible t t)
171 (get-mru-window 0 t t)
172 (get-mru-window t t t)
173 (error "No other window")))
175 (defun compare-windows-get-next-window ()
176 "Return the window next in the cyclic ordering of windows.
177 In the selected frame contains only one window, consider windows
178 on all visible frames."
179 (let ((w2 (next-window)))
180 (if (eq w2 (selected-window))
181 (setq w2 (next-window (selected-window) nil 'visible)))
182 (if (eq w2 (selected-window))
183 (error "No other window"))
184 w2))
186 ;;;###autoload
187 (defun compare-windows (ignore-whitespace)
188 "Compare text in current window with text in another window.
189 The option `compare-windows-get-window-function' defines how
190 to get another window.
192 Compares the text starting at point in each window,
193 moving over text in each one as far as they match.
195 This command pushes the mark in each window
196 at the prior location of point in that window.
197 If both windows display the same buffer,
198 the mark is pushed twice in that buffer:
199 first in the other window, then in the selected window.
201 A prefix arg means reverse the value of variable
202 `compare-ignore-whitespace'. If `compare-ignore-whitespace' is
203 nil, then a prefix arg means ignore changes in whitespace. If
204 `compare-ignore-whitespace' is non-nil, then a prefix arg means
205 don't ignore changes in whitespace. The variable
206 `compare-windows-whitespace' controls how whitespace is skipped.
207 If `compare-ignore-case' is non-nil, changes in case are also
208 ignored.
210 If `compare-windows-sync' is non-nil, then successive calls of
211 this command work in interlaced mode:
212 on first call it advances points to the next difference,
213 on second call it synchronizes points by skipping the difference,
214 on third call it again advances points to the next difference and so on."
215 (interactive "P")
216 (if compare-ignore-whitespace
217 (setq ignore-whitespace (not ignore-whitespace)))
218 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
219 (progress 1)
220 (opoint1 (point))
221 opoint2
222 skip-func-1
223 skip-func-2
224 (sync-func (if (stringp compare-windows-sync)
225 'compare-windows-sync-regexp
226 compare-windows-sync)))
227 (setq p1 (point) b1 (current-buffer))
228 (setq w2 (funcall compare-windows-get-window-function))
229 (setq p2 (window-point w2)
230 b2 (window-buffer w2))
231 (setq opoint2 p2)
232 (setq maxp1 (point-max))
234 (setq skip-func-1 (if ignore-whitespace
235 (if (stringp compare-windows-whitespace)
236 (lambda (pos)
237 (compare-windows-skip-whitespace pos)
239 compare-windows-whitespace)))
241 (with-current-buffer b2
242 (setq skip-func-2 (if ignore-whitespace
243 (if (stringp compare-windows-whitespace)
244 (lambda (pos)
245 (compare-windows-skip-whitespace pos)
247 compare-windows-whitespace)))
248 (push-mark p2 t)
249 (setq maxp2 (point-max)))
250 (push-mark)
252 (while (> progress 0)
253 ;; If both windows have whitespace next to point,
254 ;; optionally skip over it.
255 (and skip-func-1
256 (save-excursion
257 (let (p1a p2a result1 result2)
258 (setq result1 (funcall skip-func-1 opoint1))
259 (setq p1a (point))
260 (set-buffer b2)
261 (goto-char p2)
262 (setq result2 (funcall skip-func-2 opoint2))
263 (setq p2a (point))
264 (if (and result1 result2 (eq result1 result2))
265 (setq p1 p1a
266 p2 p2a)))))
268 (let ((size (min (- maxp1 p1) (- maxp2 p2)))
269 (case-fold-search compare-ignore-case))
270 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
271 b1 p1 (+ size p1)))
272 (setq progress (if (zerop progress) size (1- (abs progress))))
273 (setq p1 (+ p1 progress) p2 (+ p2 progress)))
274 ;; Advance point now rather than later, in case we're interrupted.
275 (goto-char p1)
276 (set-window-point w2 p2)
277 (when compare-windows-recenter
278 (recenter (car compare-windows-recenter))
279 (with-selected-window w2 (recenter (cadr compare-windows-recenter)))))
281 (if (= (point) opoint1)
282 (if (not sync-func)
283 (ding)
284 ;; If points are not advanced (i.e. already on mismatch position),
285 ;; then synchronize points between both windows
286 (save-excursion
287 (setq compare-windows-sync-point nil)
288 (funcall sync-func)
289 (setq p1 (point))
290 (set-buffer b2)
291 (goto-char p2)
292 (funcall sync-func)
293 (setq p2 (point)))
294 (goto-char p1)
295 (set-window-point w2 p2)
296 (when compare-windows-recenter
297 (recenter (car compare-windows-recenter))
298 (with-selected-window w2 (recenter (cadr compare-windows-recenter))))
299 ;; If points are still not synchronized, then ding
300 (if (and (= p1 opoint1) (= p2 opoint2))
301 (progn
302 ;; Display error message when current points in two windows
303 ;; are unmatched and next matching points can't be found.
304 (compare-windows-dehighlight)
305 (ding)
306 (message "No more matches with %s" b2))
307 (message "Diff -%s,%s +%s,%s with %s" opoint2 p2 opoint1 p1 b2)))
308 (message "Match -%s,%s +%s,%s with %s" opoint2 p2 opoint1 p1 b2))))
310 ;; Move forward over whatever might be called whitespace.
311 ;; compare-windows-whitespace is a regexp that matches whitespace.
312 ;; Match it at various starting points before the original point
313 ;; and find the latest point at which a match ends.
314 ;; Don't try starting points before START, though.
315 ;; Value is non-nil if whitespace is found.
316 ;; If there is whitespace before point, but none after,
317 ;; then return t, but don't advance point.
318 (defun compare-windows-skip-whitespace (start)
319 (let ((end (point))
320 (beg (point))
321 (opoint (point)))
322 (while (or (and (looking-at compare-windows-whitespace)
323 (<= end (match-end 0))
324 ;; This match goes past END, so advance END.
325 (progn (setq end (match-end 0))
326 (> (point) start)))
327 (and (/= (point) start)
328 ;; Consider at least the char before point,
329 ;; unless it is also before START.
330 (= (point) opoint)))
331 ;; keep going back until whitespace
332 ;; doesn't extend to or past end
333 (forward-char -1))
334 (setq beg (point))
335 (goto-char end)
336 (or (/= beg opoint)
337 (/= end opoint))))
339 ;; Move forward to the next synchronization regexp.
340 (defun compare-windows-sync-regexp ()
341 (if (stringp compare-windows-sync)
342 (re-search-forward compare-windows-sync nil t)))
344 ;; Function works in two passes: one call on each window.
345 ;; On the first call both matching points are computed,
346 ;; and one of them is stored in compare-windows-sync-point
347 ;; to be used when this function is called on second window.
348 (defun compare-windows-sync-default-function ()
349 (if (not compare-windows-sync-point)
350 (let* ((w1 (selected-window))
351 (w2 (funcall compare-windows-get-window-function))
352 (b2 (window-buffer w2))
353 (point-max2 (with-current-buffer b2 (point-max)))
354 (op2 (window-point w2))
355 (op1 (point))
356 (region-size compare-windows-sync-string-size)
357 (string-size compare-windows-sync-string-size)
358 in-bounds-p s1 p2 p12s p12)
359 (while (and
360 ;; until matching points are found
361 (not p12s)
362 ;; until size exceeds the maximum points of both buffers
363 ;; (bounds below take care to not overdo in each of them)
364 (or (setq in-bounds-p (< region-size (max (- (point-max) op1)
365 (- point-max2 op2))))
366 ;; until string size becomes smaller than 4
367 (> string-size 4)))
368 (if in-bounds-p
369 ;; make the next search in the double-sized region;
370 ;; on first iteration it is 2*compare-windows-sync-string-size,
371 ;; on last iterations it exceeds both buffers maximum points
372 (setq region-size (* region-size 2))
373 ;; if region size exceeds the maximum points of both buffers,
374 ;; then start to halve the string size until 4;
375 ;; this helps to find differences near the end of buffers
376 (setq string-size (/ string-size 2)))
377 (let ((p1 op1)
378 (bound1 (- (min (+ op1 region-size) (point-max)) string-size))
379 (bound2 (min (+ op2 region-size) point-max2)))
380 (while (< p1 bound1)
381 (setq s1 (buffer-substring-no-properties p1 (+ p1 string-size)))
382 (setq p2 (with-current-buffer b2
383 (goto-char op2)
384 (let ((case-fold-search compare-ignore-case))
385 (search-forward s1 bound2 t))))
386 (when p2
387 (setq p2 (- p2 string-size))
388 (setq p12s (cons (list (+ p1 p2) p1 p2) p12s)))
389 (setq p1 (1+ p1)))))
390 (when p12s
391 ;; use closest matching points (i.e. points with minimal sum)
392 (setq p12 (cdr (assq (apply 'min (mapcar 'car p12s)) p12s)))
393 (goto-char (car p12))
394 (compare-windows-highlight op1 (car p12) (current-buffer) w1
395 op2 (cadr p12) b2 w2))
396 (setq compare-windows-sync-point (or (cadr p12) t)))
397 ;; else set point in the second window to the pre-calculated value
398 (if (numberp compare-windows-sync-point)
399 (goto-char compare-windows-sync-point))
400 (setq compare-windows-sync-point nil)))
402 ;; Highlight differences
403 (defun compare-windows-highlight (beg1 end1 b1 w1 beg2 end2 b2 w2)
404 (when compare-windows-highlight
405 (if compare-windows-overlay1
406 (move-overlay compare-windows-overlay1 beg1 end1 b1)
407 (setq compare-windows-overlay1 (make-overlay beg1 end1 b1))
408 (overlay-put compare-windows-overlay1 'face 'compare-windows-added)
409 (overlay-put compare-windows-overlay1 'priority 1000))
410 (overlay-put compare-windows-overlay1 'window w1)
411 (if compare-windows-overlay2
412 (move-overlay compare-windows-overlay2 beg2 end2 b2)
413 (setq compare-windows-overlay2 (make-overlay beg2 end2 b2))
414 (overlay-put compare-windows-overlay2 'face 'compare-windows-removed)
415 (overlay-put compare-windows-overlay2 'priority 1000))
416 (overlay-put compare-windows-overlay2 'window w2)
417 (if (not (eq compare-windows-highlight 'persistent))
418 ;; Remove highlighting before next command is executed
419 (add-hook 'pre-command-hook 'compare-windows-dehighlight)
420 (when compare-windows-overlay1
421 (push (copy-overlay compare-windows-overlay1) compare-windows-overlays1)
422 (delete-overlay compare-windows-overlay1))
423 (when compare-windows-overlay2
424 (push (copy-overlay compare-windows-overlay2) compare-windows-overlays2)
425 (delete-overlay compare-windows-overlay2)))))
427 (defun compare-windows-dehighlight ()
428 "Remove highlighting created by function `compare-windows-highlight'."
429 (interactive)
430 (remove-hook 'pre-command-hook 'compare-windows-dehighlight)
431 (mapc 'delete-overlay compare-windows-overlays1)
432 (mapc 'delete-overlay compare-windows-overlays2)
433 (and compare-windows-overlay1 (delete-overlay compare-windows-overlay1))
434 (and compare-windows-overlay2 (delete-overlay compare-windows-overlay2)))
436 (provide 'compare-w)
438 ;;; compare-w.el ends here