*** empty log message ***
[emacs.git] / lisp / compare-w.el
blobb4f4e6f383fa768f8bfaa14dc657664b75dc1411
1 ;;; compare-w.el --- compare text between windows for Emacs.
3 ;; Copyright (C) 1986, 1989 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 (defvar compare-windows-whitespace " \t\n"
22 "*String of characters considered whitespace for \\[compare-windows].
23 Changes in whitespace are optionally ignored.
25 The value of `compare-windows-whitespace' may instead be a function; this
26 function is called in each buffer, with point at the current scanning point.
27 The function's job is to categorize any whitespace around (including before)
28 point; it should also advance past any whitespace.
30 The function is passed one argument, the point where `compare-windows'
31 was originally called; it should not consider any text before that point.
32 If the function returns the same value for both buffers, then the
33 whitespace is considered to match, and is skipped.")
35 (defvar compare-ignore-case nil
36 "*Non-nil means \\[compare-windows] ignores case differences.")
38 ;;;###autoload
39 (defun compare-windows (ignore-whitespace)
40 "Compare text in current window with text in next window.
41 Compares the text starting at point in each window,
42 moving over text in each one as far as they match.
44 A prefix arg means ignore changes in whitespace.
45 The variable `compare-windows-whitespace' controls how whitespace is skipped.
46 If `compare-ignore-case' is non-nil, changes in case are also ignored."
47 (interactive "P")
48 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
49 success size
50 (opoint1 (point))
51 opoint2
52 (skip-whitespace (if ignore-whitespace
53 compare-windows-whitespace))
54 (skip-whitespace-regexp (concat "[" skip-whitespace "]+")))
55 (setq p1 (point) b1 (current-buffer))
56 (setq w2 (next-window (selected-window)))
57 (if (eq w2 (selected-window))
58 (error "No other window"))
59 (setq p2 (window-point w2)
60 b2 (window-buffer w2))
61 (setq opoint2 p2)
62 (setq maxp1 (point-max))
63 (save-excursion
64 (set-buffer b2)
65 (setq maxp2 (point-max)))
67 (setq success t)
68 (while success
69 (setq success nil)
70 ;; if interrupted, show how far we've gotten
71 (goto-char p1)
72 (set-window-point w2 p2)
74 ;; If both buffers have whitespace next to point,
75 ;; optionally skip over it.
77 (and skip-whitespace
78 (save-excursion
79 (let (p1a p2a w1 w2 result1 result2)
80 (if (stringp skip-whitespace)
81 (progn
82 (if (not (eobp))
83 (skip-chars-backward skip-whitespace opoint1))
84 (and (looking-at skip-whitespace-regexp)
85 (setq p1a (match-end 0) result1 t)))
86 (setq result1 (funcall skip-whitespace opoint1))
87 (setq p1a (point)))
88 (set-buffer b2)
89 (goto-char p2)
90 (if (stringp skip-whitespace)
91 (progn
92 (if (not (eobp))
93 (skip-chars-backward skip-whitespace opoint2))
94 (and (looking-at skip-whitespace-regexp)
95 (setq p2a (match-end 0) result2 t)))
96 (setq result2 (funcall skip-whitespace opoint2))
97 (setq p2a (point)))
98 (and result1 result2 (eq result1 result2)
99 (setq p1 p1a
100 p2 p2a)))))
102 ;; Try advancing comparing 1000 chars at a time.
103 ;; When that fails, go 500 chars at a time, and so on.
104 (let ((size 1000)
105 success-1)
106 (while (> size 0)
107 (setq success-1 t)
108 (while success-1
109 (setq size (min size (- maxp1 p1) (- maxp2 p2)))
110 (save-excursion
111 (set-buffer b2)
112 (setq s2 (buffer-substring p2 (+ size p2))))
113 (setq success-1
114 (and (> size 0)
115 (if compare-ignore-case
116 (let ((case-fold-search t))
117 (save-excursion
118 (search-forward s2 (+ p1 size) t)))
119 (equal (buffer-substring p1 (+ size p1)) s2))))
120 (if success-1
121 (setq p1 (+ p1 size) p2 (+ p2 size)
122 success t)))
123 (setq size (/ size 2)))))
125 (goto-char p1)
126 (set-window-point w2 p2)
127 (if (= (point) opoint1)
128 (ding))))
130 (provide 'compare-w)
132 ;;; compare-w.el ends here