Commit | Line | Data |
---|---|---|
ed779c49 JL |
1 | ;;; isearch-multi.el --- isearch extensions for multi-buffer search |
2 | ||
dcb8ac09 | 3 | ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc. |
ed779c49 JL |
4 | |
5 | ;; Author: Juri Linkov <juri@jurta.org> | |
6 | ;; Keywords: matching | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
ed779c49 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
ed779c49 JL |
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 | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
ed779c49 JL |
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 | (defgroup isearch-buffers nil | |
34 | "Using isearch to search through multiple buffers." | |
35 | :version "23.1" | |
36 | :group 'isearch) | |
37 | ||
9097e8af RS |
38 | (defcustom isearch-buffers-multi t |
39 | "Non-nil enables searching multiple related buffers, in certain modes." | |
40 | :type 'boolean | |
41 | :version "23.1" | |
42 | :group 'isearch-buffers) | |
43 | ||
ed779c49 JL |
44 | (defcustom isearch-buffers-pause t |
45 | "A choice defining where to pause the search. | |
46 | If the value is nil, don't pause before going to the next buffer. | |
47 | If the value is `initial', pause only after a failing search in the | |
48 | initial buffer. | |
49 | If t, pause in all buffers that contain the search string." | |
50 | :type '(choice | |
51 | (const :tag "Don't pause" nil) | |
52 | (const :tag "Only in initial buffer" initial) | |
53 | (const :tag "All buffers" t)) | |
54 | :version "23.1" | |
55 | :group 'isearch-buffers) | |
56 | ||
57 | ;;;###autoload | |
58 | (defvar isearch-buffers-current-buffer nil | |
59 | "The buffer where the search is currently searching. | |
60 | The value is nil when the search still is in the initial buffer.") | |
61 | ||
62 | ;;;###autoload | |
63 | (defvar isearch-buffers-next-buffer-function nil | |
64 | "Function to call to get the next buffer to search. | |
65 | ||
66 | When this variable is set to a function that returns a buffer, then | |
1d910147 JB |
67 | after typing another \\[isearch-forward] or \\[isearch-backward] \ |
68 | at a failing search, the search goes | |
ed779c49 JL |
69 | to the next buffer in the series and continues searching for the |
70 | next occurrence. | |
71 | ||
72 | The first argument of this function is the current buffer where the | |
73 | search is currently searching. It defines the base buffer relative to | |
74 | which this function should find the next buffer. When the isearch | |
1d910147 JB |
75 | direction is backward (when `isearch-forward' is nil), this function |
76 | should return the previous buffer to search. If the second argument of | |
ed779c49 JL |
77 | this function WRAP is non-nil, then it should return the first buffer |
78 | in the series; and for the backward search, it should return the last | |
79 | buffer in the series.") | |
80 | ||
81 | ;;;###autoload | |
82 | (define-minor-mode isearch-buffers-minor-mode | |
83 | "Minor mode for using isearch to search through multiple buffers. | |
84 | With arg, turn isearch-buffers minor mode on if arg is positive, off otherwise." | |
85 | :group 'isearch-buffers ;; :lighter " X" | |
86 | (if isearch-buffers-minor-mode | |
87 | (progn | |
88 | (add-hook 'isearch-mode-hook 'isearch-buffers-init nil t) | |
89 | (set (make-local-variable 'isearch-search-fun-function) | |
90 | 'isearch-buffers-search-fun) | |
91 | (set (make-local-variable 'isearch-wrap-function) | |
92 | 'isearch-buffers-wrap) | |
93 | (set (make-local-variable 'isearch-push-state-function) | |
94 | 'isearch-buffers-push-state)) | |
95 | (remove-hook 'isearch-mode-hook 'isearch-buffers-init t) | |
96 | (kill-local-variable 'isearch-search-fun-function) | |
97 | (kill-local-variable 'isearch-wrap-function) | |
98 | (kill-local-variable 'isearch-push-state-function))) | |
99 | ||
100 | (defun isearch-buffers-init () | |
101 | "Set up isearch to search multiple buffers. | |
102 | Intended to be added to `isearch-mode-hook'." | |
103 | (setq isearch-buffers-current-buffer nil)) | |
104 | ||
105 | (defun isearch-buffers-search-fun () | |
106 | "Return the proper search function, for isearch in multiple buffers." | |
107 | (lambda (string bound noerror) | |
108 | (let ((search-fun | |
109 | ;; Use standard functions to search within one buffer | |
110 | (cond | |
111 | (isearch-word | |
112 | (if isearch-forward 'word-search-forward 'word-search-backward)) | |
113 | (isearch-regexp | |
114 | (if isearch-forward 're-search-forward 're-search-backward)) | |
115 | (t | |
116 | (if isearch-forward 'search-forward 'search-backward)))) | |
117 | found buffer) | |
118 | (or | |
119 | ;; 1. First try searching in the initial buffer | |
120 | (let ((res (funcall search-fun string bound noerror))) | |
121 | ;; Reset wrapping for all-buffers pause after successful search | |
122 | (if (and res (eq isearch-buffers-pause t)) | |
123 | (setq isearch-buffers-current-buffer nil)) | |
124 | res) | |
125 | ;; 2. If the above search fails, start visiting next/prev buffers | |
126 | ;; successively, and search the string in them. Do this only | |
127 | ;; when bound is nil (i.e. not while lazy-highlighting search | |
128 | ;; strings in the current buffer). | |
9097e8af | 129 | (when (and (not bound) isearch-buffers-multi) |
ed779c49 JL |
130 | ;; If no-pause or there was one attempt to leave the current buffer |
131 | (if (or (null isearch-buffers-pause) | |
132 | (and isearch-buffers-pause isearch-buffers-current-buffer)) | |
133 | (condition-case nil | |
134 | (progn | |
135 | (while (not found) | |
136 | ;; Find the next buffer to search | |
137 | (setq buffer (funcall isearch-buffers-next-buffer-function | |
138 | buffer)) | |
139 | (with-current-buffer buffer | |
140 | (goto-char (if isearch-forward (point-min) (point-max))) | |
141 | (setq isearch-barrier (point) isearch-opoint (point)) | |
142 | ;; After visiting the next/prev buffer search the | |
143 | ;; string in it again, until the function in | |
144 | ;; isearch-buffers-next-buffer-function raises an error | |
145 | ;; at the beginning/end of the buffer sequence. | |
146 | (setq found (funcall search-fun string bound noerror)))) | |
147 | ;; Set buffer for isearch-search-string to switch | |
148 | (if buffer (setq isearch-buffers-current-buffer buffer)) | |
149 | ;; Return point of the new search result | |
150 | found) | |
151 | ;; Return nil when isearch-buffers-next-buffer-function fails | |
152 | (error nil)) | |
153 | (signal 'search-failed (list string "Repeat for next buffer")))))))) | |
154 | ||
155 | (defun isearch-buffers-wrap () | |
156 | "Wrap the multiple buffers search when search is failed. | |
157 | Switch buffer to the first buffer for a forward search, | |
158 | or to the last buffer for a backward search. | |
159 | Set `isearch-buffers-current-buffer' to the current buffer to display | |
160 | the isearch suffix message [initial buffer] only when isearch leaves | |
161 | the initial buffer." | |
162 | (if (or (null isearch-buffers-pause) | |
163 | (and isearch-buffers-pause isearch-buffers-current-buffer)) | |
164 | (progn | |
165 | (switch-to-buffer | |
166 | (setq isearch-buffers-current-buffer | |
167 | (funcall isearch-buffers-next-buffer-function | |
168 | (current-buffer) t))) | |
169 | (goto-char (if isearch-forward (point-min) (point-max)))) | |
170 | (setq isearch-buffers-current-buffer (current-buffer)) | |
171 | (setq isearch-wrapped nil))) | |
172 | ||
173 | (defun isearch-buffers-push-state () | |
174 | "Save a function restoring the state of multiple buffers search. | |
175 | Save the current buffer to the additional state parameter in the | |
176 | search status stack." | |
177 | `(lambda (cmd) | |
178 | (isearch-buffers-pop-state cmd ,(current-buffer)))) | |
179 | ||
180 | (defun isearch-buffers-pop-state (cmd buffer) | |
181 | "Restore the multiple buffers search state. | |
182 | Switch to the buffer restored from the search status stack." | |
183 | (unless (equal buffer (current-buffer)) | |
184 | (switch-to-buffer (setq isearch-buffers-current-buffer buffer)))) | |
185 | ||
186 | (provide 'isearch-multi) | |
893e7169 MB |
187 | |
188 | ;; arch-tag: a6d38ffa-4d14-4e39-8ac6-46af9d6a6773 | |
ed779c49 | 189 | ;;; isearch-multi.el ends here |