1 ;;; compare-w.el --- compare text between windows for Emacs.
3 ;; Copyright (C) 1986, 1989 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 (defvar compare-windows-whitespace
" \t\n"
26 "*String of characters considered whitespace for \\[compare-windows].
27 Changes in whitespace are optionally ignored.
29 The value of `compare-windows-whitespace' may instead be a function; this
30 function is called in each buffer, with point at the current scanning point.
31 The function's job is to categorize any whitespace around (including before)
32 point; it should also advance past any whitespace.
34 The function is passed one argument, the point where `compare-windows'
35 was originally called; it should not consider any text before that point.
36 If the function returns the same value for both buffers, then the
37 whitespace is considered to match, and is skipped.")
39 (defvar compare-ignore-case nil
40 "*Non-nil means \\[compare-windows] ignores case differences.")
43 (defun compare-windows (ignore-whitespace)
44 "Compare text in current window with text in next window.
45 Compares the text starting at point in each window,
46 moving over text in each one as far as they match.
48 A prefix arg means ignore changes in whitespace.
49 The variable `compare-windows-whitespace' controls how whitespace is skipped.
50 If `compare-ignore-case' is non-nil, changes in case are also ignored."
52 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
56 (skip-whitespace (if ignore-whitespace
57 compare-windows-whitespace
))
58 (skip-whitespace-regexp (concat "[" skip-whitespace
"]+")))
59 (setq p1
(point) b1
(current-buffer))
60 (setq w2
(next-window (selected-window)))
61 (if (eq w2
(selected-window))
62 (error "No other window"))
63 (setq p2
(window-point w2
)
64 b2
(window-buffer w2
))
66 (setq maxp1
(point-max))
69 (setq maxp2
(point-max)))
74 ;; if interrupted, show how far we've gotten
76 (set-window-point w2 p2
)
78 ;; If both buffers have whitespace next to point,
79 ;; optionally skip over it.
83 (let (p1a p2a w1 w2 result1 result2
)
84 (if (stringp skip-whitespace
)
87 (skip-chars-backward skip-whitespace opoint1
))
88 (and (looking-at skip-whitespace-regexp
)
89 (setq p1a
(match-end 0) result1 t
)))
90 (setq result1
(funcall skip-whitespace opoint1
))
94 (if (stringp skip-whitespace
)
97 (skip-chars-backward skip-whitespace opoint2
))
98 (and (looking-at skip-whitespace-regexp
)
99 (setq p2a
(match-end 0) result2 t
)))
100 (setq result2
(funcall skip-whitespace opoint2
))
102 (and result1 result2
(eq result1 result2
)
106 ;; Try advancing comparing 1000 chars at a time.
107 ;; When that fails, go 500 chars at a time, and so on.
113 (setq size
(min size
(- maxp1 p1
) (- maxp2 p2
)))
116 (setq s2
(buffer-substring p2
(+ size p2
))))
119 (if compare-ignore-case
120 (let ((case-fold-search t
))
122 (search-forward s2
(+ p1 size
) t
)))
123 (equal (buffer-substring p1
(+ size p1
)) s2
))))
125 (setq p1
(+ p1 size
) p2
(+ p2 size
)
127 (setq size
(/ size
2)))))
130 (set-window-point w2 p2
)
131 (if (= (point) opoint1
)
136 ;;; compare-w.el ends here