isearch.el 110 KB

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