Commit | Line | Data |
---|---|---|
76550a57 | 1 | ;;; isearch.el --- incremental search minor mode. |
3b1e4dd1 | 2 | |
b78559b0 | 3 | ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc. |
3a801d0c | 4 | |
3b1e4dd1 ER |
5 | ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu> |
6 | ||
b78559b0 | 7 | ;; |$Date: 1993/06/04 06:40:45 $|$Revision: 1.40 $ |
8e1cae6d | 8 | |
08200510 RS |
9 | ;; This file is not yet part of GNU Emacs, but it is based almost |
10 | ;; entirely on isearch.el which is part of GNU Emacs. | |
8e1cae6d JB |
11 | |
12 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 | ;; but WITHOUT ANY WARRANTY. No author or distributor | |
14 | ;; accepts responsibility to anyone for the consequences of using it | |
15 | ;; or for whether it serves any particular purpose or works at all, | |
16 | ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
17 | ;; License for full details. | |
18 | ||
19 | ;; Everyone is granted permission to copy, modify and redistribute | |
20 | ;; GNU Emacs, but only under the conditions described in the | |
21 | ;; GNU Emacs General Public License. A copy of this license is | |
22 | ;; supposed to have been given to you along with GNU Emacs so you | |
23 | ;; can know your rights and responsibilities. It should be in a | |
24 | ;; file named COPYING. Among other things, the copyright notice | |
25 | ;; and this notice must be preserved on all copies. | |
26 | ||
3b1e4dd1 ER |
27 | ;;; Commentary: |
28 | ||
8e1cae6d JB |
29 | ;;;==================================================================== |
30 | ;; Instructions | |
31 | ||
32 | ;; Searching with isearch-mode.el should work just like isearch.el, | |
33 | ;; except it is done in a temporary minor mode that terminates when | |
34 | ;; you finish searching. | |
35 | ||
36 | ;; To use isearch-mode instead of the standard isearch.el, add the | |
37 | ;; following to your .emacs file. The standard key bindings to | |
38 | ;; isearch-forward, etc, will then use isearch-mode instead of | |
39 | ;; isearch. | |
40 | ||
b457cc3b | 41 | ;; (defalias 'isearch 'isearch-mode) |
8e1cae6d JB |
42 | ;; (autoload 'isearch-mode "isearch-mode") |
43 | ||
08200510 RS |
44 | ;; For programmed use of isearch-mode, e.g. calling (isearch-forward), |
45 | ;; isearch-mode behaves modally and does not return until the search | |
46 | ;; is completed. It uses a recursive-edit to behave this way. Note: | |
47 | ;; gnus does it wrong: (call-interactively 'isearch-forward). | |
48 | ||
49 | ;; If any package you use invokes isearching non-interactively to get | |
50 | ;; the modal behavior described above, you must use the redefinitions | |
51 | ;; of isearch-forward, etc. found in this file instead of those in | |
52 | ;; loaddefs.el. The simplest way to ensure this is to just load | |
53 | ;; isearch-mode explicitly in your .emacs instead of using the above | |
b457cc3b | 54 | ;; defalias and autoload. |
08200510 RS |
55 | |
56 | ;; (load "isearch-mode") | |
57 | ||
8e1cae6d | 58 | ;; The key bindings active within isearch-mode are defined below in |
08200510 RS |
59 | ;; `isearch-mode-map' which is given bindings close to the default |
60 | ;; characters of the original isearch.el. With `isearch-mode', | |
61 | ;; however, you can bind multi-character keys and it should be easier | |
62 | ;; to add new commands. One bug though: keys with meta-prefix cannot | |
63 | ;; be longer than two chars. Also see minibuffer-local-isearch-map | |
64 | ;; for bindings active during `isearch-edit-string'. | |
65 | ||
66 | ;; Note to emacs version 19 users: isearch-mode should work even if | |
67 | ;; you switch windows with the mouse, in which case isearch-mode is | |
68 | ;; terminated automatically before the switch. This is true of lemacs | |
69 | ;; too, with a few more cleanups I've neglected in this release. | |
70 | ;; No one has supplied patches for epoch yet. | |
71 | ||
72 | ;; The search ring and completion commands automatically put you in | |
73 | ;; the minibuffer to edit the string. This gives you a chance to | |
74 | ;; modify the search string before executing the search. There are | |
75 | ;; three commands to terminate the editing: C-s and C-r exit the | |
76 | ;; minibuffer and search forward and reverse respectively, while C-m | |
77 | ;; exits and does a nonincremental search. | |
78 | ||
79 | ;; Exiting immediately from isearch uses isearch-edit-string instead | |
80 | ;; of nonincremental-search, if search-nonincremental-instead is non-nil. | |
81 | ;; The name of this option should probably be changed if we decide to | |
82 | ;; keep the behavior. No point in forcing nonincremental search until | |
83 | ;; the last possible moment. | |
84 | ||
85 | ;; TODO | |
86 | ;; - Integrate the emacs 19 generalized commmand history. | |
87 | ;; - Think about incorporating query-replace. | |
88 | ;; - Hooks and options for failed search. | |
282d89c0 | 89 | |
3b1e4dd1 ER |
90 | ;;; Change Log: |
91 | ||
8cb2ceaa RS |
92 | ;;; Changes before those recorded in ChangeLog: |
93 | ||
08200510 RS |
94 | ;;; Revision 1.4 92/09/14 16:26:02 liberte |
95 | ;;; Added prefix args to isearch-forward, etc. to switch between | |
96 | ;;; string and regular expression searching. | |
97 | ;;; Added some support for lemacs. | |
98 | ;;; Added general isearch-highlight option - but only for lemacs so far. | |
99 | ;;; Added support for frame switching in emacs 19. | |
100 | ;;; Added word search option to isearch-edit-string. | |
101 | ;;; Renamed isearch-quit to isearch-abort. | |
102 | ;;; Numerous changes to comments and doc strings. | |
282d89c0 | 103 | ;;; |
08200510 RS |
104 | ;;; Revision 1.3 92/06/29 13:10:08 liberte |
105 | ;;; Moved modal isearch-mode handling into isearch-mode. | |
106 | ;;; Got rid of buffer-local isearch variables. | |
107 | ;;; isearch-edit-string used by ring adjustments, completion, and | |
108 | ;;; nonincremental searching. C-s and C-r are additional exit commands. | |
109 | ;;; Renamed all regex to regexp. | |
110 | ;;; Got rid of found-start and found-point globals. | |
111 | ;;; Generalized handling of upper-case chars. | |
112 | ||
113 | ;;; Revision 1.2 92/05/27 11:33:57 liberte | |
114 | ;;; Emacs version 19 has a search ring, which is supported here. | |
115 | ;;; Other fixes found in the version 19 isearch are included here. | |
116 | ;;; | |
117 | ;;; Also see variables search-caps-disable-folding, | |
118 | ;;; search-nonincremental-instead, search-whitespace-regexp, and | |
119 | ;;; commands isearch-toggle-regexp, isearch-edit-string. | |
120 | ;;; | |
121 | ;;; semi-modal isearching is supported. | |
282d89c0 ER |
122 | |
123 | ;;; Changes for 1.1 | |
124 | ;;; 3/18/92 Fixed invalid-regexp. | |
125 | ;;; 3/18/92 Fixed yanking in regexps. | |
126 | ||
3b1e4dd1 | 127 | ;;; Code: |
08200510 RS |
128 | |
129 | \f | |
130 | ;;;========================================================================= | |
131 | ;;; Emacs features | |
132 | ||
133 | ;; isearch-mode takes advantage of the features provided by several | |
134 | ;; different versions of emacs. Rather than testing the version of | |
135 | ;; emacs, several constants are defined, one for each of the features. | |
136 | ;; Each of the tests below must work on any version of emacs. | |
137 | ;; (Perhaps provide and featurep could be used for this purpose.) | |
138 | ||
1a3a6707 | 139 | (defconst isearch-gnu-emacs-events (fboundp 'set-frame-height)) ;; emacs 19 |
08200510 | 140 | (defconst isearch-pre-command-hook-exists (boundp 'pre-command-hook)) ;; lemacs |
a5dcf63f | 141 | (defconst isearch-event-data-type nil) ;; lemacs |
08200510 | 142 | |
fdf5afa4 | 143 | (defconst search-exit-option t |
a8f783b2 | 144 | "*Non-nil means random control characters terminate incremental search.") |
fdf5afa4 RS |
145 | |
146 | (defvar search-slow-window-lines 1 | |
147 | "*Number of lines in slow search display windows. | |
148 | These are the short windows used during incremental search on slow terminals. | |
149 | Negative means put the slow search window at the top (normally it's at bottom) | |
150 | and the value is minus the number of lines.") | |
151 | ||
152 | (defvar search-slow-speed 1200 | |
153 | "*Highest terminal speed at which to use \"slow\" style incremental search. | |
154 | This is the style where a one-line window is created to show the line | |
155 | that the search has reached.") | |
8e1cae6d JB |
156 | |
157 | ;;;======================================================================== | |
158 | ;;; Some additional options and constants. | |
159 | ||
b78559b0 | 160 | (defvar search-upper-case 'not-yanks |
8e1cae6d | 161 | "*If non-nil, upper case chars disable case fold searching. |
08200510 RS |
162 | That is, upper and lower case chars must match exactly. |
163 | This applies no matter where the chars come from, but does not | |
fdf5afa4 RS |
164 | apply to chars in regexps that are prefixed with `\\'. |
165 | If this value is `not-yanks', yanked text is always downcased.") | |
8e1cae6d JB |
166 | |
167 | (defvar search-nonincremental-instead t | |
168 | "*If non-nil, do a nonincremental search instead if exiting immediately. | |
08200510 RS |
169 | Actually, `isearch-edit-string' is called to let you enter the search |
170 | string, and RET terminates editing and does a nonincremental search.") | |
8e1cae6d JB |
171 | |
172 | (defconst search-whitespace-regexp "\\s-+" | |
173 | "*If non-nil, regular expression to match a sequence of whitespace chars. | |
174 | You might want to use something like \"[ \\t\\r\\n]+\" instead.") | |
175 | ||
a8f783b2 RS |
176 | ;; I removed the * from the doc string because highlighting is not |
177 | ;; currently a clean thing to do. Once highlighting is made clean, | |
178 | ;; this feature can be re-enabled and advertised. | |
08200510 | 179 | (defvar search-highlight nil |
b78559b0 | 180 | "*Non-nil means incremental search highlights the current match.") |
08200510 RS |
181 | |
182 | (defvar isearch-mode-hook nil | |
183 | "Function(s) to call after starting up an incremental search.") | |
184 | ||
185 | (defvar isearch-mode-end-hook nil | |
186 | "Function(s) to call after terminating an incremental search.") | |
187 | ||
8e1cae6d JB |
188 | ;;;================================================================== |
189 | ;;; Search ring. | |
8e1cae6d JB |
190 | |
191 | (defvar search-ring nil | |
192 | "List of search string sequences.") | |
08200510 | 193 | (defvar regexp-search-ring nil |
8e1cae6d JB |
194 | "List of regular expression search string sequences.") |
195 | ||
196 | (defconst search-ring-max 16 | |
197 | "*Maximum length of search ring before oldest elements are thrown away.") | |
08200510 RS |
198 | (defconst regexp-search-ring-max 16 |
199 | "*Maximum length of regexp search ring before oldest elements are thrown away.") | |
8e1cae6d JB |
200 | |
201 | (defvar search-ring-yank-pointer nil | |
a5dcf63f RS |
202 | "Index in `search-ring' of last string reused. |
203 | nil if none yet.") | |
08200510 | 204 | (defvar regexp-search-ring-yank-pointer nil |
a5dcf63f RS |
205 | "Index in `regexp-search-ring' of last string reused. |
206 | nil if none yet.") | |
8e1cae6d | 207 | |
08200510 RS |
208 | (defvar search-ring-update nil |
209 | "*Non-nil if advancing or retreating in the search ring should cause search. | |
210 | Default value, nil, means edit the string instead.") | |
211 | ||
8e1cae6d JB |
212 | ;;;==================================================== |
213 | ;;; Define isearch-mode keymap. | |
214 | ||
215 | (defvar isearch-mode-map nil | |
216 | "Keymap for isearch-mode.") | |
217 | ||
08200510 RS |
218 | (or isearch-mode-map |
219 | (let* ((i 0) | |
fdf5afa4 | 220 | (map (make-keymap))) |
fcf8ba15 RS |
221 | (or (vectorp (nth 1 map)) |
222 | (error "The initialization of isearch-mode-map must be updated")) | |
223 | ;; Give this map a vector 256 long, for dense binding | |
224 | ;; of a larger range of ordinary characters. | |
225 | (setcar (cdr map) (make-vector 256 nil)) | |
08200510 | 226 | |
ac689515 RS |
227 | ;; Make function keys, etc, exit the search. |
228 | (define-key map [t] 'isearch-other-control-char) | |
08200510 | 229 | ;; Control chars, by default, end isearch mode transparently. |
ac689515 RS |
230 | ;; We need these explicit definitions because, in a dense keymap, |
231 | ;; the binding for t does not affect characters. | |
232 | ;; We use a dense keymap to save space. | |
08200510 RS |
233 | (while (< i ?\ ) |
234 | (define-key map (make-string 1 i) 'isearch-other-control-char) | |
235 | (setq i (1+ i))) | |
236 | ||
8cb2ceaa | 237 | ;; Printing chars extend the search string by default. |
ac689515 | 238 | (setq i ?\ ) |
8d7e4e80 | 239 | (while (< i (length (nth 1 map))) |
8cb2ceaa | 240 | (define-key map (vector i) 'isearch-printing-char) |
08200510 RS |
241 | (setq i (1+ i))) |
242 | ||
243 | ;; Several non-printing chars change the searching behavior. | |
244 | (define-key map "\C-s" 'isearch-repeat-forward) | |
245 | (define-key map "\C-r" 'isearch-repeat-backward) | |
246 | (define-key map "\177" 'isearch-delete-char) | |
247 | (define-key map "\C-g" 'isearch-abort) | |
8e1cae6d | 248 | |
08200510 RS |
249 | (define-key map "\C-q" 'isearch-quote-char) |
250 | ||
08200510 RS |
251 | (define-key map "\r" 'isearch-exit) |
252 | (define-key map "\C-j" 'isearch-printing-char) | |
253 | (define-key map "\t" 'isearch-printing-char) | |
254 | (define-key map " " 'isearch-whitespace-chars) | |
8e1cae6d | 255 | |
08200510 RS |
256 | (define-key map "\C-w" 'isearch-yank-word) |
257 | (define-key map "\C-y" 'isearch-yank-line) | |
258 | ||
e7cb0106 RS |
259 | ;; Bind the ASCII-equivalent "function keys" explicitly |
260 | ;; if we bind their equivalents, | |
261 | ;; since otherwise the default binding would override. | |
262 | ;; We bind [escape] below. | |
263 | (define-key map [tab] 'isearch-printing-char) | |
264 | (define-key map [delete] 'isearch-delete-char) | |
265 | (define-key map [backspace] 'isearch-delete-char) | |
266 | (define-key map [return] 'isearch-exit) | |
267 | (define-key map [newline] 'isearch-printing-char) | |
268 | ||
08200510 RS |
269 | ;; Define keys for regexp chars * ? |. |
270 | ;; Nothing special for + because it matches at least once. | |
271 | (define-key map "*" 'isearch-*-char) | |
272 | (define-key map "?" 'isearch-*-char) | |
273 | (define-key map "|" 'isearch-|-char) | |
274 | ||
58f1634a RS |
275 | ;;; Turned off because I find I expect to get the global definition--rms. |
276 | ;;; ;; Instead bind C-h to special help command for isearch-mode. | |
277 | ;;; (define-key map "\C-h" 'isearch-mode-help) | |
08200510 RS |
278 | |
279 | ;; To handle local bindings with meta char prefix keys, define | |
280 | ;; another full keymap. This must be done for any other prefix | |
281 | ;; keys as well, one full keymap per char of the prefix key. It | |
282 | ;; would be simpler to disable the global keymap, and/or have a | |
283 | ;; default local key binding for any key not otherwise bound. | |
e7cb0106 RS |
284 | (let ((meta-map (make-sparse-keymap))) |
285 | (define-key map (char-to-string meta-prefix-char) meta-map) | |
286 | (define-key map [escape] meta-map)) | |
ac689515 | 287 | (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char) |
08200510 RS |
288 | |
289 | (define-key map "\M-n" 'isearch-ring-advance) | |
290 | (define-key map "\M-p" 'isearch-ring-retreat) | |
1a3a6707 | 291 | |
08200510 RS |
292 | (define-key map "\M-\t" 'isearch-complete) |
293 | ||
294 | ;; For emacs 19, switching frames should terminate isearch-mode | |
1a3a6707 | 295 | (if isearch-gnu-emacs-events |
08200510 RS |
296 | (define-key map [switch-frame] 'isearch-switch-frame-handler)) |
297 | ||
298 | (setq isearch-mode-map map) | |
299 | )) | |
8e1cae6d | 300 | |
08200510 RS |
301 | ;; Some bindings you may want to put in your isearch-mode-hook. |
302 | ;; Suggest some alternates... | |
303 | ;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-regexp) | |
304 | ;; (define-key isearch-mode-map "\C-^" 'isearch-edit-string) | |
8e1cae6d | 305 | |
8e1cae6d | 306 | |
08200510 RS |
307 | (defvar minibuffer-local-isearch-map nil |
308 | "Keymap for editing isearch strings in the minibuffer.") | |
8e1cae6d | 309 | |
08200510 RS |
310 | (or minibuffer-local-isearch-map |
311 | (let ((map (copy-keymap minibuffer-local-map))) | |
312 | (define-key map "\r" 'isearch-nonincremental-exit-minibuffer) | |
35a4d143 RS |
313 | (define-key map "\M-n" 'isearch-ring-advance-edit) |
314 | (define-key map "\M-p" 'isearch-ring-retreat-edit) | |
08200510 RS |
315 | (define-key map "\M-\t" 'isearch-complete-edit) |
316 | (define-key map "\C-s" 'isearch-forward-exit-minibuffer) | |
317 | (define-key map "\C-r" 'isearch-reverse-exit-minibuffer) | |
318 | (setq minibuffer-local-isearch-map map) | |
319 | )) | |
8e1cae6d JB |
320 | |
321 | ;;;======================================================== | |
322 | ;; Internal variables declared globally for byte-compiler. | |
08200510 RS |
323 | ;; These are all set with setq while isearching |
324 | ;; and bound locally while editing the search string. | |
325 | ||
326 | (defvar isearch-forward nil) ; Searching in the forward direction. | |
327 | (defvar isearch-regexp nil) ; Searching for a regexp. | |
328 | (defvar isearch-word nil) ; Searching for words. | |
329 | ||
330 | (defvar isearch-cmds nil) ; Stack of search status sets. | |
331 | (defvar isearch-string "") ; The current search string. | |
332 | (defvar isearch-message "") ; text-char-description version of isearch-string | |
333 | ||
334 | (defvar isearch-success t) ; Searching is currently successful. | |
335 | (defvar isearch-invalid-regexp nil) ; Regexp not well formed. | |
336 | (defvar isearch-other-end nil) ; Start (end) of match if forward (backward). | |
337 | (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom). | |
8e1cae6d JB |
338 | (defvar isearch-barrier 0) |
339 | ||
08200510 | 340 | (defvar isearch-case-fold-search nil) ; case-fold-search while searching. |
8e1cae6d JB |
341 | |
342 | (defvar isearch-adjusted nil) | |
343 | (defvar isearch-slow-terminal-mode nil) | |
08200510 RS |
344 | ;;; If t, using a small window. |
345 | (defvar isearch-small-window nil) | |
8e1cae6d | 346 | (defvar isearch-opoint 0) |
08200510 RS |
347 | ;;; The window configuration active at the beginning of the search. |
348 | (defvar isearch-window-configuration nil) | |
349 | (defvar isearch-old-local-map nil) | |
8e1cae6d | 350 | |
08200510 RS |
351 | ;; Flag to indicate a yank occurred, so don't move the cursor. |
352 | (defvar isearch-yank-flag nil) | |
8e1cae6d | 353 | |
08200510 RS |
354 | ;;; A function to be called after each input character is processed. |
355 | ;;; (It is not called after characters that exit the search.) | |
356 | ;;; It is only set from an optional argument to `isearch-mode'. | |
357 | (defvar isearch-op-fun nil) | |
8e1cae6d | 358 | |
08200510 RS |
359 | ;;; Is isearch-mode in a recursive edit for modal searching. |
360 | (defvar isearch-recursive-edit nil) | |
8e1cae6d | 361 | |
08200510 RS |
362 | ;;; Should isearch be terminated after doing one search? |
363 | (defvar isearch-nonincremental nil) | |
364 | ||
365 | ;; New value of isearch-forward after isearch-edit-string. | |
366 | (defvar isearch-new-forward nil) | |
8e1cae6d | 367 | |
8e1cae6d JB |
368 | |
369 | ;;;============================================================== | |
370 | ;; Minor-mode-alist changes - kind of redundant with the | |
371 | ;; echo area, but if isearching in multiple windows, it can be useful. | |
372 | ||
373 | (or (assq 'isearch-mode minor-mode-alist) | |
374 | (nconc minor-mode-alist | |
375 | (list '(isearch-mode isearch-mode)))) | |
376 | ||
08200510 | 377 | (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil. |
8e1cae6d JB |
378 | (make-variable-buffer-local 'isearch-mode) |
379 | ||
fdf5afa4 RS |
380 | (define-key global-map "\C-s" 'isearch-forward) |
381 | (define-key esc-map "\C-s" 'isearch-forward-regexp) | |
382 | (define-key global-map "\C-r" 'isearch-backward) | |
383 | (define-key esc-map "\C-r" 'isearch-backward-regexp) | |
384 | ||
8e1cae6d JB |
385 | ;;;=============================================================== |
386 | ;;; Entry points to isearch-mode. | |
08200510 | 387 | ;;; These four functions should replace those in loaddefs.el |
b457cc3b | 388 | ;;; An alternative is to defalias isearch-forward etc to isearch-mode, |
08200510 | 389 | ;;; and look at this-command to set the options accordingly. |
8e1cae6d | 390 | |
eceee2c0 | 391 | (defun isearch-forward (&optional regexp-p no-recursive-edit) |
8e1cae6d JB |
392 | "\ |
393 | Do incremental search forward. | |
08200510 RS |
394 | With a prefix argument, do an incremental regular expression search instead. |
395 | \\<isearch-mode-map> | |
8e1cae6d | 396 | As you type characters, they add to the search string and are found. |
08200510 | 397 | The following non-printing keys are bound in `isearch-mode-map'. |
8e1cae6d | 398 | |
08200510 | 399 | Type \\[isearch-delete-char] to cancel characters from end of search string. |
8e1cae6d | 400 | Type \\[isearch-exit] to exit, leaving point at location found. |
08200510 RS |
401 | Type LFD (C-j) to match end of line. |
402 | Type \\[isearch-repeat-forward] to search again forward,\ | |
403 | \\[isearch-repeat-backward] to search again backward. | |
404 | Type \\[isearch-yank-word] to yank word from buffer onto end of search\ | |
405 | string and search for it. | |
406 | Type \\[isearch-yank-line] to yank rest of line onto end of search string\ | |
407 | and search for it. | |
8e1cae6d | 408 | Type \\[isearch-quote-char] to quote control character to search for it. |
08200510 RS |
409 | \\[isearch-abort] while searching or when search has failed cancels input\ |
410 | back to what has | |
411 | been found successfully. | |
412 | \\[isearch-abort] when search is successful aborts and moves point to\ | |
413 | starting point. | |
8e1cae6d JB |
414 | |
415 | Also supported is a search ring of the previous 16 search strings. | |
416 | Type \\[isearch-ring-advance] to search for the next item in the search ring. | |
08200510 RS |
417 | Type \\[isearch-ring-retreat] to search for the previous item in the search\ |
418 | ring. | |
419 | Type \\[isearch-complete] to complete the search string using the search ring. | |
8e1cae6d | 420 | |
08200510 RS |
421 | The above keys, bound in `isearch-mode-map', are often controlled by |
422 | options; do M-x apropos on search-.* to find them. | |
8e1cae6d | 423 | Other control and meta characters terminate the search |
08200510 | 424 | and are then executed normally (depending on `search-exit-option'). |
8e1cae6d | 425 | |
08200510 RS |
426 | If this function is called non-interactively, it does not return to |
427 | the calling function until the search is done." | |
8e1cae6d | 428 | |
eceee2c0 | 429 | (interactive "P\np") |
4ae7d00a | 430 | (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit))) |
8e1cae6d | 431 | |
eceee2c0 | 432 | (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit) |
8e1cae6d JB |
433 | "\ |
434 | Do incremental search forward for regular expression. | |
08200510 | 435 | With a prefix argument, do a regular string search instead. |
8e1cae6d JB |
436 | Like ordinary incremental search except that your input |
437 | is treated as a regexp. See \\[isearch-forward] for more info." | |
eceee2c0 | 438 | (interactive "P\np") |
4ae7d00a | 439 | (isearch-mode t (null not-regexp) nil (not no-recursive-edit))) |
8e1cae6d | 440 | |
eceee2c0 | 441 | (defun isearch-backward (&optional regexp-p no-recursive-edit) |
8e1cae6d JB |
442 | "\ |
443 | Do incremental search backward. | |
08200510 | 444 | With a prefix argument, do a regular expression search instead. |
8e1cae6d | 445 | See \\[isearch-forward] for more information." |
eceee2c0 | 446 | (interactive "P\np") |
4ae7d00a | 447 | (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit))) |
8e1cae6d | 448 | |
eceee2c0 | 449 | (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit) |
8e1cae6d JB |
450 | "\ |
451 | Do incremental search backward for regular expression. | |
08200510 | 452 | With a prefix argument, do a regular string search instead. |
8e1cae6d JB |
453 | Like ordinary incremental search except that your input |
454 | is treated as a regexp. See \\[isearch-forward] for more info." | |
eceee2c0 | 455 | (interactive "P\np") |
4ae7d00a | 456 | (isearch-mode nil (null not-regexp) nil (not no-recursive-edit))) |
08200510 RS |
457 | |
458 | ||
459 | (defun isearch-mode-help () | |
460 | (interactive) | |
461 | (describe-function 'isearch-forward) | |
462 | (isearch-update)) | |
8e1cae6d JB |
463 | |
464 | \f | |
465 | ;;;================================================================== | |
466 | ;; isearch-mode only sets up incremental search for the minor mode. | |
467 | ;; All the work is done by the isearch-mode commands. | |
468 | ||
08200510 RS |
469 | ;; Not used yet: |
470 | ;;(defconst isearch-commands '(isearch-forward isearch-backward | |
471 | ;; isearch-forward-regexp isearch-backward-regexp) | |
472 | ;; "List of commands for which isearch-mode does not recursive-edit.") | |
473 | ||
474 | ||
475 | (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p) | |
8e1cae6d | 476 | "Start isearch minor mode. Called by isearch-forward, etc." |
8e1cae6d JB |
477 | |
478 | ;; Initialize global vars. | |
479 | (setq isearch-forward forward | |
480 | isearch-regexp regexp | |
08200510 | 481 | isearch-word word-p |
8e1cae6d JB |
482 | isearch-op-fun op-fun |
483 | isearch-case-fold-search case-fold-search | |
484 | isearch-string "" | |
485 | isearch-message "" | |
486 | isearch-cmds nil | |
487 | isearch-success t | |
488 | isearch-wrapped nil | |
489 | isearch-barrier (point) | |
490 | isearch-adjusted nil | |
491 | isearch-yank-flag nil | |
492 | isearch-invalid-regexp nil | |
dd492f04 JA |
493 | ;; Use (baud-rate) for now, for sake of other versions. |
494 | isearch-slow-terminal-mode (and (<= (baud-rate) search-slow-speed) | |
8e1cae6d JB |
495 | (> (window-height) |
496 | (* 4 search-slow-window-lines))) | |
497 | isearch-other-end nil | |
498 | isearch-small-window nil | |
8e1cae6d | 499 | |
8e1cae6d | 500 | isearch-opoint (point) |
a5dcf63f RS |
501 | isearch-old-local-map (current-local-map) |
502 | search-ring-yank-pointer nil | |
503 | regexp-search-ring-yank-pointer nil) | |
70418205 RS |
504 | (if isearch-slow-terminal-mode |
505 | (setq isearch-window-configuration (current-window-configuration))) | |
506 | ||
2ed45c8b RS |
507 | ;; This was for Lucid Emacs. But now that we have pre-command-hook, |
508 | ;; it causes trouble. | |
509 | ;; (if isearch-pre-command-hook-exists | |
510 | ;; (add-hook 'pre-command-hook 'isearch-pre-command-hook)) | |
8e1cae6d JB |
511 | (setq isearch-mode " Isearch") ;; forward? regexp? |
512 | (set-buffer-modified-p (buffer-modified-p)) ; update modeline | |
513 | ||
7492a21e RS |
514 | ;; It is ugly to show region highlighting while the search |
515 | ;; is going on. And we don't want the mark active at the end either. | |
516 | (setq deactivate-mark t) | |
517 | ||
8e1cae6d JB |
518 | (isearch-push-state) |
519 | ||
520 | (use-local-map isearch-mode-map) | |
521 | (isearch-update) | |
522 | (run-hooks 'isearch-mode-hook) | |
08200510 RS |
523 | |
524 | ;; isearch-mode can be made modal (in the sense of not returning to | |
525 | ;; the calling function until searching is completed) by entering | |
526 | ;; a recursive-edit and exiting it when done isearching. | |
130bf67c RS |
527 | (if recursive-edit |
528 | (let ((isearch-recursive-edit t)) | |
529 | (recursive-edit))) | |
8e1cae6d JB |
530 | ) |
531 | ||
532 | ||
533 | ;;;==================================================== | |
534 | ;; Some high level utilities. Others below. | |
535 | ||
536 | (defun isearch-update () | |
537 | ;; Called after each command to update the display. | |
a5dcf63f | 538 | (if (if isearch-event-data-type |
08200510 | 539 | (null unread-command-event) |
1a3a6707 | 540 | (if isearch-gnu-emacs-events |
dbc4e1c1 | 541 | (null unread-command-events) |
08200510 | 542 | (< unread-command-char 0))) |
8e1cae6d JB |
543 | (progn |
544 | (if (not (input-pending-p)) | |
545 | (isearch-message)) | |
546 | (if (and isearch-slow-terminal-mode | |
547 | (not (or isearch-small-window | |
548 | (pos-visible-in-window-p)))) | |
08200510 | 549 | (let ((found-point (point))) |
8e1cae6d | 550 | (setq isearch-small-window t) |
8e1cae6d JB |
551 | (move-to-window-line 0) |
552 | (let ((window-min-height 1)) | |
553 | (split-window nil (if (< search-slow-window-lines 0) | |
554 | (1+ (- search-slow-window-lines)) | |
555 | (- (window-height) | |
556 | (1+ search-slow-window-lines))))) | |
557 | (if (< search-slow-window-lines 0) | |
558 | (progn (vertical-motion (- 1 search-slow-window-lines)) | |
559 | (set-window-start (next-window) (point)) | |
560 | (set-window-hscroll (next-window) | |
561 | (window-hscroll)) | |
562 | (set-window-hscroll (selected-window) 0)) | |
563 | (other-window 1)) | |
08200510 RS |
564 | (goto-char found-point))) |
565 | (if isearch-other-end | |
566 | (if (< isearch-other-end (point)) ; isearch-forward? | |
567 | (isearch-highlight isearch-other-end (point)) | |
568 | (isearch-highlight (point) isearch-other-end))) | |
569 | )) | |
8e1cae6d JB |
570 | (setq ;; quit-flag nil not for isearch-mode |
571 | isearch-adjusted nil | |
572 | isearch-yank-flag nil) | |
573 | ) | |
574 | ||
575 | ||
35a4d143 | 576 | (defun isearch-done (&optional nopush) |
8e1cae6d | 577 | ;; Called by all commands that terminate isearch-mode. |
35a4d143 | 578 | ;; If NOPUSH is non-nil, we don't push the string on the search ring. |
8e1cae6d | 579 | (use-local-map isearch-old-local-map) |
08200510 RS |
580 | ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs |
581 | (isearch-dehighlight t) | |
582 | (let ((found-start (window-start (selected-window))) | |
583 | (found-point (point))) | |
70418205 RS |
584 | (if isearch-window-configuration |
585 | (set-window-configuration isearch-window-configuration)) | |
08200510 RS |
586 | |
587 | ;; If there was movement, mark the starting position. | |
588 | ;; Maybe should test difference between and set mark iff > threshold. | |
589 | (if (/= (point) isearch-opoint) | |
7872ee4d RS |
590 | (progn |
591 | (push-mark isearch-opoint t) | |
20f3a745 RS |
592 | (if transient-mark-mode |
593 | (setq mark-active nil)) | |
7872ee4d RS |
594 | (or executing-macro (> (minibuffer-depth) 0) |
595 | (message "Mark saved where search started"))) | |
08200510 RS |
596 | ;; (message "") why is this needed? |
597 | ) | |
598 | (if isearch-small-window | |
599 | (goto-char found-point) | |
600 | ;; Exiting the save-window-excursion clobbers window-start; restore it. | |
601 | (set-window-start (selected-window) found-start t))) | |
602 | ||
603 | (setq isearch-mode nil) | |
604 | (set-buffer-modified-p (buffer-modified-p)) ;; update modeline | |
8e1cae6d | 605 | |
35a4d143 | 606 | (if (and (> (length isearch-string) 0) (not nopush)) |
8e1cae6d JB |
607 | ;; Update the ring data. |
608 | (if isearch-regexp | |
a8f783b2 RS |
609 | (if (or (null regexp-search-ring) |
610 | (not (string= isearch-string (car regexp-search-ring)))) | |
8e1cae6d | 611 | (progn |
08200510 | 612 | (setq regexp-search-ring |
a5dcf63f | 613 | (cons isearch-string regexp-search-ring)) |
08200510 RS |
614 | (if (> (length regexp-search-ring) regexp-search-ring-max) |
615 | (setcdr (nthcdr (1- search-ring-max) regexp-search-ring) | |
8e1cae6d | 616 | nil)))) |
a8f783b2 RS |
617 | (if (or (null search-ring) |
618 | (not (string= isearch-string (car search-ring)))) | |
8e1cae6d | 619 | (progn |
a5dcf63f | 620 | (setq search-ring (cons isearch-string search-ring)) |
8e1cae6d JB |
621 | (if (> (length search-ring) search-ring-max) |
622 | (setcdr (nthcdr (1- search-ring-max) search-ring) nil)))))) | |
623 | ||
8e1cae6d | 624 | (run-hooks 'isearch-mode-end-hook) |
08200510 RS |
625 | (if isearch-recursive-edit (exit-recursive-edit))) |
626 | ||
627 | ;;;======================================================= | |
628 | ;;; Switching buffers should first terminate isearch-mode. | |
629 | ;;; This is done quite differently for each varient of emacs. | |
630 | ;;; For lemacs, see Exiting in lemacs below | |
631 | ||
632 | ;; For Emacs 19, the frame switch event is handled. | |
633 | (defun isearch-switch-frame-handler () | |
634 | (interactive) ;; Is this necessary? | |
635 | ;; First terminate isearch-mode. | |
636 | (isearch-done) | |
dba1ec55 | 637 | (handle-switch-frame (car (cdr (isearch-last-command-char))))) |
08200510 RS |
638 | |
639 | ;;;======================================================== | |
8e1cae6d JB |
640 | |
641 | \f | |
642 | ;;;==================================================== | |
643 | ;; Commands active while inside of the isearch minor mode. | |
644 | ||
645 | (defun isearch-exit () | |
646 | "Exit search normally. | |
647 | However, if this is the first command after starting incremental | |
648 | search and `search-nonincremental-instead' is non-nil, do a | |
08200510 | 649 | nonincremental search instead via `isearch-edit-string'." |
8e1cae6d JB |
650 | (interactive) |
651 | (if (and search-nonincremental-instead | |
652 | (= 0 (length isearch-string))) | |
08200510 RS |
653 | (let ((isearch-nonincremental t)) |
654 | (isearch-edit-string))) | |
8e1cae6d JB |
655 | (isearch-done)) |
656 | ||
657 | ||
658 | (defun isearch-edit-string () | |
08200510 RS |
659 | "Edit the search string in the minibuffer. |
660 | The following additional command keys are active while editing. | |
661 | \\<minibuffer-local-isearch-map> | |
662 | \\[exit-minibuffer] to resume incremental searching with the edited string. | |
663 | \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search. | |
664 | \\[isearch-forward-exit-minibuffer] to resume isearching forward. | |
665 | \\[isearch-backward-exit-minibuffer] to resume isearching backward. | |
666 | \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring. | |
667 | \\[isearch-ring-retreat-edit] to replace the search string with the previou item in the search ring. | |
668 | \\[isearch-complete-edit] to complete the search string using the search ring. | |
669 | ||
670 | If first char entered is \\[isearch-yank-word], then do word search instead." | |
671 | ||
672 | ;; This code is very hairy for several reasons, explained in the code. | |
673 | ;; Mainly, isearch-mode must be terminated while editing and then restarted. | |
674 | ;; If there were a way to catch any change of buffer from the minibuffer, | |
675 | ;; this could be simplified greatly. | |
676 | ;; Editing doesnt back up the search point. Should it? | |
8e1cae6d | 677 | (interactive) |
08200510 | 678 | (condition-case err |
a5dcf63f | 679 | (let (isearch-nonincremental ; should search nonincrementally? |
08200510 RS |
680 | |
681 | ;; Locally bind all isearch global variables to protect them | |
682 | ;; from recursive isearching. | |
683 | ;; isearch-string -message and -forward are not bound | |
684 | ;; so they may be changed. Instead, save the values. | |
685 | (isearch-new-string isearch-string) | |
686 | (isearch-new-message isearch-message) | |
687 | (isearch-new-forward isearch-forward) | |
688 | (isearch-new-word isearch-word) | |
689 | ||
690 | (isearch-regexp isearch-regexp) | |
691 | (isearch-op-fun isearch-op-fun) | |
692 | (isearch-cmds isearch-cmds) | |
693 | (isearch-success isearch-success) | |
694 | (isearch-wrapped isearch-wrapped) | |
695 | (isearch-barrier isearch-barrier) | |
696 | (isearch-adjusted isearch-adjusted) | |
697 | (isearch-yank-flag isearch-yank-flag) | |
698 | (isearch-invalid-regexp isearch-invalid-regexp) | |
699 | (isearch-other-end isearch-other-end) | |
700 | (isearch-opoint isearch-opoint) | |
701 | (isearch-slow-terminal-mode isearch-slow-terminal-mode) | |
702 | (isearch-small-window isearch-small-window) | |
703 | (isearch-recursive-edit isearch-recursive-edit) | |
704 | ;; Save current configuration so we can restore it here. | |
705 | (isearch-window-configuration (current-window-configuration)) | |
706 | ) | |
707 | ||
708 | ;; Actually terminate isearching until editing is done. | |
709 | ;; This is so that the user can do anything without failure, | |
710 | ;; like switch buffers and start another isearch, and return. | |
711 | (condition-case err | |
35a4d143 | 712 | (isearch-done t) |
08200510 RS |
713 | (exit nil)) ; was recursive editing |
714 | ||
715 | (isearch-message) ;; for read-char | |
716 | (unwind-protect | |
717 | (let* (;; Why does following read-char echo? | |
718 | ;;(echo-keystrokes 0) ;; not needed with above message | |
d94307ee RS |
719 | (e (let ((cursor-in-echo-area t)) |
720 | (if isearch-event-data-type | |
721 | (allocate-event) (read-char)))) | |
08200510 RS |
722 | ;; Binding minibuffer-history-symbol to nil is a work-around |
723 | ;; for some incompatibility with gmhist. | |
724 | (minibuffer-history-symbol)) | |
725 | ;; If the first character the user types when we prompt them | |
726 | ;; for a string is the yank-word character, then go into | |
727 | ;; word-search mode. Otherwise unread that character and | |
728 | ;; read a key the normal way. | |
729 | ;; Word search does not apply (yet) to regexp searches, | |
730 | ;; no check is made here. | |
731 | (message (isearch-message-prefix nil nil t)) | |
732 | (if (eq 'isearch-yank-word | |
733 | (lookup-key | |
734 | isearch-mode-map | |
735 | (char-to-string | |
a5dcf63f | 736 | (if isearch-event-data-type |
08200510 RS |
737 | (or (event-to-character (next-command-event e)) 0) |
738 | e)))) | |
739 | (setq isearch-word t ;; so message-prefix is right | |
740 | isearch-new-word t) | |
741 | (isearch-unread e)) | |
a5dcf63f RS |
742 | (setq cursor-in-echo-area nil) |
743 | (setq isearch-new-string | |
35a4d143 | 744 | (let (junk-ring) |
a5dcf63f RS |
745 | (read-from-minibuffer (isearch-message-prefix) |
746 | isearch-string | |
747 | minibuffer-local-isearch-map nil | |
35a4d143 | 748 | 'junk-ring)) |
08200510 RS |
749 | isearch-new-message (mapconcat 'text-char-description |
750 | isearch-new-string ""))) | |
751 | ;; Always resume isearching by restarting it. | |
752 | (isearch-mode isearch-forward | |
753 | isearch-regexp | |
754 | isearch-op-fun | |
755 | isearch-recursive-edit | |
756 | isearch-word) | |
757 | ||
758 | ;; Copy new local values to isearch globals | |
759 | (setq isearch-string isearch-new-string | |
760 | isearch-message isearch-new-message | |
761 | isearch-forward isearch-new-forward | |
762 | isearch-word isearch-new-word)) | |
763 | ||
764 | ;; Empty isearch-string means use default. | |
765 | (if (= 0 (length isearch-string)) | |
a8f783b2 RS |
766 | (setq isearch-string (car (if isearch-regexp regexp-search-ring |
767 | search-ring))) | |
768 | ;; This used to set the last search string, | |
769 | ;; but I think it is not right to do that here. | |
770 | ;; Only the string actually used should be saved. | |
771 | ) | |
08200510 RS |
772 | |
773 | ;; Reinvoke the pending search. | |
774 | (isearch-push-state) | |
775 | (isearch-search) | |
776 | (isearch-update) | |
777 | (if isearch-nonincremental | |
778 | (progn | |
779 | ;; (sit-for 1) ;; needed if isearch-done does: (message "") | |
780 | (isearch-done)))) | |
8e1cae6d | 781 | |
08200510 RS |
782 | (quit ; handle abort-recursive-edit |
783 | (isearch-abort) ;; outside of let to restore outside global values | |
784 | ))) | |
785 | ||
786 | (defun isearch-nonincremental-exit-minibuffer () | |
787 | (interactive) | |
788 | (setq isearch-nonincremental t) | |
789 | (exit-minibuffer)) | |
790 | ||
791 | (defun isearch-forward-exit-minibuffer () | |
792 | (interactive) | |
793 | (setq isearch-new-forward t) | |
794 | (exit-minibuffer)) | |
8e1cae6d | 795 | |
08200510 | 796 | (defun isearch-reverse-exit-minibuffer () |
8e1cae6d | 797 | (interactive) |
08200510 RS |
798 | (setq isearch-new-forward nil) |
799 | (exit-minibuffer)) | |
800 | ||
801 | ||
802 | (defun isearch-abort () | |
803 | "Abort incremental search mode if searching is successful, signalling quit. | |
804 | Otherwise, revert to previous successful search and continue searching. | |
805 | Use `isearch-exit' to quit without signalling." | |
806 | (interactive) | |
807 | ;; (ding) signal instead below, if quiting | |
8e1cae6d JB |
808 | (discard-input) |
809 | (if isearch-success | |
810 | ;; If search is successful, move back to starting point | |
811 | ;; and really do quit. | |
812 | (progn (goto-char isearch-opoint) | |
35a4d143 | 813 | (isearch-done t) ; exit isearch |
a5dcf63f | 814 | (signal 'quit nil)) ; and pass on quit signal |
08200510 | 815 | ;; If search is failing, rub out until it is once more successful. |
8e1cae6d JB |
816 | (while (not isearch-success) (isearch-pop-state)) |
817 | (isearch-update))) | |
818 | ||
819 | ||
820 | (defun isearch-repeat (direction) | |
821 | ;; Utility for isearch-repeat-forward and -backward. | |
822 | (if (eq isearch-forward (eq direction 'forward)) | |
823 | ;; C-s in forward or C-r in reverse. | |
824 | (if (equal isearch-string "") | |
825 | ;; If search string is empty, use last one. | |
826 | (setq isearch-string | |
8e1cae6d | 827 | (or (if isearch-regexp |
a5dcf63f RS |
828 | (car regexp-search-ring) |
829 | (car search-ring)) | |
8e1cae6d JB |
830 | "") |
831 | isearch-message | |
08200510 | 832 | (mapconcat 'isearch-text-char-description |
8e1cae6d JB |
833 | isearch-string "")) |
834 | ;; If already have what to search for, repeat it. | |
835 | (or isearch-success | |
836 | (progn | |
837 | ||
838 | (goto-char (if isearch-forward (point-min) (point-max))) | |
839 | (setq isearch-wrapped t)))) | |
840 | ;; C-s in reverse or C-r in forward, change direction. | |
841 | (setq isearch-forward (not isearch-forward))) | |
842 | ||
843 | (setq isearch-barrier (point)) ; For subsequent \| if regexp. | |
844 | (setq isearch-success t) | |
845 | (or (equal isearch-string "") | |
846 | (progn | |
847 | ;; If repeating a search that found | |
848 | ;; an empty string, ensure we advance. | |
849 | (if (equal (match-end 0) (match-beginning 0)) | |
850 | (forward-char (if isearch-forward 1 -1))) | |
851 | (isearch-search))) | |
852 | (isearch-push-state) | |
853 | (isearch-update)) | |
854 | ||
855 | (defun isearch-repeat-forward () | |
856 | "Repeat incremental search forwards." | |
857 | (interactive) | |
858 | (isearch-repeat 'forward)) | |
859 | ||
860 | (defun isearch-repeat-backward () | |
861 | "Repeat incremental search backwards." | |
862 | (interactive) | |
863 | (isearch-repeat 'backward)) | |
864 | ||
865 | (defun isearch-toggle-regexp () | |
866 | "Toggle regexp searching on or off." | |
867 | ;; The status stack is left unchanged. | |
868 | (interactive) | |
869 | (setq isearch-regexp (not isearch-regexp)) | |
08200510 | 870 | (if isearch-regexp (setq isearch-word nil)) |
8e1cae6d JB |
871 | (isearch-update)) |
872 | ||
873 | (defun isearch-delete-char () | |
874 | "Discard last input item and move point back. | |
875 | If no previous match was done, just beep." | |
876 | (interactive) | |
877 | (if (null (cdr isearch-cmds)) | |
878 | (ding) | |
879 | (isearch-pop-state)) | |
880 | (isearch-update)) | |
881 | ||
882 | ||
883 | (defun isearch-yank (chunk) | |
884 | ;; Helper for isearch-yank-word and isearch-yank-line | |
08200510 RS |
885 | (let ((string (save-excursion |
886 | (and (not isearch-forward) isearch-other-end | |
887 | (goto-char isearch-other-end)) | |
888 | (buffer-substring | |
889 | (point) | |
890 | (save-excursion | |
891 | (cond | |
892 | ((eq chunk 'word) | |
893 | (forward-word 1)) | |
894 | ((eq chunk 'line) | |
895 | (end-of-line))) | |
896 | (point)))))) | |
897 | ;; Downcase the string if not supposed to case-fold yanked strings. | |
898 | (if (and isearch-case-fold-search | |
fdf5afa4 | 899 | (eq 'not-yanks search-upper-case)) |
08200510 RS |
900 | (setq string (downcase string))) |
901 | (if isearch-regexp (setq string (regexp-quote string))) | |
902 | (setq isearch-string (concat isearch-string string) | |
8e1cae6d JB |
903 | isearch-message |
904 | (concat isearch-message | |
08200510 RS |
905 | (mapconcat 'isearch-text-char-description |
906 | string "")) | |
8e1cae6d JB |
907 | ;; Don't move cursor in reverse search. |
908 | isearch-yank-flag t)) | |
909 | (isearch-search-and-update)) | |
910 | ||
911 | ||
912 | (defun isearch-yank-word () | |
913 | "Pull next word from buffer into search string." | |
914 | (interactive) | |
915 | (isearch-yank 'word)) | |
916 | ||
917 | (defun isearch-yank-line () | |
918 | "Pull rest of line from buffer into search string." | |
919 | (interactive) | |
920 | (isearch-yank 'line)) | |
921 | ||
922 | ||
923 | (defun isearch-search-and-update () | |
924 | ;; Do the search and update the display. | |
925 | (if (and (not isearch-success) | |
926 | ;; unsuccessful regexp search may become | |
927 | ;; successful by addition of characters which | |
928 | ;; make isearch-string valid | |
929 | (not isearch-regexp)) | |
930 | nil | |
931 | ;; In reverse search, adding stuff at | |
932 | ;; the end may cause zero or many more chars to be | |
933 | ;; matched, in the string following point. | |
934 | ;; Allow all those possibilities without moving point as | |
935 | ;; long as the match does not extend past search origin. | |
936 | (if (and (not isearch-forward) (not isearch-adjusted) | |
937 | (condition-case () | |
938 | (looking-at (if isearch-regexp isearch-string | |
939 | (regexp-quote isearch-string))) | |
940 | (error nil)) | |
941 | (or isearch-yank-flag | |
942 | (<= (match-end 0) | |
943 | (min isearch-opoint isearch-barrier)))) | |
944 | (setq isearch-success t | |
945 | isearch-invalid-regexp nil | |
946 | isearch-other-end (match-end 0)) | |
947 | ;; Not regexp, not reverse, or no match at point. | |
948 | (if (and isearch-other-end (not isearch-adjusted)) | |
949 | (goto-char (if isearch-forward isearch-other-end | |
950 | (min isearch-opoint | |
951 | isearch-barrier | |
952 | (1+ isearch-other-end))))) | |
953 | (isearch-search) | |
954 | )) | |
955 | (isearch-push-state) | |
956 | (if isearch-op-fun (funcall isearch-op-fun)) | |
957 | (isearch-update)) | |
958 | ||
959 | ||
960 | ;; *, ?, and | chars can make a regexp more liberal. | |
08200510 | 961 | ;; They can make a regexp match sooner or make it succeed instead of failing. |
8e1cae6d JB |
962 | ;; So go back to place last successful search started |
963 | ;; or to the last ^S/^R (barrier), whichever is nearer. | |
08200510 | 964 | ;; + needs no special handling because the string must match at least once. |
8e1cae6d JB |
965 | |
966 | (defun isearch-*-char () | |
967 | "Handle * and ? specially in regexps." | |
968 | (interactive) | |
969 | (if isearch-regexp | |
970 | ||
971 | (progn | |
972 | (setq isearch-adjusted t) | |
973 | (let ((cs (nth (if isearch-forward | |
974 | 5 ; isearch-other-end | |
975 | 2) ; saved (point) | |
976 | (car (cdr isearch-cmds))))) | |
977 | ;; (car isearch-cmds) is after last search; | |
978 | ;; (car (cdr isearch-cmds)) is from before it. | |
979 | (setq cs (or cs isearch-barrier)) | |
980 | (goto-char | |
981 | (if isearch-forward | |
982 | (max cs isearch-barrier) | |
983 | (min cs isearch-barrier)))))) | |
08200510 | 984 | (isearch-process-search-char (isearch-last-command-char))) |
8e1cae6d JB |
985 | |
986 | ||
8e1cae6d JB |
987 | (defun isearch-|-char () |
988 | "If in regexp search, jump to the barrier." | |
989 | (interactive) | |
990 | (if isearch-regexp | |
991 | (progn | |
992 | (setq isearch-adjusted t) | |
993 | (goto-char isearch-barrier))) | |
08200510 | 994 | (isearch-process-search-char (isearch-last-command-char))) |
8e1cae6d JB |
995 | |
996 | ||
b457cc3b | 997 | (defalias 'isearch-other-control-char 'isearch-other-meta-char) |
8e1cae6d JB |
998 | |
999 | (defun isearch-other-meta-char () | |
8f90f594 | 1000 | "Exit the search normally and reread this key sequence. |
35a4d143 RS |
1001 | But only if `search-exit-option' is non-nil, the default. |
1002 | If it is the symbol `edit', the search string is edited in the minibuffer | |
1003 | and the meta character is unread so that it applies to editing the string." | |
8e1cae6d | 1004 | (interactive) |
35a4d143 RS |
1005 | (cond ((eq search-exit-option 'edit) |
1006 | (let ((key (this-command-keys))) | |
e7cb0106 | 1007 | (apply 'isearch-unread (listify-key-sequence key))) |
35a4d143 RS |
1008 | (isearch-edit-string)) |
1009 | (search-exit-option | |
70418205 RS |
1010 | (let ((key (this-command-keys)) |
1011 | window) | |
1012 | (apply 'isearch-unread (listify-key-sequence key)) | |
1013 | ;; If we got a mouse click, maybe it was read with the buffer | |
1014 | ;; it was clicked on. If so, that buffer, not the current one, | |
1015 | ;; is in isearch mode. So end the search in that buffer. | |
1016 | (if (and (listp (aref key 0)) | |
1017 | (setq window (posn-window (event-start (aref key 0)))) | |
1018 | (windowp window)) | |
1019 | (save-excursion | |
1020 | (set-buffer (window-buffer window)) | |
1021 | (isearch-done)) | |
1022 | (isearch-done)))) | |
35a4d143 RS |
1023 | (t;; otherwise nil |
1024 | (isearch-process-search-string (this-command-keys) | |
1025 | (this-command-keys))))) | |
8e1cae6d JB |
1026 | |
1027 | ||
1028 | (defun isearch-quote-char () | |
1029 | "Quote special characters for incremental search." | |
1030 | (interactive) | |
1031 | (isearch-process-search-char (read-quoted-char (isearch-message t)))) | |
1032 | ||
8e1cae6d JB |
1033 | (defun isearch-return-char () |
1034 | "Convert return into newline for incremental search. | |
1035 | Obsolete." | |
1036 | (interactive) | |
1037 | (isearch-process-search-char ?\n)) | |
1038 | ||
8e1cae6d | 1039 | (defun isearch-printing-char () |
b68c164d | 1040 | "Add this ordinary printing character to the search string and search." |
8e1cae6d | 1041 | (interactive) |
08200510 | 1042 | (isearch-process-search-char (isearch-last-command-char))) |
8e1cae6d JB |
1043 | |
1044 | (defun isearch-whitespace-chars () | |
08200510 | 1045 | "Match all whitespace chars, if in regexp mode. |
b68c164d | 1046 | If you want to search for just a space, type C-q SPC." |
8e1cae6d | 1047 | (interactive) |
08200510 RS |
1048 | (if isearch-regexp |
1049 | (if search-whitespace-regexp | |
1050 | (isearch-process-search-string search-whitespace-regexp " ") | |
1051 | (isearch-printing-char)) | |
1052 | (progn | |
1053 | ;; This way of doing word search doesnt correctly extend current search. | |
1054 | ;; (setq isearch-word t) | |
1055 | ;; (setq isearch-adjusted t) | |
1056 | ;; (goto-char isearch-barrier) | |
1057 | (isearch-printing-char)))) | |
8e1cae6d JB |
1058 | |
1059 | (defun isearch-process-search-char (char) | |
1060 | ;; Append the char to the search string, update the message and re-search. | |
08200510 RS |
1061 | (isearch-process-search-string |
1062 | (isearch-char-to-string char) | |
1063 | (isearch-text-char-description char))) | |
8e1cae6d JB |
1064 | |
1065 | (defun isearch-process-search-string (string message) | |
1066 | (setq isearch-string (concat isearch-string string) | |
1067 | isearch-message (concat isearch-message message)) | |
1068 | (isearch-search-and-update)) | |
1069 | ||
1070 | \f | |
1071 | ;;=========================================================== | |
1072 | ;; Search Ring | |
1073 | ||
08200510 RS |
1074 | (defun isearch-ring-adjust1 (advance) |
1075 | ;; Helper for isearch-ring-adjust | |
1076 | (let* ((ring (if isearch-regexp regexp-search-ring search-ring)) | |
8e1cae6d JB |
1077 | (length (length ring)) |
1078 | (yank-pointer-name (if isearch-regexp | |
08200510 | 1079 | 'regexp-search-ring-yank-pointer |
8e1cae6d JB |
1080 | 'search-ring-yank-pointer)) |
1081 | (yank-pointer (eval yank-pointer-name))) | |
1082 | (if (zerop length) | |
1083 | () | |
1084 | (set yank-pointer-name | |
1085 | (setq yank-pointer | |
a5dcf63f RS |
1086 | (% (+ (or yank-pointer 0) |
1087 | (if advance (1- length) 1)) | |
1088 | length))) | |
1089 | (setq isearch-string (nth yank-pointer ring) | |
08200510 RS |
1090 | isearch-message (mapconcat 'isearch-text-char-description |
1091 | isearch-string ""))))) | |
1092 | ||
1093 | (defun isearch-ring-adjust (advance) | |
1094 | ;; Helper for isearch-ring-advance and isearch-ring-retreat | |
1095 | (if (cdr isearch-cmds) ;; is there more than one thing on stack? | |
1096 | (isearch-pop-state)) | |
1097 | (isearch-ring-adjust1 advance) | |
8e1cae6d | 1098 | (isearch-push-state) |
08200510 RS |
1099 | (if search-ring-update |
1100 | (progn | |
1101 | (isearch-search) | |
1102 | (isearch-update)) | |
1103 | (isearch-edit-string) | |
1104 | )) | |
8e1cae6d JB |
1105 | |
1106 | (defun isearch-ring-advance () | |
1107 | "Advance to the next search string in the ring." | |
08200510 | 1108 | ;; This could be more general to handle a prefix arg, but who would use it. |
8e1cae6d JB |
1109 | (interactive) |
1110 | (isearch-ring-adjust 'advance)) | |
1111 | ||
1112 | (defun isearch-ring-retreat () | |
1113 | "Retreat to the previous search string in the ring." | |
1114 | (interactive) | |
1115 | (isearch-ring-adjust nil)) | |
1116 | ||
a5dcf63f RS |
1117 | (defun isearch-ring-advance-edit (n) |
1118 | "Insert the next element of the search history into the minibuffer." | |
1119 | (interactive "p") | |
1120 | (let* ((yank-pointer-name (if isearch-regexp | |
1121 | 'regexp-search-ring-yank-pointer | |
1122 | 'search-ring-yank-pointer)) | |
1123 | (yank-pointer (eval yank-pointer-name)) | |
1124 | (ring (if isearch-regexp regexp-search-ring search-ring)) | |
1125 | (length (length ring))) | |
1126 | (if (zerop length) | |
1127 | () | |
1128 | (set yank-pointer-name | |
1129 | (setq yank-pointer | |
1130 | (% (+ (or yank-pointer 0) | |
35a4d143 RS |
1131 | ;; Add LENGTH here to ensure a positive result. |
1132 | length | |
1133 | (% (- n) length)) | |
a5dcf63f RS |
1134 | length))) |
1135 | ||
a5dcf63f | 1136 | (erase-buffer) |
35a4d143 | 1137 | (insert (nth yank-pointer ring)) |
49c13105 | 1138 | (goto-char (point-max))))) |
a5dcf63f RS |
1139 | |
1140 | (defun isearch-ring-retreat-edit (n) | |
1141 | "Inserts the previous element of the search history into the minibuffer." | |
1142 | (interactive "p") | |
1143 | (isearch-ring-advance-edit (- n))) | |
1144 | ||
1145 | ;;(defun isearch-ring-adjust-edit (advance) | |
1146 | ;; "Use the next or previous search string in the ring while in minibuffer." | |
1147 | ;; (isearch-ring-adjust1 advance) | |
1148 | ;; (erase-buffer) | |
1149 | ;; (insert isearch-string)) | |
1150 | ||
1151 | ;;(defun isearch-ring-advance-edit () | |
1152 | ;; (interactive) | |
1153 | ;; (isearch-ring-adjust-edit 'advance)) | |
1154 | ||
1155 | ;;(defun isearch-ring-retreat-edit () | |
1156 | ;; "Retreat to the previous search string in the ring while in the minibuffer." | |
1157 | ;; (interactive) | |
1158 | ;; (isearch-ring-adjust-edit nil)) | |
08200510 RS |
1159 | |
1160 | ||
1161 | (defun isearch-complete1 () | |
1162 | ;; Helper for isearch-complete and isearch-complete-edit | |
1163 | ;; Return t if completion OK, nil if no completion exists. | |
1164 | (let* ((ring (if isearch-regexp regexp-search-ring search-ring)) | |
1165 | (alist (mapcar (function (lambda (string) (list string))) ring)) | |
1166 | (completion-ignore-case case-fold-search) | |
1167 | (completion (try-completion isearch-string alist))) | |
1168 | (cond | |
1169 | ((eq completion t) | |
1170 | ;; isearch-string stays the same | |
1171 | t) | |
1172 | ((or completion ; not nil, must be a string | |
1173 | (= 0 (length isearch-string))) ; shouldnt have to say this | |
1174 | (if (equal completion isearch-string) ;; no extension? | |
1175 | (if completion-auto-help | |
1176 | (with-output-to-temp-buffer "*Isearch completions*" | |
1177 | (display-completion-list | |
1178 | (all-completions isearch-string alist)))) | |
1179 | (setq isearch-string completion)) | |
1180 | t) | |
1181 | (t | |
1182 | (message "No completion") ; waits a second if in minibuffer | |
1183 | nil)))) | |
1184 | ||
1185 | (defun isearch-complete () | |
1186 | "Complete the search string from the strings on the search ring. | |
1187 | The completed string is then editable in the minibuffer. | |
1188 | If there is no completion possible, say so and continue searching." | |
1189 | (interactive) | |
1190 | (if (isearch-complete1) | |
1191 | (isearch-edit-string) | |
1192 | ;; else | |
1193 | (sit-for 1) | |
1194 | (isearch-update))) | |
8e1cae6d | 1195 | |
08200510 RS |
1196 | (defun isearch-complete-edit () |
1197 | "Same as `isearch-complete' except in the minibuffer." | |
1198 | (interactive) | |
1199 | (setq isearch-string (buffer-string)) | |
1200 | (if (isearch-complete1) | |
8e1cae6d | 1201 | (progn |
08200510 RS |
1202 | (erase-buffer) |
1203 | (insert isearch-string)))) | |
8e1cae6d JB |
1204 | |
1205 | \f | |
1206 | ;;;============================================================== | |
1207 | ;; The search status stack (and isearch window-local variables, not used). | |
08200510 | 1208 | ;; Need a structure for this. |
8e1cae6d JB |
1209 | |
1210 | (defun isearch-top-state () | |
8e1cae6d JB |
1211 | (let ((cmd (car isearch-cmds))) |
1212 | (setq isearch-string (car cmd) | |
1213 | isearch-message (car (cdr cmd)) | |
1214 | isearch-success (nth 3 cmd) | |
1215 | isearch-forward (nth 4 cmd) | |
1216 | isearch-other-end (nth 5 cmd) | |
08200510 RS |
1217 | isearch-word (nth 6 cmd) |
1218 | isearch-invalid-regexp (nth 7 cmd) | |
1219 | isearch-wrapped (nth 8 cmd) | |
1220 | isearch-barrier (nth 9 cmd)) | |
8e1cae6d JB |
1221 | (goto-char (car (cdr (cdr cmd)))))) |
1222 | ||
1223 | (defun isearch-pop-state () | |
8e1cae6d JB |
1224 | (setq isearch-cmds (cdr isearch-cmds)) |
1225 | (isearch-top-state) | |
1226 | ) | |
1227 | ||
1228 | (defun isearch-push-state () | |
1229 | (setq isearch-cmds | |
1230 | (cons (list isearch-string isearch-message (point) | |
1231 | isearch-success isearch-forward isearch-other-end | |
08200510 | 1232 | isearch-word |
8e1cae6d JB |
1233 | isearch-invalid-regexp isearch-wrapped isearch-barrier) |
1234 | isearch-cmds))) | |
1235 | ||
8e1cae6d JB |
1236 | \f |
1237 | ;;;================================================================== | |
1238 | ;; Message string | |
1239 | ||
1240 | (defun isearch-message (&optional c-q-hack ellipsis) | |
1241 | ;; Generate and print the message string. | |
1242 | (let ((cursor-in-echo-area ellipsis) | |
1243 | (m (concat | |
08200510 | 1244 | (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental) |
8e1cae6d JB |
1245 | isearch-message |
1246 | (isearch-message-suffix c-q-hack ellipsis) | |
1247 | ))) | |
1248 | (if c-q-hack m (message "%s" m)))) | |
1249 | ||
08200510 | 1250 | (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental) |
8e1cae6d JB |
1251 | ;; If about to search, and previous search regexp was invalid, |
1252 | ;; check that it still is. If it is valid now, | |
1253 | ;; let the message we display while searching say that it is valid. | |
1254 | (and isearch-invalid-regexp ellipsis | |
1255 | (condition-case () | |
1256 | (progn (re-search-forward isearch-string (point) t) | |
1257 | (setq isearch-invalid-regexp nil)) | |
1258 | (error nil))) | |
1259 | ;; If currently failing, display no ellipsis. | |
1260 | (or isearch-success (setq ellipsis nil)) | |
1261 | (let ((m (concat (if isearch-success "" "failing ") | |
1262 | (if isearch-wrapped "wrapped ") | |
08200510 | 1263 | (if isearch-word "word " "") |
8e1cae6d | 1264 | (if isearch-regexp "regexp " "") |
08200510 | 1265 | (if nonincremental "search" "I-search") |
8e1cae6d JB |
1266 | (if isearch-forward ": " " backward: ") |
1267 | ))) | |
1268 | (aset m 0 (upcase (aref m 0))) | |
1269 | m)) | |
1270 | ||
1271 | ||
1272 | (defun isearch-message-suffix (&optional c-q-hack ellipsis) | |
1273 | (concat (if c-q-hack "^Q" "") | |
1274 | (if isearch-invalid-regexp | |
1275 | (concat " [" isearch-invalid-regexp "]") | |
1276 | ""))) | |
1277 | ||
1278 | \f | |
1279 | ;;;======================================================== | |
1280 | ;;; Searching | |
1281 | ||
1282 | (defun isearch-search () | |
1283 | ;; Do the search with the current search string. | |
1284 | (isearch-message nil t) | |
43873131 | 1285 | (if (and isearch-case-fold-search search-upper-case) |
b78559b0 RS |
1286 | (setq isearch-case-fold-search |
1287 | (isearch-no-upper-case-p isearch-string isearch-regexp))) | |
8e1cae6d JB |
1288 | (condition-case lossage |
1289 | (let ((inhibit-quit nil) | |
1290 | (case-fold-search isearch-case-fold-search)) | |
1291 | (if isearch-regexp (setq isearch-invalid-regexp nil)) | |
1292 | (setq isearch-success | |
1293 | (funcall | |
08200510 RS |
1294 | (cond (isearch-word |
1295 | (if isearch-forward | |
1296 | 'word-search-forward 'word-search-backward)) | |
1297 | (isearch-regexp | |
1298 | (if isearch-forward | |
1299 | 're-search-forward 're-search-backward)) | |
1300 | (t | |
1301 | (if isearch-forward 'search-forward 'search-backward))) | |
8e1cae6d JB |
1302 | isearch-string nil t)) |
1303 | (if isearch-success | |
1304 | (setq isearch-other-end | |
1305 | (if isearch-forward (match-beginning 0) (match-end 0))))) | |
1306 | ||
08200510 | 1307 | (quit (isearch-unread ?\C-g) |
8e1cae6d JB |
1308 | (setq isearch-success nil)) |
1309 | ||
1310 | (invalid-regexp | |
1311 | (setq isearch-invalid-regexp (car (cdr lossage))) | |
1312 | (if (string-match | |
1313 | "\\`Premature \\|\\`Unmatched \\|\\`Invalid " | |
1314 | isearch-invalid-regexp) | |
f1c03125 RS |
1315 | (setq isearch-invalid-regexp "incomplete input"))) |
1316 | (error | |
1317 | ;; stack overflow in regexp search. | |
1318 | (setq isearch-invalid-regexp (car (cdr lossage))))) | |
8e1cae6d JB |
1319 | |
1320 | (if isearch-success | |
1321 | nil | |
1322 | ;; Ding if failed this time after succeeding last time. | |
1323 | (and (nth 3 (car isearch-cmds)) | |
1324 | (ding)) | |
1325 | (goto-char (nth 2 (car isearch-cmds))))) | |
1326 | ||
08200510 RS |
1327 | |
1328 | \f | |
1329 | ;;;======================================================== | |
1330 | ;;; Highlighting | |
1331 | ||
b78559b0 | 1332 | (defvar isearch-overlay nil) |
08200510 | 1333 | |
b78559b0 RS |
1334 | (defun isearch-highlight (beg end) |
1335 | (if (or (null search-highlight) (not (internal-find-face 'isearch nil))) | |
08200510 | 1336 | nil |
b78559b0 RS |
1337 | (or isearch-overlay (setq isearch-overlay (make-overlay beg end))) |
1338 | (move-overlay isearch-overlay beg end (current-buffer)) | |
1339 | (overlay-put isearch-overlay 'face 'isearch))) | |
1340 | ||
1341 | (defun isearch-dehighlight (totally) | |
1342 | (if isearch-overlay | |
1343 | (delete-overlay isearch-overlay))) | |
08200510 RS |
1344 | |
1345 | ;;;=========================================================== | |
1346 | ;;; General utilities | |
1347 | ||
08200510 | 1348 | |
b78559b0 RS |
1349 | (defun isearch-no-upper-case-p (string regexp-flag) |
1350 | "Return t if there are no upper case chars in STRING. | |
1351 | If REGEXP-FLAG is non-nil, disregard letters preceeded by `\\' (but not `\\\\') | |
1352 | since they have special meaning in a regexp." | |
08200510 | 1353 | (let ((case-fold-search nil)) |
b78559b0 RS |
1354 | (not (string-match (if regexp-flag "\\(^\\|\\\\\\\\\\|[^\\]\\)[A-Z]" |
1355 | "[A-Z]") | |
1356 | string)))) | |
08200510 RS |
1357 | |
1358 | ||
8e1cae6d | 1359 | ;;;================================================= |
b78559b0 | 1360 | ;; Portability functions to support various Emacs versions. |
8e1cae6d | 1361 | |
08200510 RS |
1362 | ;; To quiet the byte-compiler. |
1363 | (defvar unread-command-event) | |
dbc4e1c1 | 1364 | (defvar unread-command-events) |
08200510 | 1365 | (defvar last-command-event) |
8e1cae6d | 1366 | |
08200510 RS |
1367 | (defun isearch-char-to-string (c) |
1368 | (if (integerp c) | |
1369 | (make-string 1 c) | |
8d7e4e80 RS |
1370 | (if (and (symbolp c) (get c 'ascii-character)) |
1371 | (make-string 1 (get c 'ascii-character)) | |
1372 | (make-string 1 (event-to-character c))))) | |
08200510 RS |
1373 | |
1374 | (defun isearch-text-char-description (c) | |
1375 | (isearch-char-to-string c)) | |
1376 | ||
8f90f594 | 1377 | (defun isearch-unread (&rest char-or-events) |
8f90f594 RS |
1378 | ;; General function to unread characters or events. |
1379 | (if isearch-gnu-emacs-events | |
e7cb0106 RS |
1380 | (setq unread-command-events |
1381 | (append char-or-events unread-command-events)) | |
8f90f594 | 1382 | (let ((char (if (cdr char-or-events) |
75e86b2e RS |
1383 | (progn |
1384 | (while (cdr char-or-events) | |
1385 | (setq char-or-events (cdr char-or-events))) | |
1386 | (+ 128 (car char-or-events))) | |
8f90f594 RS |
1387 | (car char-or-events)))) |
1388 | (if isearch-event-data-type | |
1389 | (setq unread-command-event char) | |
1390 | (setq unread-command-char char))))) | |
08200510 RS |
1391 | |
1392 | (defun isearch-last-command-char () | |
1393 | ;; General function to return the last command character. | |
a5dcf63f | 1394 | (if isearch-event-data-type |
08200510 RS |
1395 | last-command-event |
1396 | last-command-char)) | |
1397 | ||
3b1e4dd1 | 1398 | ;;; isearch.el ends here |