]> code.delx.au - gnu-emacs/blob - lisp/compare-w.el
(mail-extr-all-top-level-domains): Update names.
[gnu-emacs] / lisp / compare-w.el
1 ;;; compare-w.el --- compare text between windows for Emacs
2
3 ;; Copyright (C) 1986, 1989, 1993, 1997 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: convenience files
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This package provides one entry point, compare-windows. It compares
28 ;; text starting from point in two adjacent windows, advancing point
29 ;; until it finds a difference. Option variables permit you to ignore
30 ;; whitespace differences, or case differences, or both.
31
32 ;;; Code:
33
34 (defgroup compare-w nil
35 "Compare text between windows."
36 :prefix "compare-"
37 :group 'tools)
38
39 (defcustom compare-windows-whitespace "\\s-+"
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 :type '(choice regexp function)
53 :group 'compare-w)
54
55 (defcustom compare-ignore-case nil
56 "*Non-nil means \\[compare-windows] ignores case differences."
57 :type 'boolean
58 :group 'compare-w)
59
60 ;;;###autoload
61 (defun compare-windows (ignore-whitespace)
62 "Compare text in current window with text in next window.
63 Compares the text starting at point in each window,
64 moving over text in each one as far as they match.
65
66 This command pushes the mark in each window
67 at the prior location of point in that window.
68 If both windows display the same buffer,
69 the mark is pushed twice in that buffer:
70 first in the other window, then in the selected window.
71
72 A prefix arg means ignore changes in whitespace.
73 The variable `compare-windows-whitespace' controls how whitespace is skipped.
74 If `compare-ignore-case' is non-nil, changes in case are also ignored."
75 (interactive "P")
76 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
77 (progress 1)
78 (opoint1 (point))
79 opoint2
80 (skip-func (if ignore-whitespace
81 (if (stringp compare-windows-whitespace)
82 'compare-windows-skip-whitespace
83 compare-windows-whitespace))))
84 (setq p1 (point) b1 (current-buffer))
85 (setq w2 (next-window (selected-window)))
86 (if (eq w2 (selected-window))
87 (setq w2 (next-window (selected-window) nil 'visible)))
88 (if (eq w2 (selected-window))
89 (error "No other window"))
90 (setq p2 (window-point w2)
91 b2 (window-buffer w2))
92 (setq opoint2 p2)
93 (setq maxp1 (point-max))
94 (save-excursion
95 (set-buffer b2)
96 (push-mark p2 t)
97 (setq maxp2 (point-max)))
98 (push-mark)
99
100 (while (> progress 0)
101 ;; If both buffers have whitespace next to point,
102 ;; optionally skip over it.
103
104 (and skip-func
105 (save-excursion
106 (let (p1a p2a w1 w2 result1 result2)
107 (setq result1 (funcall skip-func opoint1))
108 (setq p1a (point))
109 (set-buffer b2)
110 (goto-char p2)
111 (setq result2 (funcall skip-func opoint2))
112 (setq p2a (point))
113 (if (or (stringp compare-windows-whitespace)
114 (and result1 result2 (eq result1 result2)))
115 (setq p1 p1a
116 p2 p2a)))))
117
118 (let ((size (min (- maxp1 p1) (- maxp2 p2)))
119 (case-fold-search compare-ignore-case))
120 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
121 b1 p1 (+ size p1)))
122 (setq progress (if (zerop progress) size (1- (abs progress))))
123 (setq p1 (+ p1 progress) p2 (+ p2 progress)))
124 ;; Advance point now rather than later, in case we're interrupted.
125 (goto-char p1)
126 (set-window-point w2 p2))
127
128 (if (= (point) opoint1)
129 (ding))))
130
131 ;; Move forward over whatever might be called whitespace.
132 ;; compare-windows-whitespace is a regexp that matches whitespace.
133 ;; Match it at various starting points before the original point
134 ;; and find the latest point at which a match ends.
135 ;; Don't try starting points before START, though.
136 ;; Value is non-nil if whitespace is found.
137
138 ;; If there is whitespace before point, but none after,
139 ;; then return t, but don't advance point.
140 (defun compare-windows-skip-whitespace (start)
141 (let ((end (point))
142 (beg (point))
143 (opoint (point)))
144 (while (or (and (looking-at compare-windows-whitespace)
145 (<= end (match-end 0))
146 ;; This match goes past END, so advance END.
147 (progn (setq end (match-end 0))
148 (> (point) start)))
149 (and (/= (point) start)
150 ;; Consider at least the char before point,
151 ;; unless it is also before START.
152 (= (point) opoint)))
153 ;; keep going back until whitespace
154 ;; doesn't extend to or past end
155 (forward-char -1))
156 (setq beg (point))
157 (goto-char end)
158 (or (/= beg opoint)
159 (/= end opoint))))
160
161 (provide 'compare-w)
162
163 ;;; compare-w.el ends here