(ibuffer-backward-line, ibuffer-forward-line)
[emacs.git] / lisp / compare-w.el
blob8772cfcc6df9f0ba68eda6749414aca2e7410866
1 ;;; compare-w.el --- compare text between windows for Emacs
3 ;; Copyright (C) 1986, 1989, 1993, 1997 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: convenience files
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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.
32 ;;; Code:
34 (defgroup compare-w nil
35 "Compare text between windows."
36 :prefix "compare-"
37 :group 'tools)
39 (defcustom compare-windows-whitespace "\\(\\s-\\|\n\\)+"
40 "*Regexp that defines whitespace sequences for \\[compare-windows].
41 That command optionally ignores changes in whitespace.
43 The value of `compare-windows-whitespace' is normally a regexp, but it
44 can also be a function. The function's job is to categorize any
45 whitespace around (including before) point; it should also advance
46 past any whitespace. The function is called in each buffer, with
47 point at the current scanning point. It gets one argument, the point
48 where `compare-windows' was originally called; it should not look at
49 any text before that point.
51 If the function returns the same value for both buffers, then the
52 whitespace is considered to match, and is skipped."
53 :type '(choice regexp function)
54 :group 'compare-w)
56 (defcustom compare-ignore-case nil
57 "*Non-nil means \\[compare-windows] ignores case differences."
58 :type 'boolean
59 :group 'compare-w)
61 ;;;###autoload
62 (defun compare-windows (ignore-whitespace)
63 "Compare text in current window with text in next window.
64 Compares the text starting at point in each window,
65 moving over text in each one as far as they match.
67 This command pushes the mark in each window
68 at the prior location of point in that window.
69 If both windows display the same buffer,
70 the mark is pushed twice in that buffer:
71 first in the other window, then in the selected window.
73 A prefix arg means ignore changes in whitespace.
74 The variable `compare-windows-whitespace' controls how whitespace is skipped.
75 If `compare-ignore-case' is non-nil, changes in case are also ignored."
76 (interactive "P")
77 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
78 (progress 1)
79 (opoint1 (point))
80 opoint2
81 (skip-func (if ignore-whitespace
82 (if (stringp compare-windows-whitespace)
83 'compare-windows-skip-whitespace
84 compare-windows-whitespace))))
85 (setq p1 (point) b1 (current-buffer))
86 (setq w2 (next-window (selected-window)))
87 (if (eq w2 (selected-window))
88 (setq w2 (next-window (selected-window) nil 'visible)))
89 (if (eq w2 (selected-window))
90 (error "No other window"))
91 (setq p2 (window-point w2)
92 b2 (window-buffer w2))
93 (setq opoint2 p2)
94 (setq maxp1 (point-max))
95 (save-excursion
96 (set-buffer b2)
97 (push-mark p2 t)
98 (setq maxp2 (point-max)))
99 (push-mark)
101 (while (> progress 0)
102 ;; If both buffers have whitespace next to point,
103 ;; optionally skip over it.
105 (and skip-func
106 (save-excursion
107 (let (p1a p2a w1 w2 result1 result2)
108 (setq result1 (funcall skip-func opoint1))
109 (setq p1a (point))
110 (set-buffer b2)
111 (goto-char p2)
112 (setq result2 (funcall skip-func opoint2))
113 (setq p2a (point))
114 (if (or (stringp compare-windows-whitespace)
115 (and result1 result2 (eq result1 result2)))
116 (setq p1 p1a
117 p2 p2a)))))
119 (let ((size (min (- maxp1 p1) (- maxp2 p2)))
120 (case-fold-search compare-ignore-case))
121 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
122 b1 p1 (+ size p1)))
123 (setq progress (if (zerop progress) size (1- (abs progress))))
124 (setq p1 (+ p1 progress) p2 (+ p2 progress)))
125 ;; Advance point now rather than later, in case we're interrupted.
126 (goto-char p1)
127 (set-window-point w2 p2))
129 (if (= (point) opoint1)
130 (ding))))
132 ;; Move forward over whatever might be called whitespace.
133 ;; compare-windows-whitespace is a regexp that matches whitespace.
134 ;; Match it at various starting points before the original point
135 ;; and find the latest point at which a match ends.
136 ;; Don't try starting points before START, though.
137 ;; Value is non-nil if whitespace is found.
139 ;; If there is whitespace before point, but none after,
140 ;; then return t, but don't advance point.
141 (defun compare-windows-skip-whitespace (start)
142 (let ((end (point))
143 (beg (point))
144 (opoint (point)))
145 (while (or (and (looking-at compare-windows-whitespace)
146 (<= end (match-end 0))
147 ;; This match goes past END, so advance END.
148 (progn (setq end (match-end 0))
149 (> (point) start)))
150 (and (/= (point) start)
151 ;; Consider at least the char before point,
152 ;; unless it is also before START.
153 (= (point) opoint)))
154 ;; keep going back until whitespace
155 ;; doesn't extend to or past end
156 (forward-char -1))
157 (setq beg (point))
158 (goto-char end)
159 (or (/= beg opoint)
160 (/= end opoint))))
162 (provide 'compare-w)
164 ;;; compare-w.el ends here