Commit | Line | Data |
---|---|---|
76550a57 | 1 | ;;; isearch.el --- incremental search minor mode. |
3b1e4dd1 | 2 | |
9c1db929 | 3 | ;; Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc. |
3a801d0c | 4 | |
3b1e4dd1 | 5 | ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu> |
0f09b616 | 6 | ;; Maintainer: FSF |
8e1cae6d | 7 | |
54d2ecd3 | 8 | ;; This file is part of GNU Emacs. |
8e1cae6d | 9 | |
59243403 RS |
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 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
8e1cae6d | 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
59243403 RS |
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 | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
8e1cae6d | 24 | |
3b1e4dd1 ER |
25 | ;;; Commentary: |
26 | ||
8e1cae6d JB |
27 | ;; Instructions |
28 | ||
08200510 RS |
29 | ;; For programmed use of isearch-mode, e.g. calling (isearch-forward), |
30 | ;; isearch-mode behaves modally and does not return until the search | |
31 | ;; is completed. It uses a recursive-edit to behave this way. Note: | |
32 | ;; gnus does it wrong: (call-interactively 'isearch-forward). | |
33 | ||
8e1cae6d | 34 | ;; The key bindings active within isearch-mode are defined below in |
08200510 RS |
35 | ;; `isearch-mode-map' which is given bindings close to the default |
36 | ;; characters of the original isearch.el. With `isearch-mode', | |
37 | ;; however, you can bind multi-character keys and it should be easier | |
38 | ;; to add new commands. One bug though: keys with meta-prefix cannot | |
39 | ;; be longer than two chars. Also see minibuffer-local-isearch-map | |
40 | ;; for bindings active during `isearch-edit-string'. | |
41 | ||
42 | ;; Note to emacs version 19 users: isearch-mode should work even if | |
43 | ;; you switch windows with the mouse, in which case isearch-mode is | |
44 | ;; terminated automatically before the switch. This is true of lemacs | |
45 | ;; too, with a few more cleanups I've neglected in this release. | |
46 | ;; No one has supplied patches for epoch yet. | |
47 | ||
48 | ;; The search ring and completion commands automatically put you in | |
49 | ;; the minibuffer to edit the string. This gives you a chance to | |
50 | ;; modify the search string before executing the search. There are | |
51 | ;; three commands to terminate the editing: C-s and C-r exit the | |
52 | ;; minibuffer and search forward and reverse respectively, while C-m | |
53 | ;; exits and does a nonincremental search. | |
54 | ||
55 | ;; Exiting immediately from isearch uses isearch-edit-string instead | |
56 | ;; of nonincremental-search, if search-nonincremental-instead is non-nil. | |
57 | ;; The name of this option should probably be changed if we decide to | |
58 | ;; keep the behavior. No point in forcing nonincremental search until | |
59 | ;; the last possible moment. | |
60 | ||
61 | ;; TODO | |
eb8c3be9 | 62 | ;; - Integrate the emacs 19 generalized command history. |
08200510 RS |
63 | ;; - Think about incorporating query-replace. |
64 | ;; - Hooks and options for failed search. | |
282d89c0 | 65 | |
3b1e4dd1 ER |
66 | ;;; Change Log: |
67 | ||
b578f267 EN |
68 | ;; Changes before those recorded in ChangeLog: |
69 | ||
70 | ;; Revision 1.4 92/09/14 16:26:02 liberte | |
71 | ;; Added prefix args to isearch-forward, etc. to switch between | |
72 | ;; string and regular expression searching. | |
73 | ;; Added some support for lemacs. | |
74 | ;; Added general isearch-highlight option - but only for lemacs so far. | |
75 | ;; Added support for frame switching in emacs 19. | |
76 | ;; Added word search option to isearch-edit-string. | |
77 | ;; Renamed isearch-quit to isearch-abort. | |
78 | ;; Numerous changes to comments and doc strings. | |
79 | ;; | |
80 | ;; Revision 1.3 92/06/29 13:10:08 liberte | |
81 | ;; Moved modal isearch-mode handling into isearch-mode. | |
82 | ;; Got rid of buffer-local isearch variables. | |
83 | ;; isearch-edit-string used by ring adjustments, completion, and | |
84 | ;; nonincremental searching. C-s and C-r are additional exit commands. | |
85 | ;; Renamed all regex to regexp. | |
86 | ;; Got rid of found-start and found-point globals. | |
87 | ;; Generalized handling of upper-case chars. | |
88 | ||
89 | ;; Revision 1.2 92/05/27 11:33:57 liberte | |
90 | ;; Emacs version 19 has a search ring, which is supported here. | |
91 | ;; Other fixes found in the version 19 isearch are included here. | |
92 | ;; | |
93 | ;; Also see variables search-caps-disable-folding, | |
94 | ;; search-nonincremental-instead, search-whitespace-regexp, and | |
95 | ;; commands isearch-toggle-regexp, isearch-edit-string. | |
96 | ;; | |
97 | ;; semi-modal isearching is supported. | |
98 | ||
99 | ;; Changes for 1.1 | |
100 | ;; 3/18/92 Fixed invalid-regexp. | |
101 | ;; 3/18/92 Fixed yanking in regexps. | |
282d89c0 | 102 | |
3b1e4dd1 | 103 | ;;; Code: |
08200510 RS |
104 | |
105 | \f | |
58451992 | 106 | ;;; Some additional options and constants. |
08200510 | 107 | |
9c1db929 RS |
108 | (defgroup isearch nil |
109 | "Incremental search minor mode." | |
110 | :prefix "isearch-" | |
111 | :prefix "search-" | |
112 | :group 'matching) | |
fdf5afa4 | 113 | |
9c1db929 RS |
114 | |
115 | (defcustom search-exit-option t | |
116 | "*Non-nil means random control characters terminate incremental search." | |
117 | :type 'boolean | |
118 | :group 'isearch) | |
119 | ||
120 | (defcustom search-slow-window-lines 1 | |
fdf5afa4 RS |
121 | "*Number of lines in slow search display windows. |
122 | These are the short windows used during incremental search on slow terminals. | |
123 | Negative means put the slow search window at the top (normally it's at bottom) | |
9c1db929 RS |
124 | and the value is minus the number of lines." |
125 | :type 'integer | |
126 | :group 'isearch) | |
fdf5afa4 | 127 | |
9c1db929 | 128 | (defcustom search-slow-speed 1200 |
fdf5afa4 RS |
129 | "*Highest terminal speed at which to use \"slow\" style incremental search. |
130 | This is the style where a one-line window is created to show the line | |
9c1db929 RS |
131 | that the search has reached." |
132 | :type 'integer | |
133 | :group 'isearch) | |
8e1cae6d | 134 | |
9c1db929 | 135 | (defcustom search-upper-case 'not-yanks |
8e1cae6d | 136 | "*If non-nil, upper case chars disable case fold searching. |
08200510 RS |
137 | That is, upper and lower case chars must match exactly. |
138 | This applies no matter where the chars come from, but does not | |
fdf5afa4 | 139 | apply to chars in regexps that are prefixed with `\\'. |
9c1db929 RS |
140 | If this value is `not-yanks', yanked text is always downcased." |
141 | :type '(choice (const :tag "off" nil) | |
142 | (const not-yanks) | |
143 | (sexp :tag "on" :format "%t\n" t)) | |
144 | :group 'isearch) | |
8e1cae6d | 145 | |
9c1db929 | 146 | (defcustom search-nonincremental-instead t |
8e1cae6d | 147 | "*If non-nil, do a nonincremental search instead if exiting immediately. |
08200510 | 148 | Actually, `isearch-edit-string' is called to let you enter the search |
9c1db929 RS |
149 | string, and RET terminates editing and does a nonincremental search." |
150 | :type 'boolean | |
151 | :group 'isearch) | |
8e1cae6d | 152 | |
9c1db929 | 153 | (defcustom search-whitespace-regexp "\\s-+" |
8e1cae6d | 154 | "*If non-nil, regular expression to match a sequence of whitespace chars. |
9c1db929 RS |
155 | You might want to use something like \"[ \\t\\r\\n]+\" instead." |
156 | :type 'regexp | |
157 | :group 'isearch) | |
8e1cae6d | 158 | |
30178dde | 159 | (defcustom search-highlight t |
9c1db929 RS |
160 | "*Non-nil means incremental search highlights the current match." |
161 | :type 'boolean | |
162 | :group 'isearch) | |
08200510 | 163 | |
0352b205 RS |
164 | (defcustom search-invisible 'open |
165 | "If t incremental search can match hidden text. | |
166 | nil means don't match invisible text. | |
167 | If the value is `open', if the text matched is made invisible by | |
168 | an overlay having an `invisible' property and that overlay has a property | |
169 | `isearch-open-invisible', then incremental search will show the contents. | |
170 | \(This applies when using `outline.el' and `hideshow.el'.)" | |
171 | :type '(choice (const :tag "Match hidden text" t) | |
172 | (const :tag "Open overlays" open) | |
79c7a4fa | 173 | (const :tag "Don't match hidden text" nil)) |
0352b205 RS |
174 | :group 'isearch) |
175 | ||
176 | (defcustom isearch-hide-immediately t | |
68c18d6d RS |
177 | "If non-nil, re-hide an invisible match right away. |
178 | This variable makes a difference when `search-invisible' is set to `open'. | |
179 | It means that after search makes some invisible text visible | |
180 | to show the match, it makes the text invisible again when the match moves. | |
181 | Ordinarily the text becomes invisible again at the end of the search." | |
0352b205 RS |
182 | :type 'boolean |
183 | :group 'isearch) | |
86bfaffe | 184 | |
08200510 RS |
185 | (defvar isearch-mode-hook nil |
186 | "Function(s) to call after starting up an incremental search.") | |
187 | ||
188 | (defvar isearch-mode-end-hook nil | |
189 | "Function(s) to call after terminating an incremental search.") | |
190 | ||
8e1cae6d | 191 | ;;; Search ring. |
8e1cae6d JB |
192 | |
193 | (defvar search-ring nil | |
194 | "List of search string sequences.") | |
08200510 | 195 | (defvar regexp-search-ring nil |
8e1cae6d JB |
196 | "List of regular expression search string sequences.") |
197 | ||
9c1db929 RS |
198 | (defcustom search-ring-max 16 |
199 | "*Maximum length of search ring before oldest elements are thrown away." | |
200 | :type 'integer | |
201 | :group 'isearch) | |
202 | (defcustom regexp-search-ring-max 16 | |
203 | "*Maximum length of regexp search ring before oldest elements are thrown away." | |
204 | :type 'integer | |
205 | :group 'isearch) | |
8e1cae6d JB |
206 | |
207 | (defvar search-ring-yank-pointer nil | |
a5dcf63f RS |
208 | "Index in `search-ring' of last string reused. |
209 | nil if none yet.") | |
08200510 | 210 | (defvar regexp-search-ring-yank-pointer nil |
a5dcf63f RS |
211 | "Index in `regexp-search-ring' of last string reused. |
212 | nil if none yet.") | |
8e1cae6d | 213 | |
9c1db929 | 214 | (defcustom search-ring-update nil |
08200510 | 215 | "*Non-nil if advancing or retreating in the search ring should cause search. |
9c1db929 RS |
216 | Default value, nil, means edit the string instead." |
217 | :type 'boolean | |
218 | :group 'isearch) | |
08200510 | 219 | |
8e1cae6d JB |
220 | ;;; Define isearch-mode keymap. |
221 | ||
222 | (defvar isearch-mode-map nil | |
223 | "Keymap for isearch-mode.") | |
224 | ||
08200510 RS |
225 | (or isearch-mode-map |
226 | (let* ((i 0) | |
fdf5afa4 | 227 | (map (make-keymap))) |
fcf8ba15 | 228 | (or (vectorp (nth 1 map)) |
280e8e04 | 229 | (char-table-p (nth 1 map)) |
fcf8ba15 | 230 | (error "The initialization of isearch-mode-map must be updated")) |
01a6ef79 | 231 | ;; Make Latin-1, Latin-2, Latin-3 and Latin-4 characters |
280e8e04 | 232 | ;; search for themselves. |
01a6ef79 RS |
233 | (aset (nth 1 map) (make-char 'latin-iso8859-1) 'isearch-printing-char) |
234 | (aset (nth 1 map) (make-char 'latin-iso8859-2) 'isearch-printing-char) | |
235 | (aset (nth 1 map) (make-char 'latin-iso8859-3) 'isearch-printing-char) | |
236 | (aset (nth 1 map) (make-char 'latin-iso8859-4) 'isearch-printing-char) | |
d64b1d96 | 237 | (aset (nth 1 map) (make-char 'latin-iso8859-9) 'isearch-printing-char) |
ac689515 RS |
238 | ;; Make function keys, etc, exit the search. |
239 | (define-key map [t] 'isearch-other-control-char) | |
08200510 | 240 | ;; Control chars, by default, end isearch mode transparently. |
ac689515 RS |
241 | ;; We need these explicit definitions because, in a dense keymap, |
242 | ;; the binding for t does not affect characters. | |
243 | ;; We use a dense keymap to save space. | |
08200510 RS |
244 | (while (< i ?\ ) |
245 | (define-key map (make-string 1 i) 'isearch-other-control-char) | |
246 | (setq i (1+ i))) | |
247 | ||
8cb2ceaa | 248 | ;; Printing chars extend the search string by default. |
ac689515 | 249 | (setq i ?\ ) |
8d7e4e80 | 250 | (while (< i (length (nth 1 map))) |
8cb2ceaa | 251 | (define-key map (vector i) 'isearch-printing-char) |
08200510 RS |
252 | (setq i (1+ i))) |
253 | ||
ec121bad RS |
254 | ;; To handle local bindings with meta char prefix keys, define |
255 | ;; another full keymap. This must be done for any other prefix | |
256 | ;; keys as well, one full keymap per char of the prefix key. It | |
257 | ;; would be simpler to disable the global keymap, and/or have a | |
258 | ;; default local key binding for any key not otherwise bound. | |
259 | (let ((meta-map (make-sparse-keymap))) | |
260 | (define-key map (char-to-string meta-prefix-char) meta-map) | |
261 | (define-key map [escape] meta-map)) | |
262 | (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char) | |
263 | ||
08200510 RS |
264 | ;; Several non-printing chars change the searching behavior. |
265 | (define-key map "\C-s" 'isearch-repeat-forward) | |
266 | (define-key map "\C-r" 'isearch-repeat-backward) | |
267 | (define-key map "\177" 'isearch-delete-char) | |
268 | (define-key map "\C-g" 'isearch-abort) | |
ec121bad RS |
269 | ;; This assumes \e is the meta-prefix-char. |
270 | (or (= ?\e meta-prefix-char) | |
271 | (error "Inconsistency in isearch.el")) | |
19993c54 RS |
272 | (define-key map "\e\e\e" 'isearch-cancel) |
273 | (define-key map [escape escape escape] 'isearch-cancel) | |
8e1cae6d | 274 | |
08200510 RS |
275 | (define-key map "\C-q" 'isearch-quote-char) |
276 | ||
08200510 RS |
277 | (define-key map "\r" 'isearch-exit) |
278 | (define-key map "\C-j" 'isearch-printing-char) | |
279 | (define-key map "\t" 'isearch-printing-char) | |
280 | (define-key map " " 'isearch-whitespace-chars) | |
8e1cae6d | 281 | |
08200510 RS |
282 | (define-key map "\C-w" 'isearch-yank-word) |
283 | (define-key map "\C-y" 'isearch-yank-line) | |
284 | ||
285 | ;; Define keys for regexp chars * ? |. | |
286 | ;; Nothing special for + because it matches at least once. | |
287 | (define-key map "*" 'isearch-*-char) | |
288 | (define-key map "?" 'isearch-*-char) | |
289 | (define-key map "|" 'isearch-|-char) | |
290 | ||
58f1634a RS |
291 | ;;; Turned off because I find I expect to get the global definition--rms. |
292 | ;;; ;; Instead bind C-h to special help command for isearch-mode. | |
293 | ;;; (define-key map "\C-h" 'isearch-mode-help) | |
08200510 | 294 | |
08200510 RS |
295 | (define-key map "\M-n" 'isearch-ring-advance) |
296 | (define-key map "\M-p" 'isearch-ring-retreat) | |
30d47262 | 297 | (define-key map "\M-y" 'isearch-yank-kill) |
1a3a6707 | 298 | |
08200510 RS |
299 | (define-key map "\M-\t" 'isearch-complete) |
300 | ||
5f48fc17 KH |
301 | ;; Pass frame events transparently so they won't exit the search. |
302 | ;; In particular, if we have more than one display open, then a | |
303 | ;; switch-frame might be generated by someone typing at another keyboard. | |
304 | (define-key map [switch-frame] nil) | |
305 | (define-key map [delete-frame] nil) | |
306 | (define-key map [iconify-frame] nil) | |
307 | (define-key map [make-frame-visible] nil) | |
f46d5091 KH |
308 | ;; For searching multilingual text. |
309 | (define-key map "\C-\\" 'isearch-toggle-input-method) | |
310 | (define-key map "\C-^" 'isearch-toggle-specified-input-method) | |
5f48fc17 | 311 | |
08200510 RS |
312 | (setq isearch-mode-map map) |
313 | )) | |
8e1cae6d | 314 | |
08200510 RS |
315 | ;; Some bindings you may want to put in your isearch-mode-hook. |
316 | ;; Suggest some alternates... | |
4453091d | 317 | ;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-case-fold) |
08200510 RS |
318 | ;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-regexp) |
319 | ;; (define-key isearch-mode-map "\C-^" 'isearch-edit-string) | |
8e1cae6d | 320 | |
8e1cae6d | 321 | |
08200510 RS |
322 | (defvar minibuffer-local-isearch-map nil |
323 | "Keymap for editing isearch strings in the minibuffer.") | |
8e1cae6d | 324 | |
08200510 RS |
325 | (or minibuffer-local-isearch-map |
326 | (let ((map (copy-keymap minibuffer-local-map))) | |
327 | (define-key map "\r" 'isearch-nonincremental-exit-minibuffer) | |
35a4d143 RS |
328 | (define-key map "\M-n" 'isearch-ring-advance-edit) |
329 | (define-key map "\M-p" 'isearch-ring-retreat-edit) | |
08200510 RS |
330 | (define-key map "\M-\t" 'isearch-complete-edit) |
331 | (define-key map "\C-s" 'isearch-forward-exit-minibuffer) | |
332 | (define-key map "\C-r" 'isearch-reverse-exit-minibuffer) | |
333 | (setq minibuffer-local-isearch-map map) | |
334 | )) | |
8e1cae6d | 335 | |
8e1cae6d | 336 | ;; Internal variables declared globally for byte-compiler. |
08200510 RS |
337 | ;; These are all set with setq while isearching |
338 | ;; and bound locally while editing the search string. | |
339 | ||
340 | (defvar isearch-forward nil) ; Searching in the forward direction. | |
341 | (defvar isearch-regexp nil) ; Searching for a regexp. | |
342 | (defvar isearch-word nil) ; Searching for words. | |
343 | ||
344 | (defvar isearch-cmds nil) ; Stack of search status sets. | |
345 | (defvar isearch-string "") ; The current search string. | |
346 | (defvar isearch-message "") ; text-char-description version of isearch-string | |
347 | ||
348 | (defvar isearch-success t) ; Searching is currently successful. | |
349 | (defvar isearch-invalid-regexp nil) ; Regexp not well formed. | |
c5f6b356 | 350 | (defvar isearch-within-brackets nil) ; Regexp has unclosed [. |
08200510 RS |
351 | (defvar isearch-other-end nil) ; Start (end) of match if forward (backward). |
352 | (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom). | |
8e1cae6d | 353 | (defvar isearch-barrier 0) |
99ae9e9f | 354 | (defvar isearch-just-started nil) |
8e1cae6d | 355 | |
4453091d RS |
356 | ; case-fold-search while searching. |
357 | ; either nil, t, or 'yes. 'yes means the same as t except that mixed | |
358 | ; case in the search string is ignored. | |
359 | (defvar isearch-case-fold-search nil) | |
8e1cae6d JB |
360 | |
361 | (defvar isearch-adjusted nil) | |
362 | (defvar isearch-slow-terminal-mode nil) | |
08200510 RS |
363 | ;;; If t, using a small window. |
364 | (defvar isearch-small-window nil) | |
8e1cae6d | 365 | (defvar isearch-opoint 0) |
08200510 RS |
366 | ;;; The window configuration active at the beginning of the search. |
367 | (defvar isearch-window-configuration nil) | |
8e1cae6d | 368 | |
08200510 RS |
369 | ;; Flag to indicate a yank occurred, so don't move the cursor. |
370 | (defvar isearch-yank-flag nil) | |
8e1cae6d | 371 | |
f46d5091 KH |
372 | ;; Flag to indicate that we are searching multibyte characaters. |
373 | (defvar isearch-multibyte-characters-flag nil) | |
374 | ||
08200510 RS |
375 | ;;; A function to be called after each input character is processed. |
376 | ;;; (It is not called after characters that exit the search.) | |
377 | ;;; It is only set from an optional argument to `isearch-mode'. | |
378 | (defvar isearch-op-fun nil) | |
8e1cae6d | 379 | |
08200510 RS |
380 | ;;; Is isearch-mode in a recursive edit for modal searching. |
381 | (defvar isearch-recursive-edit nil) | |
8e1cae6d | 382 | |
08200510 RS |
383 | ;;; Should isearch be terminated after doing one search? |
384 | (defvar isearch-nonincremental nil) | |
385 | ||
386 | ;; New value of isearch-forward after isearch-edit-string. | |
387 | (defvar isearch-new-forward nil) | |
8e1cae6d | 388 | |
0352b205 RS |
389 | ;; Accumulate here the overlays opened during searching. |
390 | (defvar isearch-opened-overlays nil) | |
8e1cae6d | 391 | |
8e1cae6d JB |
392 | ;; Minor-mode-alist changes - kind of redundant with the |
393 | ;; echo area, but if isearching in multiple windows, it can be useful. | |
394 | ||
395 | (or (assq 'isearch-mode minor-mode-alist) | |
396 | (nconc minor-mode-alist | |
397 | (list '(isearch-mode isearch-mode)))) | |
398 | ||
08200510 | 399 | (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil. |
8e1cae6d JB |
400 | (make-variable-buffer-local 'isearch-mode) |
401 | ||
fdf5afa4 RS |
402 | (define-key global-map "\C-s" 'isearch-forward) |
403 | (define-key esc-map "\C-s" 'isearch-forward-regexp) | |
404 | (define-key global-map "\C-r" 'isearch-backward) | |
405 | (define-key esc-map "\C-r" 'isearch-backward-regexp) | |
406 | ||
8e1cae6d | 407 | ;;; Entry points to isearch-mode. |
08200510 | 408 | ;;; These four functions should replace those in loaddefs.el |
b457cc3b | 409 | ;;; An alternative is to defalias isearch-forward etc to isearch-mode, |
08200510 | 410 | ;;; and look at this-command to set the options accordingly. |
8e1cae6d | 411 | |
eceee2c0 | 412 | (defun isearch-forward (&optional regexp-p no-recursive-edit) |
8e1cae6d JB |
413 | "\ |
414 | Do incremental search forward. | |
08200510 RS |
415 | With a prefix argument, do an incremental regular expression search instead. |
416 | \\<isearch-mode-map> | |
8e1cae6d | 417 | As you type characters, they add to the search string and are found. |
08200510 | 418 | The following non-printing keys are bound in `isearch-mode-map'. |
8e1cae6d | 419 | |
08200510 | 420 | Type \\[isearch-delete-char] to cancel characters from end of search string. |
8e1cae6d | 421 | Type \\[isearch-exit] to exit, leaving point at location found. |
08200510 RS |
422 | Type LFD (C-j) to match end of line. |
423 | Type \\[isearch-repeat-forward] to search again forward,\ | |
424 | \\[isearch-repeat-backward] to search again backward. | |
425 | Type \\[isearch-yank-word] to yank word from buffer onto end of search\ | |
426 | string and search for it. | |
427 | Type \\[isearch-yank-line] to yank rest of line onto end of search string\ | |
428 | and search for it. | |
de40ed89 | 429 | Type \\[isearch-yank-kill] to yank the last string of killed text. |
8e1cae6d | 430 | Type \\[isearch-quote-char] to quote control character to search for it. |
08200510 RS |
431 | \\[isearch-abort] while searching or when search has failed cancels input\ |
432 | back to what has | |
433 | been found successfully. | |
434 | \\[isearch-abort] when search is successful aborts and moves point to\ | |
435 | starting point. | |
8e1cae6d JB |
436 | |
437 | Also supported is a search ring of the previous 16 search strings. | |
438 | Type \\[isearch-ring-advance] to search for the next item in the search ring. | |
08200510 RS |
439 | Type \\[isearch-ring-retreat] to search for the previous item in the search\ |
440 | ring. | |
441 | Type \\[isearch-complete] to complete the search string using the search ring. | |
8e1cae6d | 442 | |
08200510 RS |
443 | The above keys, bound in `isearch-mode-map', are often controlled by |
444 | options; do M-x apropos on search-.* to find them. | |
8e1cae6d | 445 | Other control and meta characters terminate the search |
08200510 | 446 | and are then executed normally (depending on `search-exit-option'). |
fd49e05c | 447 | Likewise for function keys and mouse button events. |
8e1cae6d | 448 | |
08200510 RS |
449 | If this function is called non-interactively, it does not return to |
450 | the calling function until the search is done." | |
8e1cae6d | 451 | |
eceee2c0 | 452 | (interactive "P\np") |
4ae7d00a | 453 | (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit))) |
8e1cae6d | 454 | |
eceee2c0 | 455 | (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit) |
8e1cae6d JB |
456 | "\ |
457 | Do incremental search forward for regular expression. | |
08200510 | 458 | With a prefix argument, do a regular string search instead. |
8e1cae6d JB |
459 | Like ordinary incremental search except that your input |
460 | is treated as a regexp. See \\[isearch-forward] for more info." | |
eceee2c0 | 461 | (interactive "P\np") |
4ae7d00a | 462 | (isearch-mode t (null not-regexp) nil (not no-recursive-edit))) |
8e1cae6d | 463 | |
eceee2c0 | 464 | (defun isearch-backward (&optional regexp-p no-recursive-edit) |
8e1cae6d JB |
465 | "\ |
466 | Do incremental search backward. | |
08200510 | 467 | With a prefix argument, do a regular expression search instead. |
8e1cae6d | 468 | See \\[isearch-forward] for more information." |
eceee2c0 | 469 | (interactive "P\np") |
4ae7d00a | 470 | (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit))) |
8e1cae6d | 471 | |
eceee2c0 | 472 | (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit) |
8e1cae6d JB |
473 | "\ |
474 | Do incremental search backward for regular expression. | |
08200510 | 475 | With a prefix argument, do a regular string search instead. |
8e1cae6d JB |
476 | Like ordinary incremental search except that your input |
477 | is treated as a regexp. See \\[isearch-forward] for more info." | |
eceee2c0 | 478 | (interactive "P\np") |
4ae7d00a | 479 | (isearch-mode nil (null not-regexp) nil (not no-recursive-edit))) |
08200510 RS |
480 | |
481 | ||
482 | (defun isearch-mode-help () | |
483 | (interactive) | |
484 | (describe-function 'isearch-forward) | |
485 | (isearch-update)) | |
8e1cae6d JB |
486 | |
487 | \f | |
8e1cae6d JB |
488 | ;; isearch-mode only sets up incremental search for the minor mode. |
489 | ;; All the work is done by the isearch-mode commands. | |
490 | ||
08200510 | 491 | ;; Not used yet: |
d7fa5aa2 | 492 | ;;(defvar isearch-commands '(isearch-forward isearch-backward |
08200510 RS |
493 | ;; isearch-forward-regexp isearch-backward-regexp) |
494 | ;; "List of commands for which isearch-mode does not recursive-edit.") | |
495 | ||
496 | ||
497 | (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p) | |
79ce455d RS |
498 | "Start isearch minor mode. Called by `isearch-forward', etc. |
499 | ||
500 | \\{isearch-mode-map}" | |
8e1cae6d JB |
501 | |
502 | ;; Initialize global vars. | |
503 | (setq isearch-forward forward | |
504 | isearch-regexp regexp | |
08200510 | 505 | isearch-word word-p |
8e1cae6d JB |
506 | isearch-op-fun op-fun |
507 | isearch-case-fold-search case-fold-search | |
508 | isearch-string "" | |
509 | isearch-message "" | |
510 | isearch-cmds nil | |
511 | isearch-success t | |
512 | isearch-wrapped nil | |
513 | isearch-barrier (point) | |
514 | isearch-adjusted nil | |
515 | isearch-yank-flag nil | |
516 | isearch-invalid-regexp nil | |
c5f6b356 | 517 | isearch-within-brackets nil |
0cabad13 | 518 | isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed) |
8e1cae6d JB |
519 | (> (window-height) |
520 | (* 4 search-slow-window-lines))) | |
521 | isearch-other-end nil | |
522 | isearch-small-window nil | |
99ae9e9f | 523 | isearch-just-started t |
f46d5091 | 524 | isearch-multibyte-characters-flag nil |
8c5d9782 | 525 | isearch-input-method nil |
8e1cae6d | 526 | |
8e1cae6d | 527 | isearch-opoint (point) |
a5dcf63f | 528 | search-ring-yank-pointer nil |
0352b205 | 529 | isearch-opened-overlays nil |
a5dcf63f | 530 | regexp-search-ring-yank-pointer nil) |
99ae9e9f | 531 | (looking-at "") |
292a8dff RS |
532 | (setq isearch-window-configuration |
533 | (if isearch-slow-terminal-mode (current-window-configuration) nil)) | |
70418205 | 534 | |
18ce7555 RS |
535 | ;; Maybe make minibuffer frame visible and/or raise it. |
536 | (let ((frame (window-frame (minibuffer-window)))) | |
537 | (if (not (memq (frame-live-p frame) '(nil t))) | |
538 | (progn | |
539 | (make-frame-visible frame) | |
540 | (if minibuffer-auto-raise | |
541 | (raise-frame frame))))) | |
542 | ||
8e1cae6d | 543 | (setq isearch-mode " Isearch") ;; forward? regexp? |
1cd3732c | 544 | (force-mode-line-update) |
8e1cae6d JB |
545 | |
546 | (isearch-push-state) | |
547 | ||
c7955222 | 548 | (setq overriding-terminal-local-map isearch-mode-map) |
8e1cae6d JB |
549 | (isearch-update) |
550 | (run-hooks 'isearch-mode-hook) | |
08200510 | 551 | |
bfe2d334 | 552 | (add-hook 'mouse-leave-buffer-hook 'isearch-done) |
6e5cd0ae | 553 | |
08200510 RS |
554 | ;; isearch-mode can be made modal (in the sense of not returning to |
555 | ;; the calling function until searching is completed) by entering | |
556 | ;; a recursive-edit and exiting it when done isearching. | |
130bf67c RS |
557 | (if recursive-edit |
558 | (let ((isearch-recursive-edit t)) | |
559 | (recursive-edit))) | |
0daa17f3 | 560 | isearch-success) |
8e1cae6d JB |
561 | |
562 | ||
8e1cae6d JB |
563 | ;; Some high level utilities. Others below. |
564 | ||
565 | (defun isearch-update () | |
566 | ;; Called after each command to update the display. | |
58451992 | 567 | (if (null unread-command-events) |
8e1cae6d JB |
568 | (progn |
569 | (if (not (input-pending-p)) | |
570 | (isearch-message)) | |
571 | (if (and isearch-slow-terminal-mode | |
572 | (not (or isearch-small-window | |
573 | (pos-visible-in-window-p)))) | |
08200510 | 574 | (let ((found-point (point))) |
8e1cae6d | 575 | (setq isearch-small-window t) |
8e1cae6d JB |
576 | (move-to-window-line 0) |
577 | (let ((window-min-height 1)) | |
578 | (split-window nil (if (< search-slow-window-lines 0) | |
579 | (1+ (- search-slow-window-lines)) | |
580 | (- (window-height) | |
581 | (1+ search-slow-window-lines))))) | |
582 | (if (< search-slow-window-lines 0) | |
583 | (progn (vertical-motion (- 1 search-slow-window-lines)) | |
584 | (set-window-start (next-window) (point)) | |
585 | (set-window-hscroll (next-window) | |
586 | (window-hscroll)) | |
587 | (set-window-hscroll (selected-window) 0)) | |
588 | (other-window 1)) | |
08200510 RS |
589 | (goto-char found-point))) |
590 | (if isearch-other-end | |
591 | (if (< isearch-other-end (point)) ; isearch-forward? | |
592 | (isearch-highlight isearch-other-end (point)) | |
99afb671 RS |
593 | (isearch-highlight (point) isearch-other-end)) |
594 | (isearch-dehighlight nil)) | |
08200510 | 595 | )) |
8e1cae6d JB |
596 | (setq ;; quit-flag nil not for isearch-mode |
597 | isearch-adjusted nil | |
598 | isearch-yank-flag nil) | |
599 | ) | |
600 | ||
0daa17f3 | 601 | (defun isearch-done (&optional nopush edit) |
bfe2d334 | 602 | (remove-hook 'mouse-leave-buffer-hook 'isearch-done) |
8e1cae6d | 603 | ;; Called by all commands that terminate isearch-mode. |
35a4d143 | 604 | ;; If NOPUSH is non-nil, we don't push the string on the search ring. |
c7955222 | 605 | (setq overriding-terminal-local-map nil) |
08200510 RS |
606 | ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs |
607 | (isearch-dehighlight t) | |
608 | (let ((found-start (window-start (selected-window))) | |
609 | (found-point (point))) | |
70418205 RS |
610 | (if isearch-window-configuration |
611 | (set-window-configuration isearch-window-configuration)) | |
08200510 | 612 | |
d49b6338 RS |
613 | (if isearch-small-window |
614 | (goto-char found-point) | |
615 | ;; Exiting the save-window-excursion clobbers window-start; restore it. | |
9821535a | 616 | (set-window-start (selected-window) found-start t)) |
d49b6338 | 617 | |
08200510 RS |
618 | ;; If there was movement, mark the starting position. |
619 | ;; Maybe should test difference between and set mark iff > threshold. | |
620 | (if (/= (point) isearch-opoint) | |
9821535a RS |
621 | (or (and transient-mark-mode mark-active) |
622 | (progn | |
623 | (push-mark isearch-opoint t) | |
f3acf6f5 | 624 | (or executing-kbd-macro (> (minibuffer-depth) 0) |
9821535a | 625 | (message "Mark saved where search started")))))) |
08200510 RS |
626 | |
627 | (setq isearch-mode nil) | |
1cd3732c | 628 | (force-mode-line-update) |
8e1cae6d | 629 | |
35a4d143 | 630 | (if (and (> (length isearch-string) 0) (not nopush)) |
8e1cae6d | 631 | ;; Update the ring data. |
73d2bf95 | 632 | (isearch-update-ring isearch-string isearch-regexp)) |
8e1cae6d | 633 | |
8e1cae6d | 634 | (run-hooks 'isearch-mode-end-hook) |
0daa17f3 | 635 | (and (not edit) isearch-recursive-edit (exit-recursive-edit))) |
08200510 | 636 | |
73d2bf95 RS |
637 | (defun isearch-update-ring (string &optional regexp) |
638 | "Add STRING to the beginning of the search ring. | |
639 | REGEXP says which ring to use." | |
640 | (if regexp | |
641 | (if (or (null regexp-search-ring) | |
fd89878d | 642 | (not (string= string (car regexp-search-ring)))) |
73d2bf95 RS |
643 | (progn |
644 | (setq regexp-search-ring | |
fd89878d | 645 | (cons string regexp-search-ring)) |
73d2bf95 RS |
646 | (if (> (length regexp-search-ring) regexp-search-ring-max) |
647 | (setcdr (nthcdr (1- search-ring-max) regexp-search-ring) | |
648 | nil)))) | |
649 | (if (or (null search-ring) | |
fd89878d | 650 | (not (string= string (car search-ring)))) |
73d2bf95 | 651 | (progn |
fd89878d | 652 | (setq search-ring (cons string search-ring)) |
73d2bf95 RS |
653 | (if (> (length search-ring) search-ring-max) |
654 | (setcdr (nthcdr (1- search-ring-max) search-ring) nil)))))) | |
655 | ||
08200510 | 656 | ;;; Switching buffers should first terminate isearch-mode. |
eb8c3be9 | 657 | ;;; This is done quite differently for each variant of emacs. |
08200510 RS |
658 | ;;; For lemacs, see Exiting in lemacs below |
659 | ||
660 | ;; For Emacs 19, the frame switch event is handled. | |
661 | (defun isearch-switch-frame-handler () | |
662 | (interactive) ;; Is this necessary? | |
663 | ;; First terminate isearch-mode. | |
664 | (isearch-done) | |
0352b205 | 665 | (isearch-clean-overlays) |
dba1ec55 | 666 | (handle-switch-frame (car (cdr (isearch-last-command-char))))) |
08200510 | 667 | |
8e1cae6d | 668 | \f |
8e1cae6d JB |
669 | ;; Commands active while inside of the isearch minor mode. |
670 | ||
671 | (defun isearch-exit () | |
672 | "Exit search normally. | |
673 | However, if this is the first command after starting incremental | |
674 | search and `search-nonincremental-instead' is non-nil, do a | |
08200510 | 675 | nonincremental search instead via `isearch-edit-string'." |
8e1cae6d JB |
676 | (interactive) |
677 | (if (and search-nonincremental-instead | |
678 | (= 0 (length isearch-string))) | |
08200510 RS |
679 | (let ((isearch-nonincremental t)) |
680 | (isearch-edit-string))) | |
0352b205 RS |
681 | (isearch-done) |
682 | (isearch-clean-overlays)) | |
8e1cae6d JB |
683 | |
684 | ||
685 | (defun isearch-edit-string () | |
08200510 RS |
686 | "Edit the search string in the minibuffer. |
687 | The following additional command keys are active while editing. | |
688 | \\<minibuffer-local-isearch-map> | |
689 | \\[exit-minibuffer] to resume incremental searching with the edited string. | |
690 | \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search. | |
691 | \\[isearch-forward-exit-minibuffer] to resume isearching forward. | |
8eb40e74 | 692 | \\[isearch-reverse-exit-minibuffer] to resume isearching backward. |
08200510 | 693 | \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring. |
eb8c3be9 | 694 | \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring. |
08200510 | 695 | \\[isearch-complete-edit] to complete the search string using the search ring. |
8eb40e74 | 696 | \\<isearch-mode-map> |
08200510 RS |
697 | If first char entered is \\[isearch-yank-word], then do word search instead." |
698 | ||
699 | ;; This code is very hairy for several reasons, explained in the code. | |
700 | ;; Mainly, isearch-mode must be terminated while editing and then restarted. | |
701 | ;; If there were a way to catch any change of buffer from the minibuffer, | |
702 | ;; this could be simplified greatly. | |
eb8c3be9 | 703 | ;; Editing doesn't back up the search point. Should it? |
8e1cae6d | 704 | (interactive) |
08200510 | 705 | (condition-case err |
e900ced4 RS |
706 | (progn |
707 | (let ((isearch-nonincremental isearch-nonincremental) | |
708 | ||
709 | ;; Locally bind all isearch global variables to protect them | |
710 | ;; from recursive isearching. | |
711 | ;; isearch-string -message and -forward are not bound | |
712 | ;; so they may be changed. Instead, save the values. | |
713 | (isearch-new-string isearch-string) | |
714 | (isearch-new-message isearch-message) | |
715 | (isearch-new-forward isearch-forward) | |
716 | (isearch-new-word isearch-word) | |
717 | ||
718 | (isearch-regexp isearch-regexp) | |
719 | (isearch-op-fun isearch-op-fun) | |
720 | (isearch-cmds isearch-cmds) | |
721 | (isearch-success isearch-success) | |
722 | (isearch-wrapped isearch-wrapped) | |
723 | (isearch-barrier isearch-barrier) | |
724 | (isearch-adjusted isearch-adjusted) | |
725 | (isearch-yank-flag isearch-yank-flag) | |
726 | (isearch-invalid-regexp isearch-invalid-regexp) | |
727 | (isearch-within-brackets isearch-within-brackets) | |
728 | ;;; Don't bind this. We want isearch-search, below, to set it. | |
729 | ;;; And the old value won't matter after that. | |
730 | ;;; (isearch-other-end isearch-other-end) | |
731 | ;;; Perhaps some of these other variables should be bound for a | |
732 | ;;; shorter period, ending before the next isearch-search. | |
733 | ;;; But there doesn't seem to be a real bug, so let's not risk it now. | |
734 | (isearch-opoint isearch-opoint) | |
735 | (isearch-slow-terminal-mode isearch-slow-terminal-mode) | |
736 | (isearch-small-window isearch-small-window) | |
737 | (isearch-recursive-edit isearch-recursive-edit) | |
738 | ;; Save current configuration so we can restore it here. | |
739 | (isearch-window-configuration (current-window-configuration)) | |
740 | ) | |
741 | ||
742 | ;; Actually terminate isearching until editing is done. | |
743 | ;; This is so that the user can do anything without failure, | |
744 | ;; like switch buffers and start another isearch, and return. | |
745 | (condition-case err | |
746 | (isearch-done t t) | |
747 | (exit nil)) ; was recursive editing | |
748 | ||
749 | (isearch-message) ;; for read-char | |
750 | (unwind-protect | |
751 | (let* (;; Why does following read-char echo? | |
752 | ;;(echo-keystrokes 0) ;; not needed with above message | |
753 | (e (let ((cursor-in-echo-area t)) | |
754 | (read-event))) | |
755 | ;; Binding minibuffer-history-symbol to nil is a work-around | |
756 | ;; for some incompatibility with gmhist. | |
757 | (minibuffer-history-symbol) | |
758 | (message-log-max nil)) | |
759 | ;; If the first character the user types when we prompt them | |
760 | ;; for a string is the yank-word character, then go into | |
761 | ;; word-search mode. Otherwise unread that character and | |
762 | ;; read a key the normal way. | |
763 | ;; Word search does not apply (yet) to regexp searches, | |
764 | ;; no check is made here. | |
765 | (message (isearch-message-prefix nil nil t)) | |
766 | (if (eq 'isearch-yank-word | |
767 | (lookup-key isearch-mode-map (vector e))) | |
768 | (setq isearch-word t;; so message-prefix is right | |
769 | isearch-new-word t) | |
770 | (cancel-kbd-macro-events) | |
771 | (isearch-unread e)) | |
772 | (setq cursor-in-echo-area nil) | |
773 | (setq isearch-new-string | |
774 | (let (junk-ring) | |
775 | (read-from-minibuffer | |
776 | (isearch-message-prefix nil nil isearch-nonincremental) | |
777 | isearch-string | |
778 | minibuffer-local-isearch-map nil | |
779 | 'junk-ring)) | |
780 | isearch-new-message | |
781 | (mapconcat 'isearch-text-char-description | |
782 | isearch-new-string ""))) | |
783 | ;; Always resume isearching by restarting it. | |
784 | (isearch-mode isearch-forward | |
785 | isearch-regexp | |
786 | isearch-op-fun | |
787 | nil | |
788 | isearch-word) | |
789 | ||
790 | ;; Copy new local values to isearch globals | |
791 | (setq isearch-string isearch-new-string | |
792 | isearch-message isearch-new-message | |
793 | isearch-forward isearch-new-forward | |
794 | isearch-word isearch-new-word)) | |
795 | ||
796 | ;; Empty isearch-string means use default. | |
797 | (if (= 0 (length isearch-string)) | |
798 | (setq isearch-string (or (car (if isearch-regexp | |
799 | regexp-search-ring | |
800 | search-ring)) | |
801 | "")) | |
802 | ;; This used to set the last search string, | |
803 | ;; but I think it is not right to do that here. | |
804 | ;; Only the string actually used should be saved. | |
805 | )) | |
806 | ||
807 | ;; Push the state as of before this C-s. | |
808 | (isearch-push-state) | |
08200510 RS |
809 | |
810 | ;; Reinvoke the pending search. | |
08200510 RS |
811 | (isearch-search) |
812 | (isearch-update) | |
813 | (if isearch-nonincremental | |
814 | (progn | |
815 | ;; (sit-for 1) ;; needed if isearch-done does: (message "") | |
816 | (isearch-done)))) | |
8e1cae6d | 817 | |
08200510 RS |
818 | (quit ; handle abort-recursive-edit |
819 | (isearch-abort) ;; outside of let to restore outside global values | |
820 | ))) | |
821 | ||
822 | (defun isearch-nonincremental-exit-minibuffer () | |
823 | (interactive) | |
824 | (setq isearch-nonincremental t) | |
825 | (exit-minibuffer)) | |
826 | ||
827 | (defun isearch-forward-exit-minibuffer () | |
828 | (interactive) | |
829 | (setq isearch-new-forward t) | |
830 | (exit-minibuffer)) | |
8e1cae6d | 831 | |
08200510 | 832 | (defun isearch-reverse-exit-minibuffer () |
8e1cae6d | 833 | (interactive) |
08200510 RS |
834 | (setq isearch-new-forward nil) |
835 | (exit-minibuffer)) | |
836 | ||
19993c54 RS |
837 | (defun isearch-cancel () |
838 | "Terminate the search and go back to the starting point." | |
839 | (interactive) | |
840 | (goto-char isearch-opoint) | |
841 | (isearch-done t) | |
0352b205 | 842 | (isearch-clean-overlays) |
19993c54 | 843 | (signal 'quit nil)) ; and pass on quit signal |
08200510 RS |
844 | |
845 | (defun isearch-abort () | |
b7852303 | 846 | "Abort incremental search mode if searching is successful, signaling quit. |
08200510 | 847 | Otherwise, revert to previous successful search and continue searching. |
b7852303 | 848 | Use `isearch-exit' to quit without signaling." |
08200510 | 849 | (interactive) |
eb8c3be9 | 850 | ;; (ding) signal instead below, if quitting |
8e1cae6d JB |
851 | (discard-input) |
852 | (if isearch-success | |
853 | ;; If search is successful, move back to starting point | |
854 | ;; and really do quit. | |
855 | (progn (goto-char isearch-opoint) | |
509ed182 | 856 | (setq isearch-success nil) |
35a4d143 | 857 | (isearch-done t) ; exit isearch |
0352b205 | 858 | (isearch-clean-overlays) |
a5dcf63f | 859 | (signal 'quit nil)) ; and pass on quit signal |
925a67ca RS |
860 | ;; If search is failing, or has an incomplete regexp, |
861 | ;; rub out until it is once more successful. | |
862 | (while (or (not isearch-success) isearch-invalid-regexp) | |
863 | (isearch-pop-state)) | |
8e1cae6d JB |
864 | (isearch-update))) |
865 | ||
8e1cae6d JB |
866 | (defun isearch-repeat (direction) |
867 | ;; Utility for isearch-repeat-forward and -backward. | |
868 | (if (eq isearch-forward (eq direction 'forward)) | |
869 | ;; C-s in forward or C-r in reverse. | |
870 | (if (equal isearch-string "") | |
871 | ;; If search string is empty, use last one. | |
872 | (setq isearch-string | |
8e1cae6d | 873 | (or (if isearch-regexp |
a5dcf63f RS |
874 | (car regexp-search-ring) |
875 | (car search-ring)) | |
8e1cae6d JB |
876 | "") |
877 | isearch-message | |
08200510 | 878 | (mapconcat 'isearch-text-char-description |
8e1cae6d JB |
879 | isearch-string "")) |
880 | ;; If already have what to search for, repeat it. | |
881 | (or isearch-success | |
882 | (progn | |
8e1cae6d JB |
883 | (goto-char (if isearch-forward (point-min) (point-max))) |
884 | (setq isearch-wrapped t)))) | |
885 | ;; C-s in reverse or C-r in forward, change direction. | |
886 | (setq isearch-forward (not isearch-forward))) | |
887 | ||
888 | (setq isearch-barrier (point)) ; For subsequent \| if regexp. | |
6fefdc4b RS |
889 | |
890 | (if (equal isearch-string "") | |
891 | (setq isearch-success t) | |
99ae9e9f KH |
892 | (if (and isearch-success (equal (match-end 0) (match-beginning 0)) |
893 | (not isearch-just-started)) | |
8e1cae6d JB |
894 | ;; If repeating a search that found |
895 | ;; an empty string, ensure we advance. | |
6fefdc4b RS |
896 | (if (if isearch-forward (eobp) (bobp)) |
897 | ;; If there's nowhere to advance to, fail (and wrap next time). | |
898 | (progn | |
899 | (setq isearch-success nil) | |
900 | (ding)) | |
901 | (forward-char (if isearch-forward 1 -1)) | |
902 | (isearch-search)) | |
903 | (isearch-search))) | |
904 | ||
8e1cae6d JB |
905 | (isearch-push-state) |
906 | (isearch-update)) | |
907 | ||
908 | (defun isearch-repeat-forward () | |
909 | "Repeat incremental search forwards." | |
910 | (interactive) | |
911 | (isearch-repeat 'forward)) | |
912 | ||
913 | (defun isearch-repeat-backward () | |
914 | "Repeat incremental search backwards." | |
915 | (interactive) | |
916 | (isearch-repeat 'backward)) | |
917 | ||
918 | (defun isearch-toggle-regexp () | |
919 | "Toggle regexp searching on or off." | |
920 | ;; The status stack is left unchanged. | |
921 | (interactive) | |
922 | (setq isearch-regexp (not isearch-regexp)) | |
08200510 | 923 | (if isearch-regexp (setq isearch-word nil)) |
8e1cae6d JB |
924 | (isearch-update)) |
925 | ||
4453091d RS |
926 | (defun isearch-toggle-case-fold () |
927 | "Toggle case folding in searching on or off." | |
928 | (interactive) | |
929 | (setq isearch-case-fold-search | |
930 | (if isearch-case-fold-search nil 'yes)) | |
a56687f1 KH |
931 | (let ((message-log-max nil)) |
932 | (message "%s%s [case %ssensitive]" | |
933 | (isearch-message-prefix nil nil isearch-nonincremental) | |
934 | isearch-message | |
935 | (if isearch-case-fold-search "in" ""))) | |
4453091d RS |
936 | (setq isearch-adjusted t) |
937 | (sit-for 1) | |
938 | (isearch-update)) | |
939 | ||
8e1cae6d JB |
940 | (defun isearch-delete-char () |
941 | "Discard last input item and move point back. | |
942 | If no previous match was done, just beep." | |
943 | (interactive) | |
944 | (if (null (cdr isearch-cmds)) | |
945 | (ding) | |
946 | (isearch-pop-state)) | |
947 | (isearch-update)) | |
948 | ||
949 | ||
950 | (defun isearch-yank (chunk) | |
951 | ;; Helper for isearch-yank-word and isearch-yank-line | |
fd543be5 | 952 | ;; CHUNK should be word, line, kill, or x-sel. |
30d47262 RS |
953 | (let ((string (cond |
954 | ((eq chunk 'kill) | |
955 | (current-kill 0)) | |
fd543be5 KH |
956 | ((eq chunk 'x-sel) |
957 | (x-get-selection)) | |
30d47262 RS |
958 | (t |
959 | (save-excursion | |
960 | (and (not isearch-forward) isearch-other-end | |
961 | (goto-char isearch-other-end)) | |
962 | (buffer-substring | |
963 | (point) | |
964 | (save-excursion | |
965 | (cond | |
966 | ((eq chunk 'word) | |
967 | (forward-word 1)) | |
968 | ((eq chunk 'line) | |
969 | (end-of-line))) | |
970 | (point)))))))) | |
08200510 RS |
971 | ;; Downcase the string if not supposed to case-fold yanked strings. |
972 | (if (and isearch-case-fold-search | |
fdf5afa4 | 973 | (eq 'not-yanks search-upper-case)) |
08200510 RS |
974 | (setq string (downcase string))) |
975 | (if isearch-regexp (setq string (regexp-quote string))) | |
976 | (setq isearch-string (concat isearch-string string) | |
8e1cae6d JB |
977 | isearch-message |
978 | (concat isearch-message | |
08200510 RS |
979 | (mapconcat 'isearch-text-char-description |
980 | string "")) | |
8e1cae6d JB |
981 | ;; Don't move cursor in reverse search. |
982 | isearch-yank-flag t)) | |
983 | (isearch-search-and-update)) | |
984 | ||
30d47262 RS |
985 | (defun isearch-yank-kill () |
986 | "Pull string from kill ring into search string." | |
987 | (interactive) | |
988 | (isearch-yank 'kill)) | |
8e1cae6d JB |
989 | |
990 | (defun isearch-yank-word () | |
991 | "Pull next word from buffer into search string." | |
992 | (interactive) | |
993 | (isearch-yank 'word)) | |
994 | ||
995 | (defun isearch-yank-line () | |
996 | "Pull rest of line from buffer into search string." | |
997 | (interactive) | |
998 | (isearch-yank 'line)) | |
999 | ||
1000 | ||
1001 | (defun isearch-search-and-update () | |
1002 | ;; Do the search and update the display. | |
1003 | (if (and (not isearch-success) | |
1004 | ;; unsuccessful regexp search may become | |
1005 | ;; successful by addition of characters which | |
1006 | ;; make isearch-string valid | |
1007 | (not isearch-regexp)) | |
1008 | nil | |
1009 | ;; In reverse search, adding stuff at | |
1010 | ;; the end may cause zero or many more chars to be | |
1011 | ;; matched, in the string following point. | |
1012 | ;; Allow all those possibilities without moving point as | |
1013 | ;; long as the match does not extend past search origin. | |
1014 | (if (and (not isearch-forward) (not isearch-adjusted) | |
1015 | (condition-case () | |
99ae9e9f KH |
1016 | (let ((case-fold-search isearch-case-fold-search)) |
1017 | (looking-at (if isearch-regexp isearch-string | |
1018 | (regexp-quote isearch-string)))) | |
8e1cae6d JB |
1019 | (error nil)) |
1020 | (or isearch-yank-flag | |
1021 | (<= (match-end 0) | |
1022 | (min isearch-opoint isearch-barrier)))) | |
d64b1d96 RS |
1023 | (progn |
1024 | (setq isearch-success t | |
1025 | isearch-invalid-regexp nil | |
1026 | isearch-within-brackets nil | |
1027 | isearch-other-end (match-end 0)) | |
1028 | (if (and (eq isearch-case-fold-search t) search-upper-case) | |
1029 | (setq isearch-case-fold-search | |
1030 | (isearch-no-upper-case-p isearch-string isearch-regexp)))) | |
8e1cae6d JB |
1031 | ;; Not regexp, not reverse, or no match at point. |
1032 | (if (and isearch-other-end (not isearch-adjusted)) | |
1033 | (goto-char (if isearch-forward isearch-other-end | |
1034 | (min isearch-opoint | |
1035 | isearch-barrier | |
1036 | (1+ isearch-other-end))))) | |
1037 | (isearch-search) | |
1038 | )) | |
1039 | (isearch-push-state) | |
1040 | (if isearch-op-fun (funcall isearch-op-fun)) | |
1041 | (isearch-update)) | |
1042 | ||
1043 | ||
1044 | ;; *, ?, and | chars can make a regexp more liberal. | |
08200510 | 1045 | ;; They can make a regexp match sooner or make it succeed instead of failing. |
8e1cae6d JB |
1046 | ;; So go back to place last successful search started |
1047 | ;; or to the last ^S/^R (barrier), whichever is nearer. | |
08200510 | 1048 | ;; + needs no special handling because the string must match at least once. |
8e1cae6d JB |
1049 | |
1050 | (defun isearch-*-char () | |
1051 | "Handle * and ? specially in regexps." | |
1052 | (interactive) | |
1053 | (if isearch-regexp | |
1054 | ||
1055 | (progn | |
1056 | (setq isearch-adjusted t) | |
149d4fe5 RS |
1057 | ;; Get the isearch-other-end from before the last search. |
1058 | ;; We want to start from there, | |
1059 | ;; so that we don't retreat farther than that. | |
1060 | ;; (car isearch-cmds) is after last search; | |
1061 | ;; (car (cdr isearch-cmds)) is from before it. | |
1062 | (let ((cs (nth 5 (car (cdr isearch-cmds))))) | |
8e1cae6d JB |
1063 | (setq cs (or cs isearch-barrier)) |
1064 | (goto-char | |
1065 | (if isearch-forward | |
1066 | (max cs isearch-barrier) | |
1067 | (min cs isearch-barrier)))))) | |
08200510 | 1068 | (isearch-process-search-char (isearch-last-command-char))) |
8e1cae6d JB |
1069 | |
1070 | ||
8e1cae6d JB |
1071 | (defun isearch-|-char () |
1072 | "If in regexp search, jump to the barrier." | |
1073 | (interactive) | |
1074 | (if isearch-regexp | |
1075 | (progn | |
1076 | (setq isearch-adjusted t) | |
1077 | (goto-char isearch-barrier))) | |
08200510 | 1078 | (isearch-process-search-char (isearch-last-command-char))) |
8e1cae6d JB |
1079 | |
1080 | ||
b457cc3b | 1081 | (defalias 'isearch-other-control-char 'isearch-other-meta-char) |
8e1cae6d JB |
1082 | |
1083 | (defun isearch-other-meta-char () | |
8f90f594 | 1084 | "Exit the search normally and reread this key sequence. |
35a4d143 RS |
1085 | But only if `search-exit-option' is non-nil, the default. |
1086 | If it is the symbol `edit', the search string is edited in the minibuffer | |
1087 | and the meta character is unread so that it applies to editing the string." | |
8e1cae6d | 1088 | (interactive) |
c6431f12 KH |
1089 | (let* ((key (this-command-keys)) |
1090 | (main-event (aref key 0)) | |
1091 | (keylist (listify-key-sequence key))) | |
24b5caec | 1092 | (cond ((and (= (length key) 1) |
d94eb6eb | 1093 | (let ((lookup (lookup-key function-key-map key))) |
3f866b53 KH |
1094 | (not (or (null lookup) (integerp lookup) |
1095 | (keymapp lookup))))) | |
24b5caec RS |
1096 | ;; Handle a function key that translates into something else. |
1097 | ;; If the key has a global definition too, | |
1098 | ;; exit and unread the key itself, so its global definition runs. | |
1099 | ;; Otherwise, unread the translation, | |
1100 | ;; so that the translated key takes effect within isearch. | |
d94eb6eb | 1101 | (cancel-kbd-macro-events) |
24b5caec RS |
1102 | (if (lookup-key global-map key) |
1103 | (progn | |
1104 | (isearch-done) | |
1105 | (apply 'isearch-unread keylist)) | |
1106 | (apply 'isearch-unread | |
1107 | (listify-key-sequence (lookup-key function-key-map key))))) | |
1108 | ( | |
c6431f12 KH |
1109 | ;; Handle an undefined shifted control character |
1110 | ;; by downshifting it if that makes it defined. | |
1111 | ;; (As read-key-sequence would normally do, | |
1112 | ;; if we didn't have a default definition.) | |
1113 | (let ((mods (event-modifiers main-event))) | |
1114 | (and (integerp main-event) | |
1115 | (memq 'shift mods) | |
1116 | (memq 'control mods) | |
1117 | (lookup-key isearch-mode-map | |
1118 | (let ((copy (copy-sequence key))) | |
1119 | (aset copy 0 | |
1120 | (- main-event (- ?\C-\S-a ?\C-a))) | |
1121 | copy) | |
1122 | nil))) | |
1123 | (setcar keylist (- main-event (- ?\C-\S-a ?\C-a))) | |
d94eb6eb | 1124 | (cancel-kbd-macro-events) |
c6431f12 KH |
1125 | (apply 'isearch-unread keylist)) |
1126 | ((eq search-exit-option 'edit) | |
1127 | (apply 'isearch-unread keylist) | |
1128 | (isearch-edit-string)) | |
1129 | (search-exit-option | |
1130 | (let (window) | |
d94eb6eb | 1131 | (cancel-kbd-macro-events) |
c6431f12 KH |
1132 | (apply 'isearch-unread keylist) |
1133 | ;; Properly handle scroll-bar and mode-line clicks | |
1134 | ;; for which a dummy prefix event was generated as (aref key 0). | |
1135 | (and (> (length key) 1) | |
1136 | (symbolp (aref key 0)) | |
1137 | (listp (aref key 1)) | |
1138 | (not (numberp (posn-point (event-start (aref key 1))))) | |
1139 | ;; Convert the event back into its raw form, | |
1140 | ;; with the dummy prefix implicit in the mouse event, | |
1141 | ;; so it will get split up once again. | |
1142 | (progn (setq unread-command-events | |
1143 | (cdr unread-command-events)) | |
1144 | (setq main-event (car unread-command-events)) | |
1145 | (setcar (cdr (event-start main-event)) | |
1146 | (car (nth 1 (event-start main-event)))))) | |
1147 | ;; If we got a mouse click, maybe it was read with the buffer | |
1148 | ;; it was clicked on. If so, that buffer, not the current one, | |
1149 | ;; is in isearch mode. So end the search in that buffer. | |
1150 | (if (and (listp main-event) | |
1151 | (setq window (posn-window (event-start main-event))) | |
4fd017e7 RS |
1152 | (windowp window) |
1153 | (or (> (minibuffer-depth) 0) | |
1154 | (not (window-minibuffer-p window)))) | |
c6431f12 KH |
1155 | (save-excursion |
1156 | (set-buffer (window-buffer window)) | |
0352b205 RS |
1157 | (isearch-done) |
1158 | (isearch-clean-overlays)) | |
1159 | (isearch-done) | |
1160 | (isearch-clean-overlays)))) | |
c6431f12 KH |
1161 | (t;; otherwise nil |
1162 | (isearch-process-search-string key key))))) | |
8e1cae6d | 1163 | |
8e1cae6d JB |
1164 | (defun isearch-quote-char () |
1165 | "Quote special characters for incremental search." | |
1166 | (interactive) | |
92d874e6 RS |
1167 | (isearch-process-search-char (+ (read-quoted-char (isearch-message t)) |
1168 | nonascii-insert-offset))) | |
8e1cae6d | 1169 | |
8e1cae6d JB |
1170 | (defun isearch-return-char () |
1171 | "Convert return into newline for incremental search. | |
1172 | Obsolete." | |
1173 | (interactive) | |
1174 | (isearch-process-search-char ?\n)) | |
1175 | ||
8e1cae6d | 1176 | (defun isearch-printing-char () |
b68c164d | 1177 | "Add this ordinary printing character to the search string and search." |
8e1cae6d | 1178 | (interactive) |
8c5d9782 | 1179 | (if isearch-input-method |
f46d5091 KH |
1180 | (isearch-process-search-multibyte-characters (isearch-last-command-char)) |
1181 | (isearch-process-search-char (isearch-last-command-char)))) | |
8e1cae6d JB |
1182 | |
1183 | (defun isearch-whitespace-chars () | |
08200510 | 1184 | "Match all whitespace chars, if in regexp mode. |
b68c164d | 1185 | If you want to search for just a space, type C-q SPC." |
8e1cae6d | 1186 | (interactive) |
08200510 | 1187 | (if isearch-regexp |
9da6682f RS |
1188 | (if (and search-whitespace-regexp (not isearch-within-brackets) |
1189 | (not isearch-invalid-regexp)) | |
08200510 RS |
1190 | (isearch-process-search-string search-whitespace-regexp " ") |
1191 | (isearch-printing-char)) | |
1192 | (progn | |
eb8c3be9 | 1193 | ;; This way of doing word search doesn't correctly extend current search. |
08200510 RS |
1194 | ;; (setq isearch-word t) |
1195 | ;; (setq isearch-adjusted t) | |
1196 | ;; (goto-char isearch-barrier) | |
1197 | (isearch-printing-char)))) | |
8e1cae6d JB |
1198 | |
1199 | (defun isearch-process-search-char (char) | |
1200 | ;; Append the char to the search string, update the message and re-search. | |
08200510 RS |
1201 | (isearch-process-search-string |
1202 | (isearch-char-to-string char) | |
01a6ef79 RS |
1203 | (if (>= char 0200) |
1204 | (char-to-string char) | |
1205 | (isearch-text-char-description char)))) | |
8e1cae6d JB |
1206 | |
1207 | (defun isearch-process-search-string (string message) | |
1208 | (setq isearch-string (concat isearch-string string) | |
1209 | isearch-message (concat isearch-message message)) | |
1210 | (isearch-search-and-update)) | |
1211 | ||
1212 | \f | |
8e1cae6d JB |
1213 | ;; Search Ring |
1214 | ||
08200510 RS |
1215 | (defun isearch-ring-adjust1 (advance) |
1216 | ;; Helper for isearch-ring-adjust | |
1217 | (let* ((ring (if isearch-regexp regexp-search-ring search-ring)) | |
8e1cae6d JB |
1218 | (length (length ring)) |
1219 | (yank-pointer-name (if isearch-regexp | |
08200510 | 1220 | 'regexp-search-ring-yank-pointer |
8e1cae6d JB |
1221 | 'search-ring-yank-pointer)) |
1222 | (yank-pointer (eval yank-pointer-name))) | |
1223 | (if (zerop length) | |
1224 | () | |
1225 | (set yank-pointer-name | |
1226 | (setq yank-pointer | |
ffbc30b2 PE |
1227 | (mod (+ (or yank-pointer 0) |
1228 | (if advance -1 1)) | |
1229 | length))) | |
a5dcf63f | 1230 | (setq isearch-string (nth yank-pointer ring) |
08200510 RS |
1231 | isearch-message (mapconcat 'isearch-text-char-description |
1232 | isearch-string ""))))) | |
1233 | ||
1234 | (defun isearch-ring-adjust (advance) | |
1235 | ;; Helper for isearch-ring-advance and isearch-ring-retreat | |
08200510 | 1236 | (isearch-ring-adjust1 advance) |
08200510 RS |
1237 | (if search-ring-update |
1238 | (progn | |
1239 | (isearch-search) | |
1240 | (isearch-update)) | |
1241 | (isearch-edit-string) | |
2ff20b7e RS |
1242 | ) |
1243 | (isearch-push-state)) | |
8e1cae6d JB |
1244 | |
1245 | (defun isearch-ring-advance () | |
1246 | "Advance to the next search string in the ring." | |
08200510 | 1247 | ;; This could be more general to handle a prefix arg, but who would use it. |
8e1cae6d JB |
1248 | (interactive) |
1249 | (isearch-ring-adjust 'advance)) | |
1250 | ||
1251 | (defun isearch-ring-retreat () | |
1252 | "Retreat to the previous search string in the ring." | |
1253 | (interactive) | |
1254 | (isearch-ring-adjust nil)) | |
1255 | ||
a5dcf63f RS |
1256 | (defun isearch-ring-advance-edit (n) |
1257 | "Insert the next element of the search history into the minibuffer." | |
1258 | (interactive "p") | |
1259 | (let* ((yank-pointer-name (if isearch-regexp | |
1260 | 'regexp-search-ring-yank-pointer | |
1261 | 'search-ring-yank-pointer)) | |
1262 | (yank-pointer (eval yank-pointer-name)) | |
1263 | (ring (if isearch-regexp regexp-search-ring search-ring)) | |
1264 | (length (length ring))) | |
1265 | (if (zerop length) | |
1266 | () | |
1267 | (set yank-pointer-name | |
1268 | (setq yank-pointer | |
ffbc30b2 PE |
1269 | (mod (- (or yank-pointer 0) n) |
1270 | length))) | |
a5dcf63f | 1271 | |
a5dcf63f | 1272 | (erase-buffer) |
35a4d143 | 1273 | (insert (nth yank-pointer ring)) |
49c13105 | 1274 | (goto-char (point-max))))) |
a5dcf63f RS |
1275 | |
1276 | (defun isearch-ring-retreat-edit (n) | |
1277 | "Inserts the previous element of the search history into the minibuffer." | |
1278 | (interactive "p") | |
1279 | (isearch-ring-advance-edit (- n))) | |
1280 | ||
1281 | ;;(defun isearch-ring-adjust-edit (advance) | |
1282 | ;; "Use the next or previous search string in the ring while in minibuffer." | |
1283 | ;; (isearch-ring-adjust1 advance) | |
1284 | ;; (erase-buffer) | |
1285 | ;; (insert isearch-string)) | |
1286 | ||
1287 | ;;(defun isearch-ring-advance-edit () | |
1288 | ;; (interactive) | |
1289 | ;; (isearch-ring-adjust-edit 'advance)) | |
1290 | ||
1291 | ;;(defun isearch-ring-retreat-edit () | |
1292 | ;; "Retreat to the previous search string in the ring while in the minibuffer." | |
1293 | ;; (interactive) | |
1294 | ;; (isearch-ring-adjust-edit nil)) | |
08200510 RS |
1295 | |
1296 | ||
1297 | (defun isearch-complete1 () | |
1298 | ;; Helper for isearch-complete and isearch-complete-edit | |
1299 | ;; Return t if completion OK, nil if no completion exists. | |
1300 | (let* ((ring (if isearch-regexp regexp-search-ring search-ring)) | |
1301 | (alist (mapcar (function (lambda (string) (list string))) ring)) | |
1302 | (completion-ignore-case case-fold-search) | |
1303 | (completion (try-completion isearch-string alist))) | |
1304 | (cond | |
1305 | ((eq completion t) | |
1306 | ;; isearch-string stays the same | |
1307 | t) | |
1308 | ((or completion ; not nil, must be a string | |
b7852303 | 1309 | (= 0 (length isearch-string))) ; shouldn't have to say this |
08200510 | 1310 | (if (equal completion isearch-string) ;; no extension? |
36bcac3f RS |
1311 | (progn |
1312 | (if completion-auto-help | |
1313 | (with-output-to-temp-buffer "*Isearch completions*" | |
1314 | (display-completion-list | |
1315 | (all-completions isearch-string alist)))) | |
1316 | t) | |
1317 | (and completion | |
1318 | (setq isearch-string completion)))) | |
08200510 RS |
1319 | (t |
1320 | (message "No completion") ; waits a second if in minibuffer | |
1321 | nil)))) | |
1322 | ||
1323 | (defun isearch-complete () | |
1324 | "Complete the search string from the strings on the search ring. | |
1325 | The completed string is then editable in the minibuffer. | |
1326 | If there is no completion possible, say so and continue searching." | |
1327 | (interactive) | |
1328 | (if (isearch-complete1) | |
1329 | (isearch-edit-string) | |
1330 | ;; else | |
1331 | (sit-for 1) | |
1332 | (isearch-update))) | |
8e1cae6d | 1333 | |
08200510 RS |
1334 | (defun isearch-complete-edit () |
1335 | "Same as `isearch-complete' except in the minibuffer." | |
1336 | (interactive) | |
1337 | (setq isearch-string (buffer-string)) | |
1338 | (if (isearch-complete1) | |
8e1cae6d | 1339 | (progn |
08200510 RS |
1340 | (erase-buffer) |
1341 | (insert isearch-string)))) | |
8e1cae6d JB |
1342 | |
1343 | \f | |
8e1cae6d | 1344 | ;; The search status stack (and isearch window-local variables, not used). |
08200510 | 1345 | ;; Need a structure for this. |
8e1cae6d JB |
1346 | |
1347 | (defun isearch-top-state () | |
8e1cae6d JB |
1348 | (let ((cmd (car isearch-cmds))) |
1349 | (setq isearch-string (car cmd) | |
1350 | isearch-message (car (cdr cmd)) | |
1351 | isearch-success (nth 3 cmd) | |
1352 | isearch-forward (nth 4 cmd) | |
1353 | isearch-other-end (nth 5 cmd) | |
08200510 RS |
1354 | isearch-word (nth 6 cmd) |
1355 | isearch-invalid-regexp (nth 7 cmd) | |
1356 | isearch-wrapped (nth 8 cmd) | |
c5f6b356 | 1357 | isearch-barrier (nth 9 cmd) |
f551b97d RS |
1358 | isearch-within-brackets (nth 10 cmd) |
1359 | isearch-case-fold-search (nth 11 cmd)) | |
8e1cae6d JB |
1360 | (goto-char (car (cdr (cdr cmd)))))) |
1361 | ||
1362 | (defun isearch-pop-state () | |
8e1cae6d JB |
1363 | (setq isearch-cmds (cdr isearch-cmds)) |
1364 | (isearch-top-state) | |
1365 | ) | |
1366 | ||
1367 | (defun isearch-push-state () | |
1368 | (setq isearch-cmds | |
1369 | (cons (list isearch-string isearch-message (point) | |
1370 | isearch-success isearch-forward isearch-other-end | |
08200510 | 1371 | isearch-word |
c5f6b356 | 1372 | isearch-invalid-regexp isearch-wrapped isearch-barrier |
f551b97d | 1373 | isearch-within-brackets isearch-case-fold-search) |
8e1cae6d JB |
1374 | isearch-cmds))) |
1375 | ||
8e1cae6d | 1376 | \f |
8e1cae6d JB |
1377 | ;; Message string |
1378 | ||
1379 | (defun isearch-message (&optional c-q-hack ellipsis) | |
1380 | ;; Generate and print the message string. | |
1381 | (let ((cursor-in-echo-area ellipsis) | |
1382 | (m (concat | |
08200510 | 1383 | (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental) |
8e1cae6d JB |
1384 | isearch-message |
1385 | (isearch-message-suffix c-q-hack ellipsis) | |
1386 | ))) | |
a56687f1 KH |
1387 | (if c-q-hack |
1388 | m | |
1389 | (let ((message-log-max nil)) | |
1390 | (message "%s" m))))) | |
8e1cae6d | 1391 | |
08200510 | 1392 | (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental) |
8e1cae6d JB |
1393 | ;; If about to search, and previous search regexp was invalid, |
1394 | ;; check that it still is. If it is valid now, | |
1395 | ;; let the message we display while searching say that it is valid. | |
1396 | (and isearch-invalid-regexp ellipsis | |
1397 | (condition-case () | |
1398 | (progn (re-search-forward isearch-string (point) t) | |
8eb40e74 KH |
1399 | (setq isearch-invalid-regexp nil |
1400 | isearch-within-brackets nil)) | |
8e1cae6d JB |
1401 | (error nil))) |
1402 | ;; If currently failing, display no ellipsis. | |
1403 | (or isearch-success (setq ellipsis nil)) | |
1404 | (let ((m (concat (if isearch-success "" "failing ") | |
7badea30 RS |
1405 | (if (and isearch-wrapped |
1406 | (if isearch-forward | |
1407 | (> (point) isearch-opoint) | |
1408 | (< (point) isearch-opoint))) | |
1409 | "over") | |
8e1cae6d | 1410 | (if isearch-wrapped "wrapped ") |
08200510 | 1411 | (if isearch-word "word " "") |
8e1cae6d | 1412 | (if isearch-regexp "regexp " "") |
08200510 | 1413 | (if nonincremental "search" "I-search") |
f46d5091 | 1414 | (if isearch-forward "" " backward") |
8c5d9782 KH |
1415 | (if isearch-input-method |
1416 | (concat " [" isearch-input-method-title "]: ") | |
f46d5091 | 1417 | ": ") |
8e1cae6d JB |
1418 | ))) |
1419 | (aset m 0 (upcase (aref m 0))) | |
1420 | m)) | |
1421 | ||
1422 | ||
1423 | (defun isearch-message-suffix (&optional c-q-hack ellipsis) | |
1424 | (concat (if c-q-hack "^Q" "") | |
1425 | (if isearch-invalid-regexp | |
1426 | (concat " [" isearch-invalid-regexp "]") | |
1427 | ""))) | |
1428 | ||
1429 | \f | |
8e1cae6d JB |
1430 | ;;; Searching |
1431 | ||
1432 | (defun isearch-search () | |
1433 | ;; Do the search with the current search string. | |
1434 | (isearch-message nil t) | |
4453091d | 1435 | (if (and (eq isearch-case-fold-search t) search-upper-case) |
b78559b0 RS |
1436 | (setq isearch-case-fold-search |
1437 | (isearch-no-upper-case-p isearch-string isearch-regexp))) | |
8e1cae6d | 1438 | (condition-case lossage |
79c7a4fa RS |
1439 | (let ((inhibit-point-motion-hooks search-invisible) |
1440 | (inhibit-quit nil) | |
86bfaffe RS |
1441 | (case-fold-search isearch-case-fold-search) |
1442 | (retry t)) | |
8e1cae6d | 1443 | (if isearch-regexp (setq isearch-invalid-regexp nil)) |
c5f6b356 | 1444 | (setq isearch-within-brackets nil) |
86bfaffe RS |
1445 | (while retry |
1446 | (setq isearch-success | |
1447 | (funcall | |
1448 | (cond (isearch-word | |
1449 | (if isearch-forward | |
1450 | 'word-search-forward 'word-search-backward)) | |
1451 | (isearch-regexp | |
1452 | (if isearch-forward | |
1453 | 're-search-forward 're-search-backward)) | |
1454 | (t | |
1455 | (if isearch-forward 'search-forward 'search-backward))) | |
1456 | isearch-string nil t)) | |
1457 | ;; Clear RETRY unless we matched some invisible text | |
1458 | ;; and we aren't supposed to do that. | |
0352b205 | 1459 | (if (or (eq search-invisible t) |
86bfaffe RS |
1460 | (not isearch-success) |
1461 | (bobp) (eobp) | |
1462 | (= (match-beginning 0) (match-end 0)) | |
1463 | (not (isearch-range-invisible | |
1464 | (match-beginning 0) (match-end 0)))) | |
1465 | (setq retry nil))) | |
99ae9e9f | 1466 | (setq isearch-just-started nil) |
8e1cae6d JB |
1467 | (if isearch-success |
1468 | (setq isearch-other-end | |
1469 | (if isearch-forward (match-beginning 0) (match-end 0))))) | |
1470 | ||
d32696d4 | 1471 | (quit (isearch-unread ?\C-g) |
8e1cae6d JB |
1472 | (setq isearch-success nil)) |
1473 | ||
1474 | (invalid-regexp | |
1475 | (setq isearch-invalid-regexp (car (cdr lossage))) | |
c5f6b356 RS |
1476 | (setq isearch-within-brackets (string-match "\\`Unmatched \\[" |
1477 | isearch-invalid-regexp)) | |
8e1cae6d JB |
1478 | (if (string-match |
1479 | "\\`Premature \\|\\`Unmatched \\|\\`Invalid " | |
1480 | isearch-invalid-regexp) | |
f1c03125 RS |
1481 | (setq isearch-invalid-regexp "incomplete input"))) |
1482 | (error | |
1483 | ;; stack overflow in regexp search. | |
1484 | (setq isearch-invalid-regexp (car (cdr lossage))))) | |
8e1cae6d JB |
1485 | |
1486 | (if isearch-success | |
1487 | nil | |
1488 | ;; Ding if failed this time after succeeding last time. | |
1489 | (and (nth 3 (car isearch-cmds)) | |
1490 | (ding)) | |
1491 | (goto-char (nth 2 (car isearch-cmds))))) | |
1492 | ||
0352b205 RS |
1493 | |
1494 | ;;; Called when opening an overlay, and we are still in isearch. | |
1495 | (defun isearch-open-overlay-temporary (ov) | |
0352b205 RS |
1496 | (if (not (null (overlay-get ov 'isearch-open-invisible-temporary))) |
1497 | ;; Some modes would want to open the overlays temporary during | |
1498 | ;; isearch in their own way, they should set the | |
1499 | ;; `isearch-open-invisible-temporary' to a function doing this. | |
1500 | (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil) | |
1501 | ;; Store the values for the `invisible' and `intangible' | |
1502 | ;; properties, and then set them to nil. This way the text hidden | |
1503 | ;; by this overlay becomes visible. | |
1504 | ||
1505 | ;; Do we realy need to set the `intangible' property to t? Can we | |
1506 | ;; have the point inside an overlay with an `intangible' property? | |
1507 | ;; In 19.34 this does not exist so I cannot test it. | |
1508 | (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible)) | |
1509 | (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible)) | |
1510 | (overlay-put ov 'invisible nil) | |
1511 | (overlay-put ov 'intangible nil))) | |
1512 | ||
1513 | ||
1514 | ;;; This is called at the end of isearch. I will open the overlays | |
1515 | ;;; that contain the latest match. Obviously in case of a C-g the | |
1516 | ;;; point returns to the original location which surely is not contain | |
1517 | ;;; in any of these overlays, se we are safe in this case too. | |
1518 | (defun isearch-open-necessary-overlays (ov) | |
1519 | (let ((inside-overlay (and (> (point) (overlay-start ov)) | |
1520 | (< (point) (overlay-end ov)))) | |
1521 | ;; If this exists it means that the overlay was opened using | |
1522 | ;; this function, not by us tweaking the overlay properties. | |
1523 | (fct-temp (overlay-get ov 'isearch-open-invisible-temporary))) | |
1524 | (when (or inside-overlay (not fct-temp)) | |
1525 | ;; restore the values for the `invisible' and `intangible' | |
1526 | ;; properties | |
1527 | (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible)) | |
1528 | (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible)) | |
1529 | (overlay-put ov 'isearch-invisible nil) | |
1530 | (overlay-put ov 'isearch-intangible nil)) | |
1531 | (if inside-overlay | |
1532 | (funcall (overlay-get ov 'isearch-open-invisible) ov) | |
1533 | (if fct-temp | |
1534 | (funcall fct-temp ov t))))) | |
1535 | ||
1536 | ;;; This is called when exiting isearch. It closes the temporary | |
1537 | ;;; opened overlays, except the ones that contain the latest match. | |
1538 | (defun isearch-clean-overlays () | |
1539 | (when isearch-opened-overlays | |
1540 | ;; Use a cycle instead of a mapcar here? | |
1541 | (mapcar | |
1542 | (function isearch-open-necessary-overlays) isearch-opened-overlays) | |
1543 | (setq isearch-opened-overlays nil))) | |
1544 | ||
1545 | ;;; Verify if the current match is outside of each element of | |
1546 | ;;; `isearch-opened-overlays', if so close that overlay. | |
1547 | (defun isearch-close-unecessary-overlays (begin end) | |
1548 | (let ((ov-list isearch-opened-overlays) | |
1549 | ov | |
1550 | inside-overlay | |
1551 | fct-temp) | |
1552 | (setq isearch-opened-overlays nil) | |
1553 | (while ov-list | |
1554 | (setq ov (car ov-list)) | |
1555 | (setq ov-list (cdr ov-list)) | |
1556 | (setq inside-overlay (or (and (> begin (overlay-start ov)) | |
1557 | (< begin (overlay-end ov))) | |
1558 | (and (> end (overlay-start ov)) | |
1559 | (< end (overlay-end ov))))) | |
1560 | ;; If this exists it means that the overlay was opened using | |
1561 | ;; this function, not by us tweaking the overlay properties. | |
1562 | (setq fct-temp (overlay-get ov 'isearch-open-invisible-temporary)) | |
1563 | (if inside-overlay | |
1564 | (setq isearch-opened-overlays (cons ov isearch-opened-overlays)) | |
1565 | (if fct-temp | |
1566 | (funcall fct-temp ov t) | |
1567 | (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible)) | |
1568 | (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible)) | |
1569 | (overlay-put ov 'isearch-invisible nil) | |
1570 | (overlay-put ov 'isearch-intangible nil)))))) | |
1571 | ||
86bfaffe | 1572 | (defun isearch-range-invisible (beg end) |
79c7a4fa | 1573 | "Return t if all the text from BEG to END is invisible." |
86bfaffe RS |
1574 | (and (/= beg end) |
1575 | ;; Check that invisibility runs up to END. | |
1576 | (save-excursion | |
1577 | (goto-char beg) | |
0352b205 RS |
1578 | (let |
1579 | ;; can-be-opened keeps track if we can open some overlays. | |
1580 | ((can-be-opened (eq search-invisible 'open)) | |
1581 | ;; the list of overlays that could be opened | |
1582 | (crt-overlays nil)) | |
1583 | (when (and can-be-opened isearch-hide-immediately) | |
1584 | (isearch-close-unecessary-overlays beg end)) | |
1585 | ;; If the following character is currently invisible, | |
1586 | ;; skip all characters with that same `invisible' property value. | |
1587 | ;; Do that over and over. | |
1588 | (while (and (< (point) end) | |
1589 | (let ((prop | |
1590 | (get-char-property (point) 'invisible))) | |
1591 | (if (eq buffer-invisibility-spec t) | |
1592 | prop | |
1593 | (or (memq prop buffer-invisibility-spec) | |
1594 | (assq prop buffer-invisibility-spec))))) | |
1595 | (if (get-text-property (point) 'invisible) | |
1596 | (progn | |
1597 | (goto-char (next-single-property-change (point) 'invisible | |
1598 | nil end)) | |
1599 | ;; if text is hidden by an `invisible' text property | |
1600 | ;; we cannot open it at all. | |
1601 | (setq can-be-opened nil)) | |
1602 | (unless (null can-be-opened) | |
1603 | (let ((overlays (overlays-at (point))) | |
1604 | ov-list | |
1605 | o | |
1606 | invis-prop) | |
1607 | (while overlays | |
1608 | (setq o (car overlays) | |
1609 | invis-prop (overlay-get o 'invisible)) | |
1610 | (if (or (memq invis-prop buffer-invisibility-spec) | |
1611 | (assq invis-prop buffer-invisibility-spec)) | |
1612 | (if (overlay-get o 'isearch-open-invisible) | |
1613 | (setq ov-list (cons o ov-list)) | |
1614 | ;; We found one overlay that cannot be | |
1615 | ;; opened, that means the whole chunk | |
1616 | ;; cannot be opened. | |
1617 | (setq can-be-opened nil))) | |
1618 | (setq overlays (cdr overlays))) | |
1619 | (if can-be-opened | |
1620 | ;; It makes sense to append to the open | |
1621 | ;; overlays list only if we know that this is | |
1622 | ;; t. | |
1623 | (setq crt-overlays (append ov-list crt-overlays)))) | |
1624 | (goto-char (next-overlay-change (point)))))) | |
86bfaffe | 1625 | ;; See if invisibility reaches up thru END. |
0352b205 RS |
1626 | (if (>= (point) end) |
1627 | (if (and (not (null can-be-opened)) (consp crt-overlays)) | |
1628 | (progn | |
1629 | (setq isearch-opened-overlays | |
1630 | (append isearch-opened-overlays crt-overlays)) | |
1631 | ;; maybe use a cycle instead of mapcar? | |
1632 | (mapcar (function isearch-open-overlay-temporary) | |
1633 | crt-overlays) | |
1634 | nil) | |
1635 | t)))))) | |
08200510 RS |
1636 | |
1637 | \f | |
08200510 RS |
1638 | ;;; Highlighting |
1639 | ||
b78559b0 | 1640 | (defvar isearch-overlay nil) |
08200510 | 1641 | |
b78559b0 | 1642 | (defun isearch-highlight (beg end) |
f5f6a944 | 1643 | (if (or (null search-highlight) (null window-system)) |
08200510 | 1644 | nil |
b78559b0 RS |
1645 | (or isearch-overlay (setq isearch-overlay (make-overlay beg end))) |
1646 | (move-overlay isearch-overlay beg end (current-buffer)) | |
82318db5 RS |
1647 | (overlay-put isearch-overlay 'face |
1648 | (if (internal-find-face 'isearch nil) | |
1649 | 'isearch 'region)))) | |
b78559b0 RS |
1650 | |
1651 | (defun isearch-dehighlight (totally) | |
1652 | (if isearch-overlay | |
1653 | (delete-overlay isearch-overlay))) | |
08200510 | 1654 | |
08200510 RS |
1655 | ;;; General utilities |
1656 | ||
08200510 | 1657 | |
b78559b0 RS |
1658 | (defun isearch-no-upper-case-p (string regexp-flag) |
1659 | "Return t if there are no upper case chars in STRING. | |
b7852303 | 1660 | If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\') |
b78559b0 | 1661 | since they have special meaning in a regexp." |
59057198 RS |
1662 | (let (quote-flag (i 0) (len (length string)) found) |
1663 | (while (and (not found) (< i len)) | |
1664 | (let ((char (aref string i))) | |
1665 | (if (and regexp-flag (eq char ?\\)) | |
1666 | (setq quote-flag (not quote-flag)) | |
1667 | (if (and (not quote-flag) (not (eq char (downcase char)))) | |
1668 | (setq found t)))) | |
1669 | (setq i (1+ i))) | |
1670 | (not found))) | |
08200510 | 1671 | |
b78559b0 | 1672 | ;; Portability functions to support various Emacs versions. |
8e1cae6d | 1673 | |
08200510 | 1674 | (defun isearch-char-to-string (c) |
92d874e6 | 1675 | (char-to-string c)) |
08200510 RS |
1676 | |
1677 | (defun isearch-text-char-description (c) | |
9d78ff8b RS |
1678 | (if (and (integerp c) (or (< c ?\ ) (= c ?\^?))) |
1679 | (text-char-description c) | |
1680 | (isearch-char-to-string c))) | |
08200510 | 1681 | |
bd1bd125 | 1682 | ;; General function to unread characters or events. |
99ae9e9f | 1683 | ;; Also insert them in a keyboard macro being defined. |
8f90f594 | 1684 | (defun isearch-unread (&rest char-or-events) |
99ae9e9f | 1685 | (mapcar 'store-kbd-macro-event char-or-events) |
bd1bd125 RS |
1686 | (setq unread-command-events |
1687 | (append char-or-events unread-command-events))) | |
08200510 RS |
1688 | |
1689 | (defun isearch-last-command-char () | |
1690 | ;; General function to return the last command character. | |
58451992 | 1691 | last-command-char) |
08200510 | 1692 | |
3b1e4dd1 | 1693 | ;;; isearch.el ends here |