mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
isearch.el
3362 lines (2971 loc) · 137 KB
/
isearch.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; isearch.el --- incremental search minor mode -*- lexical-binding: t -*-
;; Copyright (C) 1992-1997, 1999-2019 Free Software Foundation, Inc.
;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
;; Maintainer: emacs-devel@gnu.org
;; Keywords: matching
;; Package: emacs
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Instructions
;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
;; isearch-mode behaves modally and does not return until the search
;; is completed. It uses a recursive-edit to behave this way.
;; The key bindings active within isearch-mode are defined below in
;; `isearch-mode-map'. Also see minibuffer-local-isearch-map
;; for bindings active during `isearch-edit-string'.
;; isearch-mode should work even if you switch windows with the mouse,
;; in which case isearch-mode is terminated automatically before the
;; switch.
;; The search ring and completion commands automatically put you in
;; the minibuffer to edit the string. This gives you a chance to
;; modify the search string before executing the search. There are
;; three commands to terminate the editing: C-s and C-r exit the
;; minibuffer and search forward and reverse respectively, while C-m
;; exits and searches in the last search direction.
;; Exiting immediately from isearch uses isearch-edit-string instead
;; of nonincremental-search, if search-nonincremental-instead is non-nil.
;; The name of this option should probably be changed if we decide to
;; keep the behavior. No point in forcing nonincremental search until
;; the last possible moment.
;;; Code:
(eval-when-compile (require 'cl-lib))
;; Some additional options and constants.
(defgroup isearch nil
"Incremental search minor mode."
:link '(emacs-commentary-link "isearch")
:link '(custom-manual "(emacs)Incremental Search")
:prefix "isearch-"
:prefix "search-"
:group 'matching)
(defcustom search-exit-option t
"Non-nil means random control characters terminate incremental search."
:type 'boolean)
(defcustom search-slow-window-lines 1
"Number of lines in slow search display windows.
These are the short windows used during incremental search on slow terminals.
Negative means put the slow search window at the top (normally it's at bottom)
and the value is minus the number of lines."
:type 'integer)
(defcustom search-slow-speed 1200
"Highest terminal speed at which to use \"slow\" style incremental search.
This is the style where a one-line window is created to show the line
that the search has reached."
:type 'integer)
(defcustom search-upper-case 'not-yanks
"If non-nil, upper case chars disable case fold searching.
That is, upper and lower case chars must match exactly.
This applies no matter where the chars come from, but does not
apply to chars in regexps that are prefixed with `\\'.
If this value is `not-yanks', text yanked into the search string
in Isearch mode is always downcased."
:type '(choice (const :tag "off" nil)
(const not-yanks)
(other :tag "on" t)))
(defcustom search-nonincremental-instead t
"If non-nil, do a nonincremental search instead of exiting immediately.
This affects the behavior of `isearch-exit' and any key bound to that
command: if this variable is nil, `isearch-exit' always exits the search;
if the value is non-nil, and the search string is empty, `isearch-exit'
starts a nonincremental search instead. (Actually, `isearch-edit-string'
is called to let you enter the search string, and RET terminates editing
and does a nonincremental search.)"
:type 'boolean)
(defcustom search-whitespace-regexp (purecopy "\\s-+")
"If non-nil, regular expression to match a sequence of whitespace chars.
When you enter a space or spaces in the incremental search, it
will match any sequence matched by this regexp. As an exception,
spaces are treated normally in regexp incremental search if they
occur in a regexp construct like [...] or *, + or ?.
If the value is a string, it applies to both ordinary and
regexp incremental search. If the value is nil, or
`isearch-lax-whitespace' is nil for ordinary incremental search, or
`isearch-regexp-lax-whitespace' is nil for regexp incremental search,
then each space you type matches literally, against one space.
You might want to use something like \"[ \\t\\r\\n]+\" instead.
In the Customization buffer, that is `[' followed by a space,
a tab, a carriage return (control-M), a newline, and `]+'."
:type '(choice (const :tag "Match Spaces Literally" nil)
regexp)
:version "24.3")
(defcustom search-invisible 'open
"If t incremental search/query-replace can match hidden text.
A nil value means don't match invisible text.
When the value is `open', if the text matched is made invisible by
an overlay having a non-nil `invisible' property, and that overlay
has a non-nil property `isearch-open-invisible', then incremental
search will show the hidden text. (This applies when using `outline.el'
and `hideshow.el'.)
To temporarily change the value for an active incremental search,
use \\<isearch-mode-map>\\[isearch-toggle-invisible].
See also the related option `isearch-hide-immediately'.
See also `reveal-mode' if you want overlays to automatically be opened
whenever point is in one of them."
:type '(choice (const :tag "Match hidden text" t)
(const :tag "Open overlays" open)
(const :tag "Don't match hidden text" nil)))
(defcustom isearch-hide-immediately t
"If non-nil, re-hide an invisible match right away.
This variable makes a difference when `search-invisible' is set to `open'.
If non-nil, invisible matches are re-hidden as soon as the match moves
off the invisible text surrounding the match.
If nil then do not re-hide opened invisible text when the match moves.
Whatever the value, all opened invisible text is hidden again after exiting
the search, with the exception of the last successful match, if any."
:type 'boolean)
(defcustom isearch-resume-in-command-history nil
"If non-nil, `isearch-resume' commands are added to the command history.
This allows you to resume earlier Isearch sessions through the
command history."
:type 'boolean)
(defvar isearch-mode-hook nil
"Function(s) to call after starting up an incremental search.")
(defvar isearch-update-post-hook nil
"Function(s) to call after isearch has found matches in the buffer.")
(defvar isearch-mode-end-hook nil
"Function(s) to call after terminating an incremental search.
When these functions are called, `isearch-mode-end-hook-quit'
is non-nil if the user quits the search.")
(defvar isearch-mode-end-hook-quit nil
"Non-nil while running `isearch-mode-end-hook' if the user quits the search.")
(defvar isearch-message-function nil
"Function to call to display the search prompt.
If nil, use function `isearch-message'.")
(defvar isearch-wrap-function nil
"Function to call to wrap the search when search is failed.
If nil, move point to the beginning of the buffer for a forward search,
or to the end of the buffer for a backward search.")
(defvar isearch-push-state-function nil
"Function to save a function restoring the mode-specific Isearch state
to the search status stack.")
(defvar isearch-filter-predicate #'isearch-filter-visible
"Predicate to filter hits of Isearch and replace commands.
Isearch hits that don't satisfy the predicate will be skipped.
The value should be a function of two arguments; it will be
called with the the positions of the start and the end of the
text matched by Isearch and replace commands. If this function
returns nil, Isearch and replace commands will continue searching
without stopping at resp. replacing this match.
If you use `add-function' to modify this variable, you can use the
`isearch-message-prefix' advice property to specify the prefix string
displayed in the search message.")
;; Search ring.
(defvar search-ring nil
"List of search string sequences.")
(defvar regexp-search-ring nil
"List of regular expression search string sequences.")
(defcustom search-ring-max 16
"Maximum length of search ring before oldest elements are thrown away."
:type 'integer)
(defcustom regexp-search-ring-max 16
"Maximum length of regexp search ring before oldest elements are thrown away."
:type 'integer)
(defvar search-ring-yank-pointer nil
"Index in `search-ring' of last string reused.
It is nil if none yet.")
(defvar regexp-search-ring-yank-pointer nil
"Index in `regexp-search-ring' of last string reused.
It is nil if none yet.")
(defcustom search-ring-update nil
"Non-nil if advancing or retreating in the search ring should cause search.
Default value, nil, means edit the string instead."
:type 'boolean)
(autoload 'char-fold-to-regexp "char-fold")
(defcustom search-default-mode nil
"Default mode to use when starting isearch.
Value is nil, t, or a function.
If nil, default to literal searches (note that `case-fold-search'
and `isearch-lax-whitespace' may still be applied).\\<isearch-mode-map>
If t, default to regexp searches (as if typing `\\[isearch-toggle-regexp]' during
isearch).
If a function, use that function as an `isearch-regexp-function'.
Example functions (and the keys to toggle them during isearch)
are `word-search-regexp' \(`\\[isearch-toggle-word]'), `isearch-symbol-regexp'
\(`\\[isearch-toggle-symbol]'), and `char-fold-to-regexp' \(`\\[isearch-toggle-char-fold]')."
;; :type is set below by `isearch-define-mode-toggle'.
:type '(choice (const :tag "Literal search" nil)
(const :tag "Regexp search" t)
(function :tag "Other"))
:version "25.1")
;;; isearch highlight customization.
(defcustom search-highlight t
"Non-nil means incremental search highlights the current match."
:type 'boolean)
(defface isearch
'((((class color) (min-colors 88) (background light))
;; The background must not be too dark, for that means
;; the character is hard to see when the cursor is there.
(:background "magenta3" :foreground "lightskyblue1"))
(((class color) (min-colors 88) (background dark))
(:background "palevioletred2" :foreground "brown4"))
(((class color) (min-colors 16))
(:background "magenta4" :foreground "cyan1"))
(((class color) (min-colors 8))
(:background "magenta4" :foreground "cyan1"))
(t (:inverse-video t)))
"Face for highlighting Isearch matches."
:group 'isearch
:group 'basic-faces)
(defvar isearch-face 'isearch)
(defface isearch-fail
'((((class color) (min-colors 88) (background light))
(:background "RosyBrown1"))
(((class color) (min-colors 88) (background dark))
(:background "red4"))
(((class color) (min-colors 16))
(:background "red"))
(((class color) (min-colors 8))
(:background "red"))
(((class color grayscale))
:foreground "grey")
(t (:inverse-video t)))
"Face for highlighting failed part in Isearch echo-area message."
:version "23.1")
(defcustom isearch-lazy-highlight t
"Controls the lazy-highlighting during incremental search.
When non-nil, all text in the buffer matching the current search
string is highlighted lazily (see `lazy-highlight-initial-delay'
and `lazy-highlight-interval').
When multiple windows display the current buffer, the
highlighting is displayed only on the selected window, unless
this variable is set to the symbol `all-windows'."
:type '(choice boolean
(const :tag "On, and applied to all windows" all-windows))
:group 'lazy-highlight
:group 'isearch)
;;; Lazy highlight customization.
(defgroup lazy-highlight nil
"Lazy highlighting feature for matching strings."
:prefix "lazy-highlight-"
:version "21.1"
:group 'isearch
:group 'matching)
(define-obsolete-variable-alias 'isearch-lazy-highlight-cleanup
'lazy-highlight-cleanup
"22.1")
(defcustom lazy-highlight-cleanup t
"Controls whether to remove extra highlighting after a search.
If this is nil, extra highlighting can be \"manually\" removed with
\\[lazy-highlight-cleanup]."
:type 'boolean
:group 'lazy-highlight)
(define-obsolete-variable-alias 'isearch-lazy-highlight-initial-delay
'lazy-highlight-initial-delay
"22.1")
(defcustom lazy-highlight-initial-delay 0.25
"Seconds to wait before beginning to lazily highlight all matches."
:type 'number
:group 'lazy-highlight)
(define-obsolete-variable-alias 'isearch-lazy-highlight-interval
'lazy-highlight-interval
"22.1")
(defcustom lazy-highlight-interval 0 ; 0.0625
"Seconds between lazily highlighting successive matches."
:type 'number
:group 'lazy-highlight)
(define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
'lazy-highlight-max-at-a-time
"22.1")
(defcustom lazy-highlight-max-at-a-time nil ; 20 (bug#25751)
"Maximum matches to highlight at a time (for `lazy-highlight').
Larger values may reduce Isearch's responsiveness to user input;
smaller values make matches highlight slowly.
A value of nil means highlight all matches shown on the screen."
:type '(choice (const :tag "All" nil)
(integer :tag "Some"))
:group 'lazy-highlight)
(defface lazy-highlight
'((((class color) (min-colors 88) (background light))
(:background "paleturquoise"))
(((class color) (min-colors 88) (background dark))
(:background "paleturquoise4"))
(((class color) (min-colors 16))
(:background "turquoise3"))
(((class color) (min-colors 8))
(:background "turquoise3"))
(t (:underline t)))
"Face for lazy highlighting of matches other than the current one."
:group 'lazy-highlight
:group 'basic-faces)
;; Define isearch help map.
(defvar isearch-help-map
(let ((map (make-sparse-keymap)))
(define-key map (char-to-string help-char) 'isearch-help-for-help)
(define-key map [help] 'isearch-help-for-help)
(define-key map [f1] 'isearch-help-for-help)
(define-key map "?" 'isearch-help-for-help)
(define-key map "b" 'isearch-describe-bindings)
(define-key map "k" 'isearch-describe-key)
(define-key map "m" 'isearch-describe-mode)
(define-key map "q" 'help-quit)
map)
"Keymap for characters following the Help key for Isearch mode.")
(eval-when-compile (require 'help-macro))
(make-help-screen isearch-help-for-help-internal
(purecopy "Type a help option: [bkm] or ?")
"You have typed %THIS-KEY%, the help character. Type a Help option:
\(Type \\<help-map>\\[help-quit] to exit the Help command.)
b Display all Isearch key bindings.
k KEYS Display full documentation of Isearch key sequence.
m Display documentation of Isearch mode.
You can't type here other help keys available in the global help map,
but outside of this help window when you type them in Isearch mode,
they exit Isearch mode before displaying global help."
isearch-help-map)
(defvar isearch--display-help-action '(nil (inhibit-same-window . t)))
(defun isearch-help-for-help ()
"Display Isearch help menu."
(interactive)
(let ((display-buffer-overriding-action isearch--display-help-action))
(isearch-help-for-help-internal))
(isearch-update))
(defun isearch-describe-bindings ()
"Show a list of all keys defined in Isearch mode, and their definitions.
This is like `describe-bindings', but displays only Isearch keys."
(interactive)
(let ((display-buffer-overriding-action isearch--display-help-action))
(with-help-window "*Help*"
(with-current-buffer standard-output
(princ "Isearch Mode Bindings:\n")
(princ (substitute-command-keys "\\{isearch-mode-map}"))))))
(defun isearch-describe-key ()
"Display documentation of the function invoked by isearch key."
(interactive)
(let ((display-buffer-overriding-action isearch--display-help-action))
(call-interactively 'describe-key))
(isearch-update))
(defun isearch-describe-mode ()
"Display documentation of Isearch mode."
(interactive)
(let ((display-buffer-overriding-action isearch--display-help-action))
(describe-function 'isearch-forward))
(isearch-update))
(defalias 'isearch-mode-help 'isearch-describe-mode)
;; Define isearch-mode keymap.
(defvar isearch-mode-map
(let ((i 0)
(map (make-keymap)))
(or (char-table-p (nth 1 map))
(error "The initialization of isearch-mode-map must be updated"))
;; Make all multibyte characters search for themselves.
(set-char-table-range (nth 1 map) (cons #x100 (max-char))
'isearch-printing-char)
;; Single-byte printing chars extend the search string by default.
(setq i ?\s)
(while (< i 256)
(define-key map (vector i) 'isearch-printing-char)
(setq i (1+ i)))
;; To handle local bindings with meta char prefix keys, define
;; another full keymap. This must be done for any other prefix
;; keys as well, one full keymap per char of the prefix key. It
;; would be simpler to disable the global keymap, and/or have a
;; default local key binding for any key not otherwise bound.
(let ((meta-map (make-sparse-keymap)))
(define-key map (char-to-string meta-prefix-char) meta-map))
;; Several non-printing chars change the searching behavior.
(define-key map "\C-s" 'isearch-repeat-forward)
(define-key map "\C-r" 'isearch-repeat-backward)
;; Define M-C-s and M-C-r like C-s and C-r so that the same key
;; combinations can be used to repeat regexp isearches that can
;; be used to start these searches.
(define-key map "\M-\C-s" 'isearch-repeat-forward)
(define-key map "\M-\C-r" 'isearch-repeat-backward)
(define-key map "\177" 'isearch-delete-char)
(define-key map [backspace] 'undefined) ;bug#20466.
(define-key map "\C-g" 'isearch-abort)
;; This assumes \e is the meta-prefix-char.
(or (= ?\e meta-prefix-char)
(error "Inconsistency in isearch.el"))
(define-key map "\e\e\e" 'isearch-cancel)
(define-key map "\C-q" 'isearch-quote-char)
(define-key map "\r" 'isearch-exit)
(define-key map [return] 'isearch-exit)
(define-key map "\C-j" 'isearch-printing-char)
(define-key map "\t" 'isearch-printing-char)
(define-key map [?\S-\ ] 'isearch-printing-char)
(define-key map "\C-w" 'isearch-yank-word-or-char)
(define-key map "\M-\C-w" 'isearch-del-char)
(define-key map "\M-\C-y" 'isearch-yank-char)
(define-key map "\C-y" 'isearch-yank-kill)
(define-key map "\M-s\C-e" 'isearch-yank-line)
(define-key map (char-to-string help-char) isearch-help-map)
(define-key map [help] isearch-help-map)
(define-key map [f1] isearch-help-map)
(define-key map "\M-n" 'isearch-ring-advance)
(define-key map "\M-p" 'isearch-ring-retreat)
(define-key map "\M-y" 'isearch-yank-pop)
(define-key map "\M-\t" 'isearch-complete)
;; Pass frame events transparently so they won't exit the search.
;; In particular, if we have more than one display open, then a
;; switch-frame might be generated by someone typing at another keyboard.
(define-key map [switch-frame] nil)
(define-key map [delete-frame] nil)
(define-key map [iconify-frame] nil)
(define-key map [make-frame-visible] nil)
(define-key map [mouse-movement] nil)
(define-key map [language-change] nil)
;; For searching multilingual text.
(define-key map "\C-\\" 'isearch-toggle-input-method)
(define-key map "\C-^" 'isearch-toggle-specified-input-method)
;; People expect to be able to paste with the mouse.
(define-key map [mouse-2] #'isearch-mouse-2)
(define-key map [down-mouse-2] nil)
(define-key map [xterm-paste] #'isearch-xterm-paste)
;; Some bindings you may want to put in your isearch-mode-hook.
;; Suggest some alternates...
(define-key map "\M-c" 'isearch-toggle-case-fold)
(define-key map "\M-r" 'isearch-toggle-regexp)
(define-key map "\M-e" 'isearch-edit-string)
(put 'isearch-edit-string :advertised-binding "\M-se")
(define-key map "\M-se" 'isearch-edit-string)
;; More toggles defined by `isearch-define-mode-toggle'.
(define-key map [?\M-%] 'isearch-query-replace)
(define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
(define-key map "\M-so" 'isearch-occur)
(define-key map "\M-shr" 'isearch-highlight-regexp)
;; The key translations defined in the C-x 8 prefix should add
;; characters to the search string. See iso-transl.el.
(define-key map "\C-x8\r" 'isearch-char-by-name)
map)
"Keymap for `isearch-mode'.")
(defvar minibuffer-local-isearch-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map "\r" 'exit-minibuffer)
(define-key map "\M-\t" 'isearch-complete-edit)
(define-key map "\C-s" 'isearch-forward-exit-minibuffer)
(define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
(define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
(define-key map [right] 'isearch-yank-char-in-minibuffer)
map)
"Keymap for editing Isearch strings in the minibuffer.")
;; Internal variables declared globally for byte-compiler.
;; These are all set with setq while isearching
;; and bound locally while editing the search string.
(defvar isearch-forward nil) ; Searching in the forward direction.
(defvar isearch-regexp nil) ; Searching for a regexp.
(defvar isearch-regexp-function nil
"Regexp-based search mode for words/symbols.
If the value is a function (e.g. `isearch-symbol-regexp'), it is
called to convert a plain search string to a regexp used by
regexp search functions.
The symbol property `isearch-message-prefix' put on this function
specifies the prefix string displayed in the search message.
This variable is set and changed during isearch. To change the
default behavior used for searches, see `search-default-mode'
instead.")
;; We still support setting this to t for backwards compatibility.
(define-obsolete-variable-alias 'isearch-word
'isearch-regexp-function "25.1")
(defvar isearch-lax-whitespace t
"If non-nil, a space will match a sequence of whitespace chars.
When you enter a space or spaces in ordinary incremental search, it
will match any sequence matched by the regexp defined by the variable
`search-whitespace-regexp'. If the value is nil, each space you type
matches literally, against one space. You can toggle the value of this
variable by the command `isearch-toggle-lax-whitespace'.")
(defvar isearch-regexp-lax-whitespace nil
"If non-nil, a space will match a sequence of whitespace chars.
When you enter a space or spaces in regexp incremental search, it
will match any sequence matched by the regexp defined by the variable
`search-whitespace-regexp'. If the value is nil, each space you type
matches literally, against one space. You can toggle the value of this
variable by the command `isearch-toggle-lax-whitespace'.")
(defvar isearch-cmds nil
"Stack of search status elements.
Each element is an `isearch--state' struct where the slots are
[STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
ERROR WRAPPED BARRIER CASE-FOLD-SEARCH POP-FUN]")
(defvar isearch-string "") ; The current search string.
(defvar isearch-message "") ; text-char-description version of isearch-string
(defvar isearch-message-prefix-add nil) ; Additional text for the message prefix
(defvar isearch-message-suffix-add nil) ; Additional text for the message suffix
(defvar isearch-success t) ; Searching is currently successful.
(defvar isearch-error nil) ; Error message for failed search.
(defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
(defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
(defvar isearch-barrier 0
"Recorded minimum/maximal point for the current search.")
(defvar isearch-just-started nil)
(defvar isearch-start-hscroll 0) ; hscroll when starting the search.
;; case-fold-search while searching.
;; either nil, t, or 'yes. 'yes means the same as t except that mixed
;; case in the search string is ignored.
(defvar isearch-case-fold-search nil)
;; search-invisible while searching.
;; either nil, t, or 'open. 'open means the same as t except that
;; opens hidden overlays.
(defvar isearch-invisible search-invisible)
(defvar isearch-last-case-fold-search nil)
;; Used to save default value while isearch is active
(defvar isearch-original-minibuffer-message-timeout nil)
(defvar isearch-adjusted nil)
(defvar isearch-slow-terminal-mode nil)
;; If t, using a small window.
(defvar isearch-small-window nil)
(defvar isearch-opoint 0)
;; The window configuration active at the beginning of the search.
(defvar isearch-window-configuration nil)
;; Flag to indicate a yank occurred, so don't move the cursor.
(defvar isearch-yank-flag nil)
;; A function to be called after each input character is processed.
;; (It is not called after characters that exit the search.)
;; It is only set from an optional argument to `isearch-mode'.
(defvar isearch-op-fun nil)
;; Is isearch-mode in a recursive edit for modal searching.
(defvar isearch-recursive-edit nil)
;; Should isearch be terminated after doing one search?
(defvar isearch-nonincremental nil)
;; New value of isearch-nonincremental after isearch-edit-string.
(defvar isearch-new-nonincremental nil)
;; New value of isearch-forward after isearch-edit-string.
(defvar isearch-new-forward nil)
;; Accumulate here the overlays opened during searching.
(defvar isearch-opened-overlays nil)
;; Non-nil if the string exists but is invisible.
(defvar isearch-hidden nil)
;; The value of input-method-function when isearch is invoked.
(defvar isearch-input-method-function nil)
;; A flag to tell if input-method-function is locally bound when
;; isearch is invoked.
(defvar isearch-input-method-local-p nil)
(defvar isearch--saved-overriding-local-map nil)
;; Minor-mode-alist changes - kind of redundant with the
;; echo area, but if isearching in multiple windows, it can be useful.
(or (assq 'isearch-mode minor-mode-alist)
(nconc minor-mode-alist
(list '(isearch-mode isearch-mode))))
(defvar-local isearch-mode nil) ;; Name of the minor mode, if non-nil.
(define-key global-map "\C-s" 'isearch-forward)
(define-key esc-map "\C-s" 'isearch-forward-regexp)
(define-key global-map "\C-r" 'isearch-backward)
(define-key esc-map "\C-r" 'isearch-backward-regexp)
(define-key search-map "w" 'isearch-forward-word)
(define-key search-map "_" 'isearch-forward-symbol)
(define-key search-map "." 'isearch-forward-symbol-at-point)
;; Entry points to isearch-mode.
(defun isearch-forward (&optional regexp-p no-recursive-edit)
"\
Do incremental search forward.
With a prefix argument, do an incremental regular expression search instead.
\\<isearch-mode-map>
As you type characters, they add to the search string and are found.
The following non-printing keys are bound in `isearch-mode-map'.
Type \\[isearch-delete-char] to cancel last input item from end of search string.
Type \\[isearch-exit] to exit, leaving point at location found.
Type LFD (C-j) to match end of line.
Type \\[isearch-repeat-forward] to search again forward,\
\\[isearch-repeat-backward] to search again backward.
Type \\[isearch-yank-word-or-char] to yank next word or character in buffer
onto the end of the search string, and search for it.
Type \\[isearch-del-char] to delete character from end of search string.
Type \\[isearch-yank-char] to yank char from buffer onto end of search\
string and search for it.
Type \\[isearch-yank-line] to yank rest of line onto end of search string\
and search for it.
Type \\[isearch-yank-kill] to yank the last string of killed text.
Type \\[isearch-yank-pop] to replace string just yanked into search prompt
with string killed before it.
Type \\[isearch-quote-char] to quote control character to search for it.
Type \\[isearch-char-by-name] to add a character to search by Unicode name,\
with completion.
\\[isearch-abort] while searching or when search has failed cancels input\
back to what has
been found successfully.
\\[isearch-abort] when search is successful aborts and moves point to\
starting point.
If you try to exit with the search string still empty, it invokes
nonincremental search.
Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
Type \\[isearch-toggle-invisible] to toggle search in invisible text.
Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
Type \\[isearch-toggle-word] to toggle word mode.
Type \\[isearch-toggle-symbol] to toggle symbol mode.
Type \\[isearch-toggle-char-fold] to toggle character folding.
Type \\[isearch-toggle-lax-whitespace] to toggle whitespace matching.
In incremental searches, a space or spaces normally matches any whitespace
defined by the variable `search-whitespace-regexp'; see also the variables
`isearch-lax-whitespace' and `isearch-regexp-lax-whitespace'.
Type \\[isearch-edit-string] to edit the search string in the minibuffer.
Also supported is a search ring of the previous 16 search strings.
Type \\[isearch-ring-advance] to search for the next item in the search ring.
Type \\[isearch-ring-retreat] to search for the previous item in the search\
ring.
Type \\[isearch-complete] to complete the search string using the search ring.
Type \\[isearch-query-replace] to run `query-replace' with string to\
replace from last search string.
Type \\[isearch-query-replace-regexp] to run `query-replace-regexp'\
with the last search string.
Type \\[isearch-occur] to run `occur' that shows\
the last search string.
Type \\[isearch-highlight-regexp] to run `highlight-regexp'\
that highlights the last search string.
Type \\[isearch-describe-bindings] to display all Isearch key bindings.
Type \\[isearch-describe-key] to display documentation of Isearch key.
Type \\[isearch-describe-mode] to display documentation of Isearch mode.
If an input method is turned on in the current buffer, that input
method is also active while you are typing characters to search.
To toggle the input method, type \\[isearch-toggle-input-method]. \
It also toggles the input
method in the current buffer.
To use a different input method for searching, type \
\\[isearch-toggle-specified-input-method],
and specify an input method you want to use.
The above keys, bound in `isearch-mode-map', are often controlled by
options; do \\[apropos] on search-.* to find them.
Other control and meta characters terminate the search
and are then executed normally (depending on `search-exit-option').
Likewise for function keys and mouse button events.
If this function is called non-interactively with a nil NO-RECURSIVE-EDIT,
it does not return to the calling function until the search is done.
See the function `isearch-mode' for more information."
(interactive "P\np")
(isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
"Do incremental search forward for regular expression.
With a prefix argument, do a regular string search instead.
Like ordinary incremental search except that your input is treated
as a regexp. See the command `isearch-forward' for more information.
In incremental searches, a space or spaces normally matches any
whitespace defined by the variable `search-whitespace-regexp'.
To search for a literal space and nothing else, enter C-q SPC.
To toggle whitespace matching, use `isearch-toggle-lax-whitespace'.
This command does not support character folding."
(interactive "P\np")
(isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
(defun isearch-forward-word (&optional not-word no-recursive-edit)
"Do incremental search forward for a sequence of words.
With a prefix argument, do a regular string search instead.
Like ordinary incremental search except that your input is treated
as a sequence of words without regard to how the words are separated.
See the command `isearch-forward' for more information.
This command does not support character folding, and lax space matching
has no effect on it."
(interactive "P\np")
(isearch-mode t nil nil (not no-recursive-edit) (null not-word)))
(defun isearch-forward-symbol (&optional _not-symbol no-recursive-edit)
"Do incremental search forward for a symbol.
The prefix argument is currently unused.
Like ordinary incremental search except that your input is treated
as a symbol surrounded by symbol boundary constructs \\_< and \\_>.
See the command `isearch-forward' for more information.
This command does not support character folding, and lax space matching
has no effect on it."
(interactive "P\np")
(isearch-mode t nil nil (not no-recursive-edit) 'isearch-symbol-regexp))
(defun isearch-backward (&optional regexp-p no-recursive-edit)
"Do incremental search backward.
With a prefix argument, do a regular expression search instead.
See the command `isearch-forward' for more information."
(interactive "P\np")
(isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
"Do incremental search backward for regular expression.
With a prefix argument, do a regular string search instead.
Like ordinary incremental search except that your input is treated
as a regexp. See the command `isearch-forward-regexp' for more information."
(interactive "P\np")
(isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
(defun isearch-forward-symbol-at-point ()
"Do incremental search forward for a symbol found near point.
Like ordinary incremental search except that the symbol found at point
is added to the search string initially as a regexp surrounded
by symbol boundary constructs \\_< and \\_>.
See the command `isearch-forward-symbol' for more information."
(interactive)
(isearch-forward-symbol nil 1)
(let ((bounds (find-tag-default-bounds)))
(cond
(bounds
(when (< (car bounds) (point))
(goto-char (car bounds)))
(isearch-yank-string
(buffer-substring-no-properties (car bounds) (cdr bounds))))
(t
(setq isearch-error "No symbol at point")
(isearch-push-state)
(isearch-update)))))
;; isearch-mode only sets up incremental search for the minor mode.
;; All the work is done by the isearch-mode commands.
;; Not used yet:
;;(defvar isearch-commands '(isearch-forward isearch-backward
;; isearch-forward-regexp isearch-backward-regexp)
;; "List of commands for which isearch-mode does not recursive-edit.")
(defun isearch-mode (forward &optional regexp op-fun recursive-edit regexp-function)
"Start Isearch minor mode.
It is called by the function `isearch-forward' and other related functions.
The non-nil arg FORWARD means searching in the forward direction.
The non-nil arg REGEXP does an incremental regular expression search.
The arg OP-FUN is a function to be called after each input character
is processed. (It is not called after characters that exit the search.)
When the arg RECURSIVE-EDIT is non-nil, this function behaves modally and
does not return to the calling function until the search is completed.
To behave this way it enters a recursive-edit and exits it when done
isearching.
The arg REGEXP-FUNCTION, if non-nil, should be a function. It is
used to set the value of `isearch-regexp-function'."
;; Initialize global vars.
(setq isearch-forward forward
isearch-regexp (or regexp
(and (not regexp-function)
(eq search-default-mode t)))
isearch-regexp-function (or regexp-function
(and (functionp search-default-mode)
(not regexp)
search-default-mode))
isearch-op-fun op-fun
isearch-last-case-fold-search isearch-case-fold-search
isearch-case-fold-search case-fold-search
isearch-invisible search-invisible
isearch-string ""
isearch-message ""
isearch-cmds nil
isearch-success t
isearch-wrapped nil
isearch-barrier (point)
isearch-adjusted nil
isearch-yank-flag nil
isearch-error nil
isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
(> (window-height)
(* 4
(abs search-slow-window-lines))))
isearch-other-end nil
isearch-small-window nil
isearch-just-started t
isearch-start-hscroll (window-hscroll)
isearch-opoint (point)
search-ring-yank-pointer nil
isearch-opened-overlays nil
isearch-input-method-function input-method-function
isearch-input-method-local-p (local-variable-p 'input-method-function)
regexp-search-ring-yank-pointer nil
;; Save the original value of `minibuffer-message-timeout', and
;; set it to nil so that isearch's messages don't get timed out.
isearch-original-minibuffer-message-timeout minibuffer-message-timeout
minibuffer-message-timeout nil)
;; We must bypass input method while reading key. When a user type
;; printable character, appropriate input method is turned on in
;; minibuffer to read multibyte characters.
(or isearch-input-method-local-p
(make-local-variable 'input-method-function))
(setq input-method-function nil)
(looking-at "")
(setq isearch-window-configuration
(if isearch-slow-terminal-mode (current-window-configuration) nil))
;; Maybe make minibuffer frame visible and/or raise it.
(let ((frame (window-frame (minibuffer-window))))
(unless (memq (frame-live-p frame) '(nil t))
(unless (frame-visible-p frame)
(make-frame-visible frame))
(if minibuffer-auto-raise
(raise-frame frame))))
(setq isearch-mode " Isearch") ;; forward? regexp?
(force-mode-line-update)
(setq overriding-terminal-local-map isearch-mode-map)
(run-hooks 'isearch-mode-hook)
;; Remember the initial map possibly modified
;; by external packages in isearch-mode-hook. (Bug#16035)
(setq isearch--saved-overriding-local-map overriding-terminal-local-map)
;; Pushing the initial state used to be before running isearch-mode-hook,
;; but a hook might set `isearch-push-state-function' used in
;; `isearch-push-state' to save mode-specific initial state. (Bug#4994)
(isearch-push-state)
(isearch-update)
(add-hook 'pre-command-hook 'isearch-pre-command-hook)
(add-hook 'post-command-hook 'isearch-post-command-hook)
(add-hook 'mouse-leave-buffer-hook 'isearch-done)
(add-hook 'kbd-macro-termination-hook 'isearch-done)
;; isearch-mode can be made modal (in the sense of not returning to
;; the calling function until searching is completed) by entering
;; a recursive-edit and exiting it when done isearching.
(if recursive-edit
(let ((isearch-recursive-edit t))
(recursive-edit)))
isearch-success)
;; Some high level utilities. Others below.
(defvar isearch--current-buffer nil)
(defun isearch-update ()
"This is called after every isearch command to update the display.
The second last thing it does is to run `isearch-update-post-hook'.
The last thing is to trigger a new round of lazy highlighting."
(unless (eq (current-buffer) isearch--current-buffer)
(when (buffer-live-p isearch--current-buffer)
(with-current-buffer isearch--current-buffer
(setq cursor-sensor-inhibit (delq 'isearch cursor-sensor-inhibit))))
(setq isearch--current-buffer (current-buffer))
(make-local-variable 'cursor-sensor-inhibit)
;; Suspend things like cursor-intangible during Isearch so we can search
;; even within intangible text.
(push 'isearch cursor-sensor-inhibit))
(if (and (null unread-command-events)
(null executing-kbd-macro))
(progn
(if (not (input-pending-p))
(funcall (or isearch-message-function #'isearch-message)))
(if (and isearch-slow-terminal-mode
(not (or isearch-small-window
(pos-visible-in-window-group-p))))
(let ((found-point (point)))
(setq isearch-small-window t)
(move-to-window-line 0)
(let ((window-min-height 1))