0
|
1 ;;; compare-w.el --- compare text between windows for Emacs.
|
|
2
|
|
3 ;; Copyright (C) 1986, 1989, 1993 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Maintainer: FSF
|
|
6
|
|
7 ;; This file is part of XEmacs.
|
|
8
|
|
9 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
10 ;; under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 ;; General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
21 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
22
|
|
23 ;;; Synched up with: FSF 19.30.
|
|
24
|
|
25 ;;; Whatever was here before didn't look any more correct than the
|
|
26 ;;; FSF version, so I've junked it and replaced it with the FSF version.
|
|
27 ;;; If you really don't like this, dig out the previous version from
|
|
28 ;;; 19.13. --ben
|
|
29
|
|
30 ;;; Commentary:
|
|
31
|
|
32 ;; This package provides one entry point, compare-windows. It compares
|
|
33 ;; text starting from point in two adjacent windows, advancing point
|
|
34 ;; until it finds a difference. Option variables permit you to ignore
|
|
35 ;; whitespace differences, or case differences, or both.
|
|
36
|
|
37 ;;; Code:
|
|
38
|
|
39 (defvar compare-windows-whitespace "[ \t\n]+"
|
|
40 "*Regexp that defines whitespace sequences for \\[compare-windows].
|
|
41 Changes in whitespace are optionally ignored.
|
|
42
|
|
43 The value of `compare-windows-whitespace' may instead be a function; this
|
|
44 function is called in each buffer, with point at the current scanning point.
|
|
45 The function's job is to categorize any whitespace around (including before)
|
|
46 point; it should also advance past any whitespace.
|
|
47
|
|
48 The function is passed one argument, the point where `compare-windows'
|
|
49 was originally called; it should not consider any text before that point.
|
|
50 If the function returns the same value for both buffers, then the
|
|
51 whitespace is considered to match, and is skipped.")
|
|
52
|
|
53 (defvar compare-ignore-case nil
|
|
54 "*Non-nil means \\[compare-windows] ignores case differences.")
|
|
55
|
|
56 ;;;###autoload
|
|
57 (defun compare-windows (ignore-whitespace)
|
|
58 "Compare text in current window with text in next window.
|
|
59 Compares the text starting at point in each window,
|
|
60 moving over text in each one as far as they match.
|
|
61
|
|
62 This command pushes the mark in each window
|
|
63 at the prior location of point in that window.
|
|
64 If both windows display the same buffer,
|
|
65 the mark is pushed twice in that buffer:
|
|
66 first in the other window, then in the selected window.
|
|
67
|
|
68 A prefix arg means ignore changes in whitespace.
|
|
69 The variable `compare-windows-whitespace' controls how whitespace is skipped.
|
|
70 If `compare-ignore-case' is non-nil, changes in case are also ignored."
|
|
71 (interactive "P")
|
|
72 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
|
|
73 success size
|
|
74 (opoint1 (point))
|
|
75 opoint2
|
|
76 (skip-whitespace (if ignore-whitespace
|
|
77 compare-windows-whitespace)))
|
|
78 (setq p1 (point) b1 (current-buffer))
|
|
79 (setq w2 (next-window (selected-window)))
|
|
80 (if (eq w2 (selected-window))
|
|
81 (error "No other window"))
|
|
82 (setq p2 (window-point w2)
|
|
83 b2 (window-buffer w2))
|
|
84 (setq opoint2 p2)
|
|
85 (setq maxp1 (point-max))
|
|
86 (save-excursion
|
|
87 (set-buffer b2)
|
|
88 (push-mark p2 t)
|
|
89 (setq maxp2 (point-max)))
|
|
90 (push-mark)
|
|
91
|
|
92 (setq success t)
|
|
93 (while success
|
|
94 (setq success nil)
|
|
95 ;; if interrupted, show how far we've gotten
|
|
96 (goto-char p1)
|
|
97 (set-window-point w2 p2)
|
|
98
|
|
99 ;; If both buffers have whitespace next to point,
|
|
100 ;; optionally skip over it.
|
|
101
|
|
102 (and skip-whitespace
|
|
103 (save-excursion
|
|
104 (let (p1a p2a w1 w2 result1 result2)
|
|
105 (setq result1
|
|
106 (if (stringp skip-whitespace)
|
|
107 (compare-windows-skip-whitespace opoint1)
|
|
108 (funcall skip-whitespace opoint1)))
|
|
109 (setq p1a (point))
|
|
110 (set-buffer b2)
|
|
111 (goto-char p2)
|
|
112 (setq result2
|
|
113 (if (stringp skip-whitespace)
|
|
114 (compare-windows-skip-whitespace opoint2)
|
|
115 (funcall skip-whitespace opoint2)))
|
|
116 (setq p2a (point))
|
|
117 (if (or (stringp skip-whitespace)
|
|
118 (and result1 result2 (eq result1 result2)))
|
|
119 (setq p1 p1a
|
|
120 p2 p2a)))))
|
|
121
|
|
122 ;; Try advancing comparing 1000 chars at a time.
|
|
123 ;; When that fails, go 500 chars at a time, and so on.
|
|
124 (let ((size 1000)
|
|
125 success-1
|
|
126 (case-fold-search compare-ignore-case))
|
|
127 (while (> size 0)
|
|
128 (setq success-1 t)
|
|
129 ;; Try comparing SIZE chars at a time, repeatedly, till that fails.
|
|
130 (while success-1
|
|
131 (setq size (min size (- maxp1 p1) (- maxp2 p2)))
|
|
132 (setq success-1
|
|
133 (and (> size 0)
|
|
134 (= 0 (compare-buffer-substrings b2 p2 (+ size p2)
|
|
135 b1 p1 (+ size p1)))))
|
|
136 (if success-1
|
|
137 (setq p1 (+ p1 size) p2 (+ p2 size)
|
|
138 success t)))
|
|
139 ;; If SIZE chars don't match, try fewer.
|
|
140 (setq size (/ size 2)))))
|
|
141
|
|
142 (goto-char p1)
|
|
143 (set-window-point w2 p2)
|
|
144 (if (= (point) opoint1)
|
|
145 (ding))))
|
|
146
|
|
147 ;; Move forward over whatever might be called whitespace.
|
|
148 ;; compare-windows-whitespace is a regexp that matches whitespace.
|
|
149 ;; Match it at various starting points before the original point
|
|
150 ;; and find the latest point at which a match ends.
|
|
151 ;; Don't try starting points before START, though.
|
|
152 ;; Value is non-nil if whitespace is found.
|
|
153
|
|
154 ;; If there is whitespace before point, but none after,
|
|
155 ;; then return t, but don't advance point.
|
|
156 (defun compare-windows-skip-whitespace (start)
|
|
157 (let ((end (point))
|
|
158 (beg (point))
|
|
159 (opoint (point)))
|
|
160 (while (or (and (looking-at compare-windows-whitespace)
|
|
161 (<= end (match-end 0))
|
|
162 ;; This match goes past END, so advance END.
|
|
163 (progn (setq end (match-end 0))
|
|
164 (> (point) start)))
|
|
165 (and (/= (point) start)
|
|
166 ;; Consider at least the char before point,
|
|
167 ;; unless it is also before START.
|
|
168 (= (point) opoint)))
|
|
169 ;; keep going back until whitespace
|
|
170 ;; doesn't extend to or past end
|
|
171 (forward-char -1))
|
|
172 (setq beg (point))
|
|
173 (goto-char end)
|
|
174 (or (/= beg opoint)
|
|
175 (/= end opoint))))
|
|
176
|
|
177 (provide 'compare-w)
|
|
178
|
|
179 ;;; compare-w.el ends here
|