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