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