Fix VC documentation related to switching branches.
[emacs.git] / lisp / misearch.el
blob64ea40b7a1dfc5ee49d2accdd080af4709d0b976
1 ;;; misearch.el --- isearch extensions for multi-buffer search
3 ;; Copyright (C) 2007-2014 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 (defvar multi-isearch-orig-search-fun nil)
95 (defvar multi-isearch-orig-wrap nil)
96 (defvar multi-isearch-orig-push-state nil)
99 ;;;###autoload
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)
131 (let ((search-fun
132 ;; Use standard functions to search within one buffer
133 (isearch-search-fun-default))
134 found buffer)
136 ;; 1. First try searching in the initial buffer
137 (let ((res (funcall search-fun string bound noerror)))
138 ;; Reset wrapping for all-buffers pause after successful search
139 (if (and res (not bound) (eq multi-isearch-pause t))
140 (setq multi-isearch-current-buffer nil))
141 res)
142 ;; 2. If the above search fails, start visiting next/prev buffers
143 ;; successively, and search the string in them. Do this only
144 ;; when bound is nil (i.e. not while lazy-highlighting search
145 ;; strings in the current buffer).
146 (when (and (not bound) multi-isearch-search)
147 ;; If no-pause or there was one attempt to leave the current buffer
148 (if (or (null multi-isearch-pause)
149 (and multi-isearch-pause multi-isearch-current-buffer))
150 (condition-case nil
151 (progn
152 (while (not found)
153 ;; Find the next buffer to search
154 (setq buffer (funcall multi-isearch-next-buffer-current-function
155 (or buffer (current-buffer)) nil))
156 (with-current-buffer buffer
157 (goto-char (if isearch-forward (point-min) (point-max)))
158 (setq isearch-barrier (point) isearch-opoint (point))
159 ;; After visiting the next/prev buffer search the
160 ;; string in it again, until the function in
161 ;; multi-isearch-next-buffer-current-function raises
162 ;; an error at the beginning/end of the buffer sequence.
163 (setq found (funcall search-fun string bound noerror))))
164 ;; Set buffer for isearch-search-string to switch
165 (if buffer (setq multi-isearch-current-buffer buffer))
166 ;; Return point of the new search result
167 found)
168 ;; Return nil when multi-isearch-next-buffer-current-function fails
169 ;; (`with-current-buffer' raises an error for nil returned from it).
170 (error (signal 'search-failed (list string "end of multi"))))
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
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)))
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))))
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 buffers.
211 This function is used for multiple buffers Isearch. A sequence of
212 buffers is defined by the variable `multi-isearch-buffer-list'
213 set in `multi-isearch-buffers' or `multi-isearch-buffers-regexp'."
214 (let ((buffers (if isearch-forward
215 multi-isearch-buffer-list
216 (reverse multi-isearch-buffer-list))))
217 (if wrap
218 (car buffers)
219 (cadr (member buffer buffers)))))
221 (defvar ido-ignore-item-temp-list) ; from ido.el
223 (defun multi-isearch-read-buffers ()
224 "Return a list of buffers specified interactively, one by one."
225 ;; Most code from `multi-occur'.
226 (let* ((bufs (list (read-buffer "First buffer to search: "
227 (current-buffer) t)))
228 (buf nil)
229 (ido-ignore-item-temp-list bufs))
230 (while (not (string-equal
231 (setq buf (read-buffer
232 (if (eq read-buffer-function 'ido-read-buffer)
233 "Next buffer to search (C-j to end): "
234 "Next buffer to search (RET to end): ")
235 nil t))
236 ""))
237 (add-to-list 'bufs buf)
238 (setq ido-ignore-item-temp-list bufs))
239 (nreverse bufs)))
241 (defun multi-isearch-read-matching-buffers ()
242 "Return a list of buffers whose names match specified regexp.
243 Uses `read-regexp' to read the regexp."
244 ;; Most code from `multi-occur-in-matching-buffers'
245 ;; and `kill-matching-buffers'.
246 (let ((bufregexp
247 (read-regexp "Search in buffers whose names match regexp")))
248 (when bufregexp
249 (delq nil (mapcar (lambda (buf)
250 (when (string-match bufregexp (buffer-name buf))
251 buf))
252 (buffer-list))))))
254 ;;;###autoload
255 (defun multi-isearch-buffers (buffers)
256 "Start multi-buffer Isearch on a list of BUFFERS.
257 This list can contain live buffers or their names.
258 Interactively read buffer names to search, one by one, ended with RET.
259 With a prefix argument, ask for a regexp, and search in buffers
260 whose names match the specified regexp."
261 (interactive
262 (list (if current-prefix-arg
263 (multi-isearch-read-matching-buffers)
264 (multi-isearch-read-buffers))))
265 (let ((multi-isearch-next-buffer-function
266 'multi-isearch-next-buffer-from-list))
267 (setq multi-isearch-buffer-list (mapcar #'get-buffer buffers))
268 (switch-to-buffer (car multi-isearch-buffer-list))
269 (goto-char (if isearch-forward (point-min) (point-max)))
270 (isearch-forward nil t)))
272 ;;;###autoload
273 (defun multi-isearch-buffers-regexp (buffers)
274 "Start multi-buffer regexp Isearch on a list of BUFFERS.
275 This list can contain live buffers or their names.
276 Interactively read buffer names to search, one by one, ended with RET.
277 With a prefix argument, ask for a regexp, and search in buffers
278 whose names match the specified regexp."
279 (interactive
280 (list (if current-prefix-arg
281 (multi-isearch-read-matching-buffers)
282 (multi-isearch-read-buffers))))
283 (let ((multi-isearch-next-buffer-function
284 'multi-isearch-next-buffer-from-list))
285 (setq multi-isearch-buffer-list (mapcar #'get-buffer buffers))
286 (switch-to-buffer (car multi-isearch-buffer-list))
287 (goto-char (if isearch-forward (point-min) (point-max)))
288 (isearch-forward-regexp nil t)))
291 ;;; Global multi-file search invocations
293 (defvar multi-isearch-file-list nil)
295 (defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap)
296 "Return the next buffer in the series of file buffers.
297 This function is used for multiple file buffers Isearch. A sequence
298 of files is defined by the variable `multi-isearch-file-list' set in
299 `multi-isearch-files' or `multi-isearch-files-regexp'.
300 Every next/previous file in the defined sequence is visited by
301 `find-file-noselect' that returns the corresponding file buffer."
302 (let ((files (if isearch-forward
303 multi-isearch-file-list
304 (reverse multi-isearch-file-list))))
305 (find-file-noselect
306 (if wrap
307 (car files)
308 (cadr (member (buffer-file-name buffer) files))))))
310 (defun multi-isearch-read-files ()
311 "Return a list of files specified interactively, one by one."
312 ;; Most code from `multi-occur'.
313 (let* ((files (list (read-file-name "First file to search: "
314 default-directory
315 buffer-file-name)))
316 (file nil))
317 (while (not (string-equal
318 (setq file (read-file-name
319 "Next file to search (RET to end): "
320 default-directory
321 default-directory))
322 default-directory))
323 (add-to-list 'files file))
324 (nreverse files)))
326 ;; A regexp is not the same thing as a file glob - does this matter?
327 (defun multi-isearch-read-matching-files ()
328 "Return a list of files whose names match specified wildcard.
329 Uses `read-regexp' to read the wildcard."
330 ;; Most wildcard code from `find-file-noselect'.
331 (let ((filename (read-regexp "Search in files whose names match wildcard")))
332 (when (and filename
333 (not (string-match "\\`/:" filename))
334 (string-match "[[*?]" filename))
335 (condition-case nil
336 (file-expand-wildcards filename t)
337 (error (list filename))))))
339 ;;;###autoload
340 (defun multi-isearch-files (files)
341 "Start multi-buffer Isearch on a list of FILES.
342 Relative file names in this list are expanded to absolute
343 file names using the current buffer's value of `default-directory'.
344 Interactively read file names to search, one by one, ended with RET.
345 With a prefix argument, ask for a wildcard, and search in file buffers
346 whose file names match the specified wildcard."
347 (interactive
348 (list (if current-prefix-arg
349 (multi-isearch-read-matching-files)
350 (multi-isearch-read-files))))
351 (let ((multi-isearch-next-buffer-function
352 'multi-isearch-next-file-buffer-from-list))
353 (setq multi-isearch-file-list (mapcar #'expand-file-name files))
354 (find-file (car multi-isearch-file-list))
355 (goto-char (if isearch-forward (point-min) (point-max)))
356 (isearch-forward nil t)))
358 ;;;###autoload
359 (defun multi-isearch-files-regexp (files)
360 "Start multi-buffer regexp Isearch on a list of FILES.
361 Relative file names in this list are expanded to absolute
362 file names using the current buffer's value of `default-directory'.
363 Interactively read file names to search, one by one, ended with RET.
364 With a prefix argument, ask for a wildcard, and search in file buffers
365 whose file names match the specified wildcard."
366 (interactive
367 (list (if current-prefix-arg
368 (multi-isearch-read-matching-files)
369 (multi-isearch-read-files))))
370 (let ((multi-isearch-next-buffer-function
371 'multi-isearch-next-file-buffer-from-list))
372 (setq multi-isearch-file-list (mapcar #'expand-file-name files))
373 (find-file (car multi-isearch-file-list))
374 (goto-char (if isearch-forward (point-min) (point-max)))
375 (isearch-forward-regexp nil t)))
378 (provide 'multi-isearch)
379 (provide 'misearch)
380 ;;; misearch.el ends here