Mercurial > hg > xemacs-beta
annotate lisp/isearch-mode.el @ 5402:308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
author | Mats Lidell <matsl@xemacs.org> |
---|---|
date | Thu, 14 Oct 2010 17:15:20 +0200 |
parents | da6da82d7d79 |
children | ac37a5f7e5be |
rev | line source |
---|---|
428 | 1 ;;; isearch-mode.el --- Incremental search minor mode. |
2 | |
3 ;; Copyright (C) 1992,93,94,95,96,97,98,1999 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu> | |
6 ;; Maintainer: XEmacs Development Team | |
7 ;; Keywords: extensions, dumped | |
8 | |
9 ;; This file is part of XEmacs. | |
10 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
11 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
12 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
13 ;; Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
14 ;; option) any later version. |
428 | 15 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
16 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
17 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
18 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
19 ;; for more details. |
428 | 20 |
21 ;; You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4063
diff
changeset
|
22 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
428 | 23 |
24 ;;; Synched up with: FSF 20.4. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; Instructions | |
29 | |
30 ;; Searching with isearch-mode.el should work just like isearch.el | |
31 ;; [the one from Emacs 18], except it is done in a temporary minor | |
32 ;; mode that terminates when you finish searching. | |
33 | |
34 ;; For programmed use of isearch-mode, e.g. calling (isearch-forward), | |
35 ;; isearch-mode behaves modally and does not return until the search | |
36 ;; is completed. It uses a recursive-edit to behave this way. In | |
37 ;; that case, you should still be able switch buffers, so be careful | |
38 ;; not to get things confused. | |
39 | |
40 ;; The key bindings active within isearch-mode are defined below in | |
41 ;; `isearch-mode-map' which is given bindings close to the default | |
42 ;; characters of the original isearch.el. With `isearch-mode', | |
43 ;; however, you can bind multi-character keys and it should be easier | |
44 ;; to add new commands. One bug though: keys with meta-prefix cannot | |
45 ;; be longer than two chars. Also see minibuffer-local-isearch-map | |
46 ;; for bindings active during `isearch-edit-string'. | |
47 | |
48 ;; The search ring and completion commands automatically put you in | |
49 ;; the minibuffer to edit the string. This gives you a chance to | |
50 ;; modify the search string before executing the search. There are | |
51 ;; three commands to terminate the editing: C-s and C-r exit the | |
52 ;; minibuffer and search forward and reverse respectively, while C-m | |
53 ;; exits and does a nonincremental search. | |
54 | |
55 ;; Exiting immediately from isearch uses isearch-edit-string instead | |
56 ;; of nonincremental-search, if search-nonincremental-instead is non-nil. | |
57 ;; The name of this option should probably be changed if we decide to | |
58 ;; keep the behavior. No point in forcing nonincremental search until | |
59 ;; the last possible moment. | |
60 | |
61 ;; TODO | |
62 ;; - Integrate generalized command history to isearch-edit-string. | |
63 ;; - Think about incorporating query-replace. | |
64 ;; - Hooks and options for failed search. | |
65 | |
66 ;;; Change Log: | |
67 | |
68 ;; Changes before those recorded in ChangeLog: | |
69 | |
70 ;; 20-aug-92 Hacked by jwz for Lucid Emacs 19.3. | |
71 ;; | |
72 ;; Revision 1.3 92/06/29 13:10:08 liberte | |
73 ;; Moved modal isearch-mode handling into isearch-mode. | |
74 ;; Got rid of buffer-local isearch variables. | |
75 ;; isearch-edit-string used by ring adjustments, completion, and | |
76 ;; nonincremental searching. C-s and C-r are additional exit commands. | |
77 ;; Renamed all regex to regexp. | |
78 ;; Got rid of found-start and found-point globals. | |
79 ;; Generalized handling of upper-case chars. | |
80 | |
81 ;; Revision 1.2 92/05/27 11:33:57 liberte | |
82 ;; Emacs version 19 has a search ring, which is supported here. | |
83 ;; Other fixes found in the version 19 isearch are included here. | |
84 ;; | |
85 ;; Also see variables search-caps-disable-folding, | |
86 ;; search-nonincremental-instead, search-whitespace-regexp, and | |
87 ;; commands isearch-toggle-regexp, isearch-edit-string. | |
88 ;; | |
89 ;; semi-modal isearching is supported. | |
90 | |
91 ;; Changes for 1.1 | |
92 ;; 3/18/92 Fixed invalid-regexp. | |
93 ;; 3/18/92 Fixed yanking in regexps. | |
94 | |
95 ;;; Code: | |
96 | |
97 | |
98 ;;;========================================================================= | |
99 ;;; User-accessible variables | |
100 | |
101 (defgroup isearch nil | |
102 "Incremental search minor mode." | |
103 :prefix "search-" | |
104 :group 'matching) | |
105 | |
106 | |
107 (defcustom search-exit-option t | |
108 "*Non-nil means random control characters terminate incremental search." | |
109 :type 'boolean | |
110 :group 'isearch) | |
111 | |
112 (defcustom search-slow-window-lines 1 | |
113 "*Number of lines in slow search display windows. | |
114 These are the short windows used during incremental search on slow terminals. | |
115 Negative means put the slow search window at the top (normally it's at bottom) | |
116 and the value is minus the number of lines." | |
117 :type 'integer | |
118 :group 'isearch) | |
119 | |
120 (defcustom search-slow-speed 1200 | |
121 "*Highest terminal speed at which to use \"slow\" style incremental search. | |
122 This is the style where a one-line window is created to show the line | |
123 that the search has reached." | |
124 :type 'integer | |
125 :group 'isearch) | |
126 | |
127 ;; We have `search-caps-disable-folding'. | |
128 ;(defcustom search-upper-case 'not-yanks | |
129 ; "*If non-nil, upper case chars disable case fold searching. | |
130 ;That is, upper and lower case chars must match exactly. | |
131 ;This applies no matter where the chars come from, but does not | |
132 ;apply to chars in regexps that are prefixed with `\\'. | |
133 ;If this value is `not-yanks', yanked text is always downcased." | |
134 ; :type '(choice (const :tag "off" nil) | |
135 ; (const not-yanks) | |
136 ; (other :tag "on" t)) | |
137 ; :group 'isearch) | |
138 | |
139 (defcustom search-nonincremental-instead t | |
140 "*If non-nil, do a nonincremental search instead if exiting immediately. | |
141 Actually, `isearch-edit-string' is called to let you enter the search | |
142 string, and RET terminates editing and does a nonincremental search." | |
143 :type 'boolean | |
144 :group 'isearch) | |
145 | |
146 ;; FSF default is "\\s-+", but I think our default is better so I'm | |
147 ;; leaving it. | |
148 (defcustom search-whitespace-regexp "\\(\\s-\\|[\n\r]\\)+" | |
149 "*If non-nil, regular expression to match a sequence of whitespace chars." | |
150 :type 'regexp | |
151 :group 'isearch) | |
152 | |
153 (defcustom search-highlight t | |
154 "*Whether incremental search and query-replace should highlight | |
155 the text that currently matches the search string." | |
156 :type 'boolean | |
157 :group 'isearch) | |
158 | |
159 ;; I think the name `search-highlight' makes more sense, both because | |
160 ;; of consistency with other search-* variables above, and because it | |
161 ;; also applies to query-replace. | |
162 (define-obsolete-variable-alias 'isearch-highlight 'search-highlight) | |
163 | |
164 (defcustom search-invisible 'open | |
165 "If t incremental search can match hidden text. | |
166 nil means don't match invisible text. | |
167 If the value is `open', if the text matched is made invisible by | |
168 an overlay having an `invisible' property and that overlay has a property | |
169 `isearch-open-invisible', then incremental search will show the contents. | |
170 \(This applies when using `outline.el' and `hideshow.el'.)" | |
171 :type '(choice (const :tag "Match hidden text" t) | |
172 (const :tag "Open overlays" open) | |
173 (const :tag "Don't match hidden text" nil)) | |
174 :group 'isearch) | |
175 | |
176 (defcustom isearch-hide-immediately t | |
177 "If non-nil, re-hide an invisible match right away. | |
178 This variable makes a difference when `search-invisible' is set to `open'. | |
179 It means that after search makes some invisible text visible | |
180 to show the match, it makes the text invisible again when the match moves. | |
444 | 181 Ordinarily the text becomes invisible again at the end of the search." |
182 :type 'boolean | |
428 | 183 :group 'isearch) |
184 | |
185 (defvar isearch-mode-hook nil | |
186 "Function(s) to call after starting up an incremental search.") | |
187 | |
188 (defvar isearch-mode-end-hook nil | |
189 "Function(s) to call after terminating an incremental search.") | |
190 | |
191 ;;;================================================================== | |
192 ;;; Search ring. | |
193 | |
194 (defvar search-ring nil | |
195 "List of search string sequences.") | |
196 (defvar regexp-search-ring nil | |
197 "List of regular expression search string sequences.") | |
198 | |
199 (defcustom search-ring-max 16 | |
200 "*Maximum length of search ring before oldest elements are thrown away." | |
201 :type 'integer | |
202 :group 'isearch) | |
203 (defcustom regexp-search-ring-max 16 | |
204 "*Maximum length of regexp search ring before oldest elements are thrown away." | |
205 :type 'integer | |
206 :group 'isearch) | |
207 | |
208 ;; The important difference between pre-20.4-merge yank-pointers and | |
209 ;; current code is that the yank pointers positions used to be | |
210 ;; preserved across the isearch sessions. I changed this because I | |
211 ;; think the FSF code is closer to how the feature is supposed to | |
212 ;; behave (read: to minibuffer histories.) | |
213 (defvar search-ring-yank-pointer nil | |
214 "Index in `search-ring' of last string reused. | |
215 nil if none yet.") | |
216 (defvar regexp-search-ring-yank-pointer nil | |
217 "Index in `regexp-search-ring' of last string reused. | |
218 nil if none yet.") | |
219 | |
220 (defcustom search-ring-update nil | |
221 "*Non-nil if advancing or retreating in the search ring should cause search. | |
222 Default nil means edit the string from the search ring first." | |
223 :type 'boolean | |
224 :group 'isearch) | |
225 | |
710 | 226 (defcustom isearch-mode-line-string " Isearch" |
227 "*String to display in the modeline when `isearch-mode' is active. | |
228 Set this to nil if you don't want a modeline indicator." | |
229 :type '(choice string | |
230 (const :tag "none" nil)) | |
231 :group 'isearch) | |
232 | |
428 | 233 ;;;==================================================== |
234 ;;; Define isearch-mode keymap. | |
235 | |
236 (defvar isearch-mode-map | |
237 (let ((map (make-keymap))) | |
238 (set-keymap-name map 'isearch-mode-map) | |
239 | |
240 ;; Bind all printing characters to `isearch-printing-char'. | |
241 ;; This isn't normally necessary, but if a printing character were | |
242 ;; bound to something other than self-insert-command in global-map, | |
243 ;; then it would terminate the search and be executed without this. | |
244 (let ((i 32) | |
245 (str (make-string 1 0))) | |
246 (while (< i 127) | |
247 (aset str 0 i) | |
248 (define-key map str 'isearch-printing-char) | |
249 (setq i (1+ i)))) | |
250 | |
251 ;; Here FSF sets up various kludges to handle local bindings with | |
252 ;; meta char prefix keys. We don't need isearch-other-meta-char | |
253 ;; because we handle things differently (via pre-command-hook). | |
254 | |
255 ;; Several non-printing chars change the searching behavior. | |
256 ;; | |
257 (define-key map "\C-s" 'isearch-repeat-forward) | |
258 (define-key map "\M-\C-s" 'isearch-repeat-forward) | |
259 (define-key map "\C-r" 'isearch-repeat-backward) | |
260 (define-key map "\C-g" 'isearch-abort) | |
261 | |
771 | 262 (define-key map [(meta escape)] 'isearch-cancel) |
428 | 263 |
264 (define-key map "\C-q" 'isearch-quote-char) | |
265 | |
266 (define-key map "\C-m" 'isearch-exit) | |
267 (define-key map "\C-j" 'isearch-printing-char) | |
268 (define-key map "\t" 'isearch-printing-char) | |
269 ;; I prefer our default. | |
270 ;(define-key map " " 'isearch-whitespace-chars) | |
271 (define-key map "\M- " 'isearch-whitespace-chars) | |
272 | |
273 (define-key map "\C-w" 'isearch-yank-word) | |
274 (define-key map "\C-y" 'isearch-yank-line) | |
275 (define-key map "\M-y" 'isearch-yank-kill) | |
276 | |
277 ;; Define keys for regexp chars * ? |. | |
278 ;; Nothing special for + because it matches at least once. | |
279 (define-key map "*" 'isearch-*-char) | |
280 (define-key map "?" 'isearch-*-char) | |
281 (define-key map "|" 'isearch-|-char) | |
282 | |
283 ;; delete and backspace delete backward, f1 is help, and C-h can be either | |
284 (define-key map 'delete 'isearch-delete-char) | |
285 (define-key map 'backspace 'isearch-delete-char) | |
286 (define-key map '(control h) 'isearch-help-or-delete-char) | |
287 (define-key map 'f1 'isearch-mode-help) | |
288 (define-key map 'help 'isearch-mode-help) | |
289 | |
290 (define-key map "\M-n" 'isearch-ring-advance) | |
291 (define-key map "\M-p" 'isearch-ring-retreat) | |
292 (define-key map "\M-\t" 'isearch-complete) | |
293 | |
294 ;; I find this binding somewhat unintuitive, because it doesn't | |
295 ;; work if the mouse pointer is over the echo area -- it has to be | |
296 ;; over the search window. | |
297 (define-key map 'button2 'isearch-yank-selection) | |
298 | |
299 map) | |
300 "Keymap for isearch-mode.") | |
301 | |
302 ;; Some bindings you may want to put in your isearch-mode-hook. | |
303 ;; Suggest some alternates... | |
304 ;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-case-fold) | |
305 ;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-regexp) | |
306 ;; (define-key isearch-mode-map "\C-^" 'isearch-edit-string) | |
307 | |
308 (defvar minibuffer-local-isearch-map | |
309 (let ((map (make-sparse-keymap))) | |
310 ;; #### - this should also be minor-mode-ified | |
311 (set-keymap-parents map (list minibuffer-local-map)) | |
312 (set-keymap-name map 'minibuffer-local-isearch-map) | |
313 | |
314 ;;#### This should just arrange to use the usual Emacs minibuffer histories | |
315 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer) | |
316 (define-key map "\M-n" 'isearch-ring-advance-edit) | |
317 (define-key map "\M-p" 'isearch-ring-retreat-edit) | |
318 (define-key map 'down 'isearch-ring-advance-edit) | |
319 (define-key map 'up 'isearch-ring-retreat-edit) | |
320 (define-key map "\M-\t" 'isearch-complete-edit) | |
321 (define-key map "\C-s" 'isearch-forward-exit-minibuffer) | |
322 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer) | |
323 map) | |
324 "Keymap for editing isearch strings in the minibuffer.") | |
325 | |
326 ;;;======================================================== | |
327 ;; Internal variables declared globally for byte-compiler. | |
328 ;; These are all set with setq while isearching | |
329 ;; and bound locally while editing the search string. | |
330 | |
331 (defvar isearch-forward nil) ; Searching in the forward direction. | |
332 (defvar isearch-regexp nil) ; Searching for a regexp. | |
333 (defvar isearch-word nil) ; Searching for words. | |
334 | |
335 (defvar isearch-cmds nil) ; Stack of search status sets. | |
336 (defvar isearch-string "") ; The current search string. | |
337 (defvar isearch-message "") ; text-char-description version of isearch-string | |
338 | |
339 (defvar isearch-success t) ; Searching is currently successful. | |
340 (defvar isearch-invalid-regexp nil) ; Regexp not well formed. | |
341 (defvar isearch-within-brackets nil) ; Regexp has unclosed [. | |
342 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward). | |
343 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom). | |
344 (defvar isearch-barrier 0) | |
345 (defvar isearch-just-started nil) | |
346 (defvar isearch-buffer nil) ; the buffer we've frobbed the keymap of | |
347 | |
348 (defvar isearch-case-fold-search nil) | |
349 | |
350 ;; Need this for toggling case in isearch-toggle-case-fold. When this | |
351 ;; is non-nil, the case-sensitiveness of the search is set by the | |
352 ;; user, and is may no longer be dynamically changed as per | |
353 ;; search-caps-disable-folding. | |
354 (defvar isearch-fixed-case nil) | |
355 | |
356 (defvar isearch-adjusted nil) | |
357 (defvar isearch-slow-terminal-mode nil) | |
358 ;;; If t, using a small window. | |
359 (defvar isearch-small-window nil) | |
360 (defvar isearch-opoint 0) | |
361 ;;; The window configuration active at the beginning of the search. | |
362 (defvar isearch-window-configuration nil) | |
363 (defvar isearch-selected-frame nil) | |
364 | |
365 ;; Flag to indicate a yank occurred, so don't move the cursor. | |
366 (defvar isearch-yank-flag nil) | |
367 | |
368 ;;; A function to be called after each input character is processed. | |
369 ;;; (It is not called after characters that exit the search.) | |
370 ;;; It is only set from an optional argument to `isearch-mode'. | |
371 (defvar isearch-op-fun nil) | |
372 | |
373 ;;; Is isearch-mode in a recursive edit for modal searching. | |
374 (defvar isearch-recursive-edit nil) | |
375 | |
376 ;;; Should isearch be terminated after doing one search? | |
377 (defvar isearch-nonincremental nil) | |
378 | |
379 ;; New value of isearch-forward after isearch-edit-string. | |
380 (defvar isearch-new-forward nil) | |
381 | |
382 ;; Accumulate here the extents unhidden during searching. | |
383 (defvar isearch-unhidden-extents nil) ; in FSF: isearch-opened-overlays | |
384 | |
385 | |
386 ;;;============================================================== | |
387 ;; Minor-mode-alist changes - kind of redundant with the | |
388 ;; echo area, but if isearching in multiple windows, it can be useful. | |
389 | |
710 | 390 (add-minor-mode 'isearch-mode 'isearch-mode-line-string) |
428 | 391 |
392 (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil. | |
393 (make-variable-buffer-local 'isearch-mode) | |
394 | |
395 ;; We bind these in keydefs.el. | |
396 ;(define-key global-map "\C-s" 'isearch-forward) | |
397 ;(define-key global-map "\C-r" 'isearch-backward) | |
398 ;(define-key global-map "\M-\C-s" 'isearch-forward-regexp) | |
399 ;(define-key global-map "\M-\C-r" 'isearch-backward-regexp) | |
400 | |
401 ;;;=============================================================== | |
402 ;;; Entry points to isearch-mode. | |
403 ;;; These four functions should replace those in loaddefs.el | |
404 ;;; An alternative is to defalias isearch-forward etc to isearch-mode, | |
405 ;;; and look at this-command to set the options accordingly. | |
406 | |
407 (defun isearch-forward (&optional regexp-p no-recursive-edit) | |
408 "\ | |
409 Do incremental search forward. | |
410 With a prefix argument, do an incremental regular expression search instead. | |
411 \\<isearch-mode-map> | |
412 As you type characters, they add to the search string and are found. | |
413 The following non-printing keys are bound in `isearch-mode-map'. | |
414 | |
415 Type \\[isearch-delete-char] to cancel characters from end of search string. | |
416 Type \\[isearch-exit] to exit, leaving point at location found. | |
417 Type LFD (C-j) to match end of line. | |
418 Type \\[isearch-repeat-forward] to search again forward,\ | |
419 \\[isearch-repeat-backward] to search again backward. | |
420 Type \\[isearch-yank-word] to yank word from buffer onto end of search\ | |
421 string and search for it. | |
422 Type \\[isearch-yank-line] to yank rest of line onto end of search string\ | |
423 and search for it. | |
424 Type \\[isearch-yank-kill] to yank last killed text onto end of search string\ | |
425 and search for it. | |
426 Type \\[isearch-quote-char] to quote control character to search for it. | |
427 Type \\[isearch-whitespace-chars] to match all whitespace chars in regexp. | |
428 \\[isearch-abort] while searching or when search has failed cancels input\ | |
429 back to what has | |
430 been found successfully. | |
431 \\[isearch-abort] when search is successful aborts and moves point to\ | |
2068 | 432 starting point, |
433 leaving the search string at the head of the search ring. Note that this | |
434 behavior differs from GNU Emacs, which forgets the current search string | |
435 on abort regardless of success or failure. | |
428 | 436 |
437 Also supported is a search ring of the previous 16 search strings. | |
438 Type \\[isearch-ring-advance] to search for the next item in the search ring. | |
439 Type \\[isearch-ring-retreat] to search for the previous item in the search\ | |
440 ring. | |
441 Type \\[isearch-complete] to complete the search string using the search ring. | |
442 | |
443 The above keys are bound in the isearch-mode-map. To change the keys which | |
444 are special to isearch-mode, simply change the bindings in that map. | |
445 | |
446 Other control and meta characters terminate the search | |
447 and are then executed normally (depending on `search-exit-option'). | |
448 | |
449 If this function is called non-interactively, it does not return to | |
450 the calling function until the search is done. | |
451 | |
452 The bindings, more precisely: | |
453 \\{isearch-mode-map}" | |
454 | |
455 ;; Non-standard bindings | |
456 ;; Type \\[isearch-toggle-regexp] to toggle regular expression with normal searching. | |
457 ;; Type \\[isearch-edit-string] to edit the search string in the minibuffer. | |
458 ;; Terminate editing and return to incremental searching with CR. | |
459 | |
460 (interactive "_P\np") | |
461 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit))) | |
462 | |
463 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit) | |
464 "\ | |
465 Do incremental search forward for regular expression. | |
466 With a prefix argument, do a regular string search instead. | |
467 Like ordinary incremental search except that your input | |
468 is treated as a regexp. See \\[isearch-forward] for more info." | |
469 (interactive "_P\np") | |
470 (isearch-mode t (null not-regexp) nil (not no-recursive-edit))) | |
471 | |
472 (defun isearch-backward (&optional regexp-p no-recursive-edit) | |
473 "\ | |
474 Do incremental search backward. | |
475 With a prefix argument, do a regular expression search instead. | |
476 See \\[isearch-forward] for more information." | |
477 (interactive "_P\np") | |
478 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit))) | |
479 | |
480 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit) | |
481 "\ | |
482 Do incremental search backward for regular expression. | |
483 With a prefix argument, do a regular string search instead. | |
484 Like ordinary incremental search except that your input | |
485 is treated as a regexp. See \\[isearch-forward] for more info." | |
486 (interactive "_P\np") | |
487 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit))) | |
488 | |
489 ;; The problem here is that you can't scroll the help screen; as soon | |
490 ;; as you press a key, it's gone. I don't know of a good way to fix | |
491 ;; it, though. -hniksic | |
492 (defun isearch-mode-help () | |
493 (interactive "_") | |
1601 | 494 (let ((config (current-window-configuration))) |
495 (let ((w (selected-window))) | |
496 (describe-function 'isearch-forward) | |
497 (select-window w)) | |
498 (isearch-update) | |
499 (setq isearch-window-configuration config))) | |
428 | 500 |
501 | |
502 ;;;================================================================== | |
503 ;; isearch-mode only sets up incremental search for the minor mode. | |
504 ;; All the work is done by the isearch-mode commands. | |
505 | |
506 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p) | |
507 "Start isearch minor mode. Called by `isearch-forward', etc. | |
508 | |
509 \\{isearch-mode-map}" | |
510 | |
511 (if executing-kbd-macro (setq recursive-edit nil)) | |
512 | |
513 (let ((inhibit-quit t)) ; don't leave things in an inconsistent state... | |
514 | |
515 ;; Initialize global vars. | |
516 (setq isearch-buffer (current-buffer) | |
517 isearch-forward forward | |
518 isearch-regexp regexp | |
519 isearch-word word-p | |
520 isearch-op-fun op-fun | |
521 isearch-case-fold-search case-fold-search | |
522 isearch-fixed-case nil | |
523 isearch-string "" | |
524 isearch-message "" | |
525 isearch-cmds nil | |
526 isearch-success t | |
527 isearch-wrapped nil | |
528 isearch-barrier (point) | |
529 isearch-adjusted nil | |
530 isearch-yank-flag nil | |
531 isearch-invalid-regexp nil | |
532 isearch-within-brackets nil | |
533 isearch-slow-terminal-mode (and (<= (device-baud-rate) | |
534 search-slow-speed) | |
535 (> (window-height) | |
536 (* 4 search-slow-window-lines))) | |
537 isearch-other-end nil | |
538 isearch-small-window nil | |
539 isearch-just-started t | |
540 | |
541 isearch-opoint (point) | |
542 search-ring-yank-pointer nil | |
543 regexp-search-ring-yank-pointer nil | |
440 | 544 isearch-unhidden-extents nil |
428 | 545 |
546 ;; #### What we really need is a buffer-local | |
547 ;; overriding-local-map. See isearch-pre-command-hook for | |
548 ;; more details. | |
549 overriding-local-map (progn | |
550 (set-keymap-parents isearch-mode-map | |
551 (nconc (current-minor-mode-maps) | |
552 (and (current-local-map) | |
553 (list (current-local-map))))) | |
554 isearch-mode-map) | |
555 isearch-selected-frame (selected-frame) | |
556 | |
557 ) | |
558 | |
559 ;; XEmacs change: without clearing the match data, sometimes old values | |
560 ;; of isearch-other-end get used. Don't ask me why... | |
561 (store-match-data nil) | |
562 | |
563 (add-hook 'pre-command-hook 'isearch-pre-command-hook) | |
564 | |
565 (setq isearch-mode (gettext " Isearch")) | |
566 (redraw-modeline) | |
567 | |
568 (isearch-push-state) | |
569 | |
570 ) ; inhibit-quit is t before here | |
571 | |
572 (isearch-update) | |
573 (run-hooks 'isearch-mode-hook) | |
574 | |
575 ;; isearch-mode can be made modal (in the sense of not returning to | |
576 ;; the calling function until searching is completed) by entering | |
577 ;; a recursive-edit and exiting it when done isearching. | |
578 (if recursive-edit | |
579 (let ((isearch-recursive-edit t)) | |
580 (recursive-edit))) | |
581 isearch-success) | |
582 | |
583 | |
584 ;;;==================================================== | |
585 ;; Some high level utilities. Others below. | |
586 | |
587 (defun isearch-update () | |
588 ;; Called after each command to update the display. | |
1601 | 589 (if isearch-window-configuration |
590 (progn | |
591 (set-window-configuration isearch-window-configuration) | |
592 (setq isearch-window-configuration nil))) | |
428 | 593 (if (null unread-command-events) |
594 (progn | |
595 (if (not (input-pending-p)) | |
596 (isearch-message)) | |
597 (if (and isearch-slow-terminal-mode | |
598 (not (or isearch-small-window | |
599 (pos-visible-in-window-p)))) | |
600 (let ((found-point (point))) | |
601 (setq isearch-small-window t) | |
602 (move-to-window-line 0) | |
603 (let ((window-min-height 1)) | |
604 (split-window nil (if (< search-slow-window-lines 0) | |
605 (1+ (- search-slow-window-lines)) | |
606 (- (window-height) | |
607 (1+ search-slow-window-lines))))) | |
608 (if (< search-slow-window-lines 0) | |
609 (progn (vertical-motion (- 1 search-slow-window-lines)) | |
610 (set-window-start (next-window) (point)) | |
611 (set-window-hscroll (next-window) | |
612 (window-hscroll)) | |
613 (set-window-hscroll (selected-window) 0)) | |
614 (other-window 1)) | |
615 (goto-char found-point))) | |
616 (if isearch-other-end | |
617 (if (< isearch-other-end (point)) | |
618 (isearch-highlight isearch-other-end (point)) | |
619 (isearch-highlight (point) isearch-other-end)) | |
620 (isearch-dehighlight)) | |
621 )) | |
622 (setq ;; quit-flag nil not for isearch-mode | |
623 isearch-adjusted nil | |
624 isearch-yank-flag nil) | |
625 (isearch-highlight-all-update) | |
626 ) | |
627 | |
628 | |
629 (defun isearch-done (&optional nopush edit) | |
630 ;; Called by all commands that terminate isearch-mode. | |
1601 | 631 (setq isearch-window-configuration nil) |
428 | 632 (let ((inhibit-quit t)) ; danger danger! |
633 (if (and isearch-buffer (buffer-live-p isearch-buffer)) | |
634 ;; Some loser process filter might have switched the window's | |
635 ;; buffer, so be sure to set these variables back in the | |
636 ;; buffer we frobbed them in. But only if the buffer is still | |
637 ;; alive. | |
638 (with-current-buffer isearch-buffer | |
639 (setq overriding-local-map nil) | |
640 ;; Use remove-hook instead of just setting it to our saved value | |
641 ;; in case some process filter has created a buffer and modified | |
642 ;; the pre-command-hook in that buffer... yeah, this is obscure, | |
643 ;; and yeah, I was getting screwed by it. -jwz | |
644 (remove-hook 'pre-command-hook 'isearch-pre-command-hook) | |
645 (set-keymap-parents isearch-mode-map nil) | |
646 (setq isearch-mode nil) | |
647 (redraw-modeline) | |
648 (isearch-dehighlight) | |
649 (isearch-highlight-all-cleanup) | |
650 (isearch-restore-invisible-extents nil nil) | |
651 )) | |
652 | |
653 ;; it's not critical that this be inside inhibit-quit, but leaving | |
654 ;; things in small-window-mode would be bad. | |
655 (let ((found-start (window-start (selected-window))) | |
656 (found-point (point))) | |
657 (cond ((eq (selected-frame) isearch-selected-frame) | |
658 (if isearch-small-window | |
659 (goto-char found-point) | |
660 ;; Exiting the save-window-excursion clobbers | |
661 ;; window-start; restore it. | |
662 (set-window-start (selected-window) found-start t)))) | |
663 ;; If there was movement, mark the starting position. | |
664 ;; Maybe should test difference between and set mark iff > threshold. | |
665 (if (and (buffer-live-p isearch-buffer) | |
666 (/= (point isearch-buffer) isearch-opoint)) | |
667 ;; #### FSF doesn't do this if the region is active. Should | |
668 ;; we do the same? | |
669 (progn | |
670 (push-mark isearch-opoint t nil isearch-buffer) | |
671 (or executing-kbd-macro (> (minibuffer-depth) 0) | |
672 (display-message 'command "Mark saved where search started"))))) | |
673 (setq isearch-buffer nil) | |
674 ) ; inhibit-quit is t before here | |
675 | |
676 (if (and (> (length isearch-string) 0) (not nopush)) | |
677 ;; Update the ring data. | |
678 (isearch-update-ring isearch-string isearch-regexp)) | |
679 | |
680 (run-hooks 'isearch-mode-end-hook) | |
681 | |
682 (and (not edit) isearch-recursive-edit (exit-recursive-edit))) | |
683 | |
684 (defun isearch-update-ring (string &optional regexp) | |
685 "Add STRING to the beginning of the search ring. | |
686 REGEXP says which ring to use." | |
444 | 687 (if regexp |
428 | 688 (if (or (null regexp-search-ring) |
689 (not (string= string (car regexp-search-ring)))) | |
690 (progn | |
691 (setq regexp-search-ring | |
692 (cons string regexp-search-ring)) | |
693 (if (> (length regexp-search-ring) regexp-search-ring-max) | |
694 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring) | |
695 nil)))) | |
696 (if (or (null search-ring) | |
697 (not (string= string (car search-ring)))) | |
698 (progn | |
699 (setq search-ring (cons string search-ring)) | |
700 (if (> (length search-ring) search-ring-max) | |
701 (setcdr (nthcdr (1- search-ring-max) search-ring) nil)))))) | |
702 | |
703 | |
704 ;;;==================================================== | |
705 ;; Commands active while inside of the isearch minor mode. | |
706 | |
707 (defun isearch-exit () | |
708 "Exit search normally. | |
709 However, if this is the first command after starting incremental | |
710 search and `search-nonincremental-instead' is non-nil, do a | |
711 nonincremental search instead via `isearch-edit-string'." | |
712 (interactive) | |
4063 | 713 (if (and (or search-nonincremental-instead executing-kbd-macro) |
428 | 714 (= 0 (length isearch-string))) |
715 (let ((isearch-nonincremental t) | |
716 ;; Highlighting only gets in the way of nonincremental | |
717 ;; search. | |
718 (search-highlight nil) | |
719 (isearch-highlight-all-matches nil)) | |
720 (isearch-edit-string)) | |
721 (isearch-done))) | |
722 | |
723 | |
724 (defun isearch-edit-string () | |
725 "Edit the search string in the minibuffer. | |
726 The following additional command keys are active while editing. | |
727 \\<minibuffer-local-isearch-map> | |
728 \\[exit-minibuffer] to resume incremental searching with the edited string. | |
729 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search. | |
730 \\[isearch-forward-exit-minibuffer] to resume isearching forward. | |
731 \\[isearch-reverse-exit-minibuffer] to resume isearching backward. | |
732 \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring. | |
733 \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring. | |
734 \\[isearch-complete-edit] to complete the search string using the search ring. | |
735 \\<isearch-mode-map> | |
736 If first char entered is \\[isearch-yank-word], then do word search instead." | |
737 | |
738 ;; This code is very hairy for several reasons, explained in the code. | |
739 ;; Mainly, isearch-mode must be terminated while editing and then restarted. | |
740 ;; If there were a way to catch any change of buffer from the minibuffer, | |
741 ;; this could be simplified greatly. | |
742 ;; Editing doesn't back up the search point. Should it? | |
743 (interactive) | |
744 | |
745 (condition-case nil | |
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-fixed-case isearch-fixed-case) | |
766 (isearch-yank-flag isearch-yank-flag) | |
767 (isearch-invalid-regexp isearch-invalid-regexp) | |
768 (isearch-within-brackets isearch-within-brackets) | |
769 ;;; Don't bind this. We want isearch-search, below, to set it. | |
770 ;;; And the old value won't matter after that. | |
771 ;;; (isearch-other-end isearch-other-end) | |
772 (isearch-opoint isearch-opoint) | |
773 (isearch-slow-terminal-mode isearch-slow-terminal-mode) | |
774 (isearch-small-window isearch-small-window) | |
775 (isearch-recursive-edit isearch-recursive-edit) | |
776 (isearch-selected-frame (selected-frame)) | |
777 ) | |
778 ;; Actually terminate isearching until editing is done. | |
779 ;; This is so that the user can do anything without failure, | |
780 ;; like switch buffers and start another isearch, and return. | |
781 ;; (condition-case nil | |
782 (isearch-done t t) | |
783 ;;#### What does this mean? There is no such condition! | |
784 ;; (exit nil)) ; was recursive editing | |
785 | |
786 (unwind-protect | |
787 (progn | |
788 ;; Fake the prompt message for the sake of | |
789 ;; next-command-event below. | |
790 (isearch-message) | |
791 ;; If the first character the user types when we | |
792 ;; prompt them for a string is the yank-word | |
793 ;; character, then go into word-search mode. | |
794 ;; Otherwise unread that character and read a string | |
795 ;; the normal way. | |
796 (let* ((cursor-in-echo-area t) | |
797 (event (next-command-event))) | |
798 (if (eq 'isearch-yank-word | |
799 (lookup-key isearch-mode-map (vector event))) | |
800 (setq isearch-word t;; so message-prefix is right | |
801 isearch-new-word t) | |
802 (setq unread-command-event event))) | |
803 (setq isearch-new-string | |
804 (read-from-minibuffer | |
805 (isearch-message-prefix nil isearch-nonincremental) | |
806 isearch-string | |
807 minibuffer-local-isearch-map | |
808 nil | |
809 't ;does its own history (but shouldn't) | |
810 ) | |
811 isearch-new-message (mapconcat | |
812 'isearch-text-char-description | |
813 isearch-new-string ""))) | |
814 ;; Always resume isearching by restarting it. | |
815 (isearch-mode isearch-forward | |
816 isearch-regexp | |
817 isearch-op-fun | |
818 isearch-recursive-edit | |
819 isearch-word) | |
820 | |
821 ;; Copy new values in outer locals to isearch globals | |
822 (setq isearch-string isearch-new-string | |
823 isearch-message isearch-new-message | |
824 isearch-forward isearch-new-forward | |
825 isearch-word isearch-new-word)) | |
826 | |
827 ;; Empty isearch-string means use default. | |
828 (if (= 0 (length isearch-string)) | |
829 (setq isearch-string (or (car (if isearch-regexp | |
830 regexp-search-ring | |
831 search-ring)) | |
832 "")))) | |
833 | |
834 ;; Reinvoke the pending search. | |
835 (isearch-push-state) | |
836 (isearch-search) | |
837 (isearch-update) | |
838 (if isearch-nonincremental (isearch-done))) | |
839 | |
840 (quit ; handle abort-recursive-edit | |
841 (isearch-abort) ;; outside of let to restore outside global values | |
842 ))) | |
843 | |
844 (defun isearch-nonincremental-exit-minibuffer () | |
845 (interactive) | |
846 (setq isearch-nonincremental t) | |
847 (exit-minibuffer)) | |
848 | |
849 (defun isearch-forward-exit-minibuffer () | |
850 (interactive) | |
851 (setq isearch-new-forward t) | |
852 (exit-minibuffer)) | |
853 | |
854 (defun isearch-reverse-exit-minibuffer () | |
855 (interactive) | |
856 (setq isearch-new-forward nil) | |
857 (exit-minibuffer)) | |
858 | |
859 (defun isearch-cancel () | |
860 "Terminate the search and go back to the starting point." | |
861 (interactive) | |
862 (goto-char isearch-opoint) | |
863 (isearch-done t) | |
864 (signal 'quit '(isearch))) ; and pass on quit signal | |
865 | |
866 (defun isearch-abort () | |
867 "Abort incremental search mode if searching is successful, signaling quit. | |
2068 | 868 Leave the search string at the head of the search ring. Note that this |
869 behavior differs from GNU Emacs, which forgets the current search string | |
870 on abort regardless of success or failure. Use `isearch-cancel' to abort | |
871 and forget regardless of success or failure. | |
872 | |
428 | 873 Otherwise, revert to previous successful search and continue searching. |
874 Use `isearch-exit' to quit without signaling." | |
875 (interactive) | |
876 ;; (ding) signal instead below, if quitting | |
877 (discard-input) | |
878 (if isearch-success | |
879 ;; If search is successful, move back to starting point | |
880 ;; and really do quit. | |
881 (progn (goto-char isearch-opoint) | |
882 (setq isearch-success nil) | |
2030 | 883 (isearch-done) ; exit and push target string |
428 | 884 (signal 'quit '(isearch))) ; and pass on quit signal |
885 ;; If search is failing, or has an incomplete regexp, | |
886 ;; rub out until it is once more successful. | |
887 (while (or (not isearch-success) isearch-invalid-regexp) | |
888 (isearch-pop-state)) | |
889 (isearch-update))) | |
890 | |
891 (defun isearch-repeat (direction) | |
892 ;; Utility for isearch-repeat-forward and -backward. | |
893 (if (eq isearch-forward (eq direction 'forward)) | |
894 ;; C-s in forward or C-r in reverse. | |
895 (if (equal isearch-string "") | |
896 ;; If search string is empty, use last one. | |
897 (setq isearch-string | |
898 (or (if isearch-regexp | |
899 (car regexp-search-ring) | |
900 (car search-ring)) | |
901 "") | |
902 isearch-message | |
903 (mapconcat 'isearch-text-char-description | |
904 isearch-string "")) | |
905 ;; If already have what to search for, repeat it. | |
906 (or isearch-success | |
907 (progn | |
908 (goto-char (if isearch-forward (point-min) (point-max))) | |
909 (setq isearch-wrapped t)))) | |
910 ;; C-s in reverse or C-r in forward, change direction. | |
911 (setq isearch-forward (not isearch-forward))) | |
912 | |
913 (setq isearch-barrier (point)) ; For subsequent \| if regexp. | |
914 | |
915 (if (equal isearch-string "") | |
916 (setq isearch-success t) | |
917 (if (and isearch-success (equal (match-end 0) (match-beginning 0)) | |
918 (not isearch-just-started)) | |
919 ;; If repeating a search that found | |
920 ;; an empty string, ensure we advance. | |
921 (if (if isearch-forward (eobp) (bobp)) | |
922 ;; If there's nowhere to advance to, fail (and wrap next time). | |
923 (progn | |
924 (setq isearch-success nil) | |
925 (and executing-kbd-macro | |
926 (not defining-kbd-macro) | |
927 (isearch-done)) | |
928 (ding nil 'isearch-failed)) | |
929 (forward-char (if isearch-forward 1 -1)) | |
930 (isearch-search)) | |
931 (isearch-search))) | |
932 | |
933 (isearch-push-state) | |
934 (isearch-update)) | |
935 | |
936 (defun isearch-repeat-forward () | |
937 "Repeat incremental search forwards." | |
938 (interactive) | |
939 (isearch-repeat 'forward)) | |
940 | |
941 (defun isearch-repeat-backward () | |
942 "Repeat incremental search backwards." | |
943 (interactive) | |
944 (isearch-repeat 'backward)) | |
945 | |
946 (defun isearch-toggle-regexp () | |
947 "Toggle regexp searching on or off." | |
948 ;; The status stack is left unchanged. | |
949 (interactive) | |
950 (setq isearch-regexp (not isearch-regexp)) | |
951 (if isearch-regexp (setq isearch-word nil)) | |
952 (isearch-update)) | |
953 | |
954 (defun isearch-toggle-case-fold () | |
955 "Toggle case folding in searching on or off." | |
956 (interactive) | |
957 (setq isearch-case-fold-search (if isearch-case-fold-search nil 'yes) | |
958 isearch-fixed-case t) | |
959 (lmessage 'progress "%s%s [case %ssensitive]" | |
960 (isearch-message-prefix) | |
961 isearch-message | |
962 (if isearch-case-fold-search "in" "")) | |
963 (setq isearch-adjusted t) | |
964 ;; Update the highlighting here so that it gets done before the | |
965 ;; one-second pause. | |
966 (isearch-highlight-all-update) | |
967 (sit-for 1) | |
968 (isearch-update)) | |
969 | |
970 (defun isearch-delete-char () | |
971 "Discard last input item and move point back. | |
972 If no previous match was done, just beep." | |
973 (interactive) | |
974 (if (null (cdr isearch-cmds)) | |
975 (ding nil 'isearch-quit) | |
976 (isearch-pop-state)) | |
977 (isearch-update)) | |
978 | |
979 (defun isearch-help-or-delete-char () | |
980 "Show Isearch help or delete backward in the search string. | |
981 Deletes when `delete-key-deletes-forward' is t and C-h is used for deleting | |
982 backwards." | |
983 (interactive) | |
984 (if (and delete-key-deletes-forward | |
985 (case (device-type) | |
776 | 986 (tty (eq (declare-boundp tty-erase-char) ?\C-h)) |
987 (x (not (declare-fboundp | |
988 (x-keysym-on-keyboard-sans-modifiers-p 'backspace)))))) | |
428 | 989 (isearch-delete-char) |
990 (isearch-mode-help))) | |
991 | |
992 ;; This is similar to FSF isearch-yank-string, but more general. | |
993 (defun isearch-yank (chunk) | |
994 ;; Helper for isearch-yank-* functions. CHUNK can be a string or a | |
995 ;; function. | |
996 (let ((word (if (stringp chunk) | |
997 chunk | |
998 (save-excursion | |
999 (and (not isearch-forward) isearch-other-end | |
1000 (goto-char isearch-other-end)) | |
1001 (buffer-substring | |
1002 (point) | |
1003 (progn | |
1004 (funcall chunk) | |
1005 (point))))))) | |
1006 ;; if configured so that typing upper-case characters turns off case | |
1007 ;; folding, then downcase the string so that yanking an upper-case | |
1008 ;; word doesn't mess with case-foldedness. | |
1009 (if (and search-caps-disable-folding isearch-case-fold-search) | |
1010 (setq word (downcase word))) | |
1011 (if isearch-regexp (setq word (regexp-quote word))) | |
1012 (setq isearch-string (concat isearch-string word) | |
1013 isearch-message | |
1014 (concat isearch-message | |
1015 (mapconcat 'isearch-text-char-description | |
1016 word "")) | |
1017 ;; Don't move cursor in reverse search. | |
1018 isearch-yank-flag t)) | |
1019 (isearch-search-and-update)) | |
1020 | |
1021 (defun isearch-yank-word () | |
1022 "Pull next word from buffer into search string." | |
1023 (interactive) | |
1024 (isearch-yank (function (lambda () (forward-word 1))))) | |
1025 | |
1026 (defun isearch-yank-line () | |
1027 "Pull rest of line from buffer into search string." | |
1028 (interactive) | |
1029 (isearch-yank 'end-of-line)) | |
1030 | |
1031 (defun isearch-yank-kill () | |
1032 "Pull rest of line from kill ring into search string." | |
1033 (interactive) | |
1034 (isearch-yank (current-kill 0))) | |
1035 | |
1036 (defun isearch-yank-sexp () | |
1037 "Pull next expression from buffer into search string." | |
1038 (interactive) | |
1039 (isearch-yank 'forward-sexp)) | |
1040 | |
1041 (defun isearch-yank-selection () | |
1042 "Pull the current selection into the search string." | |
1043 (interactive) | |
1044 (isearch-yank (get-selection))) | |
1045 | |
1046 (defun isearch-yank-clipboard () | |
1047 "Pull the current clipboard selection into the search string." | |
1048 (interactive) | |
1049 (isearch-yank (get-clipboard))) | |
1050 | |
1051 (defun isearch-fix-case () | |
1052 ;; The commented-out (and ...) form implies that, once | |
1053 ;; isearch-case-fold-search becomes nil due to a capital letter | |
1054 ;; typed in, it can never be restored to the original value. In | |
1055 ;; that case, it's impossible to revert a case-sensitive search back | |
1056 ;; to case-insensitive. | |
1057 (if ;(and isearch-case-fold-search search-caps-disable-folding) | |
1058 (and case-fold-search | |
1059 ;; Make sure isearch-toggle-case-fold works. | |
1060 (not isearch-fixed-case) | |
1061 search-caps-disable-folding) | |
1062 (setq isearch-case-fold-search | |
1063 (no-upper-case-p isearch-string isearch-regexp))) | |
1064 (setq isearch-mode (if case-fold-search | |
1065 (if isearch-case-fold-search | |
1066 " Isearch" ;As God Intended Mode | |
1067 " ISeARch") ;Warn about evil case via StuDLYcAps. | |
1068 " Isearch"))) | |
1069 | |
1070 (defun isearch-search-and-update () | |
1071 ;; Do the search and update the display. | |
1072 (if (and (not isearch-success) | |
1073 ;; unsuccessful regexp search may become | |
1074 ;; successful by addition of characters which | |
1075 ;; make isearch-string valid | |
1076 (not isearch-regexp)) | |
1077 nil | |
1078 ;; In reverse search, adding stuff at | |
1079 ;; the end may cause zero or many more chars to be | |
1080 ;; matched, in the string following point. | |
1081 ;; Allow all those possibilities without moving point as | |
1082 ;; long as the match does not extend past search origin. | |
1083 (if (and (not isearch-forward) (not isearch-adjusted) | |
1084 (condition-case () | |
1085 (progn | |
1086 (isearch-fix-case) | |
1087 (let ((case-fold-search isearch-case-fold-search)) | |
1088 (looking-at (if isearch-regexp isearch-string | |
1089 (regexp-quote isearch-string))))) | |
1090 (error nil)) | |
1091 (or isearch-yank-flag | |
1092 (<= (match-end 0) | |
1093 (min isearch-opoint isearch-barrier)))) | |
1094 (setq isearch-success t | |
1095 isearch-invalid-regexp nil | |
1096 isearch-within-brackets nil | |
1097 isearch-other-end (match-end 0)) | |
1098 ;; Not regexp, not reverse, or no match at point. | |
1099 (if (and isearch-other-end (not isearch-adjusted)) | |
1100 (goto-char (if isearch-forward isearch-other-end | |
1101 (min isearch-opoint | |
1102 isearch-barrier | |
1103 (1+ isearch-other-end))))) | |
1104 (isearch-search) | |
1105 )) | |
1106 (isearch-push-state) | |
1107 (if isearch-op-fun (funcall isearch-op-fun)) | |
1108 (isearch-update)) | |
1109 | |
1110 | |
1111 ;; *, ?, and | chars can make a regexp more liberal. | |
1112 ;; They can make a regexp match sooner or make it succeed instead of failing. | |
1113 ;; So go back to place last successful search started | |
1114 ;; or to the last ^S/^R (barrier), whichever is nearer. | |
1115 ;; + needs no special handling because the string must match at least once. | |
1116 | |
1117 (defun isearch-*-char () | |
1118 "Handle * and ? specially in regexps." | |
1119 (interactive) | |
1120 (if isearch-regexp | |
1121 (let ((idx (length isearch-string))) | |
1122 (while (and (> idx 0) | |
1123 (eq (aref isearch-string (1- idx)) ?\\)) | |
1124 (setq idx (1- idx))) | |
1125 (when (= (mod (- (length isearch-string) idx) 2) 0) | |
1126 (setq isearch-adjusted t) | |
1127 ;; Get the isearch-other-end from before the last search. | |
1128 ;; We want to start from there, | |
1129 ;; so that we don't retreat farther than that. | |
1130 ;; (car isearch-cmds) is after last search; | |
1131 ;; (car (cdr isearch-cmds)) is from before it. | |
1132 (let ((cs (nth 5 (car (cdr isearch-cmds))))) | |
1133 (setq cs (or cs isearch-barrier)) | |
1134 (goto-char | |
1135 (if isearch-forward | |
1136 (max cs isearch-barrier) | |
1137 (min cs isearch-barrier))))))) | |
1138 (isearch-process-search-char last-command-event)) | |
1139 | |
1140 | |
1141 | |
1142 (defun isearch-|-char () | |
1143 "If in regexp search, jump to the barrier." | |
1144 (interactive) | |
1145 (if isearch-regexp | |
1146 (progn | |
1147 (setq isearch-adjusted t) | |
1148 (goto-char isearch-barrier))) | |
1149 (isearch-process-search-char last-command-event)) | |
1150 | |
1151 ;; FSF: | |
1152 ;(defalias 'isearch-other-control-char 'isearch-other-meta-char) | |
1153 ; | |
1154 ;(defun isearch-other-meta-char () | |
1155 ;... | |
1156 ; | |
1157 | |
1158 (defun isearch-quote-char () | |
1159 "Quote special characters for incremental search." | |
1160 (interactive) | |
1161 ;; #### Here FSF does some special conversion of chars in 0200-0377 | |
1162 ;; range. Maybe we should do the same. | |
1163 (isearch-process-search-char (read-quoted-char (isearch-message t)))) | |
1164 | |
1165 (defun isearch-return-char () | |
1166 "Convert return into newline for incremental search. | |
1167 Obsolete." | |
1168 (interactive) | |
1169 (isearch-process-search-char ?\n)) | |
1170 | |
1171 (defun isearch-printing-char () | |
1172 "Add this ordinary printing character to the search string and search." | |
1173 (interactive) | |
1174 (let ((event last-command-event)) | |
1175 ;; If we are called by isearch-whitespace-chars because the | |
1176 ;; context disallows whitespace search (e.g. within brackets), | |
1177 ;; replace M-SPC with a space. FSF has similar code. | |
1178 (and (eq this-command 'isearch-whitespace-chars) | |
1179 (null (event-to-character event)) | |
1180 (setq event (character-to-event ?\ ))) | |
1181 (isearch-process-search-char event))) | |
1182 | |
1183 (defun isearch-whitespace-chars () | |
1184 "Match all whitespace chars, if in regexp mode." | |
1185 ;; FSF docstring adds: "If you want to search for just a space, type | |
1186 ;; C-q SPC." But we don't need the addition because we have a | |
1187 ;; different (better) default for the variable. | |
1188 (interactive) | |
1189 (if isearch-regexp | |
1190 (if (and search-whitespace-regexp (not isearch-within-brackets) | |
1191 (not isearch-invalid-regexp)) | |
1192 (isearch-process-search-string search-whitespace-regexp " ") | |
1193 (isearch-printing-char)) | |
1194 (progn | |
1195 ;; This way of doing word search doesn't correctly extend current search. | |
1196 ;; (setq isearch-word t) | |
1197 ;; (setq isearch-adjusted t) | |
1198 ;; (goto-char isearch-barrier) | |
1199 (isearch-printing-char)))) | |
1200 | |
1201 (defun isearch-process-search-char (char) | |
1202 ;; Append the char to the search string, update the message and re-search. | |
1203 (isearch-process-search-string (isearch-char-to-string char) | |
1204 (isearch-text-char-description char))) | |
1205 | |
1206 (defun isearch-process-search-string (string message) | |
1207 (setq isearch-string (concat isearch-string string) | |
1208 isearch-message (concat isearch-message message)) | |
1209 (isearch-search-and-update)) | |
1210 | |
1211 | |
1212 ;;=========================================================== | |
1213 ;; Search Ring | |
1214 | |
1215 (defun isearch-ring-adjust1 (advance) | |
1216 ;; Helper for isearch-ring-adjust | |
1217 (let* ((ring (if isearch-regexp regexp-search-ring search-ring)) | |
1218 (length (length ring)) | |
1219 (yank-pointer-name (if isearch-regexp | |
1220 'regexp-search-ring-yank-pointer | |
1221 'search-ring-yank-pointer)) | |
1222 (yank-pointer (eval yank-pointer-name))) | |
1223 (if (zerop length) | |
1224 () | |
1225 (set yank-pointer-name | |
1226 (setq yank-pointer | |
1227 (mod (+ (or yank-pointer 0) | |
442 | 1228 ;; XEmacs change |
1229 (if advance -1 (if yank-pointer 1 0))) | |
428 | 1230 length))) |
1231 (setq isearch-string (nth yank-pointer ring) | |
1232 isearch-message (mapconcat 'isearch-text-char-description | |
1233 isearch-string ""))))) | |
1234 | |
1235 (defun isearch-ring-adjust (advance) | |
1236 ;; Helper for isearch-ring-advance and isearch-ring-retreat | |
1237 ; (if (cdr isearch-cmds) ;; is there more than one thing on stack? | |
1238 ; (isearch-pop-state)) | |
1239 (isearch-ring-adjust1 advance) | |
1240 (if search-ring-update | |
1241 (progn | |
1242 (isearch-search) | |
1243 (isearch-update)) | |
1244 (isearch-edit-string) | |
1245 ) | |
1246 (isearch-push-state)) | |
1247 | |
1248 (defun isearch-ring-advance () | |
1249 "Advance to the next search string in the ring." | |
1250 ;; This could be more general to handle a prefix arg, but who would use it. | |
1251 (interactive) | |
1252 (isearch-ring-adjust 'advance)) | |
1253 | |
1254 (defun isearch-ring-retreat () | |
1255 "Retreat to the previous search string in the ring." | |
1256 (interactive) | |
1257 (isearch-ring-adjust nil)) | |
1258 | |
1259 (defun isearch-ring-advance-edit (n) | |
1260 "Insert the next element of the search history into the minibuffer." | |
1261 (interactive "p") | |
1262 (let* ((yank-pointer-name (if isearch-regexp | |
1263 'regexp-search-ring-yank-pointer | |
1264 'search-ring-yank-pointer)) | |
1265 (yank-pointer (eval yank-pointer-name)) | |
1266 (ring (if isearch-regexp regexp-search-ring search-ring)) | |
1267 (length (length ring))) | |
1268 (if (zerop length) | |
1269 () | |
1270 (set yank-pointer-name | |
1271 (setq yank-pointer | |
1272 (mod (- (or yank-pointer 0) n) | |
1273 length))) | |
1274 | |
1275 (erase-buffer) | |
1276 (insert (nth yank-pointer ring)) | |
1277 (goto-char (point-max))))) | |
1278 | |
1279 (defun isearch-ring-retreat-edit (n) | |
1280 "Inserts the previous element of the search history into the minibuffer." | |
1281 (interactive "p") | |
1282 (isearch-ring-advance-edit (- n))) | |
1283 | |
1284 ;; Merging note: FSF comments out these functions and implements them | |
1285 ;; differently (see above), presumably because the versions below mess | |
1286 ;; with isearch-string, while what we really want them to do is simply | |
1287 ;; to insert the correct string to the minibuffer. | |
1288 | |
1289 ;;(defun isearch-ring-adjust-edit (advance) | |
1290 ;; "Use the next or previous search string in the ring while in minibuffer." | |
1291 ;; (isearch-ring-adjust1 advance) | |
1292 ;; (erase-buffer) | |
1293 ;; (insert isearch-string)) | |
1294 | |
1295 ;;(defun isearch-ring-advance-edit () | |
1296 ;; (interactive) | |
1297 ;; (isearch-ring-adjust-edit 'advance)) | |
1298 | |
1299 ;;(defun isearch-ring-retreat-edit () | |
1300 ;; "Retreat to the previous search string in the ring while in the minibuffer." | |
1301 ;; (interactive) | |
1302 ;; (isearch-ring-adjust-edit nil)) | |
1303 | |
1304 | |
1305 (defun isearch-complete1 () | |
1306 ;; Helper for isearch-complete and isearch-complete-edit | |
1307 ;; Return t if completion OK, nil if no completion exists. | |
1308 (let* ((ring (if isearch-regexp regexp-search-ring search-ring)) | |
1309 (alist (mapcar (function (lambda (string) (list string))) ring)) | |
1310 (completion-ignore-case case-fold-search) | |
1311 (completion (try-completion isearch-string alist))) | |
1312 (cond | |
1313 ((eq completion t) | |
1314 ;; isearch-string stays the same | |
1315 t) | |
1316 ((or completion ; not nil, must be a string | |
1317 (= 0 (length isearch-string))) ; shouldn't have to say this | |
1318 (if (equal completion isearch-string) ;; no extension? | |
1319 (progn | |
1320 (if completion-auto-help | |
1321 (with-output-to-temp-buffer "*Isearch completions*" | |
1322 (display-completion-list | |
1323 (all-completions isearch-string alist)))) | |
1324 t) | |
1325 (and completion | |
1326 (setq isearch-string completion)))) | |
1327 (t | |
1328 (temp-minibuffer-message "No completion") | |
1329 nil)))) | |
1330 | |
1331 (defun isearch-complete () | |
1332 "Complete the search string from the strings on the search ring. | |
1333 The completed string is then editable in the minibuffer. | |
1334 If there is no completion possible, say so and continue searching." | |
1335 (interactive) | |
1336 (if (isearch-complete1) | |
1337 (isearch-edit-string) | |
1338 ;; else | |
1339 (sit-for 1) | |
1340 (isearch-update))) | |
1341 | |
1342 (defun isearch-complete-edit () | |
1343 "Same as `isearch-complete' except in the minibuffer." | |
1344 (interactive) | |
1345 (setq isearch-string (buffer-string)) | |
1346 (if (isearch-complete1) | |
1347 (progn | |
1348 (erase-buffer) | |
1349 (insert isearch-string)))) | |
1350 | |
1351 | |
1352 ;;;============================================================== | |
1353 ;; The search status stack. | |
1354 | |
1355 (defun isearch-top-state () | |
1356 (let ((cmd (car isearch-cmds))) | |
1357 ;; #### Grr, this is so error-prone. If you add something to | |
440 | 1358 ;; isearch-push-state, don't forget to update this. I thought I'd |
428 | 1359 ;; make a list of variables, and just do (mapcar* #'set vars |
1360 ;; values), but the (point) thing would spoil it, leaving to more | |
1361 ;; complication. | |
1362 (setq isearch-string (car cmd) | |
1363 isearch-message (car (cdr cmd)) | |
1364 isearch-success (nth 3 cmd) | |
1365 isearch-forward (nth 4 cmd) | |
1366 isearch-other-end (nth 5 cmd) | |
1367 isearch-word (nth 6 cmd) | |
1368 isearch-invalid-regexp (nth 7 cmd) | |
1369 isearch-wrapped (nth 8 cmd) | |
1370 isearch-barrier (nth 9 cmd) | |
1371 isearch-within-brackets (nth 10 cmd)) | |
1372 (goto-char (car (cdr (cdr cmd)))))) | |
1373 | |
1374 (defun isearch-pop-state () | |
1375 (pop isearch-cmds) | |
1376 (isearch-top-state) | |
1377 | |
1378 ;; Make sure isearch-case-fold-search gets the correct value. FSF | |
1379 ;; simply stores isearch-case-fold-search to isearch-cmds. We | |
1380 ;; should probably do the same. | |
1381 (isearch-fix-case) | |
1382 | |
1383 ;; Here, as well as in isearch-search we must deal with the point | |
1384 ;; landing at an invisible area which may need unhiding. | |
1385 (if (or (not (eq search-invisible 'open)) | |
1386 (not isearch-hide-immediately)) | |
1387 ;; If search-invisible is t, invisible text is just like any | |
1388 ;; other text. If it is nil, it is always skipped and we can't | |
1389 ;; land inside. In both cases, we don't need to do anything. | |
1390 ;; | |
1391 ;; Similarly, if isearch-hide-immediately is nil, needn't | |
1392 ;; re-hide the area here, and neither can we land back into a | |
1393 ;; hidden one. | |
1394 nil | |
1395 (when isearch-other-end | |
1396 ;; This will unhide the extents. | |
1397 (isearch-range-invisible (point) isearch-other-end)) | |
1398 (isearch-restore-invisible-extents (point) | |
1399 (or isearch-other-end (point))))) | |
1400 | |
1401 (defun isearch-push-state () | |
1402 (setq isearch-cmds | |
1403 (cons (list isearch-string isearch-message (point) | |
1404 isearch-success isearch-forward isearch-other-end | |
1405 isearch-word | |
1406 isearch-invalid-regexp isearch-wrapped isearch-barrier | |
1407 isearch-within-brackets) | |
1408 isearch-cmds))) | |
1409 | |
1410 | |
1411 ;;;================================================================== | |
1412 ;; Message string | |
1413 | |
1414 (defun isearch-message (&optional c-q-hack ellipsis) | |
1415 ;; Generate and print the message string. | |
1416 (let ((cursor-in-echo-area ellipsis) | |
1417 (m (concat | |
1418 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental) | |
1419 isearch-message | |
1420 (isearch-message-suffix c-q-hack ellipsis) | |
1421 ))) | |
1422 (if c-q-hack | |
1423 m | |
1424 (display-message 'progress (format "%s" m))))) | |
1425 | |
1426 (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental) | |
1427 ;; If about to search, and previous search regexp was invalid, | |
1428 ;; check that it still is. If it is valid now, | |
1429 ;; let the message we display while searching say that it is valid. | |
1430 (and isearch-invalid-regexp ellipsis | |
1431 (condition-case () | |
1432 (progn (re-search-forward isearch-string (point) t) | |
1433 (setq isearch-invalid-regexp nil | |
1434 isearch-within-brackets nil)) | |
1435 (error nil))) | |
1436 ;; If currently failing, display no ellipsis. | |
1437 (or isearch-success (setq ellipsis nil)) | |
1438 ;; #### - ! Emacs assembles strings all over the place, they can't | |
1439 ;; all be internationalized in the manner proposed below... Add an | |
1440 ;; explicit call to `gettext' and have the string snarfer pluck the | |
1441 ;; english strings out of the comment below. XEmacs is on a | |
1442 ;; purespace diet! -Stig | |
1443 | |
1444 ;; The comment below is dead and buried, but it can be rebuilt if | |
1445 ;; necessary. -hniksic | |
1446 (let ((m (concat (if isearch-success nil "failing ") | |
1447 (if (and isearch-wrapped | |
1448 (if isearch-forward | |
1449 (> (point) isearch-opoint) | |
1450 (< (point) isearch-opoint))) | |
1451 "overwrapped " | |
1452 (if isearch-wrapped "wrapped ")) | |
1453 (if isearch-word "word ") | |
1454 (if isearch-regexp "regexp ") | |
1455 (if nonincremental "search" "I-search") | |
1456 (if isearch-forward nil " backward") | |
1457 ": " | |
1458 ))) | |
1459 (aset m 0 (upcase (aref m 0))) | |
1460 (gettext m))) | |
1461 | |
1462 (defun isearch-message-suffix (&optional c-q-hack ellipsis) | |
1463 (concat (if c-q-hack "^Q" "") | |
1464 (if isearch-invalid-regexp | |
1465 (concat " [" isearch-invalid-regexp "]") | |
1466 ""))) | |
1467 | |
1468 ;;;(let ((i (logior (if isearch-success 32 0) | |
1469 ;;; (if isearch-wrapped 16 0) | |
1470 ;;; (if isearch-word 8 0) | |
1471 ;;; (if isearch-regexp 4 0) | |
1472 ;;; (if nonincremental 2 0) | |
1473 ;;; (if isearch-forward 1 0)))) | |
1474 ;;; (cond | |
1475 ;;; ((= i 63) (gettext "Wrapped word regexp search: ")) ; 111111 | |
1476 ;;; ...and so on, ad nauseam... | |
1477 ;;; ((= i 0) (gettext "Failing I-search backward: ")) ; 000000 | |
1478 ;;; (t (error "Something's rotten"))))) | |
1479 | |
1480 | |
1481 ;;;======================================================== | |
1482 ;;; Exiting | |
1483 | |
1484 (put 'isearch-printing-char 'isearch-command t) | |
1485 (put 'isearch-return-char 'isearch-command t) | |
1486 (put 'isearch-repeat-forward 'isearch-command t) | |
1487 (put 'isearch-repeat-backward 'isearch-command t) | |
1488 (put 'isearch-delete-char 'isearch-command t) | |
1489 (put 'isearch-help-or-delete-char 'isearch-command t) | |
1490 (put 'isearch-cancel 'isearch-command t) | |
1491 (put 'isearch-abort 'isearch-command t) | |
1492 (put 'isearch-quote-char 'isearch-command t) | |
1493 (put 'isearch-exit 'isearch-command t) | |
1494 (put 'isearch-printing-char 'isearch-command t) | |
1495 (put 'isearch-printing-char 'isearch-command t) | |
1496 (put 'isearch-yank-word 'isearch-command t) | |
1497 (put 'isearch-yank-line 'isearch-command t) | |
1498 (put 'isearch-yank-kill 'isearch-command t) | |
1499 (put 'isearch-yank-sexp 'isearch-command t) | |
1500 (put 'isearch-*-char 'isearch-command t) | |
1501 (put 'isearch-*-char 'isearch-command t) | |
1502 (put 'isearch-|-char 'isearch-command t) | |
1503 (put 'isearch-toggle-regexp 'isearch-command t) | |
1504 (put 'isearch-toggle-case-fold 'isearch-command t) | |
1505 (put 'isearch-edit-string 'isearch-command t) | |
1506 (put 'isearch-mode-help 'isearch-command t) | |
1507 (put 'isearch-ring-advance 'isearch-command t) | |
1508 (put 'isearch-ring-retreat 'isearch-command t) | |
1509 (put 'isearch-ring-advance-edit 'isearch-command t) | |
1510 (put 'isearch-ring-retreat-edit 'isearch-command t) | |
1511 (put 'isearch-whitespace-chars 'isearch-command t) | |
1512 (put 'isearch-complete 'isearch-command t) | |
1513 (put 'isearch-complete-edit 'isearch-command t) | |
1514 (put 'isearch-edit-string 'isearch-command t) | |
1515 (put 'isearch-toggle-regexp 'isearch-command t) | |
1516 (put 'isearch-forward-exit-minibuffer 'isearch-command t) | |
1517 (put 'isearch-reverse-exit-minibuffer 'isearch-command t) | |
1518 (put 'isearch-nonincremental-exit-minibuffer 'isearch-command t) | |
1519 (put 'isearch-yank-selection 'isearch-command t) | |
1520 (put 'isearch-yank-clipboard 'isearch-command t) | |
1521 (put 'isearch-yank-x-selection 'isearch-command t) | |
1522 (put 'isearch-yank-x-clipboard 'isearch-command t) | |
1523 | |
1524 ;; scrolling the scrollbar should not terminate isearch. | |
1525 | |
1526 ;; vertical scrollbar: | |
1527 (put 'scrollbar-line-up 'isearch-command t) | |
1528 (put 'scrollbar-line-down 'isearch-command t) | |
1529 (put 'scrollbar-page-up 'isearch-command t) | |
1530 (put 'scrollbar-page-down 'isearch-command t) | |
1531 (put 'scrollbar-to-top 'isearch-command t) | |
1532 (put 'scrollbar-to-bottom 'isearch-command t) | |
1533 (put 'scrollbar-vertical-drag 'isearch-command t) | |
1534 | |
1535 ;; horizontal scrollbar: | |
1536 (put 'scrollbar-char-left 'isearch-command t) | |
1537 (put 'scrollbar-char-right 'isearch-command t) | |
1538 (put 'scrollbar-page-left 'isearch-command t) | |
1539 (put 'scrollbar-page-right 'isearch-command t) | |
1540 (put 'scrollbar-to-left 'isearch-command t) | |
1541 (put 'scrollbar-to-right 'isearch-command t) | |
1542 (put 'scrollbar-horizontal-drag 'isearch-command t) | |
1543 | |
1544 (defun isearch-pre-command-hook () | |
1545 ;; | |
1546 ;; For use as the value of `pre-command-hook' when isearch-mode is active. | |
1547 ;; If the command about to be executed is not one of the isearch commands, | |
1548 ;; then isearch-mode is turned off before that command is executed. | |
1549 ;; | |
1550 ;; If the command about to be executed is self-insert-command, or is a | |
1551 ;; keyboard macro of a single key sequence which is bound to self-insert- | |
1552 ;; command, then we add those chars to the search ring instead of inserting | |
1553 ;; them in the buffer. In this way, the set of self-searching characters | |
1554 ;; need not be exhaustively enumerated, but is derived from other maps. | |
1555 ;; | |
1556 (cond ((not (eq (current-buffer) isearch-buffer)) | |
1557 ;; If the buffer (likely meaning "frame") has changed, bail. | |
1558 ;; This can happen if the user types something into another | |
1559 ;; frame. It can also happen if a proc filter has popped up | |
1560 ;; another buffer, which is arguably a bad thing for it to | |
1561 ;; have done, but the way in which isearch would have hosed | |
1562 ;; you in that case is unarguably even worse. -jwz | |
1563 (isearch-done) | |
1564 | |
1565 ;; `this-command' is set according to the value of | |
1566 ;; `overriding-local-map', set by isearch-mode. This is | |
1567 ;; wrong because that keymap makes sense only in isearch | |
1568 ;; buffer. To make sure the right command is called, adjust | |
1569 ;; `this-command' to the appropriate value, now that | |
1570 ;; `isearch-done' has set `overriding-local-map' to nil. | |
1571 | |
1572 ;; FSF does similar magic in `isearch-other-meta-char', which | |
1573 ;; is horribly complex. I *hope* what we do works in all | |
1574 ;; cases. | |
1575 (setq this-command (key-binding (this-command-keys)))) | |
1576 (t | |
1577 (isearch-maybe-frob-keyboard-macros) | |
1578 (if (and this-command | |
1579 (symbolp this-command) | |
1580 (get this-command 'isearch-command)) | |
1581 nil ; then continue. | |
1582 (isearch-done))))) | |
1583 | |
1584 (defun isearch-maybe-frob-keyboard-macros () | |
1585 ;; | |
1586 ;; If the command about to be executed is `self-insert-command' then change | |
1587 ;; the command to `isearch-printing-char' instead, meaning add the last- | |
1588 ;; typed character to the search string. | |
1589 ;; | |
1590 ;; If `this-command' is a string or a vector (that is, a keyboard macro) | |
1591 ;; and it contains only one command, which is bound to self-insert-command, | |
1592 ;; then do the same thing as for self-inserting commands: arrange for that | |
1593 ;; character to be added to the search string. If we didn't do this, then | |
1594 ;; typing a compose sequence (a la x-compose.el) would terminate the search | |
1595 ;; and insert the character, instead of searching for that character. | |
1596 ;; | |
1597 ;; We should continue doing this, since it's pretty much the behavior one | |
1598 ;; would expect, but it will stop being so necessary once key-translation- | |
1599 ;; map exists and is used by x-compose.el and things like it, since the | |
1600 ;; translation will have been done before we see the keys. | |
1601 ;; | |
1602 (cond ((eq this-command 'self-insert-command) | |
1603 (setq this-command 'isearch-printing-char)) | |
1604 ((and (or (stringp this-command) (vectorp this-command)) | |
1605 (eq (key-binding this-command) 'self-insert-command)) | |
1606 (setq last-command-event (character-to-event (aref this-command 0)) | |
1607 last-command-char (and (stringp this-command) | |
1608 (aref this-command 0)) | |
1609 this-command 'isearch-printing-char)) | |
1610 )) | |
1611 | |
1612 | |
1613 ;;;======================================================== | |
1614 ;;; Highlighting | |
1615 | |
1616 (defvar isearch-extent nil) | |
1617 | |
1618 ;; this face is initialized by faces.el since isearch is preloaded. | |
1619 ;(make-face 'isearch) | |
1620 | |
1621 (defun isearch-make-extent (begin end) | |
1622 (let ((x (make-extent begin end (current-buffer)))) | |
440 | 1623 ;; make the isearch extent always take precedence over any mouse- |
428 | 1624 ;; highlighted extents we may be passing through, since isearch, being |
1625 ;; modal, is more interesting (there's nothing they could do with a | |
1626 ;; mouse-highlighted extent while in the midst of a search anyway). | |
1627 (set-extent-priority x (+ mouse-highlight-priority 2)) | |
1628 (set-extent-face x 'isearch) | |
1629 (setq isearch-extent x))) | |
1630 | |
1631 (defun isearch-highlight (begin end) | |
1632 (if (null search-highlight) | |
1633 nil | |
1634 ;; make sure isearch-extent is in the current buffer | |
1635 (or (and (extentp isearch-extent) | |
1636 (extent-live-p isearch-extent)) | |
1637 (isearch-make-extent begin end)) | |
1638 (set-extent-endpoints isearch-extent begin end (current-buffer)))) | |
1639 | |
1640 ;; This used to have a TOTALLY flag that also deleted the extent. I | |
1641 ;; don't think this is necessary any longer, as isearch-highlight can | |
1642 ;; simply move the extent to another buffer. The IGNORED argument is | |
1643 ;; for the code that calls this function with an argument. --hniksic | |
1644 (defun isearch-dehighlight (&optional ignored) | |
1645 (and search-highlight | |
1646 (extentp isearch-extent) | |
1647 (extent-live-p isearch-extent) | |
1648 (detach-extent isearch-extent))) | |
1649 | |
1650 | |
1651 ;;;======================================================== | |
1652 ;;; Searching | |
1653 | |
1654 (defun isearch-search () | |
1655 ;; Do the search with the current search string. | |
1656 (isearch-message nil t) | |
1657 (isearch-fix-case) | |
1658 (condition-case lossage | |
1659 (let ((inhibit-quit nil) | |
1660 (case-fold-search isearch-case-fold-search) | |
1661 (retry t)) | |
1662 (if isearch-regexp (setq isearch-invalid-regexp nil)) | |
1663 (setq isearch-within-brackets nil) | |
1664 (while retry | |
1665 (setq isearch-success | |
1666 (funcall | |
1667 (cond (isearch-word | |
1668 (if isearch-forward | |
1669 'word-search-forward 'word-search-backward)) | |
1670 (isearch-regexp | |
1671 (if isearch-forward | |
1672 're-search-forward 're-search-backward)) | |
1673 (t | |
1674 (if isearch-forward 'search-forward 'search-backward))) | |
1675 isearch-string nil t)) | |
1676 ;; Clear RETRY unless we matched some invisible text | |
1677 ;; and we aren't supposed to do that. | |
1678 (if (or (eq search-invisible t) | |
1679 (not isearch-success) | |
1680 (bobp) (eobp) | |
1681 (= (match-beginning 0) (match-end 0)) | |
1682 (not (isearch-range-invisible | |
1683 (match-beginning 0) (match-end 0)))) | |
1684 (setq retry nil))) | |
1685 (setq isearch-just-started nil) | |
1686 (when isearch-success | |
1687 (setq isearch-other-end | |
1688 (if isearch-forward (match-beginning 0) (match-end 0))) | |
1689 (and isearch-hide-immediately | |
1690 (isearch-restore-invisible-extents (match-beginning 0) | |
1691 (match-end 0))))) | |
1692 | |
1693 (quit (setq unread-command-events (nconc unread-command-events | |
1694 (character-to-event (quit-char)))) | |
1695 (setq isearch-success nil)) | |
1696 | |
1697 (invalid-regexp | |
1698 (setq isearch-invalid-regexp (car (cdr lossage))) | |
1699 (setq isearch-within-brackets (string-match "\\`Unmatched \\[" | |
1700 isearch-invalid-regexp)) | |
1701 (if (string-match | |
1702 "\\`Premature \\|\\`Unmatched \\|\\`Invalid " | |
1703 isearch-invalid-regexp) | |
1704 (setq isearch-invalid-regexp (gettext "incomplete input")))) | |
1705 (error | |
1706 ;; stack overflow in regexp search. | |
1707 (setq isearch-invalid-regexp (car (cdr lossage))))) | |
1708 | |
1709 (if isearch-success | |
1710 nil | |
1711 | |
1712 ;; If we're being run inside a keyboard macro, then the call to | |
1713 ;; ding will signal an error (to terminate the macro). We must | |
1714 ;; turn off isearch-mode first, so that we aren't still in isearch | |
1715 ;; mode after the macro exits. Note that isearch-recursive-edit | |
1716 ;; must not be true if a keyboard macro is executing. | |
1717 (if (and executing-kbd-macro (not defining-kbd-macro)) | |
1718 (progn | |
1719 (isearch-done) | |
1720 (ding nil 'isearch-failed))) | |
1721 | |
1722 ;; Ding if failed this time after succeeding last time. | |
1723 (and (nth 3 (car isearch-cmds)) | |
1724 (ding nil 'isearch-failed)) | |
1725 (goto-char (nth 2 (car isearch-cmds))))) | |
1726 | |
1727 ;; Replaced with isearch-edit-string. | |
1728 ;(defun nonincremental-search (forward regexp) | |
1729 ;... | |
1730 | |
1731 (defun isearch-unhide-extent (extent) | |
1732 ;; Store the values for the `invisible' and `intangible' | |
1733 ;; properties, and then set them to nil. This way the text hidden | |
1734 ;; by this extent becomes visible. | |
1735 (put extent 'isearch-invisible (get extent 'invisible)) | |
1736 (put extent 'isearch-intangible (get extent 'intangible)) | |
1737 (put extent 'invisible nil) | |
1738 (put extent 'intangible nil)) | |
1739 | |
444 | 1740 (defun isearch-range-invisible (start end) |
1741 "Return t if all the text from START to END is invisible. | |
428 | 1742 Before that, if search-invisible is `open', unhide the extents with an |
1743 `isearch-open-invisible' property." | |
1744 ;; isearch-search uses this to skip the extents that are invisible, | |
1745 ;; but don't have `isearch-open-invisible' set. It is unclear | |
444 | 1746 ;; what's supposed to happen if only a part of [START, END) overlaps |
428 | 1747 ;; the extent. |
1748 (let (to-be-unhidden) | |
1749 (if (map-extents | |
1750 (lambda (extent ignored) | |
444 | 1751 (if (and (<= (extent-start-position extent) start) |
428 | 1752 (>= (extent-end-position extent) end)) |
1753 ;; All of the region is covered by the extent. | |
1754 (if (and (eq search-invisible 'open) | |
1755 (get extent 'isearch-open-invisible)) | |
1756 (progn | |
1757 (push extent to-be-unhidden) | |
1758 nil) ; keep mapping | |
1759 ;; We can't or won't unhide this extent, so we must | |
1760 ;; skip the whole match. We return from map-extents | |
1761 ;; immediately. | |
1762 t) | |
1763 ;; Else, keep looking. | |
1764 nil)) | |
444 | 1765 nil start end nil 'all-extents-closed 'invisible) |
428 | 1766 ;; The whole match must be skipped. Signal it by returning t |
1767 ;; to the caller. | |
1768 t | |
1769 ;; If any extents need to be unhidden, unhide them. | |
1770 (mapc #'isearch-unhide-extent to-be-unhidden) | |
1771 ;; Will leave this assert for some time, to catch bugs. | |
1772 (assert (null (intersection to-be-unhidden isearch-unhidden-extents))) | |
1773 (setq isearch-unhidden-extents (nconc to-be-unhidden | |
1774 isearch-unhidden-extents)) | |
1775 nil))) | |
1776 | |
1777 (defun isearch-restore-extent (extent) | |
1778 (put extent 'invisible (get extent 'isearch-invisible)) | |
1779 (put extent 'intangible (get extent 'isearch-intangible)) | |
1780 (remprop extent 'isearch-invisible) | |
1781 (remprop extent 'isearch-intangible)) | |
1782 | |
1783 ;; FSF calls this function `isearch-clean-overlays'. | |
444 | 1784 (defun isearch-restore-invisible-extents (start end) |
428 | 1785 (cond |
444 | 1786 ((null start) |
428 | 1787 ;; Delete all -- this is called at the end of isearch. |
1788 (mapc #'isearch-restore-extent isearch-unhidden-extents) | |
1789 (setq isearch-unhidden-extents nil)) | |
1790 (t | |
1791 ;; Extents that do not overlap the match area can be safely | |
1792 ;; restored to their hidden state. | |
1793 (setq isearch-unhidden-extents | |
1794 (delete-if (lambda (extent) | |
444 | 1795 (unless (extent-in-region-p extent start end |
428 | 1796 'all-extents-closed) |
1797 (isearch-restore-extent extent) | |
1798 t)) | |
1799 isearch-unhidden-extents))))) | |
1800 | |
1801 (defun isearch-no-upper-case-p (string) | |
1802 "Return t if there are no upper case chars in string. | |
1803 But upper case chars preceded by \\ do not count since they | |
1804 have special meaning in a regexp." | |
1805 ;; this incorrectly returns t for "\\\\A" | |
1806 (let ((case-fold-search nil)) | |
1807 (not (string-match "\\(^\\|[^\\]\\)[A-Z]" string)))) | |
1808 (make-obsolete 'isearch-no-upper-case-p 'no-upper-case-p) | |
1809 | |
1810 ;; Portability functions to support various Emacs versions. | |
1811 | |
1812 (defun isearch-char-to-string (c) | |
1813 (if (eventp c) | |
2828 | 1814 (make-string 1 (event-to-character c)) |
428 | 1815 (make-string 1 c))) |
1816 | |
1817 ;(defun isearch-text-char-description (c) | |
1818 ; (isearch-char-to-string c)) | |
1819 | |
1820 (define-function 'isearch-text-char-description 'text-char-description) | |
1821 | |
1822 ;; Used by etags.el and info.el | |
1823 (defmacro with-caps-disable-folding (string &rest body) "\ | |
1824 Eval BODY with `case-fold-search' let to nil if STRING contains | |
1825 uppercase letters and `search-caps-disable-folding' is t." | |
1826 `(let ((case-fold-search | |
1827 (if (and case-fold-search search-caps-disable-folding) | |
1828 (isearch-no-upper-case-p ,string) | |
1829 case-fold-search))) | |
1830 ,@body)) | |
1831 (make-obsolete 'with-caps-disable-folding 'with-search-caps-disable-folding) | |
1832 (put 'with-caps-disable-folding 'lisp-indent-function 1) | |
1833 (put 'with-caps-disable-folding 'edebug-form-spec '(form body)) | |
1834 | |
1835 | |
1836 ;;;======================================================== | |
1837 ;;; Advanced highlighting | |
1838 | |
1839 ;; When active, *every* visible match for the current search string is | |
1840 ;; highlighted: the current one using the normal isearch match color | |
1841 ;; and all the others using the `isearch-secondary' face. The extra | |
1842 ;; highlighting makes it easier to anticipate where the cursor will | |
1843 ;; land each time you press C-s or C-r to repeat a pending search. | |
1844 ;; Only the matches visible at any point are highlighted -- when you | |
1845 ;; move through the buffer, the highlighting is readjusted. | |
1846 | |
1847 ;; This is based on ideas from Bob Glickstein's `ishl' package. It | |
1848 ;; has been merged with XEmacs by Darryl Okahata, and then completely | |
1849 ;; rewritten by Hrvoje Niksic. | |
1850 | |
1851 ;; The code makes the following assumptions about the rest of this | |
1852 ;; file, so be careful when modifying it. | |
1853 | |
1854 ;; * `isearch-highlight-all-update' should get called when the search | |
1855 ;; string changes, or when the search advances. This is done from | |
1856 ;; `isearch-update'. | |
1857 ;; * `isearch-highlight-all-cleanup' should get called when the search | |
1858 ;; is done. This is performed in `isearch-done'. | |
1859 ;; * `isearch-string' is expected to contain the current search string | |
1860 ;; as entered by the user. | |
1861 ;; * `isearch-opoint' is expected to contain the location where the | |
1862 ;; current search began. | |
1863 ;; * the type of the current search is expected to be given by | |
1864 ;; `isearch-word' and `isearch-regexp'. | |
1865 ;; * the variable `isearch-invalid-regexp' is expected to be true iff | |
1866 ;; `isearch-string' is an invalid regexp. | |
1867 | |
1868 (defcustom isearch-highlight-all-matches search-highlight | |
1869 "*Non-nil means highlight all visible matches." | |
1870 :type 'boolean | |
1871 :group 'isearch) | |
1872 | |
1873 ;; We can't create this face here, as isearch.el is preloaded. | |
1874 ;; #### Think up a better name for this! | |
1875 ;(defface isearch-secondary '((t (:foreground "red3"))) | |
1876 ; "Face to use for highlighting all matches." | |
1877 ; :group 'isearch) | |
1878 | |
1879 (defvar isearch-highlight-extents nil) | |
1880 (defvar isearch-window-start nil) | |
1881 (defvar isearch-window-end nil) | |
1882 ;; We compare isearch-string and isearch-case-fold-search to saved | |
1883 ;; values for better efficiency. | |
1884 (defvar isearch-highlight-last-string nil) | |
1885 (defvar isearch-highlight-last-case-fold-search nil) | |
1886 (defvar isearch-highlight-last-regexp nil) | |
1887 | |
1888 (defun isearch-delete-extents-in-range (start end) | |
1889 ;; Delete all highlighting extents that overlap [START, END). | |
1890 (setq isearch-highlight-extents | |
1891 (delete-if (lambda (extent) | |
1892 (when (extent-in-region-p extent start end) | |
1893 (delete-extent extent) | |
1894 t)) | |
1895 isearch-highlight-extents))) | |
1896 | |
1897 (defun isearch-highlight-all-cleanup () | |
1898 ;; Stop lazily highlighting and remove extra highlighting from | |
1899 ;; buffer. | |
1900 (mapc #'delete-extent isearch-highlight-extents) | |
1901 (setq isearch-highlight-extents nil) | |
442 | 1902 (setq isearch-window-end nil |
428 | 1903 isearch-highlight-last-string nil)) |
1904 | |
1905 (defun isearch-highlight-all-update () | |
1906 ;; Update the highlighting if necessary. This needs to check if the | |
1907 ;; search string has changed, or if the window has changed position | |
1908 ;; in the buffer. | |
1909 (let ((need-start-over nil)) | |
1910 ;; NB: we don't check for isearch-success because if the point is | |
1911 ;; after the last match, the search can be unsuccessful, and yet | |
1912 ;; there are things to highlight. | |
1913 (cond ((not isearch-highlight-all-matches)) | |
1914 ((or (equal isearch-string "") | |
1915 isearch-invalid-regexp) | |
1916 (isearch-highlight-all-cleanup)) | |
1917 ((not (eq isearch-case-fold-search | |
1918 isearch-highlight-last-case-fold-search)) | |
1919 ;; This case is usually caused by search string being | |
1920 ;; changed, which would be caught below, but it can also be | |
1921 ;; tripped using isearch-toggle-case-fold. | |
1922 (setq need-start-over t)) | |
1923 ((not (eq isearch-regexp isearch-highlight-last-regexp)) | |
1924 ;; Ditto for isearch-toggle-regexp. | |
1925 (setq need-start-over t)) | |
1926 ((equal isearch-string isearch-highlight-last-string) | |
1927 ;; The search string is the same. We need to do something | |
1928 ;; if our position has changed. | |
1929 | |
1930 ;; It would be nice if we didn't have to do this; however, | |
1931 ;; window-start doesn't support a GUARANTEE flag, so we must | |
440 | 1932 ;; force redisplay to get the correct value for start and end |
428 | 1933 ;; of window. |
1934 (sit-for 0) | |
1935 | |
1936 ;; Check whether our location has changed. | |
1937 (let ((start (window-start)) | |
1938 (end (min (window-end) (point-max)))) | |
1939 (cond ((and (= start isearch-window-start) | |
1940 (= end isearch-window-end)) | |
1941 ;; Our position is unchanged -- do nothing. | |
1942 ) | |
1943 ((and (> start isearch-window-start) | |
1944 (> end isearch-window-end) | |
1945 (<= start isearch-window-end)) | |
1946 ;; We've migrated downward, but we overlap the old | |
1947 ;; region. Delete the old non-overlapping extents | |
1948 ;; and fill in the rest. | |
1949 (isearch-delete-extents-in-range isearch-window-start start) | |
1950 (isearch-highlightify-region isearch-window-end end) | |
1951 (setq isearch-window-start start | |
1952 isearch-window-end end)) | |
1953 ((and (<= start isearch-window-start) | |
1954 (<= end isearch-window-end) | |
1955 (> end isearch-window-start)) | |
1956 ;; We've migrated upward, but we overlap the old | |
1957 ;; region. Delete the old non-overlapping extents | |
1958 ;; and fill in the rest. | |
1959 (isearch-delete-extents-in-range | |
1960 end isearch-window-end) | |
1961 (isearch-highlightify-region start isearch-window-start) | |
1962 (setq isearch-window-start start | |
1963 isearch-window-end end)) | |
1964 (t | |
1965 ;; The regions don't overlap, or they overlap in a | |
1966 ;; weird way. | |
1967 (setq need-start-over t))))) | |
1968 (t | |
1969 ;; The search string has changed. | |
1970 | |
1971 ;; If more input is pending, don't start over because | |
1972 ;; starting over forces redisplay, and that slows down | |
1973 ;; typing. | |
1974 (unless (input-pending-p) | |
1975 (setq need-start-over t)))) | |
1976 (when need-start-over | |
1977 ;; Force redisplay before removing the old extents, in order to | |
1978 ;; avoid flicker. | |
1979 (sit-for 0) | |
1980 (isearch-highlight-all-cleanup) | |
1981 (setq isearch-window-start (window-start) | |
1982 isearch-window-end (min (window-end) (point-max))) | |
1983 (isearch-highlightify-region isearch-window-start isearch-window-end)) | |
1984 | |
1985 (setq isearch-highlight-last-string isearch-string | |
1986 isearch-highlight-last-case-fold-search isearch-case-fold-search | |
1987 isearch-highlight-last-regexp isearch-regexp))) | |
1988 | |
1989 (defun isearch-highlight-advance (string forwardp) | |
1990 ;; Search ahead for the next or previous match. This is the same as | |
1991 ;; isearch-search, but without the extra baggage. Maybe it should | |
1992 ;; be in a separate function. | |
1993 (let ((case-fold-search isearch-case-fold-search)) | |
1994 (funcall (cond (isearch-word (if forwardp | |
1995 'word-search-forward | |
1996 'word-search-backward)) | |
1997 (isearch-regexp (if forwardp | |
1998 're-search-forward | |
1999 're-search-backward)) | |
2000 (t (if forwardp | |
2001 'search-forward | |
2002 'search-backward))) | |
2003 string nil t))) | |
2004 | |
2005 (defun isearch-highlightify-region (start end) | |
2006 ;; Highlight all occurrences of isearch-string between START and | |
2007 ;; END. To do this right, we have to search forward as long as | |
2008 ;; there are matches that overlap [START, END), and then search | |
2009 ;; backward the same way. | |
2010 (save-excursion | |
2011 (goto-char isearch-opoint) | |
2012 (let ((lastpoint (point))) | |
2013 (while (and (isearch-highlight-advance isearch-string t) | |
2014 (/= lastpoint (point)) | |
2015 (< (match-beginning 0) end)) | |
2016 (let ((extent (make-extent (match-beginning 0) | |
2017 (match-end 0)))) | |
2018 (set-extent-priority extent (1+ mouse-highlight-priority)) | |
2019 (put extent 'face 'isearch-secondary) | |
2020 (push extent isearch-highlight-extents)) | |
2021 (setq lastpoint (point)))) | |
2022 (goto-char isearch-opoint) | |
2023 (let ((lastpoint (point))) | |
2024 (while (and (isearch-highlight-advance isearch-string nil) | |
2025 (/= lastpoint (point)) | |
2026 (>= (match-end 0) start)) | |
2027 (let ((extent (make-extent (match-beginning 0) | |
2028 (match-end 0)))) | |
2029 (set-extent-priority extent (1+ mouse-highlight-priority)) | |
2030 (put extent 'face 'isearch-secondary) | |
2031 (push extent isearch-highlight-extents)) | |
2032 (setq lastpoint (point)))))) | |
2033 | |
2034 ;;; isearch-mode.el ends here |