Add 2008 to copyright years.
[bpt/emacs.git] / lisp / isearch-multi.el
CommitLineData
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
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, or (at your option)
13;; 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; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
24
25;;; Commentary:
26
27;; This file adds more dimensions to the search space. It implements
28;; various features that extend isearch. One of them is an ability to
29;; search through multiple buffers.
30
31;;; Code:
32
33;;; Search multiple buffers
34
35(defgroup isearch-buffers nil
36 "Using isearch to search through multiple buffers."
37 :version "23.1"
38 :group 'isearch)
39
9097e8af
RS
40(defcustom isearch-buffers-multi t
41 "Non-nil enables searching multiple related buffers, in certain modes."
42 :type 'boolean
43 :version "23.1"
44 :group 'isearch-buffers)
45
ed779c49
JL
46(defcustom isearch-buffers-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 'isearch-buffers)
58
59;;;###autoload
60(defvar isearch-buffers-current-buffer nil
61 "The buffer where the search is currently searching.
62The value is nil when the search still is in the initial buffer.")
63
64;;;###autoload
65(defvar isearch-buffers-next-buffer-function nil
66 "Function to call to get the next buffer to search.
67
68When this variable is set to a function that returns a buffer, then
1d910147
JB
69after typing another \\[isearch-forward] or \\[isearch-backward] \
70at a failing search, the search goes
ed779c49
JL
71to the next buffer in the series and continues searching for the
72next occurrence.
73
74The first argument of this function is the current buffer where the
75search is currently searching. It defines the base buffer relative to
76which this function should find the next buffer. When the isearch
1d910147
JB
77direction is backward (when `isearch-forward' is nil), this function
78should return the previous buffer to search. If the second argument of
ed779c49
JL
79this function WRAP is non-nil, then it should return the first buffer
80in the series; and for the backward search, it should return the last
81buffer in the series.")
82
83;;;###autoload
84(define-minor-mode isearch-buffers-minor-mode
85 "Minor mode for using isearch to search through multiple buffers.
86With arg, turn isearch-buffers minor mode on if arg is positive, off otherwise."
87 :group 'isearch-buffers ;; :lighter " X"
88 (if isearch-buffers-minor-mode
89 (progn
90 (add-hook 'isearch-mode-hook 'isearch-buffers-init nil t)
91 (set (make-local-variable 'isearch-search-fun-function)
92 'isearch-buffers-search-fun)
93 (set (make-local-variable 'isearch-wrap-function)
94 'isearch-buffers-wrap)
95 (set (make-local-variable 'isearch-push-state-function)
96 'isearch-buffers-push-state))
97 (remove-hook 'isearch-mode-hook 'isearch-buffers-init t)
98 (kill-local-variable 'isearch-search-fun-function)
99 (kill-local-variable 'isearch-wrap-function)
100 (kill-local-variable 'isearch-push-state-function)))
101
102(defun isearch-buffers-init ()
103 "Set up isearch to search multiple buffers.
104Intended to be added to `isearch-mode-hook'."
105 (setq isearch-buffers-current-buffer nil))
106
107(defun isearch-buffers-search-fun ()
108 "Return the proper search function, for isearch in multiple buffers."
109 (lambda (string bound noerror)
110 (let ((search-fun
111 ;; Use standard functions to search within one buffer
112 (cond
113 (isearch-word
114 (if isearch-forward 'word-search-forward 'word-search-backward))
115 (isearch-regexp
116 (if isearch-forward 're-search-forward 're-search-backward))
117 (t
118 (if isearch-forward 'search-forward 'search-backward))))
119 found buffer)
120 (or
121 ;; 1. First try searching in the initial buffer
122 (let ((res (funcall search-fun string bound noerror)))
123 ;; Reset wrapping for all-buffers pause after successful search
124 (if (and res (eq isearch-buffers-pause t))
125 (setq isearch-buffers-current-buffer nil))
126 res)
127 ;; 2. If the above search fails, start visiting next/prev buffers
128 ;; successively, and search the string in them. Do this only
129 ;; when bound is nil (i.e. not while lazy-highlighting search
130 ;; strings in the current buffer).
9097e8af 131 (when (and (not bound) isearch-buffers-multi)
ed779c49
JL
132 ;; If no-pause or there was one attempt to leave the current buffer
133 (if (or (null isearch-buffers-pause)
134 (and isearch-buffers-pause isearch-buffers-current-buffer))
135 (condition-case nil
136 (progn
137 (while (not found)
138 ;; Find the next buffer to search
139 (setq buffer (funcall isearch-buffers-next-buffer-function
140 buffer))
141 (with-current-buffer buffer
142 (goto-char (if isearch-forward (point-min) (point-max)))
143 (setq isearch-barrier (point) isearch-opoint (point))
144 ;; After visiting the next/prev buffer search the
145 ;; string in it again, until the function in
146 ;; isearch-buffers-next-buffer-function raises an error
147 ;; at the beginning/end of the buffer sequence.
148 (setq found (funcall search-fun string bound noerror))))
149 ;; Set buffer for isearch-search-string to switch
150 (if buffer (setq isearch-buffers-current-buffer buffer))
151 ;; Return point of the new search result
152 found)
153 ;; Return nil when isearch-buffers-next-buffer-function fails
154 (error nil))
155 (signal 'search-failed (list string "Repeat for next buffer"))))))))
156
157(defun isearch-buffers-wrap ()
158 "Wrap the multiple buffers search when search is failed.
159Switch buffer to the first buffer for a forward search,
160or to the last buffer for a backward search.
161Set `isearch-buffers-current-buffer' to the current buffer to display
162the isearch suffix message [initial buffer] only when isearch leaves
163the initial buffer."
164 (if (or (null isearch-buffers-pause)
165 (and isearch-buffers-pause isearch-buffers-current-buffer))
166 (progn
167 (switch-to-buffer
168 (setq isearch-buffers-current-buffer
169 (funcall isearch-buffers-next-buffer-function
170 (current-buffer) t)))
171 (goto-char (if isearch-forward (point-min) (point-max))))
172 (setq isearch-buffers-current-buffer (current-buffer))
173 (setq isearch-wrapped nil)))
174
175(defun isearch-buffers-push-state ()
176 "Save a function restoring the state of multiple buffers search.
177Save the current buffer to the additional state parameter in the
178search status stack."
179 `(lambda (cmd)
180 (isearch-buffers-pop-state cmd ,(current-buffer))))
181
182(defun isearch-buffers-pop-state (cmd buffer)
183 "Restore the multiple buffers search state.
184Switch to the buffer restored from the search status stack."
185 (unless (equal buffer (current-buffer))
186 (switch-to-buffer (setq isearch-buffers-current-buffer buffer))))
187
188(provide 'isearch-multi)
893e7169
MB
189
190;; arch-tag: a6d38ffa-4d14-4e39-8ac6-46af9d6a6773
ed779c49 191;;; isearch-multi.el ends here