Merge from trunk
[bpt/emacs.git] / lisp / misearch.el
CommitLineData
72ece9e8
JL
1;;; misearch.el --- isearch extensions for multi-buffer search
2
73b0cd50 3;; Copyright (C) 2007-2011 Free Software Foundation, Inc.
72ece9e8
JL
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.
48If the value is nil, don't pause before going to the next buffer.
49If the value is `initial', pause only after a failing search in the
50initial buffer.
51If 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
63When this variable is set to a function that returns a buffer, then
64after typing another \\[isearch-forward] or \\[isearch-backward] \
65at a failing search, the search goes
66to the next buffer in the series and continues searching for the
67next occurrence.
68
9b7322d8
JL
69This function should return the next buffer (it doesn't need to switch
70to it), or nil if it can't find the next buffer (when it reaches the
71end of the search space).
72
72ece9e8
JL
73The first argument of this function is the current buffer where the
74search is currently searching. It defines the base buffer relative to
75which this function should find the next buffer. When the isearch
76direction is backward (when `isearch-forward' is nil), this function
9b7322d8
JL
77should return the previous buffer to search.
78
79If the second argument of this function WRAP is non-nil, then it
80should return the first buffer in the series; and for the backward
81search, it should return the last buffer in the series.")
72ece9e8
JL
82
83;;;###autoload
84(defvar multi-isearch-next-buffer-current-function nil
85 "The currently active function to get the next buffer to search.
86Initialized from `multi-isearch-next-buffer-function' when
87Isearch starts.")
88
89;;;###autoload
90(defvar multi-isearch-current-buffer nil
91 "The buffer where the search is currently searching.
92The value is nil when the search still is in the initial buffer.")
93
94(defvar multi-isearch-orig-search-fun nil)
95(defvar multi-isearch-orig-wrap nil)
96(defvar multi-isearch-orig-push-state nil)
97
98\f
99;;;###autoload
100(defun multi-isearch-setup ()
101 "Set up isearch to search multiple buffers.
102Intended 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)))
118
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))
127
128(defun multi-isearch-search-fun ()
129 "Return the proper search function, for isearch in multiple buffers."
130 (lambda (string bound noerror)
131 (let ((search-fun
132 ;; Use standard functions to search within one buffer
133 (cond
134 (isearch-word
135 (if isearch-forward 'word-search-forward 'word-search-backward))
136 (isearch-regexp
137 (if isearch-forward 're-search-forward 're-search-backward))
138 (t
139 (if isearch-forward 'search-forward 'search-backward))))
140 found buffer)
141 (or
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))
147 res)
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))
156 (condition-case nil
157 (progn
158 (while (not found)
159 ;; Find the next buffer to search
160 (setq buffer (funcall multi-isearch-next-buffer-current-function
054ae856 161 (or buffer (current-buffer)) nil))
72ece9e8
JL
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
173 found)
174 ;; Return nil when multi-isearch-next-buffer-current-function fails
9b7322d8 175 ;; (`with-current-buffer' raises an error for nil returned from it).
72ece9e8
JL
176 (error nil))
177 (signal 'search-failed (list string "Repeat for next buffer"))))))))
178
179(defun multi-isearch-wrap ()
180 "Wrap the multiple buffers search when search is failed.
181Switch buffer to the first buffer for a forward search,
182or to the last buffer for a backward search.
183Set `multi-isearch-current-buffer' to the current buffer to display
184the isearch suffix message [initial buffer] only when isearch leaves
185the initial buffer."
186 (if (or (null multi-isearch-pause)
187 (and multi-isearch-pause multi-isearch-current-buffer))
188 (progn
189 (switch-to-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)))
196
197(defun multi-isearch-push-state ()
198 "Save a function restoring the state of multiple buffers search.
199Save the current buffer to the additional state parameter in the
200search status stack."
201 `(lambda (cmd)
202 (multi-isearch-pop-state cmd ,(current-buffer))))
203
204(defun multi-isearch-pop-state (cmd buffer)
205 "Restore the multiple buffers search state.
206Switch 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))))
209
210\f
211;;; Global multi-buffer search invocations
212
213(defvar multi-isearch-buffer-list nil)
214
215(defun multi-isearch-next-buffer-from-list (&optional buffer wrap)
92f8bfc7
JL
216 "Return the next buffer in the series of buffers.
217This function is used for multiple buffers Isearch. A sequence of
218buffers is defined by the variable `multi-isearch-buffer-list'
219set in `multi-isearch-buffers' or `multi-isearch-buffers-regexp'."
72ece9e8
JL
220 (let ((buffers (if isearch-forward
221 multi-isearch-buffer-list
222 (reverse multi-isearch-buffer-list))))
223 (if wrap
224 (car buffers)
054ae856 225 (cadr (member buffer buffers)))))
72ece9e8 226
c585bf32
JL
227(defun multi-isearch-read-buffers ()
228 "Return a list of buffers specified interactively, one by one."
229 ;; Most code from `multi-occur'.
230 (let* ((bufs (list (read-buffer "First buffer to search: "
231 (current-buffer) t)))
232 (buf nil)
233 (ido-ignore-item-temp-list bufs))
234 (while (not (string-equal
235 (setq buf (read-buffer
236 (if (eq read-buffer-function 'ido-read-buffer)
237 "Next buffer to search (C-j to end): "
238 "Next buffer to search (RET to end): ")
239 nil t))
240 ""))
241 (add-to-list 'bufs buf)
242 (setq ido-ignore-item-temp-list bufs))
67296dda 243 (nreverse bufs)))
c585bf32
JL
244
245(defun multi-isearch-read-matching-buffers ()
246 "Return a list of buffers whose names match specified regexp."
247 ;; Most code from `multi-occur-in-matching-buffers'
248 ;; and `kill-matching-buffers'.
249 (let ((bufregexp
250 (read-regexp "Search in buffers whose names match regexp")))
251 (when bufregexp
252 (delq nil (mapcar (lambda (buf)
253 (when (string-match bufregexp (buffer-name buf))
254 buf))
255 (buffer-list))))))
256
72ece9e8
JL
257;;;###autoload
258(defun multi-isearch-buffers (buffers)
c585bf32 259 "Start multi-buffer Isearch on a list of BUFFERS.
67296dda 260This list can contain live buffers or their names.
c585bf32
JL
261Interactively read buffer names to search, one by one, ended with RET.
262With a prefix argument, ask for a regexp, and search in buffers
263whose names match the specified regexp."
264 (interactive
265 (list (if current-prefix-arg
266 (multi-isearch-read-matching-buffers)
267 (multi-isearch-read-buffers))))
72ece9e8
JL
268 (let ((multi-isearch-next-buffer-function
269 'multi-isearch-next-buffer-from-list)
67296dda
JL
270 (multi-isearch-buffer-list (mapcar #'get-buffer buffers)))
271 (switch-to-buffer (car multi-isearch-buffer-list))
72ece9e8
JL
272 (goto-char (if isearch-forward (point-min) (point-max)))
273 (isearch-forward)))
274
275;;;###autoload
276(defun multi-isearch-buffers-regexp (buffers)
c585bf32 277 "Start multi-buffer regexp Isearch on a list of BUFFERS.
67296dda 278This list can contain live buffers or their names.
c585bf32
JL
279Interactively read buffer names to search, one by one, ended with RET.
280With a prefix argument, ask for a regexp, and search in buffers
281whose names match the specified regexp."
282 (interactive
283 (list (if current-prefix-arg
284 (multi-isearch-read-matching-buffers)
285 (multi-isearch-read-buffers))))
72ece9e8
JL
286 (let ((multi-isearch-next-buffer-function
287 'multi-isearch-next-buffer-from-list)
67296dda
JL
288 (multi-isearch-buffer-list (mapcar #'get-buffer buffers)))
289 (switch-to-buffer (car multi-isearch-buffer-list))
72ece9e8
JL
290 (goto-char (if isearch-forward (point-min) (point-max)))
291 (isearch-forward-regexp)))
292
293\f
294;;; Global multi-file search invocations
295
296(defvar multi-isearch-file-list nil)
297
298(defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap)
92f8bfc7
JL
299 "Return the next buffer in the series of file buffers.
300This function is used for multiple file buffers Isearch. A sequence
301of files is defined by the variable `multi-isearch-file-list' set in
302`multi-isearch-files' or `multi-isearch-files-regexp'.
303Every next/previous file in the defined sequence is visited by
304`find-file-noselect' that returns the corresponding file buffer."
72ece9e8
JL
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))))))
312
c585bf32
JL
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)))
328
329(defun multi-isearch-read-matching-files ()
330 "Return a list of files whose names match specified wildcard."
331 ;; Most wildcard code from `find-file-noselect'.
332 (let ((filename (read-regexp "Search in files whose names match wildcard")))
333 (when (and filename
334 (not (string-match "\\`/:" filename))
335 (string-match "[[*?]" filename))
336 (condition-case nil
337 (file-expand-wildcards filename t)
338 (error (list filename))))))
339
72ece9e8
JL
340;;;###autoload
341(defun multi-isearch-files (files)
c585bf32 342 "Start multi-buffer Isearch on a list of FILES.
67296dda
JL
343Relative file names in this list are expanded to absolute
344file names using the current buffer's value of `default-directory'.
c585bf32
JL
345Interactively read file names to search, one by one, ended with RET.
346With a prefix argument, ask for a wildcard, and search in file buffers
347whose file names match the specified wildcard."
348 (interactive
349 (list (if current-prefix-arg
350 (multi-isearch-read-matching-files)
351 (multi-isearch-read-files))))
72ece9e8
JL
352 (let ((multi-isearch-next-buffer-function
353 'multi-isearch-next-file-buffer-from-list)
67296dda
JL
354 (multi-isearch-file-list (mapcar #'expand-file-name files)))
355 (find-file (car multi-isearch-file-list))
72ece9e8
JL
356 (goto-char (if isearch-forward (point-min) (point-max)))
357 (isearch-forward)))
358
359;;;###autoload
360(defun multi-isearch-files-regexp (files)
c585bf32 361 "Start multi-buffer regexp Isearch on a list of FILES.
67296dda
JL
362Relative file names in this list are expanded to absolute
363file names using the current buffer's value of `default-directory'.
c585bf32
JL
364Interactively read file names to search, one by one, ended with RET.
365With a prefix argument, ask for a wildcard, and search in file buffers
366whose file names match the specified wildcard."
367 (interactive
368 (list (if current-prefix-arg
369 (multi-isearch-read-matching-files)
370 (multi-isearch-read-files))))
72ece9e8
JL
371 (let ((multi-isearch-next-buffer-function
372 'multi-isearch-next-file-buffer-from-list)
67296dda
JL
373 (multi-isearch-file-list (mapcar #'expand-file-name files)))
374 (find-file (car multi-isearch-file-list))
72ece9e8
JL
375 (goto-char (if isearch-forward (point-min) (point-max)))
376 (isearch-forward-regexp)))
377
378\f
379(provide 'multi-isearch)
380
72ece9e8 381;;; misearch.el ends here