annotate lisp/packages/compare-w.el @ 0:376386a54a3c r19-14

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