comparison lisp/compare-w.el @ 257:e5ba2ba35226

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