Commit | Line | Data |
---|---|---|
72ece9e8 JL |
1 | ;;; misearch.el --- isearch extensions for multi-buffer search |
2 | ||
3 | ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Juri Linkov <juri@jurta.org> | |
6 | ;; Keywords: matching | |
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 3 of the License, or | |
13 | ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | ;;; Commentary: | |
24 | ||
25 | ;; This file adds more dimensions to the search space. It implements | |
26 | ;; various features that extend isearch. One of them is an ability to | |
27 | ;; search through multiple buffers. | |
28 | ||
29 | ;;; Code: | |
30 | ||
31 | ;;; Search multiple buffers | |
32 | ||
33 | ;;;###autoload (add-hook 'isearch-mode-hook 'multi-isearch-setup) | |
34 | ||
35 | (defgroup multi-isearch nil | |
36 | "Using isearch to search through multiple buffers." | |
37 | :version "23.1" | |
38 | :group 'isearch) | |
39 | ||
40 | (defcustom multi-isearch-search t | |
41 | "Non-nil enables searching multiple related buffers, in certain modes." | |
42 | :type 'boolean | |
43 | :version "23.1" | |
44 | :group 'multi-isearch) | |
45 | ||
46 | (defcustom multi-isearch-pause t | |
47 | "A choice defining where to pause the search. | |
48 | If the value is nil, don't pause before going to the next buffer. | |
49 | If the value is `initial', pause only after a failing search in the | |
50 | initial buffer. | |
51 | If t, pause in all buffers that contain the search string." | |
52 | :type '(choice | |
53 | (const :tag "Don't pause" nil) | |
54 | (const :tag "Only in initial buffer" initial) | |
55 | (const :tag "All buffers" t)) | |
56 | :version "23.1" | |
57 | :group 'multi-isearch) | |
58 | ||
59 | ;;;###autoload | |
60 | (defvar multi-isearch-next-buffer-function nil | |
61 | "Function to call to get the next buffer to search. | |
62 | ||
63 | When this variable is set to a function that returns a buffer, then | |
64 | after typing another \\[isearch-forward] or \\[isearch-backward] \ | |
65 | at a failing search, the search goes | |
66 | to the next buffer in the series and continues searching for the | |
67 | next occurrence. | |
68 | ||
69 | The first argument of this function is the current buffer where the | |
70 | search is currently searching. It defines the base buffer relative to | |
71 | which this function should find the next buffer. When the isearch | |
72 | direction is backward (when `isearch-forward' is nil), this function | |
73 | should return the previous buffer to search. If the second argument of | |
74 | this function WRAP is non-nil, then it should return the first buffer | |
75 | in the series; and for the backward search, it should return the last | |
76 | buffer in the series.") | |
77 | ||
78 | ;;;###autoload | |
79 | (defvar multi-isearch-next-buffer-current-function nil | |
80 | "The currently active function to get the next buffer to search. | |
81 | Initialized from `multi-isearch-next-buffer-function' when | |
82 | Isearch starts.") | |
83 | ||
84 | ;;;###autoload | |
85 | (defvar multi-isearch-current-buffer nil | |
86 | "The buffer where the search is currently searching. | |
87 | The value is nil when the search still is in the initial buffer.") | |
88 | ||
89 | (defvar multi-isearch-orig-search-fun nil) | |
90 | (defvar multi-isearch-orig-wrap nil) | |
91 | (defvar multi-isearch-orig-push-state nil) | |
92 | ||
93 | \f | |
94 | ;;;###autoload | |
95 | (defun multi-isearch-setup () | |
96 | "Set up isearch to search multiple buffers. | |
97 | Intended to be added to `isearch-mode-hook'." | |
98 | (when (and multi-isearch-search | |
99 | multi-isearch-next-buffer-function) | |
100 | (setq multi-isearch-current-buffer nil | |
101 | multi-isearch-next-buffer-current-function | |
102 | multi-isearch-next-buffer-function | |
103 | multi-isearch-orig-search-fun | |
104 | (default-value 'isearch-search-fun-function) | |
105 | multi-isearch-orig-wrap | |
106 | (default-value 'isearch-wrap-function) | |
107 | multi-isearch-orig-push-state | |
108 | (default-value 'isearch-push-state-function)) | |
109 | (setq-default isearch-search-fun-function 'multi-isearch-search-fun | |
110 | isearch-wrap-function 'multi-isearch-wrap | |
111 | isearch-push-state-function 'multi-isearch-push-state) | |
112 | (add-hook 'isearch-mode-end-hook 'multi-isearch-end))) | |
113 | ||
114 | (defun multi-isearch-end () | |
115 | "Clean up the multi-buffer search after terminating isearch." | |
116 | (setq multi-isearch-current-buffer nil | |
117 | multi-isearch-next-buffer-current-function nil) | |
118 | (setq-default isearch-search-fun-function multi-isearch-orig-search-fun | |
119 | isearch-wrap-function multi-isearch-orig-wrap | |
120 | isearch-push-state-function multi-isearch-orig-push-state) | |
121 | (remove-hook 'isearch-mode-end-hook 'multi-isearch-end)) | |
122 | ||
123 | (defun multi-isearch-search-fun () | |
124 | "Return the proper search function, for isearch in multiple buffers." | |
125 | (lambda (string bound noerror) | |
126 | (let ((search-fun | |
127 | ;; Use standard functions to search within one buffer | |
128 | (cond | |
129 | (isearch-word | |
130 | (if isearch-forward 'word-search-forward 'word-search-backward)) | |
131 | (isearch-regexp | |
132 | (if isearch-forward 're-search-forward 're-search-backward)) | |
133 | (t | |
134 | (if isearch-forward 'search-forward 'search-backward)))) | |
135 | found buffer) | |
136 | (or | |
137 | ;; 1. First try searching in the initial buffer | |
138 | (let ((res (funcall search-fun string bound noerror))) | |
139 | ;; Reset wrapping for all-buffers pause after successful search | |
140 | (if (and res (eq multi-isearch-pause t)) | |
141 | (setq multi-isearch-current-buffer nil)) | |
142 | res) | |
143 | ;; 2. If the above search fails, start visiting next/prev buffers | |
144 | ;; successively, and search the string in them. Do this only | |
145 | ;; when bound is nil (i.e. not while lazy-highlighting search | |
146 | ;; strings in the current buffer). | |
147 | (when (and (not bound) multi-isearch-search) | |
148 | ;; If no-pause or there was one attempt to leave the current buffer | |
149 | (if (or (null multi-isearch-pause) | |
150 | (and multi-isearch-pause multi-isearch-current-buffer)) | |
151 | (condition-case nil | |
152 | (progn | |
153 | (while (not found) | |
154 | ;; Find the next buffer to search | |
155 | (setq buffer (funcall multi-isearch-next-buffer-current-function | |
156 | buffer)) | |
157 | (with-current-buffer buffer | |
158 | (goto-char (if isearch-forward (point-min) (point-max))) | |
159 | (setq isearch-barrier (point) isearch-opoint (point)) | |
160 | ;; After visiting the next/prev buffer search the | |
161 | ;; string in it again, until the function in | |
162 | ;; multi-isearch-next-buffer-current-function raises | |
163 | ;; an error at the beginning/end of the buffer sequence. | |
164 | (setq found (funcall search-fun string bound noerror)))) | |
165 | ;; Set buffer for isearch-search-string to switch | |
166 | (if buffer (setq multi-isearch-current-buffer buffer)) | |
167 | ;; Return point of the new search result | |
168 | found) | |
169 | ;; Return nil when multi-isearch-next-buffer-current-function fails | |
170 | (error nil)) | |
171 | (signal 'search-failed (list string "Repeat for next buffer")))))))) | |
172 | ||
173 | (defun multi-isearch-wrap () | |
174 | "Wrap the multiple buffers search when search is failed. | |
175 | Switch buffer to the first buffer for a forward search, | |
176 | or to the last buffer for a backward search. | |
177 | Set `multi-isearch-current-buffer' to the current buffer to display | |
178 | the isearch suffix message [initial buffer] only when isearch leaves | |
179 | the initial buffer." | |
180 | (if (or (null multi-isearch-pause) | |
181 | (and multi-isearch-pause multi-isearch-current-buffer)) | |
182 | (progn | |
183 | (switch-to-buffer | |
184 | (setq multi-isearch-current-buffer | |
185 | (funcall multi-isearch-next-buffer-current-function | |
186 | (current-buffer) t))) | |
187 | (goto-char (if isearch-forward (point-min) (point-max)))) | |
188 | (setq multi-isearch-current-buffer (current-buffer)) | |
189 | (setq isearch-wrapped nil))) | |
190 | ||
191 | (defun multi-isearch-push-state () | |
192 | "Save a function restoring the state of multiple buffers search. | |
193 | Save the current buffer to the additional state parameter in the | |
194 | search status stack." | |
195 | `(lambda (cmd) | |
196 | (multi-isearch-pop-state cmd ,(current-buffer)))) | |
197 | ||
198 | (defun multi-isearch-pop-state (cmd buffer) | |
199 | "Restore the multiple buffers search state. | |
200 | Switch to the buffer restored from the search status stack." | |
201 | (unless (equal buffer (current-buffer)) | |
202 | (switch-to-buffer (setq multi-isearch-current-buffer buffer)))) | |
203 | ||
204 | \f | |
205 | ;;; Global multi-buffer search invocations | |
206 | ||
207 | (defvar multi-isearch-buffer-list nil) | |
208 | ||
209 | (defun multi-isearch-next-buffer-from-list (&optional buffer wrap) | |
210 | "Return the next buffer in the series of ChangeLog file buffers. | |
211 | This function is used for multiple buffers isearch. | |
212 | A sequence of buffers is formed by ChangeLog files with decreasing | |
213 | numeric file name suffixes in the directory of the initial ChangeLog | |
214 | file were isearch was started." | |
215 | (let ((buffers (if isearch-forward | |
216 | multi-isearch-buffer-list | |
217 | (reverse multi-isearch-buffer-list)))) | |
218 | (if wrap | |
219 | (car buffers) | |
220 | (cadr (member (or buffer (current-buffer)) buffers))))) | |
221 | ||
222 | ;;;###autoload | |
223 | (defun multi-isearch-buffers (buffers) | |
224 | "Start multi-buffer Isearch on a list of BUFFERS." | |
225 | (let ((multi-isearch-next-buffer-function | |
226 | 'multi-isearch-next-buffer-from-list) | |
227 | (multi-isearch-buffer-list buffers)) | |
228 | (switch-to-buffer (car buffers)) | |
229 | (goto-char (if isearch-forward (point-min) (point-max))) | |
230 | (isearch-forward))) | |
231 | ||
232 | ;;;###autoload | |
233 | (defun multi-isearch-buffers-regexp (buffers) | |
234 | "Start multi-buffer regexp Isearch on a list of BUFFERS." | |
235 | (let ((multi-isearch-next-buffer-function | |
236 | 'multi-isearch-next-buffer-from-list) | |
237 | (multi-isearch-buffer-list buffers)) | |
238 | (switch-to-buffer (car buffers)) | |
239 | (goto-char (if isearch-forward (point-min) (point-max))) | |
240 | (isearch-forward-regexp))) | |
241 | ||
242 | \f | |
243 | ;;; Global multi-file search invocations | |
244 | ||
245 | (defvar multi-isearch-file-list nil) | |
246 | ||
247 | (defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap) | |
248 | "Return the next buffer in the series of ChangeLog file buffers. | |
249 | This function is used for multiple buffers isearch. | |
250 | A sequence of buffers is formed by ChangeLog files with decreasing | |
251 | numeric file name suffixes in the directory of the initial ChangeLog | |
252 | file were isearch was started." | |
253 | (let ((files (if isearch-forward | |
254 | multi-isearch-file-list | |
255 | (reverse multi-isearch-file-list)))) | |
256 | (find-file-noselect | |
257 | (if wrap | |
258 | (car files) | |
259 | (cadr (member (buffer-file-name buffer) files)))))) | |
260 | ||
261 | ;;;###autoload | |
262 | (defun multi-isearch-files (files) | |
263 | "Start multi-buffer Isearch on a list of FILES." | |
264 | (let ((multi-isearch-next-buffer-function | |
265 | 'multi-isearch-next-file-buffer-from-list) | |
266 | (multi-isearch-file-list files)) | |
267 | (find-file (car files)) | |
268 | (goto-char (if isearch-forward (point-min) (point-max))) | |
269 | (isearch-forward))) | |
270 | ||
271 | ;;;###autoload | |
272 | (defun multi-isearch-files-regexp (files) | |
273 | "Start multi-buffer regexp Isearch on a list of FILES." | |
274 | (let ((multi-isearch-next-buffer-function | |
275 | 'multi-isearch-next-file-buffer-from-list) | |
276 | (multi-isearch-file-list files)) | |
277 | (find-file (car files)) | |
278 | (goto-char (if isearch-forward (point-min) (point-max))) | |
279 | (isearch-forward-regexp))) | |
280 | ||
281 | \f | |
282 | (provide 'multi-isearch) | |
283 | ||
284 | ;; arch-tag: a6d38ffa-4d14-4e39-8ac6-46af9d6a6773 | |
285 | ;;; misearch.el ends here |