Update copyright year to 2015
[emacs.git] / lisp / misearch.el
blob6daae243fbb53e106717307cfda64da712982a09
1 ;;; misearch.el --- isearch extensions for multi-buffer search
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Juri Linkov <juri@jurta.org>
6 ;; Keywords: matching
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/>.
23 ;;; Commentary:
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.
29 ;;; Code:
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."
37 :version "23.1"
38 :group 'isearch)
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)
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)
59 ;;;###autoload
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
67 next occurrence.
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 option `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.")
83 ;;;###autoload
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
87 Isearch starts.")
89 ;;;###autoload
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 ;;;###autoload
95 (defvar multi-isearch-buffer-list nil)
96 ;;;###autoload
97 (defvar multi-isearch-file-list nil)
99 (defvar multi-isearch-orig-search-fun nil)
100 (defvar multi-isearch-orig-wrap nil)
101 (defvar multi-isearch-orig-push-state nil)
104 ;;;###autoload
105 (defun multi-isearch-setup ()
106 "Set up isearch to search multiple buffers.
107 Intended to be added to `isearch-mode-hook'."
108 (when (and multi-isearch-search
109 multi-isearch-next-buffer-function)
110 (setq multi-isearch-current-buffer nil
111 multi-isearch-next-buffer-current-function
112 multi-isearch-next-buffer-function
113 multi-isearch-orig-search-fun
114 (default-value 'isearch-search-fun-function)
115 multi-isearch-orig-wrap
116 (default-value 'isearch-wrap-function)
117 multi-isearch-orig-push-state
118 (default-value 'isearch-push-state-function))
119 (setq-default isearch-search-fun-function 'multi-isearch-search-fun
120 isearch-wrap-function 'multi-isearch-wrap
121 isearch-push-state-function 'multi-isearch-push-state)
122 (add-hook 'isearch-mode-end-hook 'multi-isearch-end)))
124 (defun multi-isearch-end ()
125 "Clean up the multi-buffer search after terminating isearch."
126 (setq multi-isearch-current-buffer nil
127 multi-isearch-next-buffer-current-function nil
128 multi-isearch-buffer-list nil
129 multi-isearch-file-list nil)
130 (setq-default isearch-search-fun-function multi-isearch-orig-search-fun
131 isearch-wrap-function multi-isearch-orig-wrap
132 isearch-push-state-function multi-isearch-orig-push-state)
133 (remove-hook 'isearch-mode-end-hook 'multi-isearch-end))
135 (defun multi-isearch-search-fun ()
136 "Return the proper search function, for isearch in multiple buffers."
137 (lambda (string bound noerror)
138 (let ((search-fun
139 ;; Use standard functions to search within one buffer
140 (isearch-search-fun-default))
141 found buffer)
143 ;; 1. First try searching in the initial buffer
144 (let ((res (funcall search-fun string bound noerror)))
145 ;; Reset wrapping for all-buffers pause after successful search
146 (if (and res (not bound) (eq multi-isearch-pause t))
147 (setq multi-isearch-current-buffer nil))
148 res)
149 ;; 2. If the above search fails, start visiting next/prev buffers
150 ;; successively, and search the string in them. Do this only
151 ;; when bound is nil (i.e. not while lazy-highlighting search
152 ;; strings in the current buffer).
153 (when (and (not bound) multi-isearch-search)
154 ;; If no-pause or there was one attempt to leave the current buffer
155 (if (or (null multi-isearch-pause)
156 (and multi-isearch-pause multi-isearch-current-buffer))
157 (condition-case nil
158 (progn
159 (while (not found)
160 ;; Find the next buffer to search
161 (setq buffer (funcall multi-isearch-next-buffer-current-function
162 (or buffer (current-buffer)) nil))
163 (with-current-buffer buffer
164 (goto-char (if isearch-forward (point-min) (point-max)))
165 (setq isearch-barrier (point) isearch-opoint (point))
166 ;; After visiting the next/prev buffer search the
167 ;; string in it again, until the function in
168 ;; multi-isearch-next-buffer-current-function raises
169 ;; an error at the beginning/end of the buffer sequence.
170 (setq found (funcall search-fun string bound noerror))))
171 ;; Set buffer for isearch-search-string to switch
172 (if buffer (setq multi-isearch-current-buffer buffer))
173 ;; Return point of the new search result
174 found)
175 ;; Return nil when multi-isearch-next-buffer-current-function fails
176 ;; (`with-current-buffer' raises an error for nil returned from it).
177 (error (signal 'search-failed (list string "end of multi"))))
178 (signal 'search-failed (list string "repeat for next buffer"))))))))
180 (defun multi-isearch-wrap ()
181 "Wrap the multiple buffers search when search is failed.
182 Switch buffer to the first buffer for a forward search,
183 or to the last buffer for a backward search.
184 Set `multi-isearch-current-buffer' to the current buffer to display
185 the isearch suffix message [initial buffer] only when isearch leaves
186 the initial buffer."
187 (if (or (null multi-isearch-pause)
188 (and multi-isearch-pause multi-isearch-current-buffer))
189 (progn
190 (switch-to-buffer
191 (setq multi-isearch-current-buffer
192 (funcall multi-isearch-next-buffer-current-function
193 (current-buffer) t)))
194 (goto-char (if isearch-forward (point-min) (point-max))))
195 (setq multi-isearch-current-buffer (current-buffer))
196 (setq isearch-wrapped nil)))
198 (defun multi-isearch-push-state ()
199 "Save a function restoring the state of multiple buffers search.
200 Save the current buffer to the additional state parameter in the
201 search status stack."
202 `(lambda (cmd)
203 (multi-isearch-pop-state cmd ,(current-buffer))))
205 (defun multi-isearch-pop-state (_cmd buffer)
206 "Restore the multiple buffers search state.
207 Switch to the buffer restored from the search status stack."
208 (unless (equal buffer (current-buffer))
209 (switch-to-buffer (setq multi-isearch-current-buffer buffer))))
212 ;;; Global multi-buffer search invocations
214 (defun multi-isearch-next-buffer-from-list (&optional buffer wrap)
215 "Return the next buffer in the series of buffers.
216 This function is used for multiple buffers Isearch. A sequence of
217 buffers is defined by the variable `multi-isearch-buffer-list'
218 set in `multi-isearch-buffers' or `multi-isearch-buffers-regexp'."
219 (let ((buffers (if isearch-forward
220 multi-isearch-buffer-list
221 (reverse multi-isearch-buffer-list))))
222 (if wrap
223 (car buffers)
224 (cadr (member buffer buffers)))))
226 (defvar ido-ignore-item-temp-list) ; from ido.el
228 (defun multi-isearch-read-buffers ()
229 "Return a list of buffers specified interactively, one by one."
230 ;; Most code from `multi-occur'.
231 (let* ((bufs (list (read-buffer "First buffer to search: "
232 (current-buffer) t)))
233 (buf nil)
234 (ido-ignore-item-temp-list bufs))
235 (while (not (string-equal
236 (setq buf (read-buffer
237 (if (eq read-buffer-function 'ido-read-buffer)
238 "Next buffer to search (C-j to end): "
239 "Next buffer to search (RET to end): ")
240 nil t))
241 ""))
242 (add-to-list 'bufs buf)
243 (setq ido-ignore-item-temp-list bufs))
244 (nreverse bufs)))
246 (defun multi-isearch-read-matching-buffers ()
247 "Return a list of buffers whose names match specified regexp.
248 Uses `read-regexp' to read the regexp."
249 ;; Most code from `multi-occur-in-matching-buffers'
250 ;; and `kill-matching-buffers'.
251 (let ((bufregexp
252 (read-regexp "Search in buffers whose names match regexp")))
253 (when bufregexp
254 (delq nil (mapcar (lambda (buf)
255 (when (string-match bufregexp (buffer-name buf))
256 buf))
257 (buffer-list))))))
259 ;;;###autoload
260 (defun multi-isearch-buffers (buffers)
261 "Start multi-buffer Isearch on a list of BUFFERS.
262 This list can contain live buffers or their names.
263 Interactively read buffer names to search, one by one, ended with RET.
264 With a prefix argument, ask for a regexp, and search in buffers
265 whose names match the specified regexp."
266 (interactive
267 (list (if current-prefix-arg
268 (multi-isearch-read-matching-buffers)
269 (multi-isearch-read-buffers))))
270 (let ((multi-isearch-next-buffer-function
271 'multi-isearch-next-buffer-from-list))
272 (setq multi-isearch-buffer-list (mapcar #'get-buffer buffers))
273 (switch-to-buffer (car multi-isearch-buffer-list))
274 (goto-char (if isearch-forward (point-min) (point-max)))
275 (isearch-forward nil t)))
277 ;;;###autoload
278 (defun multi-isearch-buffers-regexp (buffers)
279 "Start multi-buffer regexp Isearch on a list of BUFFERS.
280 This list can contain live buffers or their names.
281 Interactively read buffer names to search, one by one, ended with RET.
282 With a prefix argument, ask for a regexp, and search in buffers
283 whose names match the specified regexp."
284 (interactive
285 (list (if current-prefix-arg
286 (multi-isearch-read-matching-buffers)
287 (multi-isearch-read-buffers))))
288 (let ((multi-isearch-next-buffer-function
289 'multi-isearch-next-buffer-from-list))
290 (setq multi-isearch-buffer-list (mapcar #'get-buffer buffers))
291 (switch-to-buffer (car multi-isearch-buffer-list))
292 (goto-char (if isearch-forward (point-min) (point-max)))
293 (isearch-forward-regexp nil t)))
296 ;;; Global multi-file search invocations
298 (defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap)
299 "Return the next buffer in the series of file buffers.
300 This function is used for multiple file buffers Isearch. A sequence
301 of files is defined by the variable `multi-isearch-file-list' set in
302 `multi-isearch-files' or `multi-isearch-files-regexp'.
303 Every next/previous file in the defined sequence is visited by
304 `find-file-noselect' that returns the corresponding file buffer."
305 (let ((files (if isearch-forward
306 multi-isearch-file-list
307 (reverse multi-isearch-file-list))))
308 (find-file-noselect
309 (if wrap
310 (car files)
311 (cadr (member (buffer-file-name buffer) files))))))
313 (defun multi-isearch-read-files ()
314 "Return a list of files specified interactively, one by one."
315 ;; Most code from `multi-occur'.
316 (let* ((files (list (read-file-name "First file to search: "
317 default-directory
318 buffer-file-name)))
319 (file nil))
320 (while (not (string-equal
321 (setq file (read-file-name
322 "Next file to search (RET to end): "
323 default-directory
324 default-directory))
325 default-directory))
326 (add-to-list 'files file))
327 (nreverse files)))
329 ;; A regexp is not the same thing as a file glob - does this matter?
330 (defun multi-isearch-read-matching-files ()
331 "Return a list of files whose names match specified wildcard.
332 Uses `read-regexp' to read the wildcard."
333 ;; Most wildcard code from `find-file-noselect'.
334 (let ((filename (read-regexp "Search in files whose names match wildcard")))
335 (when (and filename
336 (not (string-match "\\`/:" filename))
337 (string-match "[[*?]" filename))
338 (condition-case nil
339 (file-expand-wildcards filename t)
340 (error (list filename))))))
342 ;;;###autoload
343 (defun multi-isearch-files (files)
344 "Start multi-buffer Isearch on a list of FILES.
345 Relative file names in this list are expanded to absolute
346 file names using the current buffer's value of `default-directory'.
347 Interactively read file names to search, one by one, ended with RET.
348 With a prefix argument, ask for a wildcard, and search in file buffers
349 whose file names match the specified wildcard."
350 (interactive
351 (list (if current-prefix-arg
352 (multi-isearch-read-matching-files)
353 (multi-isearch-read-files))))
354 (let ((multi-isearch-next-buffer-function
355 'multi-isearch-next-file-buffer-from-list))
356 (setq multi-isearch-file-list (mapcar #'expand-file-name files))
357 (find-file (car multi-isearch-file-list))
358 (goto-char (if isearch-forward (point-min) (point-max)))
359 (isearch-forward nil t)))
361 ;;;###autoload
362 (defun multi-isearch-files-regexp (files)
363 "Start multi-buffer regexp Isearch on a list of FILES.
364 Relative file names in this list are expanded to absolute
365 file names using the current buffer's value of `default-directory'.
366 Interactively read file names to search, one by one, ended with RET.
367 With a prefix argument, ask for a wildcard, and search in file buffers
368 whose file names match the specified wildcard."
369 (interactive
370 (list (if current-prefix-arg
371 (multi-isearch-read-matching-files)
372 (multi-isearch-read-files))))
373 (let ((multi-isearch-next-buffer-function
374 'multi-isearch-next-file-buffer-from-list))
375 (setq multi-isearch-file-list (mapcar #'expand-file-name files))
376 (find-file (car multi-isearch-file-list))
377 (goto-char (if isearch-forward (point-min) (point-max)))
378 (isearch-forward-regexp nil t)))
381 (provide 'multi-isearch)
382 (provide 'misearch)
383 ;;; misearch.el ends here