comparison lisp/isearch-multi.el @ 85568:76aa2c1e4934

New file.
author Juri Linkov <juri@jurta.org>
date Mon, 22 Oct 2007 23:43:38 +0000
parents
children 14c6e96eeaad
comparison
equal deleted inserted replaced
85567:dccc8254b066 85568:76aa2c1e4934
1 ;;; isearch-multi.el --- isearch extensions for multi-buffer search
2
3 ;; Copyright (C) 2007 Free Software Foundation, Inc.
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
40 (defcustom isearch-buffers-pause t
41 "A choice defining where to pause the search.
42 If the value is nil, don't pause before going to the next buffer.
43 If the value is `initial', pause only after a failing search in the
44 initial buffer.
45 If t, pause in all buffers that contain the search string."
46 :type '(choice
47 (const :tag "Don't pause" nil)
48 (const :tag "Only in initial buffer" initial)
49 (const :tag "All buffers" t))
50 :version "23.1"
51 :group 'isearch-buffers)
52
53 ;;;###autoload
54 (defvar isearch-buffers-current-buffer nil
55 "The buffer where the search is currently searching.
56 The value is nil when the search still is in the initial buffer.")
57
58 ;;;###autoload
59 (defvar isearch-buffers-next-buffer-function nil
60 "Function to call to get the next buffer to search.
61
62 When this variable is set to a function that returns a buffer, then
63 after typing another C-s or C-r at a failing search, the search goes
64 to the next buffer in the series and continues searching for the
65 next occurrence.
66
67 The first argument of this function is the current buffer where the
68 search is currently searching. It defines the base buffer relative to
69 which this function should find the next buffer. When the isearch
70 direction is backward (when isearch-forward is nil), this function
71 should return the previous buffer to search. If the second argument of
72 this function WRAP is non-nil, then it should return the first buffer
73 in the series; and for the backward search, it should return the last
74 buffer in the series.")
75
76 ;;;###autoload
77 (define-minor-mode isearch-buffers-minor-mode
78 "Minor mode for using isearch to search through multiple buffers.
79 With arg, turn isearch-buffers minor mode on if arg is positive, off otherwise."
80 :group 'isearch-buffers ;; :lighter " X"
81 (if isearch-buffers-minor-mode
82 (progn
83 (add-hook 'isearch-mode-hook 'isearch-buffers-init nil t)
84 (set (make-local-variable 'isearch-search-fun-function)
85 'isearch-buffers-search-fun)
86 (set (make-local-variable 'isearch-wrap-function)
87 'isearch-buffers-wrap)
88 (set (make-local-variable 'isearch-push-state-function)
89 'isearch-buffers-push-state))
90 (remove-hook 'isearch-mode-hook 'isearch-buffers-init t)
91 (kill-local-variable 'isearch-search-fun-function)
92 (kill-local-variable 'isearch-wrap-function)
93 (kill-local-variable 'isearch-push-state-function)))
94
95 (defun isearch-buffers-init ()
96 "Set up isearch to search multiple buffers.
97 Intended to be added to `isearch-mode-hook'."
98 (setq isearch-buffers-current-buffer nil))
99
100 (defun isearch-buffers-search-fun ()
101 "Return the proper search function, for isearch in multiple buffers."
102 (lambda (string bound noerror)
103 (let ((search-fun
104 ;; Use standard functions to search within one buffer
105 (cond
106 (isearch-word
107 (if isearch-forward 'word-search-forward 'word-search-backward))
108 (isearch-regexp
109 (if isearch-forward 're-search-forward 're-search-backward))
110 (t
111 (if isearch-forward 'search-forward 'search-backward))))
112 found buffer)
113 (or
114 ;; 1. First try searching in the initial buffer
115 (let ((res (funcall search-fun string bound noerror)))
116 ;; Reset wrapping for all-buffers pause after successful search
117 (if (and res (eq isearch-buffers-pause t))
118 (setq isearch-buffers-current-buffer nil))
119 res)
120 ;; 2. If the above search fails, start visiting next/prev buffers
121 ;; successively, and search the string in them. Do this only
122 ;; when bound is nil (i.e. not while lazy-highlighting search
123 ;; strings in the current buffer).
124 (unless bound
125 ;; If no-pause or there was one attempt to leave the current buffer
126 (if (or (null isearch-buffers-pause)
127 (and isearch-buffers-pause isearch-buffers-current-buffer))
128 (condition-case nil
129 (progn
130 (while (not found)
131 ;; Find the next buffer to search
132 (setq buffer (funcall isearch-buffers-next-buffer-function
133 buffer))
134 (with-current-buffer buffer
135 (goto-char (if isearch-forward (point-min) (point-max)))
136 (setq isearch-barrier (point) isearch-opoint (point))
137 ;; After visiting the next/prev buffer search the
138 ;; string in it again, until the function in
139 ;; isearch-buffers-next-buffer-function raises an error
140 ;; at the beginning/end of the buffer sequence.
141 (setq found (funcall search-fun string bound noerror))))
142 ;; Set buffer for isearch-search-string to switch
143 (if buffer (setq isearch-buffers-current-buffer buffer))
144 ;; Return point of the new search result
145 found)
146 ;; Return nil when isearch-buffers-next-buffer-function fails
147 (error nil))
148 (signal 'search-failed (list string "Repeat for next buffer"))))))))
149
150 (defun isearch-buffers-wrap ()
151 "Wrap the multiple buffers search when search is failed.
152 Switch buffer to the first buffer for a forward search,
153 or to the last buffer for a backward search.
154 Set `isearch-buffers-current-buffer' to the current buffer to display
155 the isearch suffix message [initial buffer] only when isearch leaves
156 the initial buffer."
157 (if (or (null isearch-buffers-pause)
158 (and isearch-buffers-pause isearch-buffers-current-buffer))
159 (progn
160 (switch-to-buffer
161 (setq isearch-buffers-current-buffer
162 (funcall isearch-buffers-next-buffer-function
163 (current-buffer) t)))
164 (goto-char (if isearch-forward (point-min) (point-max))))
165 (setq isearch-buffers-current-buffer (current-buffer))
166 (setq isearch-wrapped nil)))
167
168 (defun isearch-buffers-push-state ()
169 "Save a function restoring the state of multiple buffers search.
170 Save the current buffer to the additional state parameter in the
171 search status stack."
172 `(lambda (cmd)
173 (isearch-buffers-pop-state cmd ,(current-buffer))))
174
175 (defun isearch-buffers-pop-state (cmd buffer)
176 "Restore the multiple buffers search state.
177 Switch to the buffer restored from the search status stack."
178 (unless (equal buffer (current-buffer))
179 (switch-to-buffer (setq isearch-buffers-current-buffer buffer))))
180
181 (provide 'isearch-multi)
182 ;;; isearch-multi.el ends here