Handle remote file names in gdb-mi.el
[emacs.git] / lisp / misearch.el
blob1b9cc9288c7f95e77191376e0fba32390f9293f6
1 ;;; misearch.el --- isearch extensions for multi-buffer search
3 ;; Copyright (C) 2007-2016 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 "Sequence of buffers visited by multiple buffers Isearch.
97 This is nil if Isearch is not currently searching more than one buffer.")
98 ;;;###autoload
99 (defvar multi-isearch-file-list nil
100 "Sequence of files visited by multiple file buffers Isearch.")
102 (defvar multi-isearch-orig-search-fun nil)
103 (defvar multi-isearch-orig-wrap nil)
104 (defvar multi-isearch-orig-push-state nil)
107 ;;;###autoload
108 (defun multi-isearch-setup ()
109 "Set up isearch to search multiple buffers.
110 Intended to be added to `isearch-mode-hook'."
111 (when (and multi-isearch-search
112 multi-isearch-next-buffer-function)
113 (setq multi-isearch-current-buffer nil
114 multi-isearch-next-buffer-current-function
115 multi-isearch-next-buffer-function
116 multi-isearch-orig-search-fun
117 (default-value 'isearch-search-fun-function)
118 multi-isearch-orig-wrap
119 (default-value 'isearch-wrap-function)
120 multi-isearch-orig-push-state
121 (default-value 'isearch-push-state-function))
122 (setq-default isearch-search-fun-function 'multi-isearch-search-fun
123 isearch-wrap-function 'multi-isearch-wrap
124 isearch-push-state-function 'multi-isearch-push-state)
125 (add-hook 'isearch-mode-end-hook 'multi-isearch-end)))
127 (defun multi-isearch-end ()
128 "Clean up the multi-buffer search after terminating isearch."
129 (setq multi-isearch-current-buffer nil
130 multi-isearch-next-buffer-current-function nil
131 multi-isearch-buffer-list nil
132 multi-isearch-file-list nil)
133 (setq-default isearch-search-fun-function multi-isearch-orig-search-fun
134 isearch-wrap-function multi-isearch-orig-wrap
135 isearch-push-state-function multi-isearch-orig-push-state)
136 (remove-hook 'isearch-mode-end-hook 'multi-isearch-end))
138 (defun multi-isearch-search-fun ()
139 "Return the proper search function, for isearch in multiple buffers."
140 (lambda (string bound noerror)
141 (let ((search-fun
142 ;; Use standard functions to search within one buffer
143 (isearch-search-fun-default))
144 found buffer)
146 ;; 1. First try searching in the initial buffer
147 (let ((res (funcall search-fun string bound noerror)))
148 ;; Reset wrapping for all-buffers pause after successful search
149 (if (and res (not bound) (eq multi-isearch-pause t))
150 (setq multi-isearch-current-buffer nil))
151 res)
152 ;; 2. If the above search fails, start visiting next/prev buffers
153 ;; successively, and search the string in them. Do this only
154 ;; when bound is nil (i.e. not while lazy-highlighting search
155 ;; strings in the current buffer).
156 (when (and (not bound) multi-isearch-search)
157 ;; If no-pause or there was one attempt to leave the current buffer
158 (if (or (null multi-isearch-pause)
159 (and multi-isearch-pause multi-isearch-current-buffer))
160 (condition-case nil
161 (progn
162 (while (not found)
163 ;; Find the next buffer to search
164 (setq buffer (funcall multi-isearch-next-buffer-current-function
165 (or buffer (current-buffer)) nil))
166 (with-current-buffer buffer
167 (goto-char (if isearch-forward (point-min) (point-max)))
168 (setq isearch-barrier (point) isearch-opoint (point))
169 ;; After visiting the next/prev buffer search the
170 ;; string in it again, until the function in
171 ;; multi-isearch-next-buffer-current-function raises
172 ;; an error at the beginning/end of the buffer sequence.
173 (setq found (funcall search-fun string bound noerror))))
174 ;; Set buffer for isearch-search-string to switch
175 (if buffer (setq multi-isearch-current-buffer buffer))
176 ;; Return point of the new search result
177 found)
178 ;; Return nil when multi-isearch-next-buffer-current-function fails
179 ;; (`with-current-buffer' raises an error for nil returned from it).
180 (error (signal 'search-failed (list string "end of multi"))))
181 (signal 'search-failed (list string "repeat for next buffer"))))))))
183 (defun multi-isearch-wrap ()
184 "Wrap the multiple buffers search when search is failed.
185 Switch buffer to the first buffer for a forward search,
186 or to the last buffer for a backward search.
187 Set `multi-isearch-current-buffer' to the current buffer to display
188 the isearch suffix message [initial buffer] only when isearch leaves
189 the initial buffer."
190 (if (or (null multi-isearch-pause)
191 (and multi-isearch-pause multi-isearch-current-buffer))
192 (progn
193 (switch-to-buffer
194 (setq multi-isearch-current-buffer
195 (funcall multi-isearch-next-buffer-current-function
196 (current-buffer) t)))
197 (goto-char (if isearch-forward (point-min) (point-max))))
198 (setq multi-isearch-current-buffer (current-buffer))
199 (setq isearch-wrapped nil)))
201 (defun multi-isearch-push-state ()
202 "Save a function restoring the state of multiple buffers search.
203 Save the current buffer to the additional state parameter in the
204 search status stack."
205 `(lambda (cmd)
206 (multi-isearch-pop-state cmd ,(current-buffer))))
208 (defun multi-isearch-pop-state (_cmd buffer)
209 "Restore the multiple buffers search state.
210 Switch to the buffer restored from the search status stack."
211 (unless (equal buffer (current-buffer))
212 (switch-to-buffer (setq multi-isearch-current-buffer buffer))))
215 ;;; Global multi-buffer search invocations
217 (defun multi-isearch-next-buffer-from-list (&optional buffer wrap)
218 "Return the next buffer in the series of buffers.
219 This function is used for multiple buffers Isearch. A sequence of
220 buffers is defined by the variable `multi-isearch-buffer-list'
221 set in `multi-isearch-buffers' or `multi-isearch-buffers-regexp'."
222 (let ((buffers (if isearch-forward
223 multi-isearch-buffer-list
224 (reverse multi-isearch-buffer-list))))
225 (if wrap
226 (car buffers)
227 (cadr (member buffer buffers)))))
229 (defvar ido-ignore-item-temp-list) ; from ido.el
231 (defun multi-isearch-read-buffers ()
232 "Return a list of buffers specified interactively, one by one."
233 ;; Most code from `multi-occur'.
234 (let* ((bufs (list (read-buffer "First buffer to search: "
235 (current-buffer) t)))
236 (buf nil)
237 (ido-ignore-item-temp-list bufs))
238 (while (not (string-equal
239 (setq buf (read-buffer
240 (if (eq read-buffer-function #'ido-read-buffer)
241 "Next buffer to search (C-j to end): "
242 "Next buffer to search (RET to end): ")
243 nil t))
244 ""))
245 (add-to-list 'bufs buf)
246 (setq ido-ignore-item-temp-list bufs))
247 (nreverse bufs)))
249 (defun multi-isearch-read-matching-buffers ()
250 "Return a list of buffers whose names match specified regexp.
251 Uses `read-regexp' to read the regexp."
252 ;; Most code from `multi-occur-in-matching-buffers'
253 ;; and `kill-matching-buffers'.
254 (let ((bufregexp
255 (read-regexp "Search in buffers whose names match regexp")))
256 (when bufregexp
257 (delq nil (mapcar (lambda (buf)
258 (when (string-match bufregexp (buffer-name buf))
259 buf))
260 (buffer-list))))))
262 ;;;###autoload
263 (defun multi-isearch-buffers (buffers)
264 "Start multi-buffer Isearch on a list of BUFFERS.
265 This list can contain live buffers or their names.
266 Interactively read buffer names to search, one by one, ended with RET.
267 With a prefix argument, ask for a regexp, and search in buffers
268 whose names match the specified regexp."
269 (interactive
270 (list (if current-prefix-arg
271 (multi-isearch-read-matching-buffers)
272 (multi-isearch-read-buffers))))
273 (let ((multi-isearch-next-buffer-function
274 'multi-isearch-next-buffer-from-list))
275 (setq multi-isearch-buffer-list (mapcar #'get-buffer buffers))
276 (switch-to-buffer (car multi-isearch-buffer-list))
277 (goto-char (if isearch-forward (point-min) (point-max)))
278 (isearch-forward nil t)))
280 ;;;###autoload
281 (defun multi-isearch-buffers-regexp (buffers)
282 "Start multi-buffer regexp Isearch on a list of BUFFERS.
283 This list can contain live buffers or their names.
284 Interactively read buffer names to search, one by one, ended with RET.
285 With a prefix argument, ask for a regexp, and search in buffers
286 whose names match the specified regexp."
287 (interactive
288 (list (if current-prefix-arg
289 (multi-isearch-read-matching-buffers)
290 (multi-isearch-read-buffers))))
291 (let ((multi-isearch-next-buffer-function
292 'multi-isearch-next-buffer-from-list))
293 (setq multi-isearch-buffer-list (mapcar #'get-buffer buffers))
294 (switch-to-buffer (car multi-isearch-buffer-list))
295 (goto-char (if isearch-forward (point-min) (point-max)))
296 (isearch-forward-regexp nil t)))
299 ;;; Global multi-file search invocations
301 (defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap)
302 "Return the next buffer in the series of file buffers.
303 This function is used for multiple file buffers Isearch. A sequence
304 of files is defined by the variable `multi-isearch-file-list' set in
305 `multi-isearch-files' or `multi-isearch-files-regexp'.
306 Every next/previous file in the defined sequence is visited by
307 `find-file-noselect' that returns the corresponding file buffer."
308 (let ((files (if isearch-forward
309 multi-isearch-file-list
310 (reverse multi-isearch-file-list))))
311 (find-file-noselect
312 (if wrap
313 (car files)
314 (cadr (member (buffer-file-name buffer) files))))))
316 (defun multi-isearch-read-files ()
317 "Return a list of files specified interactively, one by one."
318 ;; Most code from `multi-occur'.
319 (let* ((files (list (read-file-name "First file to search: "
320 default-directory
321 buffer-file-name)))
322 (file nil))
323 (while (not (string-equal
324 (setq file (read-file-name
325 "Next file to search (RET to end): "
326 default-directory
327 default-directory))
328 default-directory))
329 (add-to-list 'files file))
330 (nreverse files)))
332 ;; A regexp is not the same thing as a file glob - does this matter?
333 (defun multi-isearch-read-matching-files ()
334 "Return a list of files whose names match specified wildcard.
335 Uses `read-regexp' to read the wildcard."
336 ;; Most wildcard code from `find-file-noselect'.
337 (let ((filename (read-regexp "Search in files whose names match wildcard")))
338 (when (and filename
339 (not (string-match "\\`/:" filename))
340 (string-match "[[*?]" filename))
341 (condition-case nil
342 (file-expand-wildcards filename t)
343 (error (list filename))))))
345 ;;;###autoload
346 (defun multi-isearch-files (files)
347 "Start multi-buffer Isearch on a list of FILES.
348 Relative file names in this list are expanded to absolute
349 file names using the current buffer's value of `default-directory'.
350 Interactively read file names to search, one by one, ended with RET.
351 With a prefix argument, ask for a wildcard, and search in file buffers
352 whose file names match the specified wildcard."
353 (interactive
354 (list (if current-prefix-arg
355 (multi-isearch-read-matching-files)
356 (multi-isearch-read-files))))
357 (let ((multi-isearch-next-buffer-function
358 'multi-isearch-next-file-buffer-from-list))
359 (setq multi-isearch-file-list (mapcar #'expand-file-name files))
360 (find-file (car multi-isearch-file-list))
361 (goto-char (if isearch-forward (point-min) (point-max)))
362 (isearch-forward nil t)))
364 ;;;###autoload
365 (defun multi-isearch-files-regexp (files)
366 "Start multi-buffer regexp Isearch on a list of FILES.
367 Relative file names in this list are expanded to absolute
368 file names using the current buffer's value of `default-directory'.
369 Interactively read file names to search, one by one, ended with RET.
370 With a prefix argument, ask for a wildcard, and search in file buffers
371 whose file names match the specified wildcard."
372 (interactive
373 (list (if current-prefix-arg
374 (multi-isearch-read-matching-files)
375 (multi-isearch-read-files))))
376 (let ((multi-isearch-next-buffer-function
377 'multi-isearch-next-file-buffer-from-list))
378 (setq multi-isearch-file-list (mapcar #'expand-file-name files))
379 (find-file (car multi-isearch-file-list))
380 (goto-char (if isearch-forward (point-min) (point-max)))
381 (isearch-forward-regexp nil t)))
383 (defvar unload-function-defs-list)
385 (defun multi-isearch-unload-function ()
386 "Remove autoloaded variables from `unload-function-defs-list'.
387 Also prevent the feature from being reloaded via `isearch-mode-hook'."
388 (remove-hook 'isearch-mode-hook 'multi-isearch-setup)
389 (let ((defs (list (car unload-function-defs-list)))
390 (auto '(multi-isearch-next-buffer-function
391 multi-isearch-next-buffer-current-function
392 multi-isearch-current-buffer
393 multi-isearch-buffer-list multi-isearch-file-list)))
394 (dolist (def (cdr unload-function-defs-list))
395 (unless (and (symbolp def)
396 (memq def auto))
397 (push def defs)))
398 (setq unload-function-defs-list (nreverse defs))
399 ;; .
400 nil))
402 (defalias 'misearch-unload-function 'multi-isearch-unload-function)
405 (provide 'multi-isearch)
406 (provide 'misearch)
407 ;;; misearch.el ends here