1 ;;; misearch.el --- isearch extensions for multi-buffer search
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Juri Linkov <juri@jurta.org>
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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
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.
31 ;;; Search multiple buffers
33 ;;;###autoload (add-hook 'isearch-mode-hook 'multi-isearch-setup)
35 (defgroup multi-isearch nil
36 "Using isearch to search through multiple buffers."
40 (defcustom multi-isearch-search t
41 "Non-nil enables searching multiple related buffers, in certain modes."
44 :group
'multi-isearch
)
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
51 If t, pause in all buffers that contain the search string."
53 (const :tag
"Don't pause" nil
)
54 (const :tag
"Only in initial buffer" initial
)
55 (const :tag
"All buffers" t
))
57 :group
'multi-isearch
)
60 (defvar multi-isearch-next-buffer-function nil
61 "Function to call to get the next buffer to search.
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
69 This function should return the next buffer (it doesn't need to switch
70 to it), or nil if it can't find the next buffer (when it reaches the
71 end of the search space).
73 The first argument of this function is the current buffer where the
74 search is currently searching. It defines the base buffer relative to
75 which this function should find the next buffer. When the isearch
76 direction is backward (when `isearch-forward' is nil), this function
77 should return the previous buffer to search.
79 If the second argument of this function WRAP is non-nil, then it
80 should return the first buffer in the series; and for the backward
81 search, it should return the last buffer in the series.")
84 (defvar multi-isearch-next-buffer-current-function nil
85 "The currently active function to get the next buffer to search.
86 Initialized from `multi-isearch-next-buffer-function' when
90 (defvar multi-isearch-current-buffer nil
91 "The buffer where the search is currently searching.
92 The value is nil when the search still is in the initial buffer.")
94 (defvar multi-isearch-orig-search-fun nil
)
95 (defvar multi-isearch-orig-wrap nil
)
96 (defvar multi-isearch-orig-push-state nil
)
100 (defun multi-isearch-setup ()
101 "Set up isearch to search multiple buffers.
102 Intended to be added to `isearch-mode-hook'."
103 (when (and multi-isearch-search
104 multi-isearch-next-buffer-function
)
105 (setq multi-isearch-current-buffer nil
106 multi-isearch-next-buffer-current-function
107 multi-isearch-next-buffer-function
108 multi-isearch-orig-search-fun
109 (default-value 'isearch-search-fun-function
)
110 multi-isearch-orig-wrap
111 (default-value 'isearch-wrap-function
)
112 multi-isearch-orig-push-state
113 (default-value 'isearch-push-state-function
))
114 (setq-default isearch-search-fun-function
'multi-isearch-search-fun
115 isearch-wrap-function
'multi-isearch-wrap
116 isearch-push-state-function
'multi-isearch-push-state
)
117 (add-hook 'isearch-mode-end-hook
'multi-isearch-end
)))
119 (defun multi-isearch-end ()
120 "Clean up the multi-buffer search after terminating isearch."
121 (setq multi-isearch-current-buffer nil
122 multi-isearch-next-buffer-current-function nil
)
123 (setq-default isearch-search-fun-function multi-isearch-orig-search-fun
124 isearch-wrap-function multi-isearch-orig-wrap
125 isearch-push-state-function multi-isearch-orig-push-state
)
126 (remove-hook 'isearch-mode-end-hook
'multi-isearch-end
))
128 (defun multi-isearch-search-fun ()
129 "Return the proper search function, for isearch in multiple buffers."
130 (lambda (string bound noerror
)
132 ;; Use standard functions to search within one buffer
135 (if isearch-forward
'word-search-forward
'word-search-backward
))
137 (if isearch-forward
're-search-forward
're-search-backward
))
139 (if isearch-forward
'search-forward
'search-backward
))))
142 ;; 1. First try searching in the initial buffer
143 (let ((res (funcall search-fun string bound noerror
)))
144 ;; Reset wrapping for all-buffers pause after successful search
145 (if (and res
(eq multi-isearch-pause t
))
146 (setq multi-isearch-current-buffer nil
))
148 ;; 2. If the above search fails, start visiting next/prev buffers
149 ;; successively, and search the string in them. Do this only
150 ;; when bound is nil (i.e. not while lazy-highlighting search
151 ;; strings in the current buffer).
152 (when (and (not bound
) multi-isearch-search
)
153 ;; If no-pause or there was one attempt to leave the current buffer
154 (if (or (null multi-isearch-pause
)
155 (and multi-isearch-pause multi-isearch-current-buffer
))
159 ;; Find the next buffer to search
160 (setq buffer
(funcall multi-isearch-next-buffer-current-function
162 (with-current-buffer buffer
163 (goto-char (if isearch-forward
(point-min) (point-max)))
164 (setq isearch-barrier
(point) isearch-opoint
(point))
165 ;; After visiting the next/prev buffer search the
166 ;; string in it again, until the function in
167 ;; multi-isearch-next-buffer-current-function raises
168 ;; an error at the beginning/end of the buffer sequence.
169 (setq found
(funcall search-fun string bound noerror
))))
170 ;; Set buffer for isearch-search-string to switch
171 (if buffer
(setq multi-isearch-current-buffer buffer
))
172 ;; Return point of the new search result
174 ;; Return nil when multi-isearch-next-buffer-current-function fails
175 ;; (`with-current-buffer' raises an error for nil returned from it).
177 (signal 'search-failed
(list string
"Repeat for next buffer"))))))))
179 (defun multi-isearch-wrap ()
180 "Wrap the multiple buffers search when search is failed.
181 Switch buffer to the first buffer for a forward search,
182 or to the last buffer for a backward search.
183 Set `multi-isearch-current-buffer' to the current buffer to display
184 the isearch suffix message [initial buffer] only when isearch leaves
186 (if (or (null multi-isearch-pause
)
187 (and multi-isearch-pause multi-isearch-current-buffer
))
190 (setq multi-isearch-current-buffer
191 (funcall multi-isearch-next-buffer-current-function
192 (current-buffer) t
)))
193 (goto-char (if isearch-forward
(point-min) (point-max))))
194 (setq multi-isearch-current-buffer
(current-buffer))
195 (setq isearch-wrapped nil
)))
197 (defun multi-isearch-push-state ()
198 "Save a function restoring the state of multiple buffers search.
199 Save the current buffer to the additional state parameter in the
200 search status stack."
202 (multi-isearch-pop-state cmd
,(current-buffer))))
204 (defun multi-isearch-pop-state (cmd buffer
)
205 "Restore the multiple buffers search state.
206 Switch to the buffer restored from the search status stack."
207 (unless (equal buffer
(current-buffer))
208 (switch-to-buffer (setq multi-isearch-current-buffer buffer
))))
211 ;;; Global multi-buffer search invocations
213 (defvar multi-isearch-buffer-list nil
)
215 (defun multi-isearch-next-buffer-from-list (&optional buffer wrap
)
216 "Return the next buffer in the series of ChangeLog file buffers.
217 This function is used for multiple buffers isearch.
218 A sequence of buffers is formed by ChangeLog files with decreasing
219 numeric file name suffixes in the directory of the initial ChangeLog
220 file were isearch was started."
221 (let ((buffers (if isearch-forward
222 multi-isearch-buffer-list
223 (reverse multi-isearch-buffer-list
))))
226 (cadr (member (or buffer
(current-buffer)) buffers
)))))
229 (defun multi-isearch-buffers (buffers)
230 "Start multi-buffer Isearch on a list of BUFFERS."
231 (let ((multi-isearch-next-buffer-function
232 'multi-isearch-next-buffer-from-list
)
233 (multi-isearch-buffer-list buffers
))
234 (switch-to-buffer (car buffers
))
235 (goto-char (if isearch-forward
(point-min) (point-max)))
239 (defun multi-isearch-buffers-regexp (buffers)
240 "Start multi-buffer regexp Isearch on a list of BUFFERS."
241 (let ((multi-isearch-next-buffer-function
242 'multi-isearch-next-buffer-from-list
)
243 (multi-isearch-buffer-list buffers
))
244 (switch-to-buffer (car buffers
))
245 (goto-char (if isearch-forward
(point-min) (point-max)))
246 (isearch-forward-regexp)))
249 ;;; Global multi-file search invocations
251 (defvar multi-isearch-file-list nil
)
253 (defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap
)
254 "Return the next buffer in the series of ChangeLog file buffers.
255 This function is used for multiple buffers isearch.
256 A sequence of buffers is formed by ChangeLog files with decreasing
257 numeric file name suffixes in the directory of the initial ChangeLog
258 file were isearch was started."
259 (let ((files (if isearch-forward
260 multi-isearch-file-list
261 (reverse multi-isearch-file-list
))))
265 (cadr (member (buffer-file-name buffer
) files
))))))
268 (defun multi-isearch-files (files)
269 "Start multi-buffer Isearch on a list of FILES."
270 (let ((multi-isearch-next-buffer-function
271 'multi-isearch-next-file-buffer-from-list
)
272 (multi-isearch-file-list files
))
273 (find-file (car files
))
274 (goto-char (if isearch-forward
(point-min) (point-max)))
278 (defun multi-isearch-files-regexp (files)
279 "Start multi-buffer regexp Isearch on a list of FILES."
280 (let ((multi-isearch-next-buffer-function
281 'multi-isearch-next-file-buffer-from-list
)
282 (multi-isearch-file-list files
))
283 (find-file (car files
))
284 (goto-char (if isearch-forward
(point-min) (point-max)))
285 (isearch-forward-regexp)))
288 (provide 'multi-isearch
)
290 ;; arch-tag: a6d38ffa-4d14-4e39-8ac6-46af9d6a6773
291 ;;; misearch.el ends here