annotate lisp/packages/compare-w.el @ 48:56c54cf7c5b6 r19-16b90

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