1 ;;; misearch.el --- isearch extensions for multi-buffer search
3 ;; Copyright (C) 2007, 2008 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 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.")
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
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.")
89 (defvar multi-isearch-orig-search-fun nil
)
90 (defvar multi-isearch-orig-wrap nil
)
91 (defvar multi-isearch-orig-push-state nil
)
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
)))
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
))
123 (defun multi-isearch-search-fun ()
124 "Return the proper search function, for isearch in multiple buffers."
125 (lambda (string bound noerror
)
127 ;; Use standard functions to search within one buffer
130 (if isearch-forward
'word-search-forward
'word-search-backward
))
132 (if isearch-forward
're-search-forward
're-search-backward
))
134 (if isearch-forward
'search-forward
'search-backward
))))
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
))
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
))
154 ;; Find the next buffer to search
155 (setq buffer
(funcall multi-isearch-next-buffer-current-function
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
169 ;; Return nil when multi-isearch-next-buffer-current-function fails
171 (signal 'search-failed
(list string
"Repeat for next buffer"))))))))
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
180 (if (or (null multi-isearch-pause
)
181 (and multi-isearch-pause multi-isearch-current-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
)))
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."
196 (multi-isearch-pop-state cmd
,(current-buffer))))
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
))))
205 ;;; Global multi-buffer search invocations
207 (defvar multi-isearch-buffer-list nil
)
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
))))
220 (cadr (member (or buffer
(current-buffer)) buffers
)))))
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)))
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)))
243 ;;; Global multi-file search invocations
245 (defvar multi-isearch-file-list nil
)
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
))))
259 (cadr (member (buffer-file-name buffer
) files
))))))
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)))
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)))
282 (provide 'multi-isearch
)
284 ;; arch-tag: a6d38ffa-4d14-4e39-8ac6-46af9d6a6773
285 ;;; misearch.el ends here