isearch.el 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988
  1. ;;; isearch.el --- incremental search minor mode
  2. ;; Copyright (C) 1992-1997, 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
  4. ;; Maintainer: FSF
  5. ;; Keywords: matching
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Instructions
  20. ;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
  21. ;; isearch-mode behaves modally and does not return until the search
  22. ;; is completed. It uses a recursive-edit to behave this way.
  23. ;; The key bindings active within isearch-mode are defined below in
  24. ;; `isearch-mode-map' which is given bindings close to the default
  25. ;; characters of the original isearch.el. With `isearch-mode',
  26. ;; however, you can bind multi-character keys and it should be easier
  27. ;; to add new commands. One bug though: keys with meta-prefix cannot
  28. ;; be longer than two chars. Also see minibuffer-local-isearch-map
  29. ;; for bindings active during `isearch-edit-string'.
  30. ;; isearch-mode should work even if you switch windows with the mouse,
  31. ;; in which case isearch-mode is terminated automatically before the
  32. ;; switch.
  33. ;; The search ring and completion commands automatically put you in
  34. ;; the minibuffer to edit the string. This gives you a chance to
  35. ;; modify the search string before executing the search. There are
  36. ;; three commands to terminate the editing: C-s and C-r exit the
  37. ;; minibuffer and search forward and reverse respectively, while C-m
  38. ;; exits and does a nonincremental search.
  39. ;; Exiting immediately from isearch uses isearch-edit-string instead
  40. ;; of nonincremental-search, if search-nonincremental-instead is non-nil.
  41. ;; The name of this option should probably be changed if we decide to
  42. ;; keep the behavior. No point in forcing nonincremental search until
  43. ;; the last possible moment.
  44. ;;; Code:
  45. ;; Some additional options and constants.
  46. (defgroup isearch nil
  47. "Incremental search minor mode."
  48. :link '(emacs-commentary-link "isearch")
  49. :link '(custom-manual "(emacs)Incremental Search")
  50. :prefix "isearch-"
  51. :prefix "search-"
  52. :group 'matching)
  53. (defcustom search-exit-option t
  54. "Non-nil means random control characters terminate incremental search."
  55. :type 'boolean
  56. :group 'isearch)
  57. (defcustom search-slow-window-lines 1
  58. "Number of lines in slow search display windows.
  59. These are the short windows used during incremental search on slow terminals.
  60. Negative means put the slow search window at the top (normally it's at bottom)
  61. and the value is minus the number of lines."
  62. :type 'integer
  63. :group 'isearch)
  64. (defcustom search-slow-speed 1200
  65. "Highest terminal speed at which to use \"slow\" style incremental search.
  66. This is the style where a one-line window is created to show the line
  67. that the search has reached."
  68. :type 'integer
  69. :group 'isearch)
  70. (defcustom search-upper-case 'not-yanks
  71. "If non-nil, upper case chars disable case fold searching.
  72. That is, upper and lower case chars must match exactly.
  73. This applies no matter where the chars come from, but does not
  74. apply to chars in regexps that are prefixed with `\\'.
  75. If this value is `not-yanks', text yanked into the search string
  76. in Isearch mode is always downcased."
  77. :type '(choice (const :tag "off" nil)
  78. (const not-yanks)
  79. (other :tag "on" t))
  80. :group 'isearch)
  81. (defcustom search-nonincremental-instead t
  82. "If non-nil, do a nonincremental search instead of exiting immediately.
  83. Actually, `isearch-edit-string' is called to let you enter the search
  84. string, and RET terminates editing and does a nonincremental search."
  85. :type 'boolean
  86. :group 'isearch)
  87. (defcustom search-whitespace-regexp (purecopy "\\s-+")
  88. "If non-nil, regular expression to match a sequence of whitespace chars.
  89. This applies to regular expression incremental search.
  90. When you put a space or spaces in the incremental regexp, it stands for
  91. this, unless it is inside of a regexp construct such as [...] or *, + or ?.
  92. You might want to use something like \"[ \\t\\r\\n]+\" instead.
  93. In the Customization buffer, that is `[' followed by a space,
  94. a tab, a carriage return (control-M), a newline, and `]+'.
  95. When this is nil, each space you type matches literally, against one space."
  96. :type '(choice (const :tag "Find Spaces Literally" nil)
  97. regexp)
  98. :group 'isearch)
  99. (defcustom search-invisible 'open
  100. "If t incremental search can match hidden text.
  101. A nil value means don't match invisible text.
  102. When the value is `open', if the text matched is made invisible by
  103. an overlay having an `invisible' property and that overlay has a property
  104. `isearch-open-invisible', then incremental search will show the contents.
  105. \(This applies when using `outline.el' and `hideshow.el'.)
  106. See also `reveal-mode' if you want overlays to automatically be opened
  107. whenever point is in one of them."
  108. :type '(choice (const :tag "Match hidden text" t)
  109. (const :tag "Open overlays" open)
  110. (const :tag "Don't match hidden text" nil))
  111. :group 'isearch)
  112. (defcustom isearch-hide-immediately t
  113. "If non-nil, re-hide an invisible match right away.
  114. This variable makes a difference when `search-invisible' is set to `open'.
  115. It means that after search makes some invisible text visible
  116. to show the match, it makes the text invisible again when the match moves.
  117. Ordinarily the text becomes invisible again at the end of the search."
  118. :type 'boolean
  119. :group 'isearch)
  120. (defcustom isearch-resume-in-command-history nil
  121. "If non-nil, `isearch-resume' commands are added to the command history.
  122. This allows you to resume earlier Isearch sessions through the
  123. command history."
  124. :type 'boolean
  125. :group 'isearch)
  126. (defvar isearch-mode-hook nil
  127. "Function(s) to call after starting up an incremental search.")
  128. (defvar isearch-update-post-hook nil
  129. "Function(s) to call after isearch has found matches in the buffer.")
  130. (defvar isearch-mode-end-hook nil
  131. "Function(s) to call after terminating an incremental search.
  132. When these functions are called, `isearch-mode-end-hook-quit'
  133. is non-nil if the user quits the search.")
  134. (defvar isearch-mode-end-hook-quit nil
  135. "Non-nil while running `isearch-mode-end-hook' if the user quits the search.")
  136. (defvar isearch-message-function nil
  137. "Function to call to display the search prompt.
  138. If nil, use `isearch-message'.")
  139. (defvar isearch-wrap-function nil
  140. "Function to call to wrap the search when search is failed.
  141. If nil, move point to the beginning of the buffer for a forward search,
  142. or to the end of the buffer for a backward search.")
  143. (defvar isearch-push-state-function nil
  144. "Function to save a function restoring the mode-specific Isearch state
  145. to the search status stack.")
  146. (defvar isearch-filter-predicate 'isearch-filter-visible
  147. "Predicate that filters the search hits that would normally be available.
  148. Search hits that dissatisfy the predicate are skipped. The function
  149. has two arguments: the positions of start and end of text matched by
  150. the search. If this function returns nil, continue searching without
  151. stopping at this match.")
  152. ;; Search ring.
  153. (defvar search-ring nil
  154. "List of search string sequences.")
  155. (defvar regexp-search-ring nil
  156. "List of regular expression search string sequences.")
  157. (defcustom search-ring-max 16
  158. "Maximum length of search ring before oldest elements are thrown away."
  159. :type 'integer
  160. :group 'isearch)
  161. (defcustom regexp-search-ring-max 16
  162. "Maximum length of regexp search ring before oldest elements are thrown away."
  163. :type 'integer
  164. :group 'isearch)
  165. (defvar search-ring-yank-pointer nil
  166. "Index in `search-ring' of last string reused.
  167. It is nil if none yet.")
  168. (defvar regexp-search-ring-yank-pointer nil
  169. "Index in `regexp-search-ring' of last string reused.
  170. It is nil if none yet.")
  171. (defcustom search-ring-update nil
  172. "Non-nil if advancing or retreating in the search ring should cause search.
  173. Default value, nil, means edit the string instead."
  174. :type 'boolean
  175. :group 'isearch)
  176. ;;; isearch highlight customization.
  177. (defcustom search-highlight t
  178. "Non-nil means incremental search highlights the current match."
  179. :type 'boolean
  180. :group 'isearch)
  181. (defface isearch
  182. '((((class color) (min-colors 88) (background light))
  183. ;; The background must not be too dark, for that means
  184. ;; the character is hard to see when the cursor is there.
  185. (:background "magenta3" :foreground "lightskyblue1"))
  186. (((class color) (min-colors 88) (background dark))
  187. (:background "palevioletred2" :foreground "brown4"))
  188. (((class color) (min-colors 16))
  189. (:background "magenta4" :foreground "cyan1"))
  190. (((class color) (min-colors 8))
  191. (:background "magenta4" :foreground "cyan1"))
  192. (t (:inverse-video t)))
  193. "Face for highlighting Isearch matches."
  194. :group 'isearch
  195. :group 'basic-faces)
  196. (defvar isearch-face 'isearch)
  197. (defface isearch-fail
  198. '((((class color) (min-colors 88) (background light))
  199. (:background "RosyBrown1"))
  200. (((class color) (min-colors 88) (background dark))
  201. (:background "red4"))
  202. (((class color) (min-colors 16))
  203. (:background "red"))
  204. (((class color) (min-colors 8))
  205. (:background "red"))
  206. (((class color grayscale))
  207. :foreground "grey")
  208. (t (:inverse-video t)))
  209. "Face for highlighting failed part in Isearch echo-area message."
  210. :version "23.1"
  211. :group 'isearch)
  212. (defcustom isearch-lazy-highlight t
  213. "Controls the lazy-highlighting during incremental search.
  214. When non-nil, all text in the buffer matching the current search
  215. string is highlighted lazily (see `lazy-highlight-initial-delay'
  216. and `lazy-highlight-interval')."
  217. :type 'boolean
  218. :group 'lazy-highlight
  219. :group 'isearch)
  220. ;;; Lazy highlight customization.
  221. (defgroup lazy-highlight nil
  222. "Lazy highlighting feature for matching strings."
  223. :prefix "lazy-highlight-"
  224. :version "21.1"
  225. :group 'isearch
  226. :group 'matching)
  227. (define-obsolete-variable-alias 'isearch-lazy-highlight-cleanup
  228. 'lazy-highlight-cleanup
  229. "22.1")
  230. (defcustom lazy-highlight-cleanup t
  231. "Controls whether to remove extra highlighting after a search.
  232. If this is nil, extra highlighting can be \"manually\" removed with
  233. \\[lazy-highlight-cleanup]."
  234. :type 'boolean
  235. :group 'lazy-highlight)
  236. (define-obsolete-variable-alias 'isearch-lazy-highlight-initial-delay
  237. 'lazy-highlight-initial-delay
  238. "22.1")
  239. (defcustom lazy-highlight-initial-delay 0.25
  240. "Seconds to wait before beginning to lazily highlight all matches."
  241. :type 'number
  242. :group 'lazy-highlight)
  243. (define-obsolete-variable-alias 'isearch-lazy-highlight-interval
  244. 'lazy-highlight-interval
  245. "22.1")
  246. (defcustom lazy-highlight-interval 0 ; 0.0625
  247. "Seconds between lazily highlighting successive matches."
  248. :type 'number
  249. :group 'lazy-highlight)
  250. (define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
  251. 'lazy-highlight-max-at-a-time
  252. "22.1")
  253. (defcustom lazy-highlight-max-at-a-time 20
  254. "Maximum matches to highlight at a time (for `lazy-highlight').
  255. Larger values may reduce Isearch's responsiveness to user input;
  256. smaller values make matches highlight slowly.
  257. A value of nil means highlight all matches."
  258. :type '(choice (const :tag "All" nil)
  259. (integer :tag "Some"))
  260. :group 'lazy-highlight)
  261. (defface lazy-highlight
  262. '((((class color) (min-colors 88) (background light))
  263. (:background "paleturquoise"))
  264. (((class color) (min-colors 88) (background dark))
  265. (:background "paleturquoise4"))
  266. (((class color) (min-colors 16))
  267. (:background "turquoise3"))
  268. (((class color) (min-colors 8))
  269. (:background "turquoise3"))
  270. (t (:underline t)))
  271. "Face for lazy highlighting of matches other than the current one."
  272. :group 'lazy-highlight
  273. :group 'basic-faces)
  274. (define-obsolete-face-alias 'isearch-lazy-highlight-face 'lazy-highlight "22.1")
  275. (define-obsolete-variable-alias 'isearch-lazy-highlight-face
  276. 'lazy-highlight-face
  277. "22.1")
  278. (defvar lazy-highlight-face 'lazy-highlight)
  279. ;; Define isearch help map.
  280. (defvar isearch-help-map
  281. (let ((map (make-sparse-keymap)))
  282. (define-key map [t] 'isearch-other-control-char)
  283. (define-key map (char-to-string help-char) 'isearch-help-for-help)
  284. (define-key map [help] 'isearch-help-for-help)
  285. (define-key map [f1] 'isearch-help-for-help)
  286. (define-key map "?" 'isearch-help-for-help)
  287. (define-key map "b" 'isearch-describe-bindings)
  288. (define-key map "k" 'isearch-describe-key)
  289. (define-key map "m" 'isearch-describe-mode)
  290. (define-key map "q" 'help-quit)
  291. map)
  292. "Keymap for characters following the Help key for Isearch mode.")
  293. (eval-when-compile (require 'help-macro))
  294. (make-help-screen isearch-help-for-help-internal
  295. (purecopy "Type a help option: [bkm] or ?")
  296. "You have typed %THIS-KEY%, the help character. Type a Help option:
  297. \(Type \\<help-map>\\[help-quit] to exit the Help command.)
  298. b Display all Isearch key bindings.
  299. k KEYS Display full documentation of Isearch key sequence.
  300. m Display documentation of Isearch mode.
  301. You can't type here other help keys available in the global help map,
  302. but outside of this help window when you type them in Isearch mode,
  303. they exit Isearch mode before displaying global help."
  304. isearch-help-map)
  305. (defun isearch-help-for-help ()
  306. "Display Isearch help menu."
  307. (interactive)
  308. (let (same-window-buffer-names same-window-regexps)
  309. (isearch-help-for-help-internal))
  310. (isearch-update))
  311. (defun isearch-describe-bindings ()
  312. "Show a list of all keys defined in Isearch mode, and their definitions.
  313. This is like `describe-bindings', but displays only Isearch keys."
  314. (interactive)
  315. (let (same-window-buffer-names same-window-regexps)
  316. (with-help-window "*Help*"
  317. (with-current-buffer standard-output
  318. (princ "Isearch Mode Bindings:\n")
  319. (princ (substitute-command-keys "\\{isearch-mode-map}"))))))
  320. (defun isearch-describe-key ()
  321. "Display documentation of the function invoked by isearch key."
  322. (interactive)
  323. (let (same-window-buffer-names same-window-regexps)
  324. (call-interactively 'describe-key))
  325. (isearch-update))
  326. (defun isearch-describe-mode ()
  327. "Display documentation of Isearch mode."
  328. (interactive)
  329. (let (same-window-buffer-names same-window-regexps)
  330. (describe-function 'isearch-forward))
  331. (isearch-update))
  332. (defalias 'isearch-mode-help 'isearch-describe-mode)
  333. ;; Define isearch-mode keymap.
  334. (defvar isearch-mode-map
  335. (let ((i 0)
  336. (map (make-keymap)))
  337. (or (char-table-p (nth 1 map))
  338. (error "The initialization of isearch-mode-map must be updated"))
  339. ;; Make all multibyte characters search for themselves.
  340. (set-char-table-range (nth 1 map) (cons #x100 (max-char))
  341. 'isearch-printing-char)
  342. ;; Make function keys, etc, which aren't bound to a scrolling-function
  343. ;; exit the search.
  344. (define-key map [t] 'isearch-other-control-char)
  345. ;; Control chars, by default, end isearch mode transparently.
  346. ;; We need these explicit definitions because, in a dense keymap,
  347. ;; the binding for t does not affect characters.
  348. ;; We use a dense keymap to save space.
  349. (while (< i ?\s)
  350. (define-key map (make-string 1 i) 'isearch-other-control-char)
  351. (setq i (1+ i)))
  352. ;; Single-byte printing chars extend the search string by default.
  353. (setq i ?\s)
  354. (while (< i 256)
  355. (define-key map (vector i) 'isearch-printing-char)
  356. (setq i (1+ i)))
  357. ;; To handle local bindings with meta char prefix keys, define
  358. ;; another full keymap. This must be done for any other prefix
  359. ;; keys as well, one full keymap per char of the prefix key. It
  360. ;; would be simpler to disable the global keymap, and/or have a
  361. ;; default local key binding for any key not otherwise bound.
  362. (let ((meta-map (make-sparse-keymap)))
  363. (define-key map (char-to-string meta-prefix-char) meta-map)
  364. (define-key map [escape] meta-map))
  365. (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
  366. ;; Several non-printing chars change the searching behavior.
  367. (define-key map "\C-s" 'isearch-repeat-forward)
  368. (define-key map "\C-r" 'isearch-repeat-backward)
  369. ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
  370. ;; combinations can be used to repeat regexp isearches that can
  371. ;; be used to start these searches.
  372. (define-key map "\M-\C-s" 'isearch-repeat-forward)
  373. (define-key map "\M-\C-r" 'isearch-repeat-backward)
  374. (define-key map "\177" 'isearch-delete-char)
  375. (define-key map "\C-g" 'isearch-abort)
  376. ;; This assumes \e is the meta-prefix-char.
  377. (or (= ?\e meta-prefix-char)
  378. (error "Inconsistency in isearch.el"))
  379. (define-key map "\e\e\e" 'isearch-cancel)
  380. (define-key map [escape escape escape] 'isearch-cancel)
  381. (define-key map "\C-q" 'isearch-quote-char)
  382. (define-key map "\r" 'isearch-exit)
  383. (define-key map "\C-j" 'isearch-printing-char)
  384. (define-key map "\t" 'isearch-printing-char)
  385. (define-key map [?\S-\ ] 'isearch-printing-char)
  386. (define-key map "\C-w" 'isearch-yank-word-or-char)
  387. (define-key map "\M-\C-w" 'isearch-del-char)
  388. (define-key map "\M-\C-y" 'isearch-yank-char)
  389. (define-key map "\C-y" 'isearch-yank-kill)
  390. (define-key map "\M-s\C-e" 'isearch-yank-line)
  391. (define-key map (char-to-string help-char) isearch-help-map)
  392. (define-key map [help] isearch-help-map)
  393. (define-key map [f1] isearch-help-map)
  394. (define-key map "\M-n" 'isearch-ring-advance)
  395. (define-key map "\M-p" 'isearch-ring-retreat)
  396. (define-key map "\M-y" 'isearch-yank-pop)
  397. (define-key map "\M-\t" 'isearch-complete)
  398. ;; Pass frame events transparently so they won't exit the search.
  399. ;; In particular, if we have more than one display open, then a
  400. ;; switch-frame might be generated by someone typing at another keyboard.
  401. (define-key map [switch-frame] nil)
  402. (define-key map [delete-frame] nil)
  403. (define-key map [iconify-frame] nil)
  404. (define-key map [make-frame-visible] nil)
  405. (define-key map [mouse-movement] nil)
  406. (define-key map [language-change] nil)
  407. ;; For searching multilingual text.
  408. (define-key map "\C-\\" 'isearch-toggle-input-method)
  409. (define-key map "\C-^" 'isearch-toggle-specified-input-method)
  410. ;; People expect to be able to paste with the mouse.
  411. (define-key map [mouse-2] #'isearch-mouse-2)
  412. (define-key map [down-mouse-2] nil)
  413. ;; Some bindings you may want to put in your isearch-mode-hook.
  414. ;; Suggest some alternates...
  415. (define-key map "\M-c" 'isearch-toggle-case-fold)
  416. (define-key map "\M-r" 'isearch-toggle-regexp)
  417. (define-key map "\M-e" 'isearch-edit-string)
  418. (define-key map "\M-sc" 'isearch-toggle-case-fold)
  419. (define-key map "\M-sr" 'isearch-toggle-regexp)
  420. (define-key map "\M-sw" 'isearch-toggle-word)
  421. (define-key map "\M-s_" 'isearch-toggle-symbol)
  422. (define-key map [?\M-%] 'isearch-query-replace)
  423. (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
  424. (define-key map "\M-so" 'isearch-occur)
  425. (define-key map "\M-shr" 'isearch-highlight-regexp)
  426. map)
  427. "Keymap for `isearch-mode'.")
  428. (defvar minibuffer-local-isearch-map
  429. (let ((map (make-sparse-keymap)))
  430. (set-keymap-parent map minibuffer-local-map)
  431. (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
  432. (define-key map "\M-\t" 'isearch-complete-edit)
  433. (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
  434. (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
  435. (define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
  436. (define-key map [right] 'isearch-yank-char-in-minibuffer)
  437. map)
  438. "Keymap for editing Isearch strings in the minibuffer.")
  439. ;; Internal variables declared globally for byte-compiler.
  440. ;; These are all set with setq while isearching
  441. ;; and bound locally while editing the search string.
  442. (defvar isearch-forward nil) ; Searching in the forward direction.
  443. (defvar isearch-regexp nil) ; Searching for a regexp.
  444. (defvar isearch-word nil
  445. "Regexp-based search mode for words/symbols.
  446. If t, do incremental search for a sequence of words, ignoring punctuation.
  447. If the value is a function (e.g. `isearch-symbol-regexp'), it is called to
  448. convert the search string to a regexp used by regexp search functions.
  449. The property `isearch-message-prefix' put on this function specifies the
  450. prefix string displyed in the search message.")
  451. (defvar isearch-cmds nil
  452. "Stack of search status sets.
  453. Each set is a vector of the form:
  454. [STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
  455. INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH]")
  456. (defvar isearch-string "") ; The current search string.
  457. (defvar isearch-message "") ; text-char-description version of isearch-string
  458. (defvar isearch-message-prefix-add nil) ; Additional text for the message prefix
  459. (defvar isearch-message-suffix-add nil) ; Additional text for the message suffix
  460. (defvar isearch-success t) ; Searching is currently successful.
  461. (defvar isearch-error nil) ; Error message for failed search.
  462. (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
  463. (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
  464. (defvar isearch-barrier 0
  465. "Recorded minimum/maximal point for the current search.")
  466. (defvar isearch-just-started nil)
  467. (defvar isearch-start-hscroll 0) ; hscroll when starting the search.
  468. ;; case-fold-search while searching.
  469. ;; either nil, t, or 'yes. 'yes means the same as t except that mixed
  470. ;; case in the search string is ignored.
  471. (defvar isearch-case-fold-search nil)
  472. (defvar isearch-last-case-fold-search nil)
  473. ;; Used to save default value while isearch is active
  474. (defvar isearch-original-minibuffer-message-timeout nil)
  475. (defvar isearch-adjusted nil)
  476. (defvar isearch-slow-terminal-mode nil)
  477. ;; If t, using a small window.
  478. (defvar isearch-small-window nil)
  479. (defvar isearch-opoint 0)
  480. ;; The window configuration active at the beginning of the search.
  481. (defvar isearch-window-configuration nil)
  482. ;; Flag to indicate a yank occurred, so don't move the cursor.
  483. (defvar isearch-yank-flag nil)
  484. ;; A function to be called after each input character is processed.
  485. ;; (It is not called after characters that exit the search.)
  486. ;; It is only set from an optional argument to `isearch-mode'.
  487. (defvar isearch-op-fun nil)
  488. ;; Is isearch-mode in a recursive edit for modal searching.
  489. (defvar isearch-recursive-edit nil)
  490. ;; Should isearch be terminated after doing one search?
  491. (defvar isearch-nonincremental nil)
  492. ;; New value of isearch-forward after isearch-edit-string.
  493. (defvar isearch-new-forward nil)
  494. ;; Accumulate here the overlays opened during searching.
  495. (defvar isearch-opened-overlays nil)
  496. ;; Non-nil if the string exists but is invisible.
  497. (defvar isearch-hidden nil)
  498. ;; The value of input-method-function when isearch is invoked.
  499. (defvar isearch-input-method-function nil)
  500. ;; A flag to tell if input-method-function is locally bound when
  501. ;; isearch is invoked.
  502. (defvar isearch-input-method-local-p nil)
  503. ;; Minor-mode-alist changes - kind of redundant with the
  504. ;; echo area, but if isearching in multiple windows, it can be useful.
  505. (or (assq 'isearch-mode minor-mode-alist)
  506. (nconc minor-mode-alist
  507. (list '(isearch-mode isearch-mode))))
  508. (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
  509. (make-variable-buffer-local 'isearch-mode)
  510. (define-key global-map "\C-s" 'isearch-forward)
  511. (define-key esc-map "\C-s" 'isearch-forward-regexp)
  512. (define-key global-map "\C-r" 'isearch-backward)
  513. (define-key esc-map "\C-r" 'isearch-backward-regexp)
  514. (define-key search-map "w" 'isearch-forward-word)
  515. (define-key search-map "_" 'isearch-forward-symbol)
  516. ;; Entry points to isearch-mode.
  517. (defun isearch-forward (&optional regexp-p no-recursive-edit)
  518. "\
  519. Do incremental search forward.
  520. With a prefix argument, do an incremental regular expression search instead.
  521. \\<isearch-mode-map>
  522. As you type characters, they add to the search string and are found.
  523. The following non-printing keys are bound in `isearch-mode-map'.
  524. Type \\[isearch-delete-char] to cancel last input item from end of search string.
  525. Type \\[isearch-exit] to exit, leaving point at location found.
  526. Type LFD (C-j) to match end of line.
  527. Type \\[isearch-repeat-forward] to search again forward,\
  528. \\[isearch-repeat-backward] to search again backward.
  529. Type \\[isearch-yank-word-or-char] to yank next word or character in buffer
  530. onto the end of the search string, and search for it.
  531. Type \\[isearch-del-char] to delete character from end of search string.
  532. Type \\[isearch-yank-char] to yank char from buffer onto end of search\
  533. string and search for it.
  534. Type \\[isearch-yank-line] to yank rest of line onto end of search string\
  535. and search for it.
  536. Type \\[isearch-yank-kill] to yank the last string of killed text.
  537. Type \\[isearch-yank-pop] to replace string just yanked into search prompt
  538. with string killed before it.
  539. Type \\[isearch-quote-char] to quote control character to search for it.
  540. \\[isearch-abort] while searching or when search has failed cancels input\
  541. back to what has
  542. been found successfully.
  543. \\[isearch-abort] when search is successful aborts and moves point to\
  544. starting point.
  545. If you try to exit with the search string still empty, it invokes
  546. nonincremental search.
  547. Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
  548. Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
  549. Type \\[isearch-toggle-word] to toggle word mode.
  550. Type \\[isearch-toggle-symbol] to toggle symbol mode.
  551. Type \\[isearch-edit-string] to edit the search string in the minibuffer.
  552. Also supported is a search ring of the previous 16 search strings.
  553. Type \\[isearch-ring-advance] to search for the next item in the search ring.
  554. Type \\[isearch-ring-retreat] to search for the previous item in the search\
  555. ring.
  556. Type \\[isearch-complete] to complete the search string using the search ring.
  557. Type \\[isearch-query-replace] to run `query-replace' with string to\
  558. replace from last search string.
  559. Type \\[isearch-query-replace-regexp] to run `query-replace-regexp'\
  560. with the last search string.
  561. Type \\[isearch-occur] to run `occur' that shows\
  562. the last search string.
  563. Type \\[isearch-highlight-regexp] to run `highlight-regexp'\
  564. that highlights the last search string.
  565. Type \\[isearch-describe-bindings] to display all Isearch key bindings.
  566. Type \\[isearch-describe-key] to display documentation of Isearch key.
  567. Type \\[isearch-describe-mode] to display documentation of Isearch mode.
  568. If an input method is turned on in the current buffer, that input
  569. method is also active while you are typing characters to search.
  570. To toggle the input method, type \\[isearch-toggle-input-method]. \
  571. It also toggles the input
  572. method in the current buffer.
  573. To use a different input method for searching, type \
  574. \\[isearch-toggle-specified-input-method],
  575. and specify an input method you want to use.
  576. The above keys, bound in `isearch-mode-map', are often controlled by
  577. options; do \\[apropos] on search-.* to find them.
  578. Other control and meta characters terminate the search
  579. and are then executed normally (depending on `search-exit-option').
  580. Likewise for function keys and mouse button events.
  581. If this function is called non-interactively, it does not return to
  582. the calling function until the search is done."
  583. (interactive "P\np")
  584. (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
  585. (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
  586. "\
  587. Do incremental search forward for regular expression.
  588. With a prefix argument, do a regular string search instead.
  589. Like ordinary incremental search except that your input is treated
  590. as a regexp. See the command `isearch-forward' for more information.
  591. In regexp incremental searches, a space or spaces normally matches
  592. any whitespace (the variable `search-whitespace-regexp' controls
  593. precisely what that means). If you want to search for a literal space
  594. and nothing else, enter C-q SPC."
  595. (interactive "P\np")
  596. (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
  597. (defun isearch-forward-word (&optional not-word no-recursive-edit)
  598. "\
  599. Do incremental search forward for a sequence of words.
  600. With a prefix argument, do a regular string search instead.
  601. Like ordinary incremental search except that your input is treated
  602. as a sequence of words without regard to how the words are separated.
  603. See the command `isearch-forward' for more information."
  604. (interactive "P\np")
  605. (isearch-mode t nil nil (not no-recursive-edit) (null not-word)))
  606. (defun isearch-forward-symbol (&optional not-symbol no-recursive-edit)
  607. "\
  608. Do incremental search forward for a symbol.
  609. The prefix argument is currently unused.
  610. Like ordinary incremental search except that your input is treated
  611. as a symbol surrounded by symbol boundary constructs \\_< and \\_>.
  612. See the command `isearch-forward' for more information."
  613. (interactive "P\np")
  614. (isearch-mode t nil nil (not no-recursive-edit) 'isearch-symbol-regexp))
  615. (defun isearch-backward (&optional regexp-p no-recursive-edit)
  616. "\
  617. Do incremental search backward.
  618. With a prefix argument, do a regular expression search instead.
  619. See the command `isearch-forward' for more information."
  620. (interactive "P\np")
  621. (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
  622. (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
  623. "\
  624. Do incremental search backward for regular expression.
  625. With a prefix argument, do a regular string search instead.
  626. Like ordinary incremental search except that your input is treated
  627. as a regexp. See the command `isearch-forward' for more information."
  628. (interactive "P\np")
  629. (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
  630. ;; isearch-mode only sets up incremental search for the minor mode.
  631. ;; All the work is done by the isearch-mode commands.
  632. ;; Not used yet:
  633. ;;(defvar isearch-commands '(isearch-forward isearch-backward
  634. ;; isearch-forward-regexp isearch-backward-regexp)
  635. ;; "List of commands for which isearch-mode does not recursive-edit.")
  636. (defun isearch-mode (forward &optional regexp op-fun recursive-edit word)
  637. "Start Isearch minor mode.
  638. It is called by the function `isearch-forward' and other related functions."
  639. ;; Initialize global vars.
  640. (setq isearch-forward forward
  641. isearch-regexp regexp
  642. isearch-word word
  643. isearch-op-fun op-fun
  644. isearch-last-case-fold-search isearch-case-fold-search
  645. isearch-case-fold-search case-fold-search
  646. isearch-string ""
  647. isearch-message ""
  648. isearch-cmds nil
  649. isearch-success t
  650. isearch-wrapped nil
  651. isearch-barrier (point)
  652. isearch-adjusted nil
  653. isearch-yank-flag nil
  654. isearch-error nil
  655. isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
  656. (> (window-height)
  657. (* 4
  658. (abs search-slow-window-lines))))
  659. isearch-other-end nil
  660. isearch-small-window nil
  661. isearch-just-started t
  662. isearch-start-hscroll (window-hscroll)
  663. isearch-opoint (point)
  664. search-ring-yank-pointer nil
  665. isearch-opened-overlays nil
  666. isearch-input-method-function input-method-function
  667. isearch-input-method-local-p (local-variable-p 'input-method-function)
  668. regexp-search-ring-yank-pointer nil
  669. ;; Save the original value of `minibuffer-message-timeout', and
  670. ;; set it to nil so that isearch's messages don't get timed out.
  671. isearch-original-minibuffer-message-timeout minibuffer-message-timeout
  672. minibuffer-message-timeout nil)
  673. ;; We must bypass input method while reading key. When a user type
  674. ;; printable character, appropriate input method is turned on in
  675. ;; minibuffer to read multibyte characters.
  676. (or isearch-input-method-local-p
  677. (make-local-variable 'input-method-function))
  678. (setq input-method-function nil)
  679. (looking-at "")
  680. (setq isearch-window-configuration
  681. (if isearch-slow-terminal-mode (current-window-configuration) nil))
  682. ;; Maybe make minibuffer frame visible and/or raise it.
  683. (let ((frame (window-frame (minibuffer-window))))
  684. (unless (memq (frame-live-p frame) '(nil t))
  685. (unless (frame-visible-p frame)
  686. (make-frame-visible frame))
  687. (if minibuffer-auto-raise
  688. (raise-frame frame))))
  689. (setq isearch-mode " Isearch") ;; forward? regexp?
  690. (force-mode-line-update)
  691. (setq overriding-terminal-local-map isearch-mode-map)
  692. (run-hooks 'isearch-mode-hook)
  693. ;; Pushing the initial state used to be before running isearch-mode-hook,
  694. ;; but a hook might set `isearch-push-state-function' used in
  695. ;; `isearch-push-state' to save mode-specific initial state. (Bug#4994)
  696. (isearch-push-state)
  697. (isearch-update)
  698. (add-hook 'mouse-leave-buffer-hook 'isearch-done)
  699. (add-hook 'kbd-macro-termination-hook 'isearch-done)
  700. ;; isearch-mode can be made modal (in the sense of not returning to
  701. ;; the calling function until searching is completed) by entering
  702. ;; a recursive-edit and exiting it when done isearching.
  703. (if recursive-edit
  704. (let ((isearch-recursive-edit t))
  705. (recursive-edit)))
  706. isearch-success)
  707. ;; Some high level utilities. Others below.
  708. (defun isearch-update ()
  709. "This is called after every isearch command to update the display.
  710. The last thing it does is to run `isearch-update-post-hook'."
  711. (if (and (null unread-command-events)
  712. (null executing-kbd-macro))
  713. (progn
  714. (if (not (input-pending-p))
  715. (if isearch-message-function
  716. (funcall isearch-message-function)
  717. (isearch-message)))
  718. (if (and isearch-slow-terminal-mode
  719. (not (or isearch-small-window
  720. (pos-visible-in-window-p))))
  721. (let ((found-point (point)))
  722. (setq isearch-small-window t)
  723. (move-to-window-line 0)
  724. (let ((window-min-height 1))
  725. (split-window nil (if (< search-slow-window-lines 0)
  726. (1+ (- search-slow-window-lines))
  727. (- (window-height)
  728. (1+ search-slow-window-lines)))))
  729. (if (< search-slow-window-lines 0)
  730. (progn (vertical-motion (- 1 search-slow-window-lines))
  731. (set-window-start (next-window) (point))
  732. (set-window-hscroll (next-window)
  733. (window-hscroll))
  734. (set-window-hscroll (selected-window) 0))
  735. (other-window 1))
  736. (goto-char found-point))
  737. ;; Keep same hscrolling as at the start of the search when possible
  738. (let ((current-scroll (window-hscroll)))
  739. (set-window-hscroll (selected-window) isearch-start-hscroll)
  740. (unless (pos-visible-in-window-p)
  741. (set-window-hscroll (selected-window) current-scroll))))
  742. (if isearch-other-end
  743. (if (< isearch-other-end (point)) ; isearch-forward?
  744. (isearch-highlight isearch-other-end (point))
  745. (isearch-highlight (point) isearch-other-end))
  746. (isearch-dehighlight))
  747. ))
  748. (setq ;; quit-flag nil not for isearch-mode
  749. isearch-adjusted nil
  750. isearch-yank-flag nil)
  751. (when isearch-lazy-highlight
  752. (isearch-lazy-highlight-new-loop))
  753. ;; We must prevent the point moving to the end of composition when a
  754. ;; part of the composition has just been searched.
  755. (setq disable-point-adjustment t)
  756. (run-hooks 'isearch-update-post-hook))
  757. (defun isearch-done (&optional nopush edit)
  758. "Exit Isearch mode.
  759. For successful search, pass no args.
  760. For a failing search, NOPUSH is t.
  761. For going to the minibuffer to edit the search string,
  762. NOPUSH is t and EDIT is t."
  763. (if isearch-resume-in-command-history
  764. (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
  765. ,isearch-word ,isearch-forward
  766. ,isearch-message
  767. ',isearch-case-fold-search)))
  768. (unless (equal (car command-history) command)
  769. (setq command-history (cons command command-history)))))
  770. (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
  771. (remove-hook 'kbd-macro-termination-hook 'isearch-done)
  772. (setq isearch-lazy-highlight-start nil)
  773. ;; Called by all commands that terminate isearch-mode.
  774. ;; If NOPUSH is non-nil, we don't push the string on the search ring.
  775. (setq overriding-terminal-local-map nil)
  776. ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
  777. (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
  778. (isearch-dehighlight)
  779. (lazy-highlight-cleanup lazy-highlight-cleanup)
  780. (let ((found-start (window-start (selected-window)))
  781. (found-point (point)))
  782. (when isearch-window-configuration
  783. (set-window-configuration isearch-window-configuration)
  784. (if isearch-small-window
  785. (goto-char found-point)
  786. ;; set-window-configuration clobbers window-start; restore it.
  787. ;; This has an annoying side effect of clearing the last_modiff
  788. ;; field of the window, which can cause unwanted scrolling,
  789. ;; so don't do it unless truly necessary.
  790. (set-window-start (selected-window) found-start t))))
  791. (setq isearch-mode nil)
  792. (if isearch-input-method-local-p
  793. (setq input-method-function isearch-input-method-function)
  794. (kill-local-variable 'input-method-function))
  795. (force-mode-line-update)
  796. ;; If we ended in the middle of some intangible text,
  797. ;; move to the further end of that intangible text.
  798. (let ((after (if (eobp) nil
  799. (get-text-property (point) 'intangible)))
  800. (before (if (bobp) nil
  801. (get-text-property (1- (point)) 'intangible))))
  802. (when (and before after (eq before after))
  803. (if isearch-forward
  804. (goto-char (next-single-property-change (point) 'intangible))
  805. (goto-char (previous-single-property-change (point) 'intangible)))))
  806. (if (and (> (length isearch-string) 0) (not nopush))
  807. ;; Update the ring data.
  808. (isearch-update-ring isearch-string isearch-regexp))
  809. (let ((isearch-mode-end-hook-quit (and nopush (not edit))))
  810. (run-hooks 'isearch-mode-end-hook))
  811. ;; If there was movement, mark the starting position.
  812. ;; Maybe should test difference between and set mark only if > threshold.
  813. (if (/= (point) isearch-opoint)
  814. (or (and transient-mark-mode mark-active)
  815. (progn
  816. (push-mark isearch-opoint t)
  817. (or executing-kbd-macro (> (minibuffer-depth) 0) edit
  818. (message "Mark saved where search started")))))
  819. (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
  820. (defun isearch-update-ring (string &optional regexp)
  821. "Add STRING to the beginning of the search ring.
  822. REGEXP if non-nil says use the regexp search ring."
  823. (add-to-history
  824. (if regexp 'regexp-search-ring 'search-ring)
  825. string
  826. (if regexp regexp-search-ring-max search-ring-max)))
  827. ;; Switching buffers should first terminate isearch-mode.
  828. ;; ;; For Emacs 19, the frame switch event is handled.
  829. ;; (defun isearch-switch-frame-handler ()
  830. ;; (interactive) ;; Is this necessary?
  831. ;; ;; First terminate isearch-mode.
  832. ;; (isearch-done)
  833. ;; (isearch-clean-overlays)
  834. ;; (handle-switch-frame (car (cdr last-command-event))))
  835. ;; The search status structure and stack.
  836. (defsubst isearch-string-state (frame)
  837. "Return the search string in FRAME."
  838. (aref frame 0))
  839. (defsubst isearch-message-state (frame)
  840. "Return the search string to display to the user in FRAME."
  841. (aref frame 1))
  842. (defsubst isearch-point-state (frame)
  843. "Return the point in FRAME."
  844. (aref frame 2))
  845. (defsubst isearch-success-state (frame)
  846. "Return the success flag in FRAME."
  847. (aref frame 3))
  848. (defsubst isearch-forward-state (frame)
  849. "Return the searching-forward flag in FRAME."
  850. (aref frame 4))
  851. (defsubst isearch-other-end-state (frame)
  852. "Return the other end of the match in FRAME."
  853. (aref frame 5))
  854. (defsubst isearch-word-state (frame)
  855. "Return the search-by-word flag in FRAME."
  856. (aref frame 6))
  857. (defsubst isearch-error-state (frame)
  858. "Return the regexp error message in FRAME, or nil if its regexp is valid."
  859. (aref frame 7))
  860. (defsubst isearch-wrapped-state (frame)
  861. "Return the search-wrapped flag in FRAME."
  862. (aref frame 8))
  863. (defsubst isearch-barrier-state (frame)
  864. "Return the barrier value in FRAME."
  865. (aref frame 9))
  866. (defsubst isearch-case-fold-search-state (frame)
  867. "Return the case-folding flag in FRAME."
  868. (aref frame 10))
  869. (defsubst isearch-pop-fun-state (frame)
  870. "Return the function restoring the mode-specific Isearch state in FRAME."
  871. (aref frame 11))
  872. (defun isearch-top-state ()
  873. (let ((cmd (car isearch-cmds)))
  874. (setq isearch-string (isearch-string-state cmd)
  875. isearch-message (isearch-message-state cmd)
  876. isearch-success (isearch-success-state cmd)
  877. isearch-forward (isearch-forward-state cmd)
  878. isearch-other-end (isearch-other-end-state cmd)
  879. isearch-word (isearch-word-state cmd)
  880. isearch-error (isearch-error-state cmd)
  881. isearch-wrapped (isearch-wrapped-state cmd)
  882. isearch-barrier (isearch-barrier-state cmd)
  883. isearch-case-fold-search (isearch-case-fold-search-state cmd))
  884. (if (functionp (isearch-pop-fun-state cmd))
  885. (funcall (isearch-pop-fun-state cmd) cmd))
  886. (goto-char (isearch-point-state cmd))))
  887. (defun isearch-pop-state ()
  888. (setq isearch-cmds (cdr isearch-cmds))
  889. (isearch-top-state))
  890. (defun isearch-push-state ()
  891. (setq isearch-cmds
  892. (cons (vector isearch-string isearch-message (point)
  893. isearch-success isearch-forward isearch-other-end
  894. isearch-word
  895. isearch-error isearch-wrapped isearch-barrier
  896. isearch-case-fold-search
  897. (if isearch-push-state-function
  898. (funcall isearch-push-state-function)))
  899. isearch-cmds)))
  900. ;; Commands active while inside of the isearch minor mode.
  901. (defun isearch-exit ()
  902. "Exit search normally.
  903. However, if this is the first command after starting incremental
  904. search and `search-nonincremental-instead' is non-nil, do a
  905. nonincremental search instead via `isearch-edit-string'."
  906. (interactive)
  907. (if (and search-nonincremental-instead
  908. (= 0 (length isearch-string)))
  909. (let ((isearch-nonincremental t))
  910. (isearch-edit-string)))
  911. (isearch-done)
  912. (isearch-clean-overlays))
  913. (defvar minibuffer-history-symbol) ;; from external package gmhist.el
  914. (defun isearch-fail-pos (&optional msg)
  915. "Return position of first mismatch in search string, or nil if none.
  916. If MSG is non-nil, use `isearch-message', otherwise `isearch-string'."
  917. (let ((cmds isearch-cmds)
  918. (curr-msg (if msg isearch-message isearch-string))
  919. succ-msg)
  920. (when (or (not isearch-success) isearch-error)
  921. (while (or (not (isearch-success-state (car cmds)))
  922. (isearch-error-state (car cmds)))
  923. (pop cmds))
  924. (setq succ-msg (and cmds (if msg (isearch-message-state (car cmds))
  925. (isearch-string-state (car cmds)))))
  926. (if (and (stringp succ-msg)
  927. (< (length succ-msg) (length curr-msg))
  928. (equal succ-msg
  929. (substring curr-msg 0 (length succ-msg))))
  930. (length succ-msg)
  931. 0))))
  932. (defun isearch-edit-string ()
  933. "Edit the search string in the minibuffer.
  934. The following additional command keys are active while editing.
  935. \\<minibuffer-local-isearch-map>
  936. \\[exit-minibuffer] to resume incremental searching with the edited string.
  937. \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
  938. \\[isearch-forward-exit-minibuffer] to resume isearching forward.
  939. \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
  940. \\[isearch-complete-edit] to complete the search string using the search ring."
  941. ;; This code is very hairy for several reasons, explained in the code.
  942. ;; Mainly, isearch-mode must be terminated while editing and then restarted.
  943. ;; If there were a way to catch any change of buffer from the minibuffer,
  944. ;; this could be simplified greatly.
  945. ;; Editing doesn't back up the search point. Should it?
  946. (interactive)
  947. (condition-case nil
  948. (progn
  949. (let ((isearch-nonincremental isearch-nonincremental)
  950. ;; Locally bind all isearch global variables to protect them
  951. ;; from recursive isearching.
  952. ;; isearch-string -message and -forward are not bound
  953. ;; so they may be changed. Instead, save the values.
  954. (isearch-new-string isearch-string)
  955. (isearch-new-message isearch-message)
  956. (isearch-new-forward isearch-forward)
  957. (isearch-new-word isearch-word)
  958. (isearch-new-case-fold isearch-case-fold-search)
  959. (isearch-regexp isearch-regexp)
  960. (isearch-op-fun isearch-op-fun)
  961. (isearch-cmds isearch-cmds)
  962. (isearch-success isearch-success)
  963. (isearch-wrapped isearch-wrapped)
  964. (isearch-barrier isearch-barrier)
  965. (isearch-adjusted isearch-adjusted)
  966. (isearch-yank-flag isearch-yank-flag)
  967. (isearch-error isearch-error)
  968. ;;; Don't bind this. We want isearch-search, below, to set it.
  969. ;;; And the old value won't matter after that.
  970. ;;; (isearch-other-end isearch-other-end)
  971. ;;; Perhaps some of these other variables should be bound for a
  972. ;;; shorter period, ending before the next isearch-search.
  973. ;;; But there doesn't seem to be a real bug, so let's not risk it now.
  974. (isearch-opoint isearch-opoint)
  975. (isearch-slow-terminal-mode isearch-slow-terminal-mode)
  976. (isearch-small-window isearch-small-window)
  977. (isearch-recursive-edit isearch-recursive-edit)
  978. ;; Save current configuration so we can restore it here.
  979. (isearch-window-configuration (current-window-configuration))
  980. ;; This could protect the index of the search rings,
  981. ;; but we can't reliably count the number of typed M-p
  982. ;; in `read-from-minibuffer' to adjust the index accordingly.
  983. ;; So when the following is commented out, `isearch-mode'
  984. ;; below resets the index to the predictable value nil.
  985. ;; (search-ring-yank-pointer search-ring-yank-pointer)
  986. ;; (regexp-search-ring-yank-pointer regexp-search-ring-yank-pointer)
  987. ;; Temporarily restore `minibuffer-message-timeout'.
  988. (minibuffer-message-timeout
  989. isearch-original-minibuffer-message-timeout)
  990. (isearch-original-minibuffer-message-timeout
  991. isearch-original-minibuffer-message-timeout)
  992. old-point old-other-end)
  993. ;; Actually terminate isearching until editing is done.
  994. ;; This is so that the user can do anything without failure,
  995. ;; like switch buffers and start another isearch, and return.
  996. (condition-case nil
  997. (isearch-done t t)
  998. (exit nil)) ; was recursive editing
  999. ;; Save old point and isearch-other-end before reading from minibuffer
  1000. ;; that can change their values.
  1001. (setq old-point (point) old-other-end isearch-other-end)
  1002. (unwind-protect
  1003. (let* ((message-log-max nil)
  1004. ;; Don't add a new search string to the search ring here
  1005. ;; in `read-from-minibuffer'. It should be added only
  1006. ;; by `isearch-update-ring' called from `isearch-done'.
  1007. (history-add-new-input nil)
  1008. ;; Binding minibuffer-history-symbol to nil is a work-around
  1009. ;; for some incompatibility with gmhist.
  1010. (minibuffer-history-symbol))
  1011. (setq isearch-new-string
  1012. (read-from-minibuffer
  1013. (isearch-message-prefix nil nil isearch-nonincremental)
  1014. (cons isearch-string (1+ (or (isearch-fail-pos)
  1015. (length isearch-string))))
  1016. minibuffer-local-isearch-map nil
  1017. (if isearch-regexp
  1018. (cons 'regexp-search-ring
  1019. (1+ (or regexp-search-ring-yank-pointer -1)))
  1020. (cons 'search-ring
  1021. (1+ (or search-ring-yank-pointer -1))))
  1022. nil t)
  1023. isearch-new-message
  1024. (mapconcat 'isearch-text-char-description
  1025. isearch-new-string "")))
  1026. ;; Set point at the start (end) of old match if forward (backward),
  1027. ;; so after exiting minibuffer isearch resumes at the start (end)
  1028. ;; of this match and can find it again.
  1029. (if (and old-other-end (eq old-point (point))
  1030. (eq isearch-forward isearch-new-forward))
  1031. (goto-char old-other-end))
  1032. ;; Always resume isearching by restarting it.
  1033. (isearch-mode isearch-forward
  1034. isearch-regexp
  1035. isearch-op-fun
  1036. nil
  1037. isearch-word)
  1038. ;; Copy new local values to isearch globals
  1039. (setq isearch-string isearch-new-string
  1040. isearch-message isearch-new-message
  1041. isearch-forward isearch-new-forward
  1042. isearch-word isearch-new-word
  1043. isearch-case-fold-search isearch-new-case-fold))
  1044. ;; Empty isearch-string means use default.
  1045. (when (= 0 (length isearch-string))
  1046. (setq isearch-string (or (car (if isearch-regexp
  1047. regexp-search-ring
  1048. search-ring))
  1049. "")
  1050. isearch-message
  1051. (mapconcat 'isearch-text-char-description
  1052. isearch-string ""))
  1053. ;; After taking the last element, adjust ring to previous one.
  1054. (isearch-ring-adjust1 nil)))
  1055. ;; This used to push the state as of before this C-s, but it adds
  1056. ;; an inconsistent state where part of variables are from the
  1057. ;; previous search (e.g. `isearch-success'), and part of variables
  1058. ;; are just entered from the minibuffer (e.g. `isearch-string').
  1059. ;; (isearch-push-state)
  1060. ;; Reinvoke the pending search.
  1061. (isearch-search)
  1062. (isearch-push-state) ; this pushes the correct state
  1063. (isearch-update)
  1064. (if isearch-nonincremental
  1065. (progn
  1066. ;; (sit-for 1) ;; needed if isearch-done does: (message "")
  1067. (isearch-done)
  1068. ;; The search done message is confusing when the string
  1069. ;; is empty, so erase it.
  1070. (if (equal isearch-string "")
  1071. (message "")))))
  1072. (quit ; handle abort-recursive-edit
  1073. (isearch-abort) ;; outside of let to restore outside global values
  1074. )))
  1075. (defun isearch-nonincremental-exit-minibuffer ()
  1076. (interactive)
  1077. (setq isearch-nonincremental t)
  1078. (exit-minibuffer))
  1079. (defun isearch-forward-exit-minibuffer ()
  1080. (interactive)
  1081. (setq isearch-new-forward t)
  1082. (exit-minibuffer))
  1083. (defun isearch-reverse-exit-minibuffer ()
  1084. (interactive)
  1085. (setq isearch-new-forward nil)
  1086. (exit-minibuffer))
  1087. (defun isearch-cancel ()
  1088. "Terminate the search and go back to the starting point."
  1089. (interactive)
  1090. (if (and isearch-push-state-function isearch-cmds)
  1091. ;; For defined push-state function, restore the first state.
  1092. ;; This calls pop-state function and restores original point.
  1093. (let ((isearch-cmds (last isearch-cmds)))
  1094. (isearch-top-state))
  1095. (goto-char isearch-opoint))
  1096. (isearch-done t) ; exit isearch
  1097. (isearch-clean-overlays)
  1098. (signal 'quit nil)) ; and pass on quit signal
  1099. (defun isearch-abort ()
  1100. "Abort incremental search mode if searching is successful, signaling quit.
  1101. Otherwise, revert to previous successful search and continue searching.
  1102. Use `isearch-exit' to quit without signaling."
  1103. (interactive)
  1104. ;; (ding) signal instead below, if quitting
  1105. (discard-input)
  1106. (if (and isearch-success (not isearch-error))
  1107. ;; If search is successful and has no incomplete regexp,
  1108. ;; move back to starting point and really do quit.
  1109. (progn
  1110. (setq isearch-success nil)
  1111. (isearch-cancel))
  1112. ;; If search is failing, or has an incomplete regexp,
  1113. ;; rub out until it is once more successful.
  1114. (while (or (not isearch-success) isearch-error)
  1115. (isearch-pop-state))
  1116. (isearch-update)))
  1117. (defun isearch-repeat (direction)
  1118. ;; Utility for isearch-repeat-forward and -backward.
  1119. (if (eq isearch-forward (eq direction 'forward))
  1120. ;; C-s in forward or C-r in reverse.
  1121. (if (equal isearch-string "")
  1122. ;; If search string is empty, use last one.
  1123. (if (null (if isearch-regexp regexp-search-ring search-ring))
  1124. (setq isearch-error "No previous search string")
  1125. (setq isearch-string
  1126. (if isearch-regexp
  1127. (car regexp-search-ring)
  1128. (car search-ring))
  1129. isearch-message
  1130. (mapconcat 'isearch-text-char-description
  1131. isearch-string "")
  1132. isearch-case-fold-search isearch-last-case-fold-search)
  1133. ;; After taking the last element, adjust ring to previous one.
  1134. (isearch-ring-adjust1 nil))
  1135. ;; If already have what to search for, repeat it.
  1136. (or isearch-success
  1137. (progn
  1138. ;; Set isearch-wrapped before calling isearch-wrap-function
  1139. (setq isearch-wrapped t)
  1140. (if isearch-wrap-function
  1141. (funcall isearch-wrap-function)
  1142. (goto-char (if isearch-forward (point-min) (point-max)))))))
  1143. ;; C-s in reverse or C-r in forward, change direction.
  1144. (setq isearch-forward (not isearch-forward)
  1145. isearch-success t))
  1146. (setq isearch-barrier (point)) ; For subsequent \| if regexp.
  1147. (if (equal isearch-string "")
  1148. (setq isearch-success t)
  1149. (if (and isearch-success
  1150. (equal (point) isearch-other-end)
  1151. (not isearch-just-started))
  1152. ;; If repeating a search that found
  1153. ;; an empty string, ensure we advance.
  1154. (if (if isearch-forward (eobp) (bobp))
  1155. ;; If there's nowhere to advance to, fail (and wrap next time).
  1156. (progn
  1157. (setq isearch-success nil)
  1158. (ding))
  1159. (forward-char (if isearch-forward 1 -1))
  1160. (isearch-search))
  1161. (isearch-search)))
  1162. (isearch-push-state)
  1163. (isearch-update))
  1164. (defun isearch-repeat-forward ()
  1165. "Repeat incremental search forwards."
  1166. (interactive)
  1167. (isearch-repeat 'forward))
  1168. (defun isearch-repeat-backward ()
  1169. "Repeat incremental search backwards."
  1170. (interactive)
  1171. (isearch-repeat 'backward))
  1172. (defun isearch-toggle-regexp ()
  1173. "Toggle regexp searching on or off."
  1174. ;; The status stack is left unchanged.
  1175. (interactive)
  1176. (setq isearch-regexp (not isearch-regexp))
  1177. (if isearch-regexp (setq isearch-word nil))
  1178. (setq isearch-success t isearch-adjusted t)
  1179. (isearch-update))
  1180. (defun isearch-toggle-word ()
  1181. "Toggle word searching on or off."
  1182. (interactive)
  1183. (setq isearch-word (not isearch-word))
  1184. (setq isearch-success t isearch-adjusted t)
  1185. (isearch-update))
  1186. (defun isearch-toggle-symbol ()
  1187. "Toggle symbol searching on or off."
  1188. (interactive)
  1189. (setq isearch-word (unless (eq isearch-word 'isearch-symbol-regexp)
  1190. 'isearch-symbol-regexp))
  1191. (setq isearch-success t isearch-adjusted t)
  1192. (isearch-update))
  1193. (defun isearch-toggle-case-fold ()
  1194. "Toggle case folding in searching on or off."
  1195. (interactive)
  1196. (setq isearch-case-fold-search
  1197. (if isearch-case-fold-search nil 'yes))
  1198. (let ((message-log-max nil))
  1199. (message "%s%s [case %ssensitive]"
  1200. (isearch-message-prefix nil nil isearch-nonincremental)
  1201. isearch-message
  1202. (if isearch-case-fold-search "in" "")))
  1203. (setq isearch-success t isearch-adjusted t)
  1204. (sit-for 1)
  1205. (isearch-update))
  1206. ;; Word search
  1207. (defun word-search-regexp (string &optional lax)
  1208. "Return a regexp which matches words, ignoring punctuation.
  1209. Given STRING, a string of words separated by word delimiters,
  1210. compute a regexp that matches those exact words separated by
  1211. arbitrary punctuation. If LAX is non-nil, the end of the string
  1212. need not match a word boundary unless it ends in whitespace.
  1213. Used in `word-search-forward', `word-search-backward',
  1214. `word-search-forward-lax', `word-search-backward-lax'."
  1215. (if (string-match-p "^\\W*$" string)
  1216. ""
  1217. (concat
  1218. "\\b"
  1219. (mapconcat 'identity (split-string string "\\W+" t) "\\W+")
  1220. (if (or (not lax) (string-match-p "\\W$" string)) "\\b"))))
  1221. (defun word-search-backward (string &optional bound noerror count)
  1222. "Search backward from point for STRING, ignoring differences in punctuation.
  1223. Set point to the beginning of the occurrence found, and return point.
  1224. An optional second argument bounds the search; it is a buffer position.
  1225. The match found must not extend before that position.
  1226. Optional third argument, if t, means if fail just return nil (no error).
  1227. If not nil and not t, move to limit of search and return nil.
  1228. Optional fourth argument is repeat count--search for successive occurrences.
  1229. Relies on the function `word-search-regexp' to convert a sequence
  1230. of words in STRING to a regexp used to search words without regard
  1231. to punctuation."
  1232. (interactive "sWord search backward: ")
  1233. (re-search-backward (word-search-regexp string nil) bound noerror count))
  1234. (defun word-search-forward (string &optional bound noerror count)
  1235. "Search forward from point for STRING, ignoring differences in punctuation.
  1236. Set point to the end of the occurrence found, and return point.
  1237. An optional second argument bounds the search; it is a buffer position.
  1238. The match found must not extend after that position.
  1239. Optional third argument, if t, means if fail just return nil (no error).
  1240. If not nil and not t, move to limit of search and return nil.
  1241. Optional fourth argument is repeat count--search for successive occurrences.
  1242. Relies on the function `word-search-regexp' to convert a sequence
  1243. of words in STRING to a regexp used to search words without regard
  1244. to punctuation."
  1245. (interactive "sWord search: ")
  1246. (re-search-forward (word-search-regexp string nil) bound noerror count))
  1247. (defun word-search-backward-lax (string &optional bound noerror count)
  1248. "Search backward from point for STRING, ignoring differences in punctuation.
  1249. Set point to the beginning of the occurrence found, and return point.
  1250. Unlike `word-search-backward', the end of STRING need not match a word
  1251. boundary, unless STRING ends in whitespace.
  1252. An optional second argument bounds the search; it is a buffer position.
  1253. The match found must not extend before that position.
  1254. Optional third argument, if t, means if fail just return nil (no error).
  1255. If not nil and not t, move to limit of search and return nil.
  1256. Optional fourth argument is repeat count--search for successive occurrences.
  1257. Relies on the function `word-search-regexp' to convert a sequence
  1258. of words in STRING to a regexp used to search words without regard
  1259. to punctuation."
  1260. (interactive "sWord search backward: ")
  1261. (re-search-backward (word-search-regexp string t) bound noerror count))
  1262. (defun word-search-forward-lax (string &optional bound noerror count)
  1263. "Search forward from point for STRING, ignoring differences in punctuation.
  1264. Set point to the end of the occurrence found, and return point.
  1265. Unlike `word-search-forward', the end of STRING need not match a word
  1266. boundary, unless STRING ends in whitespace.
  1267. An optional second argument bounds the search; it is a buffer position.
  1268. The match found must not extend after that position.
  1269. Optional third argument, if t, means if fail just return nil (no error).
  1270. If not nil and not t, move to limit of search and return nil.
  1271. Optional fourth argument is repeat count--search for successive occurrences.
  1272. Relies on the function `word-search-regexp' to convert a sequence
  1273. of words in STRING to a regexp used to search words without regard
  1274. to punctuation."
  1275. (interactive "sWord search: ")
  1276. (re-search-forward (word-search-regexp string t) bound noerror count))
  1277. ;; Symbol search
  1278. (defun isearch-symbol-regexp (string &optional lax)
  1279. "Return a regexp which matches STRING as a symbol.
  1280. Creates a regexp where STRING is surrounded by symbol delimiters \\_< and \\_>.
  1281. If LAX is non-nil, the end of the string need not match a symbol boundary."
  1282. (concat "\\_<" (regexp-quote string) (unless lax "\\_>")))
  1283. (put 'isearch-symbol-regexp 'isearch-message-prefix "symbol ")
  1284. (defun isearch-query-replace (&optional delimited regexp-flag)
  1285. "Start `query-replace' with string to replace from last search string.
  1286. The arg DELIMITED (prefix arg if interactive), if non-nil, means replace
  1287. only matches surrounded by word boundaries. Note that using the prefix arg
  1288. is possible only when `isearch-allow-scroll' is non-nil, and it doesn't
  1289. always provide the correct matches for `query-replace', so the preferred
  1290. way to run word replacements from Isearch is `M-s w ... M-%'."
  1291. (interactive
  1292. (list current-prefix-arg))
  1293. (barf-if-buffer-read-only)
  1294. (if regexp-flag (setq isearch-regexp t))
  1295. (let ((case-fold-search isearch-case-fold-search)
  1296. ;; set `search-upper-case' to nil to not call
  1297. ;; `isearch-no-upper-case-p' in `perform-replace'
  1298. (search-upper-case nil)
  1299. ;; Set `isearch-recursive-edit' to nil to prevent calling
  1300. ;; `exit-recursive-edit' in `isearch-done' that terminates
  1301. ;; the execution of this command when it is non-nil.
  1302. ;; We call `exit-recursive-edit' explicitly at the end below.
  1303. (isearch-recursive-edit nil))
  1304. (isearch-done nil t)
  1305. (isearch-clean-overlays)
  1306. (if (and isearch-other-end
  1307. (< isearch-other-end (point))
  1308. (not (and transient-mark-mode mark-active
  1309. (< (mark) (point)))))
  1310. (goto-char isearch-other-end))
  1311. (set query-replace-from-history-variable
  1312. (cons isearch-string
  1313. (symbol-value query-replace-from-history-variable)))
  1314. (perform-replace
  1315. isearch-string
  1316. (query-replace-read-to
  1317. isearch-string
  1318. (concat "Query replace"
  1319. (if (or delimited isearch-word) " word" "")
  1320. (if isearch-regexp " regexp" "")
  1321. (if (and transient-mark-mode mark-active) " in region" ""))
  1322. isearch-regexp)
  1323. t isearch-regexp (or delimited isearch-word) nil nil
  1324. (if (and transient-mark-mode mark-active) (region-beginning))
  1325. (if (and transient-mark-mode mark-active) (region-end))))
  1326. (and isearch-recursive-edit (exit-recursive-edit)))
  1327. (defun isearch-query-replace-regexp (&optional delimited)
  1328. "Start `query-replace-regexp' with string to replace from last search string.
  1329. See `isearch-query-replace' for more information."
  1330. (interactive
  1331. (list current-prefix-arg))
  1332. (isearch-query-replace delimited t))
  1333. (defun isearch-occur (regexp &optional nlines)
  1334. "Run `occur' using the last search string as the regexp.
  1335. Interactively, REGEXP is constructed using the search string from the
  1336. last search command. NLINES has the same meaning as in `occur'.
  1337. If the last search command was a word search, REGEXP is computed from
  1338. the search words, ignoring punctuation. If the last search
  1339. command was a regular expression search, REGEXP is the regular
  1340. expression used in that search. If the last search command searched
  1341. for a literal string, REGEXP is constructed by quoting all the special
  1342. characters in that string."
  1343. (interactive
  1344. (let* ((perform-collect (consp current-prefix-arg))
  1345. (regexp (cond
  1346. ((functionp isearch-word)
  1347. (funcall isearch-word isearch-string))
  1348. (isearch-word (word-search-regexp isearch-string))
  1349. (isearch-regexp isearch-string)
  1350. (t (regexp-quote isearch-string)))))
  1351. (list regexp
  1352. (if perform-collect
  1353. ;; Perform collect operation
  1354. (if (zerop (regexp-opt-depth regexp))
  1355. ;; No subexpression so collect the entire match.
  1356. "\\&"
  1357. ;; Get the regexp for collection pattern.
  1358. (isearch-done nil t)
  1359. (isearch-clean-overlays)
  1360. (let ((default (car occur-collect-regexp-history)))
  1361. (read-string
  1362. (format "Regexp to collect (default %s): " default)
  1363. nil 'occur-collect-regexp-history default)))
  1364. ;; Otherwise normal occur takes numerical prefix argument.
  1365. (when current-prefix-arg
  1366. (prefix-numeric-value current-prefix-arg))))))
  1367. (let ((case-fold-search isearch-case-fold-search)
  1368. ;; Set `search-upper-case' to nil to not call
  1369. ;; `isearch-no-upper-case-p' in `occur-1'.
  1370. (search-upper-case nil)
  1371. (search-spaces-regexp (if isearch-regexp search-whitespace-regexp)))
  1372. (occur regexp nlines)))
  1373. (declare-function hi-lock-read-face-name "hi-lock" ())
  1374. (defun isearch-highlight-regexp ()
  1375. "Run `highlight-regexp' with regexp from the current search string.
  1376. It exits Isearch mode and calls `hi-lock-face-buffer' with its regexp
  1377. argument from the last search regexp or a quoted search string,
  1378. and reads its face argument using `hi-lock-read-face-name'."
  1379. (interactive)
  1380. (let (
  1381. ;; Set `isearch-recursive-edit' to nil to prevent calling
  1382. ;; `exit-recursive-edit' in `isearch-done' that terminates
  1383. ;; the execution of this command when it is non-nil.
  1384. ;; We call `exit-recursive-edit' explicitly at the end below.
  1385. (isearch-recursive-edit nil))
  1386. (isearch-done nil t)
  1387. (isearch-clean-overlays))
  1388. (require 'hi-lock nil t)
  1389. (let ((string (cond (isearch-regexp isearch-string)
  1390. ((if (and (eq isearch-case-fold-search t)
  1391. search-upper-case)
  1392. (isearch-no-upper-case-p
  1393. isearch-string isearch-regexp)
  1394. isearch-case-fold-search)
  1395. ;; Turn isearch-string into a case-insensitive
  1396. ;; regexp.
  1397. (mapconcat
  1398. (lambda (c)
  1399. (let ((s (string c)))
  1400. (if (string-match "[[:alpha:]]" s)
  1401. (format "[%s%s]" (upcase s) (downcase s))
  1402. (regexp-quote s))))
  1403. isearch-string ""))
  1404. (t (regexp-quote isearch-string)))))
  1405. (hi-lock-face-buffer string (hi-lock-read-face-name)))
  1406. (and isearch-recursive-edit (exit-recursive-edit)))
  1407. (defun isearch-delete-char ()
  1408. "Discard last input item and move point back.
  1409. If no previous match was done, just beep."
  1410. (interactive)
  1411. (if (null (cdr isearch-cmds))
  1412. (ding)
  1413. (isearch-pop-state))
  1414. (isearch-update))
  1415. (defun isearch-del-char (&optional arg)
  1416. "Delete character from end of search string and search again.
  1417. If search string is empty, just beep."
  1418. (interactive "p")
  1419. (if (= 0 (length isearch-string))
  1420. (ding)
  1421. (setq isearch-string (substring isearch-string 0 (- (or arg 1)))
  1422. isearch-message (mapconcat 'isearch-text-char-description
  1423. isearch-string "")))
  1424. ;; Use the isearch-other-end as new starting point to be able
  1425. ;; to find the remaining part of the search string again.
  1426. (if isearch-other-end (goto-char isearch-other-end))
  1427. (isearch-search)
  1428. (isearch-push-state)
  1429. (isearch-update))
  1430. (defun isearch-yank-string (string)
  1431. "Pull STRING into search string."
  1432. ;; Downcase the string if not supposed to case-fold yanked strings.
  1433. (if (and isearch-case-fold-search
  1434. (eq 'not-yanks search-upper-case))
  1435. (setq string (downcase string)))
  1436. (if isearch-regexp (setq string (regexp-quote string)))
  1437. ;; Don't move cursor in reverse search.
  1438. (setq isearch-yank-flag t)
  1439. (isearch-process-search-string
  1440. string (mapconcat 'isearch-text-char-description string "")))
  1441. (defun isearch-yank-kill ()
  1442. "Pull string from kill ring into search string."
  1443. (interactive)
  1444. (isearch-yank-string (current-kill 0)))
  1445. (defun isearch-yank-pop ()
  1446. "Replace just-yanked search string with previously killed string."
  1447. (interactive)
  1448. (if (not (memq last-command '(isearch-yank-kill isearch-yank-pop)))
  1449. ;; Fall back on `isearch-yank-kill' for the benefits of people
  1450. ;; who are used to the old behavior of `M-y' in isearch mode. In
  1451. ;; future, this fallback may be changed if we ever change
  1452. ;; `yank-pop' to do something like the kill-ring-browser.
  1453. (isearch-yank-kill)
  1454. (isearch-pop-state)
  1455. (isearch-yank-string (current-kill 1))))
  1456. (defun isearch-yank-x-selection ()
  1457. "Pull current X selection into search string."
  1458. (interactive)
  1459. (isearch-yank-string (x-get-selection))
  1460. ;; If `x-get-selection' returned the text from the active region,
  1461. ;; then it "used" the mark which we should hence deactivate.
  1462. (when select-active-regions (deactivate-mark)))
  1463. (defun isearch-mouse-2 (click)
  1464. "Handle mouse-2 in Isearch mode.
  1465. For a click in the echo area, invoke `isearch-yank-x-selection'.
  1466. Otherwise invoke whatever the calling mouse-2 command sequence
  1467. is bound to outside of Isearch."
  1468. (interactive "e")
  1469. (let* ((w (posn-window (event-start click)))
  1470. (overriding-terminal-local-map nil)
  1471. (binding (key-binding (this-command-keys-vector) t)))
  1472. (if (and (window-minibuffer-p w)
  1473. (not (minibuffer-window-active-p w))) ; in echo area
  1474. (isearch-yank-x-selection)
  1475. (when (functionp binding)
  1476. (call-interactively binding)))))
  1477. (defun isearch-yank-internal (jumpform)
  1478. "Pull the text from point to the point reached by JUMPFORM.
  1479. JUMPFORM is a lambda expression that takes no arguments and returns
  1480. a buffer position, possibly having moved point to that position.
  1481. For example, it might move point forward by a word and return point,
  1482. or it might return the position of the end of the line."
  1483. (isearch-yank-string
  1484. (save-excursion
  1485. (and (not isearch-forward) isearch-other-end
  1486. (goto-char isearch-other-end))
  1487. (buffer-substring-no-properties (point) (funcall jumpform)))))
  1488. (defun isearch-yank-char-in-minibuffer (&optional arg)
  1489. "Pull next character from buffer into end of search string in minibuffer."
  1490. (interactive "p")
  1491. (if (eobp)
  1492. (insert
  1493. (with-current-buffer (cadr (buffer-list))
  1494. (buffer-substring-no-properties
  1495. (point) (progn (forward-char arg) (point)))))
  1496. (forward-char arg)))
  1497. (defun isearch-yank-char (&optional arg)
  1498. "Pull next character from buffer into search string."
  1499. (interactive "p")
  1500. (isearch-yank-internal (lambda () (forward-char arg) (point))))
  1501. (declare-function subword-forward "subword" (&optional arg))
  1502. (defun isearch-yank-word-or-char ()
  1503. "Pull next character, subword or word from buffer into search string.
  1504. Subword is used when `subword-mode' is activated. "
  1505. (interactive)
  1506. (isearch-yank-internal
  1507. (lambda ()
  1508. (if (or (= (char-syntax (or (char-after) 0)) ?w)
  1509. (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
  1510. (if (and (boundp 'subword-mode) subword-mode)
  1511. (subword-forward 1)
  1512. (forward-word 1))
  1513. (forward-char 1)) (point))))
  1514. (defun isearch-yank-word ()
  1515. "Pull next word from buffer into search string."
  1516. (interactive)
  1517. (isearch-yank-internal (lambda () (forward-word 1) (point))))
  1518. (defun isearch-yank-line ()
  1519. "Pull rest of line from buffer into search string."
  1520. (interactive)
  1521. (isearch-yank-internal
  1522. (lambda () (let ((inhibit-field-text-motion t))
  1523. (line-end-position (if (eolp) 2 1))))))
  1524. (defun isearch-search-and-update ()
  1525. ;; Do the search and update the display.
  1526. (when (or isearch-success
  1527. ;; Unsuccessful regexp search may become successful by
  1528. ;; addition of characters which make isearch-string valid
  1529. isearch-regexp
  1530. ;; If the string was found but was completely invisible,
  1531. ;; it might now be partly visible, so try again.
  1532. (prog1 isearch-hidden (setq isearch-hidden nil)))
  1533. ;; In reverse search, adding stuff at
  1534. ;; the end may cause zero or many more chars to be
  1535. ;; matched, in the string following point.
  1536. ;; Allow all those possibilities without moving point as
  1537. ;; long as the match does not extend past search origin.
  1538. (if (and (not isearch-forward) (not isearch-adjusted)
  1539. (condition-case ()
  1540. (let ((case-fold-search isearch-case-fold-search))
  1541. (if (and (eq case-fold-search t) search-upper-case)
  1542. (setq case-fold-search
  1543. (isearch-no-upper-case-p isearch-string isearch-regexp)))
  1544. (looking-at (cond
  1545. ((functionp isearch-word)
  1546. (funcall isearch-word isearch-string t))
  1547. (isearch-word (word-search-regexp isearch-string t))
  1548. (isearch-regexp isearch-string)
  1549. (t (regexp-quote isearch-string)))))
  1550. (error nil))
  1551. (or isearch-yank-flag
  1552. (<= (match-end 0)
  1553. (min isearch-opoint isearch-barrier))))
  1554. (progn
  1555. (setq isearch-success t
  1556. isearch-error nil
  1557. isearch-other-end (match-end 0))
  1558. (if (and (eq isearch-case-fold-search t) search-upper-case)
  1559. (setq isearch-case-fold-search
  1560. (isearch-no-upper-case-p isearch-string isearch-regexp))))
  1561. ;; Not regexp, not reverse, or no match at point.
  1562. (if (and isearch-other-end (not isearch-adjusted))
  1563. (goto-char (if isearch-forward isearch-other-end
  1564. (min isearch-opoint
  1565. isearch-barrier
  1566. (1+ isearch-other-end)))))
  1567. (isearch-search)
  1568. ))
  1569. (isearch-push-state)
  1570. (if isearch-op-fun (funcall isearch-op-fun))
  1571. (isearch-update))
  1572. ;; *, ?, }, and | chars can make a regexp more liberal.
  1573. ;; They can make a regexp match sooner or make it succeed instead of failing.
  1574. ;; So go back to place last successful search started
  1575. ;; or to the last ^S/^R (barrier), whichever is nearer.
  1576. ;; + needs no special handling because the string must match at least once.
  1577. (defun isearch-backslash (str)
  1578. "Return t if STR ends in an odd number of backslashes."
  1579. (= (mod (- (length str) (string-match "\\\\*\\'" str)) 2) 1))
  1580. (defun isearch-fallback (want-backslash &optional allow-invalid to-barrier)
  1581. "Return point to previous successful match to allow regexp liberalization.
  1582. \\<isearch-mode-map>
  1583. Respects \\[isearch-repeat-forward] and \\[isearch-repeat-backward] by \
  1584. stopping at `isearch-barrier' as needed.
  1585. Do nothing if a backslash is escaping the liberalizing character.
  1586. If WANT-BACKSLASH is non-nil, invert this behavior (for \\} and \\|).
  1587. Do nothing if regexp has recently been invalid unless optional
  1588. ALLOW-INVALID non-nil.
  1589. If optional TO-BARRIER non-nil, ignore previous matches and go exactly
  1590. to the barrier."
  1591. ;; (eq (not a) (not b)) makes all non-nil values equivalent
  1592. (when (and isearch-regexp (eq (not (isearch-backslash isearch-string))
  1593. (not want-backslash))
  1594. ;; We have to check 2 stack frames because the last might be
  1595. ;; invalid just because of a backslash.
  1596. (or (not isearch-error)
  1597. (not (isearch-error-state (cadr isearch-cmds)))
  1598. allow-invalid))
  1599. (if to-barrier
  1600. (progn (goto-char isearch-barrier)
  1601. (setq isearch-adjusted t))
  1602. (let* ((stack isearch-cmds)
  1603. (previous (cdr stack)) ; lookbelow in the stack
  1604. (frame (car stack)))
  1605. ;; Walk down the stack looking for a valid regexp (as of course only
  1606. ;; they can be the previous successful match); this conveniently
  1607. ;; removes all bracket-sets and groups that might be in the way, as
  1608. ;; well as partial \{\} constructs that the code below leaves behind.
  1609. ;; Also skip over postfix operators -- though horrid,
  1610. ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly valid.
  1611. (while (and previous
  1612. (or (isearch-error-state frame)
  1613. (let* ((string (isearch-string-state frame))
  1614. (lchar (aref string (1- (length string)))))
  1615. ;; The operators aren't always operators; check
  1616. ;; backslashes. This doesn't handle the case of
  1617. ;; operators at the beginning of the regexp not
  1618. ;; being special, but then we should fall back to
  1619. ;; the barrier anyway because it's all optional.
  1620. (if (isearch-backslash
  1621. (isearch-string-state (car previous)))
  1622. (eq lchar ?\})
  1623. (memq lchar '(?* ?? ?+))))))
  1624. (setq stack previous previous (cdr previous) frame (car stack)))
  1625. (when stack
  1626. ;; `stack' now refers the most recent valid regexp that is not at
  1627. ;; all optional in its last term. Now dig one level deeper and find
  1628. ;; what matched before that.
  1629. (let ((last-other-end
  1630. (or (and (car previous)
  1631. (isearch-other-end-state (car previous)))
  1632. isearch-barrier)))
  1633. (goto-char (if isearch-forward
  1634. (max last-other-end isearch-barrier)
  1635. (min last-other-end isearch-barrier)))
  1636. (setq isearch-adjusted t)))))))
  1637. (defun isearch-unread-key-sequence (keylist)
  1638. "Unread the given key-sequence KEYLIST.
  1639. Scroll-bar or mode-line events are processed appropriately."
  1640. (cancel-kbd-macro-events)
  1641. (apply 'isearch-unread keylist)
  1642. ;; If the event was a scroll-bar or mode-line click, the event will have
  1643. ;; been prefixed by a symbol such as vertical-scroll-bar. We must remove
  1644. ;; it here, because this symbol will be attached to the event again next
  1645. ;; time it gets read by read-key-sequence.
  1646. ;;
  1647. ;; (Old comment from isearch-other-meta-char: "Note that we don't have to
  1648. ;; modify the event anymore in 21 because read_key_sequence no longer
  1649. ;; modifies events to produce fake prefix keys.")
  1650. (if (and (> (length keylist) 1)
  1651. (symbolp (car keylist))
  1652. (listp (cadr keylist))
  1653. (not (numberp (posn-point
  1654. (event-start (cadr keylist) )))))
  1655. (pop unread-command-events)))
  1656. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1657. ;; scrolling within Isearch mode. Alan Mackenzie (acm@muc.de), 2003/2/24
  1658. ;;
  1659. ;; The idea here is that certain vertical scrolling commands (like C-l
  1660. ;; `recenter') should be usable WITHIN Isearch mode. For a command to be
  1661. ;; suitable, it must NOT alter the buffer, swap to another buffer or frame,
  1662. ;; tamper with isearch's state, or move point. It is unacceptable for the
  1663. ;; search string to be scrolled out of the current window. If a command
  1664. ;; attempts this, we scroll the text back again.
  1665. ;;
  1666. ;; We implement this feature with a property called `isearch-scroll'.
  1667. ;; If a command's symbol has the value t for this property or for the
  1668. ;; `scroll-command' property, it is a scrolling command. The feature
  1669. ;; needs to be enabled by setting the customizable variable
  1670. ;; `isearch-allow-scroll' to a non-nil value.
  1671. ;;
  1672. ;; The universal argument commands (e.g. C-u) in simple.el are marked
  1673. ;; as scrolling commands, and isearch.el has been amended to allow
  1674. ;; prefix arguments to be passed through to scrolling commands. Thus
  1675. ;; M-0 C-l will scroll point to the top of the window.
  1676. ;;
  1677. ;; Horizontal scrolling commands are currently not catered for.
  1678. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1679. ;; Set the isearch-scroll property on some standard functions:
  1680. ;; Scroll-bar functions:
  1681. (if (fboundp 'scroll-bar-toolkit-scroll)
  1682. (put 'scroll-bar-toolkit-scroll 'isearch-scroll t))
  1683. (if (fboundp 'w32-handle-scroll-bar-event)
  1684. (put 'w32-handle-scroll-bar-event 'isearch-scroll t))
  1685. ;; Commands which scroll the window (some scroll commands
  1686. ;; already have the `scroll-command' property on them):
  1687. (put 'recenter 'isearch-scroll t)
  1688. (put 'recenter-top-bottom 'isearch-scroll t)
  1689. (put 'reposition-window 'isearch-scroll t)
  1690. ;; Commands which act on the other window
  1691. (put 'list-buffers 'isearch-scroll t)
  1692. (put 'scroll-other-window 'isearch-scroll t)
  1693. (put 'scroll-other-window-down 'isearch-scroll t)
  1694. (put 'beginning-of-buffer-other-window 'isearch-scroll t)
  1695. (put 'end-of-buffer-other-window 'isearch-scroll t)
  1696. ;; Commands which change the window layout
  1697. (put 'delete-other-windows 'isearch-scroll t)
  1698. (put 'balance-windows 'isearch-scroll t)
  1699. (put 'split-window-right 'isearch-scroll t)
  1700. (put 'split-window-below 'isearch-scroll t)
  1701. (put 'enlarge-window 'isearch-scroll t)
  1702. ;; Aliases for split-window-*
  1703. (put 'split-window-vertically 'isearch-scroll t)
  1704. (put 'split-window-horizontally 'isearch-scroll t)
  1705. ;; Universal argument commands
  1706. (put 'universal-argument 'isearch-scroll t)
  1707. (put 'negative-argument 'isearch-scroll t)
  1708. (put 'digit-argument 'isearch-scroll t)
  1709. (defcustom isearch-allow-scroll nil
  1710. "Whether scrolling is allowed during incremental search.
  1711. If non-nil, scrolling commands can be used in Isearch mode.
  1712. However, the current match will never scroll offscreen.
  1713. If nil, scrolling commands will first cancel Isearch mode."
  1714. :type 'boolean
  1715. :group 'isearch)
  1716. (defun isearch-string-out-of-window (isearch-point)
  1717. "Test whether the search string is currently outside of the window.
  1718. Return nil if it's completely visible, or if point is visible,
  1719. together with as much of the search string as will fit; the symbol
  1720. `above' if we need to scroll the text downwards; the symbol `below',
  1721. if upwards."
  1722. (let ((w-start (window-start))
  1723. (w-end (window-end nil t))
  1724. (w-L1 (save-excursion (move-to-window-line 1) (point)))
  1725. (w-L-1 (save-excursion (move-to-window-line -1) (point)))
  1726. start end) ; start and end of search string in buffer
  1727. (if isearch-forward
  1728. (setq end isearch-point start (or isearch-other-end isearch-point))
  1729. (setq start isearch-point end (or isearch-other-end isearch-point)))
  1730. (cond ((or (and (>= start w-start) (<= end w-end))
  1731. (if isearch-forward
  1732. (and (>= isearch-point w-L-1) (< isearch-point w-end)) ; point on Line -1
  1733. (and (>= isearch-point w-start) (< isearch-point w-L1)))) ; point on Line 0
  1734. nil)
  1735. ((and (< start w-start)
  1736. (< isearch-point w-L-1))
  1737. 'above)
  1738. (t 'below))))
  1739. (defun isearch-back-into-window (above isearch-point)
  1740. "Scroll the window to bring the search string back into view.
  1741. Restore point to ISEARCH-POINT in the process. ABOVE is t when the
  1742. search string is above the top of the window, nil when it is beneath
  1743. the bottom."
  1744. (let (start end)
  1745. (if isearch-forward
  1746. (setq end isearch-point start (or isearch-other-end isearch-point))
  1747. (setq start isearch-point end (or isearch-other-end isearch-point)))
  1748. (if above
  1749. (progn
  1750. (goto-char start)
  1751. (recenter 0)
  1752. (when (>= isearch-point (window-end nil t))
  1753. (goto-char isearch-point)
  1754. (recenter -1)))
  1755. (goto-char end)
  1756. (recenter -1)
  1757. (when (< isearch-point (window-start))
  1758. (goto-char isearch-point)
  1759. (recenter 0))))
  1760. (goto-char isearch-point))
  1761. (defun isearch-reread-key-sequence-naturally (keylist)
  1762. "Reread key sequence KEYLIST with an inactive Isearch-mode keymap.
  1763. Return the key sequence as a string/vector."
  1764. (isearch-unread-key-sequence keylist)
  1765. (let (overriding-terminal-local-map)
  1766. (read-key-sequence nil))) ; This will go through function-key-map, if nec.
  1767. (defun isearch-lookup-scroll-key (key-seq)
  1768. "If KEY-SEQ is bound to a scrolling command, return it as a symbol.
  1769. Otherwise return nil."
  1770. (let* ((overriding-terminal-local-map nil)
  1771. (binding (key-binding key-seq)))
  1772. (and binding (symbolp binding) (commandp binding)
  1773. (or (eq (get binding 'isearch-scroll) t)
  1774. (eq (get binding 'scroll-command) t))
  1775. binding)))
  1776. (defalias 'isearch-other-control-char 'isearch-other-meta-char)
  1777. (defun isearch-other-meta-char (&optional arg)
  1778. "Process a miscellaneous key sequence in Isearch mode.
  1779. Try to convert the current key-sequence to something usable in Isearch
  1780. mode, either by converting it with `function-key-map', downcasing a
  1781. key with C-<upper case>, or finding a \"scrolling command\" bound to
  1782. it. \(In the last case, we may have to read more events.) If so,
  1783. either unread the converted sequence or execute the command.
  1784. Otherwise, if `search-exit-option' is non-nil (the default) unread the
  1785. key-sequence and exit the search normally. If it is the symbol
  1786. `edit', the search string is edited in the minibuffer and the meta
  1787. character is unread so that it applies to editing the string.
  1788. ARG is the prefix argument. It will be transmitted through to the
  1789. scrolling command or to the command whose key-sequence exits
  1790. Isearch mode."
  1791. (interactive "P")
  1792. (let* ((key (if current-prefix-arg ; not nec the same as ARG
  1793. (substring (this-command-keys) universal-argument-num-events)
  1794. (this-command-keys)))
  1795. (main-event (aref key 0))
  1796. (keylist (listify-key-sequence key))
  1797. scroll-command isearch-point)
  1798. (cond ((and (= (length key) 1)
  1799. (let ((lookup (lookup-key local-function-key-map key)))
  1800. (not (or (null lookup) (integerp lookup)
  1801. (keymapp lookup)))))
  1802. ;; Handle a function key that translates into something else.
  1803. ;; If the key has a global definition too,
  1804. ;; exit and unread the key itself, so its global definition runs.
  1805. ;; Otherwise, unread the translation,
  1806. ;; so that the translated key takes effect within isearch.
  1807. (cancel-kbd-macro-events)
  1808. (if (lookup-key global-map key)
  1809. (progn
  1810. (isearch-done)
  1811. (setq prefix-arg arg)
  1812. (apply 'isearch-unread keylist))
  1813. (setq keylist
  1814. (listify-key-sequence (lookup-key local-function-key-map key)))
  1815. (while keylist
  1816. (setq key (car keylist))
  1817. ;; If KEY is a printing char, we handle it here
  1818. ;; directly to avoid the input method and keyboard
  1819. ;; coding system translating it.
  1820. (if (and (integerp key)
  1821. (>= key ?\s) (/= key 127) (< key 256))
  1822. (progn
  1823. (isearch-process-search-char key)
  1824. (setq keylist (cdr keylist)))
  1825. ;; As the remaining keys in KEYLIST can't be handled
  1826. ;; here, we must reread them.
  1827. (setq prefix-arg arg)
  1828. (apply 'isearch-unread keylist)
  1829. (setq keylist nil)))))
  1830. (
  1831. ;; Handle an undefined shifted control character
  1832. ;; by downshifting it if that makes it defined.
  1833. ;; (As read-key-sequence would normally do,
  1834. ;; if we didn't have a default definition.)
  1835. (let ((mods (event-modifiers main-event)))
  1836. (and (integerp main-event)
  1837. (memq 'shift mods)
  1838. (memq 'control mods)
  1839. (not (memq (lookup-key isearch-mode-map
  1840. (let ((copy (copy-sequence key)))
  1841. (aset copy 0
  1842. (- main-event
  1843. (- ?\C-\S-a ?\C-a)))
  1844. copy)
  1845. nil)
  1846. '(nil
  1847. isearch-other-control-char)))))
  1848. (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
  1849. (cancel-kbd-macro-events)
  1850. (setq prefix-arg arg)
  1851. (apply 'isearch-unread keylist))
  1852. ((eq search-exit-option 'edit)
  1853. (setq prefix-arg arg)
  1854. (apply 'isearch-unread keylist)
  1855. (isearch-edit-string))
  1856. ;; Handle a scrolling function.
  1857. ((and isearch-allow-scroll
  1858. (progn (setq key (isearch-reread-key-sequence-naturally keylist))
  1859. (setq keylist (listify-key-sequence key))
  1860. (setq main-event (aref key 0))
  1861. (setq scroll-command (isearch-lookup-scroll-key key))))
  1862. ;; From this point onwards, KEY, KEYLIST and MAIN-EVENT hold a
  1863. ;; complete key sequence, possibly as modified by function-key-map,
  1864. ;; not merely the one or two event fragment which invoked
  1865. ;; isearch-other-meta-char in the first place.
  1866. (setq isearch-point (point))
  1867. (setq prefix-arg arg)
  1868. (command-execute scroll-command)
  1869. (let ((ab-bel (isearch-string-out-of-window isearch-point)))
  1870. (if ab-bel
  1871. (isearch-back-into-window (eq ab-bel 'above) isearch-point)
  1872. (goto-char isearch-point)))
  1873. (isearch-update))
  1874. ;; A mouse click on the isearch message starts editing the search string
  1875. ((and (eq (car-safe main-event) 'down-mouse-1)
  1876. (window-minibuffer-p (posn-window (event-start main-event))))
  1877. ;; Swallow the up-event.
  1878. (read-event)
  1879. (isearch-edit-string))
  1880. (search-exit-option
  1881. (let (window)
  1882. (setq prefix-arg arg)
  1883. (isearch-unread-key-sequence keylist)
  1884. (setq main-event (car unread-command-events))
  1885. ;; If we got a mouse click event, that event contains the
  1886. ;; window clicked on. maybe it was read with the buffer
  1887. ;; it was clicked on. If so, that buffer, not the current one,
  1888. ;; is in isearch mode. So end the search in that buffer.
  1889. ;; ??? I have no idea what this if checks for, but it's
  1890. ;; obviously wrong for the case that a down-mouse event
  1891. ;; on another window invokes this function. The event
  1892. ;; will contain the window clicked on and that window's
  1893. ;; buffer is certainly not always in Isearch mode.
  1894. ;;
  1895. ;; Leave the code in, but check for current buffer not
  1896. ;; being in Isearch mode for now, until someone tells
  1897. ;; what it's really supposed to do.
  1898. ;;
  1899. ;; --gerd 2001-08-10.
  1900. (if (and (not isearch-mode)
  1901. (listp main-event)
  1902. (setq window (posn-window (event-start main-event)))
  1903. (windowp window)
  1904. (or (> (minibuffer-depth) 0)
  1905. (not (window-minibuffer-p window))))
  1906. (with-current-buffer (window-buffer window)
  1907. (isearch-done)
  1908. (isearch-clean-overlays))
  1909. (isearch-done)
  1910. (isearch-clean-overlays)
  1911. (setq prefix-arg arg))))
  1912. (t;; otherwise nil
  1913. (isearch-process-search-string key key)))))
  1914. (defun isearch-quote-char ()
  1915. "Quote special characters for incremental search."
  1916. (interactive)
  1917. (let ((char (read-quoted-char (isearch-message t))))
  1918. ;; Assume character codes 0200 - 0377 stand for characters in some
  1919. ;; single-byte character set, and convert them to Emacs
  1920. ;; characters.
  1921. (if (and isearch-regexp (= char ?\s))
  1922. (if (subregexp-context-p isearch-string (length isearch-string))
  1923. (isearch-process-search-string "[ ]" " ")
  1924. (isearch-process-search-char char))
  1925. (and enable-multibyte-characters
  1926. (>= char ?\200)
  1927. (<= char ?\377)
  1928. (setq char (unibyte-char-to-multibyte char)))
  1929. (isearch-process-search-char char))))
  1930. (defun isearch-printing-char ()
  1931. "Add this ordinary printing character to the search string and search."
  1932. (interactive)
  1933. (let ((char last-command-event))
  1934. (if (= char ?\S-\ )
  1935. (setq char ?\s))
  1936. (if current-input-method
  1937. (isearch-process-search-multibyte-characters char)
  1938. (isearch-process-search-char char))))
  1939. (defun isearch-process-search-char (char)
  1940. ;; * and ? are special in regexps when not preceded by \.
  1941. ;; } and | are special in regexps when preceded by \.
  1942. ;; Nothing special for + because it matches at least once.
  1943. (cond
  1944. ((memq char '(?* ??)) (isearch-fallback nil))
  1945. ((eq char ?\}) (isearch-fallback t t))
  1946. ((eq char ?|) (isearch-fallback t nil t)))
  1947. ;; Append the char to the search string, update the message and re-search.
  1948. (isearch-process-search-string
  1949. (char-to-string char)
  1950. (if (>= char ?\200)
  1951. (char-to-string char)
  1952. (isearch-text-char-description char))))
  1953. (defun isearch-process-search-string (string message)
  1954. (setq isearch-string (concat isearch-string string)
  1955. isearch-message (concat isearch-message message))
  1956. (isearch-search-and-update))
  1957. ;; Search Ring
  1958. (defun isearch-ring-adjust1 (advance)
  1959. ;; Helper for isearch-ring-adjust
  1960. (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
  1961. (length (length ring))
  1962. (yank-pointer-name (if isearch-regexp
  1963. 'regexp-search-ring-yank-pointer
  1964. 'search-ring-yank-pointer))
  1965. (yank-pointer (eval yank-pointer-name)))
  1966. (if (zerop length)
  1967. ()
  1968. (set yank-pointer-name
  1969. (setq yank-pointer
  1970. (mod (+ (or yank-pointer (if advance 0 -1))
  1971. (if advance -1 1))
  1972. length)))
  1973. (setq isearch-string (nth yank-pointer ring)
  1974. isearch-message (mapconcat 'isearch-text-char-description
  1975. isearch-string "")))))
  1976. (defun isearch-ring-adjust (advance)
  1977. ;; Helper for isearch-ring-advance and isearch-ring-retreat
  1978. (isearch-ring-adjust1 advance)
  1979. (if search-ring-update
  1980. (progn
  1981. (isearch-search)
  1982. (isearch-push-state)
  1983. (isearch-update))
  1984. ;; Otherwise, edit the search string instead. Note that there is
  1985. ;; no need to push the search state after isearch-edit-string here
  1986. ;; since isearch-edit-string already pushes its state
  1987. (isearch-edit-string)))
  1988. (defun isearch-ring-advance ()
  1989. "Advance to the next search string in the ring."
  1990. ;; This could be more general to handle a prefix arg, but who would use it.
  1991. (interactive)
  1992. (isearch-ring-adjust 'advance))
  1993. (defun isearch-ring-retreat ()
  1994. "Retreat to the previous search string in the ring."
  1995. (interactive)
  1996. (isearch-ring-adjust nil))
  1997. (defun isearch-complete1 ()
  1998. ;; Helper for isearch-complete and isearch-complete-edit
  1999. ;; Return t if completion OK, nil if no completion exists.
  2000. (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
  2001. (completion-ignore-case case-fold-search)
  2002. (completion (try-completion isearch-string ring)))
  2003. (cond
  2004. ((eq completion t)
  2005. ;; isearch-string stays the same
  2006. t)
  2007. ((or completion ; not nil, must be a string
  2008. (= 0 (length isearch-string))) ; shouldn't have to say this
  2009. (if (equal completion isearch-string) ;; no extension?
  2010. (progn
  2011. (if completion-auto-help
  2012. (with-output-to-temp-buffer "*Isearch completions*"
  2013. (display-completion-list
  2014. (all-completions isearch-string ring))))
  2015. t)
  2016. (and completion
  2017. (setq isearch-string completion))))
  2018. (t
  2019. (message "No completion") ; waits a second if in minibuffer
  2020. nil))))
  2021. (defun isearch-complete ()
  2022. "Complete the search string from the strings on the search ring.
  2023. The completed string is then editable in the minibuffer.
  2024. If there is no completion possible, say so and continue searching."
  2025. (interactive)
  2026. (if (isearch-complete1)
  2027. (progn (setq isearch-message
  2028. (mapconcat 'isearch-text-char-description
  2029. isearch-string ""))
  2030. (isearch-edit-string))
  2031. ;; else
  2032. (sit-for 1)
  2033. (isearch-update)))
  2034. (defun isearch-complete-edit ()
  2035. "Same as `isearch-complete' except in the minibuffer."
  2036. (interactive)
  2037. (setq isearch-string (field-string))
  2038. (if (isearch-complete1)
  2039. (progn
  2040. (delete-field)
  2041. (insert isearch-string))))
  2042. ;; Message string
  2043. (defun isearch-message (&optional c-q-hack ellipsis)
  2044. ;; Generate and print the message string.
  2045. (let ((cursor-in-echo-area ellipsis)
  2046. (m isearch-message)
  2047. (fail-pos (isearch-fail-pos t)))
  2048. ;; Highlight failed part
  2049. (when fail-pos
  2050. (setq m (copy-sequence m))
  2051. (add-text-properties fail-pos (length m) '(face isearch-fail) m)
  2052. ;; Highlight failed trailing whitespace
  2053. (when (string-match " +$" m)
  2054. (add-text-properties (match-beginning 0) (match-end 0)
  2055. '(face trailing-whitespace) m)))
  2056. (setq m (concat
  2057. (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
  2058. m
  2059. (isearch-message-suffix c-q-hack ellipsis)))
  2060. (if c-q-hack m (let ((message-log-max nil)) (message "%s" m)))))
  2061. (defun isearch-message-prefix (&optional _c-q-hack ellipsis nonincremental)
  2062. ;; If about to search, and previous search regexp was invalid,
  2063. ;; check that it still is. If it is valid now,
  2064. ;; let the message we display while searching say that it is valid.
  2065. (and isearch-error ellipsis
  2066. (condition-case ()
  2067. (progn (re-search-forward isearch-string (point) t)
  2068. (setq isearch-error nil))
  2069. (error nil)))
  2070. ;; If currently failing, display no ellipsis.
  2071. (or isearch-success (setq ellipsis nil))
  2072. (let ((m (concat (if isearch-success "" "failing ")
  2073. (if isearch-adjusted "pending " "")
  2074. (if (and isearch-wrapped
  2075. (not isearch-wrap-function)
  2076. (if isearch-forward
  2077. (> (point) isearch-opoint)
  2078. (< (point) isearch-opoint)))
  2079. "over")
  2080. (if isearch-wrapped "wrapped ")
  2081. (if isearch-word
  2082. (or (and (symbolp isearch-word)
  2083. (get isearch-word 'isearch-message-prefix))
  2084. "word ")
  2085. "")
  2086. (if isearch-regexp "regexp " "")
  2087. (if multi-isearch-next-buffer-current-function "multi " "")
  2088. (or isearch-message-prefix-add "")
  2089. (if nonincremental "search" "I-search")
  2090. (if isearch-forward "" " backward")
  2091. (if current-input-method
  2092. ;; Input methods for RTL languages use RTL
  2093. ;; characters for their title, and that messes
  2094. ;; up the display of search text after the prompt.
  2095. (bidi-string-mark-left-to-right
  2096. (concat " [" current-input-method-title "]: "))
  2097. ": ")
  2098. )))
  2099. (propertize (concat (upcase (substring m 0 1)) (substring m 1))
  2100. 'face 'minibuffer-prompt)))
  2101. (defun isearch-message-suffix (&optional c-q-hack _ellipsis)
  2102. (concat (if c-q-hack "^Q" "")
  2103. (if isearch-error
  2104. (concat " [" isearch-error "]")
  2105. "")
  2106. (or isearch-message-suffix-add "")))
  2107. ;; Searching
  2108. (defvar isearch-search-fun-function 'isearch-search-fun-default
  2109. "Non-default value overrides the behavior of `isearch-search-fun-default'.
  2110. This variable's value should be a function, which will be called
  2111. with no arguments, and should return a function that takes three
  2112. arguments: STRING, BOUND, and NOERROR.
  2113. This returned function will be used by `isearch-search-string' to
  2114. search for the first occurrence of STRING or its translation.")
  2115. (defun isearch-search-fun ()
  2116. "Return the function to use for the search.
  2117. Can be changed via `isearch-search-fun-function' for special needs."
  2118. (funcall (or isearch-search-fun-function 'isearch-search-fun-default)))
  2119. (defun isearch-search-fun-default ()
  2120. "Return default functions to use for the search."
  2121. (cond
  2122. (isearch-word
  2123. (lambda (string &optional bound noerror count)
  2124. ;; Use lax versions to not fail at the end of the word while
  2125. ;; the user adds and removes characters in the search string
  2126. ;; (or when using nonincremental word isearch)
  2127. (let ((lax (not (or isearch-nonincremental
  2128. (eq (length isearch-string)
  2129. (length (isearch-string-state (car isearch-cmds))))))))
  2130. (funcall
  2131. (if isearch-forward #'re-search-forward #'re-search-backward)
  2132. (if (functionp isearch-word)
  2133. (funcall isearch-word string lax)
  2134. (word-search-regexp string lax))
  2135. bound noerror count))))
  2136. (isearch-regexp
  2137. (if isearch-forward 're-search-forward 're-search-backward))
  2138. (t
  2139. (if isearch-forward 'search-forward 'search-backward))))
  2140. (defun isearch-search-string (string bound noerror)
  2141. "Search for the first occurrence of STRING or its translation.
  2142. If found, move point to the end of the occurrence,
  2143. update the match data, and return point."
  2144. (let* ((func (isearch-search-fun))
  2145. (pos1 (save-excursion (funcall func string bound noerror)))
  2146. pos2)
  2147. (when (and
  2148. ;; Avoid "obsolete" warnings for translation-table-for-input.
  2149. (with-no-warnings
  2150. (char-table-p translation-table-for-input))
  2151. (multibyte-string-p string)
  2152. ;; Minor optimization.
  2153. (string-match-p "[^[:ascii:]]" string))
  2154. (let ((translated
  2155. (apply 'string
  2156. (mapcar (lambda (c)
  2157. (or
  2158. ;; Avoid "obsolete" warnings for
  2159. ;; translation-table-for-input.
  2160. (with-no-warnings
  2161. (aref translation-table-for-input c))
  2162. c))
  2163. string)))
  2164. match-data)
  2165. (when translated
  2166. (save-match-data
  2167. (save-excursion
  2168. (if (setq pos2 (funcall func translated bound noerror))
  2169. (setq match-data (match-data t)))))
  2170. (when (and pos2
  2171. (or (not pos1)
  2172. (if isearch-forward (< pos2 pos1) (> pos2 pos1))))
  2173. (setq pos1 pos2)
  2174. (set-match-data match-data)))))
  2175. (when pos1
  2176. ;; When using multiple buffers isearch, switch to the new buffer here,
  2177. ;; because `save-excursion' above doesn't allow doing it inside funcall.
  2178. (if (and multi-isearch-next-buffer-current-function
  2179. (buffer-live-p multi-isearch-current-buffer))
  2180. (switch-to-buffer multi-isearch-current-buffer))
  2181. (goto-char pos1)
  2182. pos1)))
  2183. (defun isearch-search ()
  2184. ;; Do the search with the current search string.
  2185. (if isearch-message-function
  2186. (funcall isearch-message-function nil t)
  2187. (isearch-message nil t))
  2188. (if (and (eq isearch-case-fold-search t) search-upper-case)
  2189. (setq isearch-case-fold-search
  2190. (isearch-no-upper-case-p isearch-string isearch-regexp)))
  2191. (condition-case lossage
  2192. (let ((inhibit-point-motion-hooks
  2193. (and (eq isearch-filter-predicate 'isearch-filter-visible)
  2194. search-invisible))
  2195. (inhibit-quit nil)
  2196. (case-fold-search isearch-case-fold-search)
  2197. (search-spaces-regexp search-whitespace-regexp)
  2198. (retry t))
  2199. (setq isearch-error nil)
  2200. (while retry
  2201. (setq isearch-success
  2202. (isearch-search-string isearch-string nil t))
  2203. ;; Clear RETRY unless the search predicate says
  2204. ;; to skip this search hit.
  2205. (if (or (not isearch-success)
  2206. (bobp) (eobp)
  2207. (= (match-beginning 0) (match-end 0))
  2208. (funcall isearch-filter-predicate
  2209. (match-beginning 0) (match-end 0)))
  2210. (setq retry nil)))
  2211. (setq isearch-just-started nil)
  2212. (if isearch-success
  2213. (setq isearch-other-end
  2214. (if isearch-forward (match-beginning 0) (match-end 0)))))
  2215. (quit (isearch-unread ?\C-g)
  2216. (setq isearch-success nil))
  2217. (invalid-regexp
  2218. (setq isearch-error (car (cdr lossage)))
  2219. (if (string-match
  2220. "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
  2221. isearch-error)
  2222. (setq isearch-error "incomplete input")))
  2223. (search-failed
  2224. (setq isearch-success nil)
  2225. (setq isearch-error (nth 2 lossage)))
  2226. (error
  2227. ;; stack overflow in regexp search.
  2228. (setq isearch-error (format "%s" lossage))))
  2229. (if isearch-success
  2230. nil
  2231. ;; Ding if failed this time after succeeding last time.
  2232. (and (isearch-success-state (car isearch-cmds))
  2233. (ding))
  2234. (if (functionp (isearch-pop-fun-state (car isearch-cmds)))
  2235. (funcall (isearch-pop-fun-state (car isearch-cmds)) (car isearch-cmds)))
  2236. (goto-char (isearch-point-state (car isearch-cmds)))))
  2237. ;; Called when opening an overlay, and we are still in isearch.
  2238. (defun isearch-open-overlay-temporary (ov)
  2239. (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
  2240. ;; Some modes would want to open the overlays temporary during
  2241. ;; isearch in their own way, they should set the
  2242. ;; `isearch-open-invisible-temporary' to a function doing this.
  2243. (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
  2244. ;; Store the values for the `invisible' and `intangible'
  2245. ;; properties, and then set them to nil. This way the text hidden
  2246. ;; by this overlay becomes visible.
  2247. ;; Do we really need to set the `intangible' property to t? Can we
  2248. ;; have the point inside an overlay with an `intangible' property?
  2249. ;; In 19.34 this does not exist so I cannot test it.
  2250. (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
  2251. (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
  2252. (overlay-put ov 'invisible nil)
  2253. (overlay-put ov 'intangible nil)))
  2254. ;; This is called at the end of isearch. It will open the overlays
  2255. ;; that contain the latest match. Obviously in case of a C-g the
  2256. ;; point returns to the original location which surely is not contain
  2257. ;; in any of these overlays, se we are safe in this case too.
  2258. (defun isearch-open-necessary-overlays (ov)
  2259. (let ((inside-overlay (and (> (point) (overlay-start ov))
  2260. (< (point) (overlay-end ov))))
  2261. ;; If this exists it means that the overlay was opened using
  2262. ;; this function, not by us tweaking the overlay properties.
  2263. (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
  2264. (when (or inside-overlay (not fct-temp))
  2265. ;; restore the values for the `invisible' and `intangible'
  2266. ;; properties
  2267. (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
  2268. (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
  2269. (overlay-put ov 'isearch-invisible nil)
  2270. (overlay-put ov 'isearch-intangible nil))
  2271. (if inside-overlay
  2272. (funcall (overlay-get ov 'isearch-open-invisible) ov)
  2273. (if fct-temp
  2274. (funcall fct-temp ov t)))))
  2275. ;; This is called when exiting isearch. It closes the temporary
  2276. ;; opened overlays, except the ones that contain the latest match.
  2277. (defun isearch-clean-overlays ()
  2278. (when isearch-opened-overlays
  2279. (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
  2280. (setq isearch-opened-overlays nil)))
  2281. (defun isearch-intersects-p (start0 end0 start1 end1)
  2282. "Return t if regions START0..END0 and START1..END1 intersect."
  2283. (or (and (>= start0 start1) (< start0 end1))
  2284. (and (> end0 start1) (<= end0 end1))
  2285. (and (>= start1 start0) (< start1 end0))
  2286. (and (> end1 start0) (<= end1 end0))))
  2287. ;; Verify if the current match is outside of each element of
  2288. ;; `isearch-opened-overlays', if so close that overlay.
  2289. (defun isearch-close-unnecessary-overlays (begin end)
  2290. (let ((overlays isearch-opened-overlays))
  2291. (setq isearch-opened-overlays nil)
  2292. (dolist (ov overlays)
  2293. (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
  2294. (push ov isearch-opened-overlays)
  2295. (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
  2296. (if fct-temp
  2297. ;; If this exists it means that the overlay was opened
  2298. ;; using this function, not by us tweaking the overlay
  2299. ;; properties.
  2300. (funcall fct-temp ov t)
  2301. (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
  2302. (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
  2303. (overlay-put ov 'isearch-invisible nil)
  2304. (overlay-put ov 'isearch-intangible nil)))))))
  2305. (defun isearch-range-invisible (beg end)
  2306. "Return t if all the text from BEG to END is invisible."
  2307. (when (/= beg end)
  2308. ;; Check that invisibility runs up to END.
  2309. (save-excursion
  2310. (goto-char beg)
  2311. (let (;; can-be-opened keeps track if we can open some overlays.
  2312. (can-be-opened (eq search-invisible 'open))
  2313. ;; the list of overlays that could be opened
  2314. (crt-overlays nil))
  2315. (when (and can-be-opened isearch-hide-immediately)
  2316. (isearch-close-unnecessary-overlays beg end))
  2317. ;; If the following character is currently invisible,
  2318. ;; skip all characters with that same `invisible' property value.
  2319. ;; Do that over and over.
  2320. (while (and (< (point) end) (invisible-p (point)))
  2321. (if (invisible-p (get-text-property (point) 'invisible))
  2322. (progn
  2323. (goto-char (next-single-property-change (point) 'invisible
  2324. nil end))
  2325. ;; if text is hidden by an `invisible' text property
  2326. ;; we cannot open it at all.
  2327. (setq can-be-opened nil))
  2328. (when can-be-opened
  2329. (let ((overlays (overlays-at (point)))
  2330. ov-list
  2331. o
  2332. invis-prop)
  2333. (while overlays
  2334. (setq o (car overlays)
  2335. invis-prop (overlay-get o 'invisible))
  2336. (if (invisible-p invis-prop)
  2337. (if (overlay-get o 'isearch-open-invisible)
  2338. (setq ov-list (cons o ov-list))
  2339. ;; We found one overlay that cannot be
  2340. ;; opened, that means the whole chunk
  2341. ;; cannot be opened.
  2342. (setq can-be-opened nil)))
  2343. (setq overlays (cdr overlays)))
  2344. (if can-be-opened
  2345. ;; It makes sense to append to the open
  2346. ;; overlays list only if we know that this is
  2347. ;; t.
  2348. (setq crt-overlays (append ov-list crt-overlays)))))
  2349. (goto-char (next-overlay-change (point)))))
  2350. ;; See if invisibility reaches up thru END.
  2351. (if (>= (point) end)
  2352. (if (and can-be-opened (consp crt-overlays))
  2353. (progn
  2354. (setq isearch-opened-overlays
  2355. (append isearch-opened-overlays crt-overlays))
  2356. (mapc 'isearch-open-overlay-temporary crt-overlays)
  2357. nil)
  2358. (setq isearch-hidden t)))))))
  2359. (defun isearch-filter-visible (beg end)
  2360. "Test whether the current search hit is visible at least partially.
  2361. Return non-nil if the text from BEG to END is visible to Isearch as
  2362. determined by `isearch-range-invisible' unless invisible text can be
  2363. searched too when `search-invisible' is t."
  2364. (or (eq search-invisible t)
  2365. (not (isearch-range-invisible beg end))))
  2366. ;; General utilities
  2367. (defun isearch-no-upper-case-p (string regexp-flag)
  2368. "Return t if there are no upper case chars in STRING.
  2369. If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
  2370. since they have special meaning in a regexp."
  2371. (let (quote-flag (i 0) (len (length string)) found)
  2372. (while (and (not found) (< i len))
  2373. (let ((char (aref string i)))
  2374. (if (and regexp-flag (eq char ?\\))
  2375. (setq quote-flag (not quote-flag))
  2376. (if (and (not quote-flag) (not (eq char (downcase char))))
  2377. (setq found t))
  2378. (setq quote-flag nil)))
  2379. (setq i (1+ i)))
  2380. (not (or found
  2381. ;; Even if there's no uppercase char, we want to detect the use
  2382. ;; of [:upper:] or [:lower:] char-class, which indicates
  2383. ;; clearly that the user cares about case distinction.
  2384. (and regexp-flag (string-match "\\[:\\(upp\\|low\\)er:]" string)
  2385. (condition-case err
  2386. (progn
  2387. (string-match (substring string 0 (match-beginning 0))
  2388. "")
  2389. nil)
  2390. (invalid-regexp
  2391. (equal "Unmatched [ or [^" (cadr err)))))))))
  2392. ;; Portability functions to support various Emacs versions.
  2393. (defun isearch-text-char-description (c)
  2394. (cond
  2395. ((< c ?\s) (propertize (format "^%c" (+ c 64)) 'face 'escape-glyph))
  2396. ((= c ?\^?) (propertize "^?" 'face 'escape-glyph))
  2397. (t (char-to-string c))))
  2398. ;; General function to unread characters or events.
  2399. ;; Also insert them in a keyboard macro being defined.
  2400. (defun isearch-unread (&rest char-or-events)
  2401. (mapc 'store-kbd-macro-event char-or-events)
  2402. (setq unread-command-events
  2403. (append char-or-events unread-command-events)))
  2404. ;; Highlighting
  2405. (defvar isearch-overlay nil)
  2406. (defun isearch-highlight (beg end)
  2407. (if search-highlight
  2408. (if isearch-overlay
  2409. ;; Overlay already exists, just move it.
  2410. (move-overlay isearch-overlay beg end (current-buffer))
  2411. ;; Overlay doesn't exist, create it.
  2412. (setq isearch-overlay (make-overlay beg end))
  2413. ;; 1001 is higher than lazy's 1000 and ediff's 100+
  2414. (overlay-put isearch-overlay 'priority 1001)
  2415. (overlay-put isearch-overlay 'face isearch-face))))
  2416. (defun isearch-dehighlight ()
  2417. (when isearch-overlay
  2418. (delete-overlay isearch-overlay)))
  2419. ;; isearch-lazy-highlight feature
  2420. ;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
  2421. ;; When active, *every* match for the current search string is
  2422. ;; highlighted: the current one using the normal isearch match color
  2423. ;; and all the others using `isearch-lazy-highlight'. The extra
  2424. ;; highlighting makes it easier to anticipate where the cursor will
  2425. ;; land each time you press C-s or C-r to repeat a pending search.
  2426. ;; Highlighting of these additional matches happens in a deferred
  2427. ;; fashion using "idle timers," so the cycles needed do not rob
  2428. ;; isearch of its usual snappy response.
  2429. ;; IMPLEMENTATION NOTE: This depends on some isearch internals.
  2430. ;; Specifically:
  2431. ;; - `isearch-update' is expected to be called (at least) every time
  2432. ;; the search string or window-start changes;
  2433. ;; - `isearch-string' is expected to contain the current search
  2434. ;; string as entered by the user;
  2435. ;; - the type of the current search is expected to be given by
  2436. ;; `isearch-word' and `isearch-regexp';
  2437. ;; - the direction of the current search is expected to be given by
  2438. ;; `isearch-forward';
  2439. ;; - the variable `isearch-error' is expected to be true
  2440. ;; only if `isearch-string' is an invalid regexp.
  2441. (defvar isearch-lazy-highlight-overlays nil)
  2442. (defvar isearch-lazy-highlight-wrapped nil)
  2443. (defvar isearch-lazy-highlight-start-limit nil)
  2444. (defvar isearch-lazy-highlight-end-limit nil)
  2445. (defvar isearch-lazy-highlight-start nil)
  2446. (defvar isearch-lazy-highlight-end nil)
  2447. (defvar isearch-lazy-highlight-timer nil)
  2448. (defvar isearch-lazy-highlight-last-string nil)
  2449. (defvar isearch-lazy-highlight-window nil)
  2450. (defvar isearch-lazy-highlight-window-start nil)
  2451. (defvar isearch-lazy-highlight-window-end nil)
  2452. (defvar isearch-lazy-highlight-case-fold-search nil)
  2453. (defvar isearch-lazy-highlight-regexp nil)
  2454. (defvar isearch-lazy-highlight-space-regexp nil)
  2455. (defvar isearch-lazy-highlight-word nil)
  2456. (defvar isearch-lazy-highlight-forward nil)
  2457. (defvar isearch-lazy-highlight-error nil)
  2458. (defun lazy-highlight-cleanup (&optional force)
  2459. "Stop lazy highlighting and remove extra highlighting from current buffer.
  2460. FORCE non-nil means do it whether or not `lazy-highlight-cleanup'
  2461. is nil. This function is called when exiting an incremental search if
  2462. `lazy-highlight-cleanup' is non-nil."
  2463. (interactive '(t))
  2464. (if (or force lazy-highlight-cleanup)
  2465. (while isearch-lazy-highlight-overlays
  2466. (delete-overlay (car isearch-lazy-highlight-overlays))
  2467. (setq isearch-lazy-highlight-overlays
  2468. (cdr isearch-lazy-highlight-overlays))))
  2469. (when isearch-lazy-highlight-timer
  2470. (cancel-timer isearch-lazy-highlight-timer)
  2471. (setq isearch-lazy-highlight-timer nil)))
  2472. (define-obsolete-function-alias 'isearch-lazy-highlight-cleanup
  2473. 'lazy-highlight-cleanup
  2474. "22.1")
  2475. (defun isearch-lazy-highlight-new-loop (&optional beg end)
  2476. "Cleanup any previous `lazy-highlight' loop and begin a new one.
  2477. BEG and END specify the bounds within which highlighting should occur.
  2478. This is called when `isearch-update' is invoked (which can cause the
  2479. search string to change or the window to scroll). It is also used
  2480. by other Emacs features."
  2481. (when (and (null executing-kbd-macro)
  2482. (sit-for 0) ;make sure (window-start) is credible
  2483. (or (not (equal isearch-string
  2484. isearch-lazy-highlight-last-string))
  2485. (not (eq (selected-window)
  2486. isearch-lazy-highlight-window))
  2487. (not (eq isearch-lazy-highlight-case-fold-search
  2488. isearch-case-fold-search))
  2489. (not (eq isearch-lazy-highlight-regexp
  2490. isearch-regexp))
  2491. (not (eq isearch-lazy-highlight-word
  2492. isearch-word))
  2493. (not (= (window-start)
  2494. isearch-lazy-highlight-window-start))
  2495. (not (= (window-end) ; Window may have been split/joined.
  2496. isearch-lazy-highlight-window-end))
  2497. (not (eq isearch-forward
  2498. isearch-lazy-highlight-forward))
  2499. ;; In case we are recovering from an error.
  2500. (not (equal isearch-error
  2501. isearch-lazy-highlight-error))))
  2502. ;; something important did indeed change
  2503. (lazy-highlight-cleanup t) ;kill old loop & remove overlays
  2504. (setq isearch-lazy-highlight-error isearch-error)
  2505. ;; It used to check for `(not isearch-error)' here, but actually
  2506. ;; lazy-highlighting might find matches to highlight even when
  2507. ;; `isearch-error' is non-nil. (Bug#9918)
  2508. (setq isearch-lazy-highlight-start-limit beg
  2509. isearch-lazy-highlight-end-limit end)
  2510. (setq isearch-lazy-highlight-window (selected-window)
  2511. isearch-lazy-highlight-window-start (window-start)
  2512. isearch-lazy-highlight-window-end (window-end)
  2513. isearch-lazy-highlight-start (point)
  2514. isearch-lazy-highlight-end (point)
  2515. isearch-lazy-highlight-wrapped nil
  2516. isearch-lazy-highlight-last-string isearch-string
  2517. isearch-lazy-highlight-case-fold-search isearch-case-fold-search
  2518. isearch-lazy-highlight-regexp isearch-regexp
  2519. isearch-lazy-highlight-space-regexp search-whitespace-regexp
  2520. isearch-lazy-highlight-word isearch-word
  2521. isearch-lazy-highlight-forward isearch-forward)
  2522. (unless (equal isearch-string "")
  2523. (setq isearch-lazy-highlight-timer
  2524. (run-with-idle-timer lazy-highlight-initial-delay nil
  2525. 'isearch-lazy-highlight-update)))))
  2526. (defun isearch-lazy-highlight-search ()
  2527. "Search ahead for the next or previous match, for lazy highlighting.
  2528. Attempt to do the search exactly the way the pending Isearch would."
  2529. (condition-case nil
  2530. (let ((case-fold-search isearch-lazy-highlight-case-fold-search)
  2531. (isearch-regexp isearch-lazy-highlight-regexp)
  2532. (search-spaces-regexp isearch-lazy-highlight-space-regexp)
  2533. (isearch-word isearch-lazy-highlight-word)
  2534. (search-invisible nil) ; don't match invisible text
  2535. (retry t)
  2536. (success nil)
  2537. (isearch-forward isearch-lazy-highlight-forward)
  2538. (bound (if isearch-lazy-highlight-forward
  2539. (min (or isearch-lazy-highlight-end-limit (point-max))
  2540. (if isearch-lazy-highlight-wrapped
  2541. isearch-lazy-highlight-start
  2542. (window-end)))
  2543. (max (or isearch-lazy-highlight-start-limit (point-min))
  2544. (if isearch-lazy-highlight-wrapped
  2545. isearch-lazy-highlight-end
  2546. (window-start))))))
  2547. ;; Use a loop like in `isearch-search'.
  2548. (while retry
  2549. (setq success (isearch-search-string
  2550. isearch-lazy-highlight-last-string bound t))
  2551. ;; Clear RETRY unless the search predicate says
  2552. ;; to skip this search hit.
  2553. (if (or (not success)
  2554. (= (point) bound) ; like (bobp) (eobp) in `isearch-search'.
  2555. (= (match-beginning 0) (match-end 0))
  2556. (funcall isearch-filter-predicate
  2557. (match-beginning 0) (match-end 0)))
  2558. (setq retry nil)))
  2559. success)
  2560. (error nil)))
  2561. (defun isearch-lazy-highlight-update ()
  2562. "Update highlighting of other matches for current search."
  2563. (let ((max lazy-highlight-max-at-a-time)
  2564. (looping t)
  2565. nomore)
  2566. (with-local-quit
  2567. (save-selected-window
  2568. (if (and (window-live-p isearch-lazy-highlight-window)
  2569. (not (eq (selected-window) isearch-lazy-highlight-window)))
  2570. (select-window isearch-lazy-highlight-window))
  2571. (save-excursion
  2572. (save-match-data
  2573. (goto-char (if isearch-lazy-highlight-forward
  2574. isearch-lazy-highlight-end
  2575. isearch-lazy-highlight-start))
  2576. (while looping
  2577. (let ((found (isearch-lazy-highlight-search)))
  2578. (when max
  2579. (setq max (1- max))
  2580. (if (<= max 0)
  2581. (setq looping nil)))
  2582. (if found
  2583. (let ((mb (match-beginning 0))
  2584. (me (match-end 0)))
  2585. (if (= mb me) ;zero-length match
  2586. (if isearch-lazy-highlight-forward
  2587. (if (= mb (if isearch-lazy-highlight-wrapped
  2588. isearch-lazy-highlight-start
  2589. (window-end)))
  2590. (setq found nil)
  2591. (forward-char 1))
  2592. (if (= mb (if isearch-lazy-highlight-wrapped
  2593. isearch-lazy-highlight-end
  2594. (window-start)))
  2595. (setq found nil)
  2596. (forward-char -1)))
  2597. ;; non-zero-length match
  2598. (let ((ov (make-overlay mb me)))
  2599. (push ov isearch-lazy-highlight-overlays)
  2600. ;; 1000 is higher than ediff's 100+,
  2601. ;; but lower than isearch main overlay's 1001
  2602. (overlay-put ov 'priority 1000)
  2603. (overlay-put ov 'face lazy-highlight-face)
  2604. (overlay-put ov 'window (selected-window))))
  2605. (if isearch-lazy-highlight-forward
  2606. (setq isearch-lazy-highlight-end (point))
  2607. (setq isearch-lazy-highlight-start (point)))))
  2608. ;; not found or zero-length match at the search bound
  2609. (if (not found)
  2610. (if isearch-lazy-highlight-wrapped
  2611. (setq looping nil
  2612. nomore t)
  2613. (setq isearch-lazy-highlight-wrapped t)
  2614. (if isearch-lazy-highlight-forward
  2615. (progn
  2616. (setq isearch-lazy-highlight-end (window-start))
  2617. (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))
  2618. (window-start))))
  2619. (setq isearch-lazy-highlight-start (window-end))
  2620. (goto-char (min (or isearch-lazy-highlight-end-limit (point-max))
  2621. (window-end))))))))
  2622. (unless nomore
  2623. (setq isearch-lazy-highlight-timer
  2624. (run-at-time lazy-highlight-interval nil
  2625. 'isearch-lazy-highlight-update)))))))))
  2626. (defun isearch-resume (string regexp word forward message case-fold)
  2627. "Resume an incremental search.
  2628. STRING is the string or regexp searched for.
  2629. REGEXP non-nil means the resumed search was a regexp search.
  2630. WORD non-nil means resume a word search.
  2631. FORWARD non-nil means resume a forward search.
  2632. MESSAGE is the echo-area message recorded for the search resumed.
  2633. CASE-FOLD non-nil means the search was case-insensitive."
  2634. (isearch-mode forward regexp nil nil word)
  2635. (setq isearch-string string
  2636. isearch-message message
  2637. isearch-case-fold-search case-fold)
  2638. (isearch-search)
  2639. (isearch-update))
  2640. ;;; isearch.el ends here