zsh-history-substring-search.zsh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. #!/usr/bin/env zsh
  2. ##############################################################################
  3. #
  4. # Copyright (c) 2009 Peter Stephenson
  5. # Copyright (c) 2011 Guido van Steen
  6. # Copyright (c) 2011 Suraj N. Kurapati
  7. # Copyright (c) 2011 Sorin Ionescu
  8. # Copyright (c) 2011 Vincent Guerci
  9. # Copyright (c) 2016 Geza Lore
  10. # Copyright (c) 2017 Bengt Brodersen
  11. # All rights reserved.
  12. #
  13. # Redistribution and use in source and binary forms, with or without
  14. # modification, are permitted provided that the following conditions are met:
  15. #
  16. # * Redistributions of source code must retain the above copyright
  17. # notice, this list of conditions and the following disclaimer.
  18. #
  19. # * Redistributions in binary form must reproduce the above
  20. # copyright notice, this list of conditions and the following
  21. # disclaimer in the documentation and/or other materials provided
  22. # with the distribution.
  23. #
  24. # * Neither the name of the FIZSH nor the names of its contributors
  25. # may be used to endorse or promote products derived from this
  26. # software without specific prior written permission.
  27. #
  28. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  32. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. # POSSIBILITY OF SUCH DAMAGE.
  39. #
  40. ##############################################################################
  41. #-----------------------------------------------------------------------------
  42. # declare global configuration variables
  43. #-----------------------------------------------------------------------------
  44. typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'
  45. typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
  46. typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'
  47. typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''
  48. typeset -g HISTORY_SUBSTRING_SEARCH_FUZZY=''
  49. #-----------------------------------------------------------------------------
  50. # declare internal global variables
  51. #-----------------------------------------------------------------------------
  52. typeset -g BUFFER MATCH MBEGIN MEND CURSOR
  53. typeset -g _history_substring_search_refresh_display
  54. typeset -g _history_substring_search_query_highlight
  55. typeset -g _history_substring_search_result
  56. typeset -g _history_substring_search_query
  57. typeset -g -a _history_substring_search_query_parts
  58. typeset -g -a _history_substring_search_raw_matches
  59. typeset -g -i _history_substring_search_raw_match_index
  60. typeset -g -a _history_substring_search_matches
  61. typeset -g -i _history_substring_search_match_index
  62. typeset -g -A _history_substring_search_unique_filter
  63. #-----------------------------------------------------------------------------
  64. # the main ZLE widgets
  65. #-----------------------------------------------------------------------------
  66. history-substring-search-up() {
  67. _history-substring-search-begin
  68. _history-substring-search-up-history ||
  69. _history-substring-search-up-buffer ||
  70. _history-substring-search-up-search
  71. _history-substring-search-end
  72. }
  73. history-substring-search-down() {
  74. _history-substring-search-begin
  75. _history-substring-search-down-history ||
  76. _history-substring-search-down-buffer ||
  77. _history-substring-search-down-search
  78. _history-substring-search-end
  79. }
  80. zle -N history-substring-search-up
  81. zle -N history-substring-search-down
  82. #-----------------------------------------------------------------------------
  83. # implementation details
  84. #-----------------------------------------------------------------------------
  85. zmodload -F zsh/parameter
  86. #
  87. # We have to "override" some keys and widgets if the
  88. # zsh-syntax-highlighting plugin has not been loaded:
  89. #
  90. # https://github.com/nicoulaj/zsh-syntax-highlighting
  91. #
  92. if [[ $+functions[_zsh_highlight] -eq 0 ]]; then
  93. #
  94. # Dummy implementation of _zsh_highlight() that
  95. # simply removes any existing highlights when the
  96. # user inserts printable characters into $BUFFER.
  97. #
  98. _zsh_highlight() {
  99. if [[ $KEYS == [[:print:]] ]]; then
  100. region_highlight=()
  101. fi
  102. }
  103. #
  104. # The following snippet was taken from the zsh-syntax-highlighting project:
  105. #
  106. # https://github.com/zsh-users/zsh-syntax-highlighting/blob/56b134f5d62ae3d4e66c7f52bd0cc2595f9b305b/zsh-syntax-highlighting.zsh#L126-161
  107. #
  108. # Copyright (c) 2010-2011 zsh-syntax-highlighting contributors
  109. # All rights reserved.
  110. #
  111. # Redistribution and use in source and binary forms, with or without
  112. # modification, are permitted provided that the following conditions are
  113. # met:
  114. #
  115. # * Redistributions of source code must retain the above copyright
  116. # notice, this list of conditions and the following disclaimer.
  117. #
  118. # * Redistributions in binary form must reproduce the above copyright
  119. # notice, this list of conditions and the following disclaimer in the
  120. # documentation and/or other materials provided with the distribution.
  121. #
  122. # * Neither the name of the zsh-syntax-highlighting contributors nor the
  123. # names of its contributors may be used to endorse or promote products
  124. # derived from this software without specific prior written permission.
  125. #
  126. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  127. # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  128. # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  129. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  130. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  131. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  132. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  133. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  134. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  135. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  136. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  137. #
  138. #--------------8<-------------------8<-------------------8<-----------------
  139. # Rebind all ZLE widgets to make them invoke _zsh_highlights.
  140. _zsh_highlight_bind_widgets()
  141. {
  142. # Load ZSH module zsh/zleparameter, needed to override user defined widgets.
  143. zmodload zsh/zleparameter 2>/dev/null || {
  144. echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
  145. return 1
  146. }
  147. # Override ZLE widgets to make them invoke _zsh_highlight.
  148. local cur_widget
  149. for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep|yank*)}; do
  150. case $widgets[$cur_widget] in
  151. # Already rebound event: do nothing.
  152. user:$cur_widget|user:_zsh_highlight_widget_*);;
  153. # User defined widget: override and rebind old one with prefix "orig-".
  154. user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
  155. _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
  156. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  157. # Completion widget: override and rebind old one with prefix "orig-".
  158. completion:*) eval "zle -C orig-$cur_widget ${${widgets[$cur_widget]#*:}/:/ }; \
  159. _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
  160. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  161. # Builtin widget: override and make it call the builtin ".widget".
  162. builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget -- \"\$@\" && _zsh_highlight }; \
  163. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  164. # Default: unhandled case.
  165. *) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
  166. esac
  167. done
  168. }
  169. #-------------->8------------------->8------------------->8-----------------
  170. _zsh_highlight_bind_widgets
  171. fi
  172. _history-substring-search-begin() {
  173. setopt localoptions extendedglob
  174. _history_substring_search_refresh_display=
  175. _history_substring_search_query_highlight=
  176. #
  177. # If the buffer is the same as the previously displayed history substring
  178. # search result, then just keep stepping through the match list. Otherwise
  179. # start a new search.
  180. #
  181. if [[ -n $BUFFER && $BUFFER == ${_history_substring_search_result:-} ]]; then
  182. return;
  183. fi
  184. #
  185. # Clear the previous result.
  186. #
  187. _history_substring_search_result=''
  188. if [[ -z $BUFFER ]]; then
  189. #
  190. # If the buffer is empty, we will just act like up-history/down-history
  191. # in ZSH, so we do not need to actually search the history. This should
  192. # speed things up a little.
  193. #
  194. _history_substring_search_query=
  195. _history_substring_search_query_parts=()
  196. _history_substring_search_raw_matches=()
  197. else
  198. #
  199. # For the purpose of highlighting we keep a copy of the original
  200. # query string.
  201. #
  202. _history_substring_search_query=$BUFFER
  203. #
  204. # compose search pattern
  205. #
  206. if [[ -n $HISTORY_SUBSTRING_SEARCH_FUZZY ]]; then
  207. #
  208. # `=` split string in arguments
  209. #
  210. _history_substring_search_query_parts=(${=_history_substring_search_query})
  211. else
  212. _history_substring_search_query_parts=(${_history_substring_search_query})
  213. fi
  214. #
  215. # Escape and join query parts with wildcard character '*' as seperator
  216. # `(j:CHAR:)` join array to string with CHAR as seperator
  217. #
  218. local search_pattern="*${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*"
  219. #
  220. # Find all occurrences of the search pattern in the history file.
  221. #
  222. # (k) returns the "keys" (history index numbers) instead of the values
  223. # (R) returns values in reverse older, so the index of the youngest
  224. # matching history entry is at the head of the list.
  225. #
  226. _history_substring_search_raw_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${search_pattern}]})
  227. fi
  228. #
  229. # In order to stay as responsive as possible, we will process the raw
  230. # matches lazily (when the user requests the next match) to choose items
  231. # that need to be displayed to the user.
  232. # _history_substring_search_raw_match_index holds the index of the last
  233. # unprocessed entry in _history_substring_search_raw_matches. Any items
  234. # that need to be displayed will be added to
  235. # _history_substring_search_matches.
  236. #
  237. # We use an associative array (_history_substring_search_unique_filter) as
  238. # a 'set' data structure to ensure uniqueness of the results if desired.
  239. # If an entry (key) is in the set (non-empty value), then we have already
  240. # added that entry to _history_substring_search_matches.
  241. #
  242. _history_substring_search_raw_match_index=0
  243. _history_substring_search_matches=()
  244. _history_substring_search_unique_filter=()
  245. #
  246. # If $_history_substring_search_match_index is equal to
  247. # $#_history_substring_search_matches + 1, this indicates that we
  248. # are beyond the end of $_history_substring_search_matches and that we
  249. # have also processed all entries in
  250. # _history_substring_search_raw_matches.
  251. #
  252. # If $#_history_substring_search_match_index is equal to 0, this indicates
  253. # that we are beyond the beginning of $_history_substring_search_matches.
  254. #
  255. # If we have initially pressed "up" we have to initialize
  256. # $_history_substring_search_match_index to 0 so that it will be
  257. # incremented to 1.
  258. #
  259. # If we have initially pressed "down" we have to initialize
  260. # $_history_substring_search_match_index to 1 so that it will be
  261. # decremented to 0.
  262. #
  263. if [[ $WIDGET == history-substring-search-down ]]; then
  264. _history_substring_search_match_index=1
  265. else
  266. _history_substring_search_match_index=0
  267. fi
  268. }
  269. _history-substring-search-end() {
  270. setopt localoptions extendedglob
  271. _history_substring_search_result=$BUFFER
  272. # the search was successful so display the result properly by clearing away
  273. # existing highlights and moving the cursor to the end of the result buffer
  274. if [[ $_history_substring_search_refresh_display -eq 1 ]]; then
  275. region_highlight=()
  276. CURSOR=${#BUFFER}
  277. fi
  278. # highlight command line using zsh-syntax-highlighting
  279. _zsh_highlight
  280. # highlight the search query inside the command line
  281. if [[ -n $_history_substring_search_query_highlight ]]; then
  282. # highlight first matching query parts
  283. local highlight_start_index=0
  284. local highlight_end_index=0
  285. for query_part in $_history_substring_search_query_parts; do
  286. local escaped_query_part=${query_part//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
  287. # (i) get index of pattern
  288. local query_part_match_index=${${BUFFER:$highlight_start_index}[(i)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${escaped_query_part}]}
  289. if [[ $query_part_match_index -le ${#BUFFER:$highlight_start_index} ]]; then
  290. highlight_start_index=$(( $highlight_start_index + $query_part_match_index ))
  291. highlight_end_index=$(( $highlight_start_index + ${#query_part} ))
  292. region_highlight+=("$(($highlight_start_index - 1)) $(($highlight_end_index - 1)) $_history_substring_search_query_highlight")
  293. fi
  294. done
  295. fi
  296. # For debugging purposes:
  297. # zle -R "mn: "$_history_substring_search_match_index" m#: "${#_history_substring_search_matches}
  298. # read -k -t 200 && zle -U $REPLY
  299. # Exit successfully from the history-substring-search-* widgets.
  300. return 0
  301. }
  302. _history-substring-search-up-buffer() {
  303. #
  304. # Check if the UP arrow was pressed to move the cursor within a multi-line
  305. # buffer. This amounts to three tests:
  306. #
  307. # 1. $#buflines -gt 1.
  308. #
  309. # 2. $CURSOR -ne $#BUFFER.
  310. #
  311. # 3. Check if we are on the first line of the current multi-line buffer.
  312. # If so, pressing UP would amount to leaving the multi-line buffer.
  313. #
  314. # We check this by adding an extra "x" to $LBUFFER, which makes
  315. # sure that xlbuflines is always equal to the number of lines
  316. # until $CURSOR (including the line with the cursor on it).
  317. #
  318. local buflines XLBUFFER xlbuflines
  319. buflines=(${(f)BUFFER})
  320. XLBUFFER=$LBUFFER"x"
  321. xlbuflines=(${(f)XLBUFFER})
  322. if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xlbuflines -ne 1 ]]; then
  323. zle up-line-or-history
  324. return 0
  325. fi
  326. return 1
  327. }
  328. _history-substring-search-down-buffer() {
  329. #
  330. # Check if the DOWN arrow was pressed to move the cursor within a multi-line
  331. # buffer. This amounts to three tests:
  332. #
  333. # 1. $#buflines -gt 1.
  334. #
  335. # 2. $CURSOR -ne $#BUFFER.
  336. #
  337. # 3. Check if we are on the last line of the current multi-line buffer.
  338. # If so, pressing DOWN would amount to leaving the multi-line buffer.
  339. #
  340. # We check this by adding an extra "x" to $RBUFFER, which makes
  341. # sure that xrbuflines is always equal to the number of lines
  342. # from $CURSOR (including the line with the cursor on it).
  343. #
  344. local buflines XRBUFFER xrbuflines
  345. buflines=(${(f)BUFFER})
  346. XRBUFFER="x"$RBUFFER
  347. xrbuflines=(${(f)XRBUFFER})
  348. if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xrbuflines -ne 1 ]]; then
  349. zle down-line-or-history
  350. return 0
  351. fi
  352. return 1
  353. }
  354. _history-substring-search-up-history() {
  355. #
  356. # Behave like up in ZSH, except clear the $BUFFER
  357. # when beginning of history is reached like in Fish.
  358. #
  359. if [[ -z $_history_substring_search_query ]]; then
  360. # we have reached the absolute top of history
  361. if [[ $HISTNO -eq 1 ]]; then
  362. BUFFER=
  363. # going up from somewhere below the top of history
  364. else
  365. zle up-line-or-history
  366. fi
  367. return 0
  368. fi
  369. return 1
  370. }
  371. _history-substring-search-down-history() {
  372. #
  373. # Behave like down-history in ZSH, except clear the
  374. # $BUFFER when end of history is reached like in Fish.
  375. #
  376. if [[ -z $_history_substring_search_query ]]; then
  377. # going down from the absolute top of history
  378. if [[ $HISTNO -eq 1 && -z $BUFFER ]]; then
  379. BUFFER=${history[1]}
  380. _history_substring_search_refresh_display=1
  381. # going down from somewhere above the bottom of history
  382. else
  383. zle down-line-or-history
  384. fi
  385. return 0
  386. fi
  387. return 1
  388. }
  389. _history_substring_search_process_raw_matches() {
  390. #
  391. # Process more outstanding raw matches and append any matches that need to
  392. # be displayed to the user to _history_substring_search_matches.
  393. # Return whether there were any more results appended.
  394. #
  395. #
  396. # While we have more raw matches. Process them to see if there are any more
  397. # matches that need to be displayed to the user.
  398. #
  399. while [[ $_history_substring_search_raw_match_index -lt $#_history_substring_search_raw_matches ]]; do
  400. #
  401. # Move on to the next raw entry and get its history index.
  402. #
  403. _history_substring_search_raw_match_index+=1
  404. local index=${_history_substring_search_raw_matches[$_history_substring_search_raw_match_index]}
  405. #
  406. # If HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set to a non-empty value,
  407. # then ensure that only unique matches are presented to the user.
  408. # When HIST_IGNORE_ALL_DUPS is set, ZSH already ensures a unique history,
  409. # so in this case we do not need to do anything.
  410. #
  411. if [[ ! -o HIST_IGNORE_ALL_DUPS && -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  412. #
  413. # Get the actual history entry at the new index, and check if we have
  414. # already added it to _history_substring_search_matches.
  415. #
  416. local entry=${history[$index]}
  417. if [[ -z ${_history_substring_search_unique_filter[$entry]} ]]; then
  418. #
  419. # This is a new unique entry. Add it to the filter and append the
  420. # index to _history_substring_search_matches.
  421. #
  422. _history_substring_search_unique_filter[$entry]=1
  423. _history_substring_search_matches+=($index)
  424. #
  425. # Indicate that we did find a match.
  426. #
  427. return 0
  428. fi
  429. else
  430. #
  431. # Just append the new history index to the processed matches.
  432. #
  433. _history_substring_search_matches+=($index)
  434. #
  435. # Indicate that we did find a match.
  436. #
  437. return 0
  438. fi
  439. done
  440. #
  441. # We are beyond the end of the list of raw matches. Indicate that no
  442. # more matches are available.
  443. #
  444. return 1
  445. }
  446. _history-substring-search-has-next() {
  447. #
  448. # Predicate function that returns whether any more older matches are
  449. # available.
  450. #
  451. if [[ $_history_substring_search_match_index -lt $#_history_substring_search_matches ]]; then
  452. #
  453. # We did not reach the end of the processed list, so we do have further
  454. # matches.
  455. #
  456. return 0
  457. else
  458. #
  459. # We are at the end of the processed list. Try to process further
  460. # unprocessed matches. _history_substring_search_process_raw_matches
  461. # returns whether any more matches were available, so just return
  462. # that result.
  463. #
  464. _history_substring_search_process_raw_matches
  465. return $?
  466. fi
  467. }
  468. _history-substring-search-has-prev() {
  469. #
  470. # Predicate function that returns whether any more younger matches are
  471. # available.
  472. #
  473. if [[ $_history_substring_search_match_index -gt 1 ]]; then
  474. #
  475. # We did not reach the beginning of the processed list, so we do have
  476. # further matches.
  477. #
  478. return 0
  479. else
  480. #
  481. # We are at the beginning of the processed list. We do not have any more
  482. # matches.
  483. #
  484. return 1
  485. fi
  486. }
  487. _history-substring-search-found() {
  488. #
  489. # A match is available. The index of the match is held in
  490. # $_history_substring_search_match_index
  491. #
  492. # 1. Make $BUFFER equal to the matching history entry.
  493. #
  494. # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
  495. # to highlight the current buffer.
  496. #
  497. BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
  498. _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
  499. }
  500. _history-substring-search-not-found() {
  501. #
  502. # No more matches are available.
  503. #
  504. # 1. Make $BUFFER equal to $_history_substring_search_query so the user can
  505. # revise it and search again.
  506. #
  507. # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
  508. # to highlight the current buffer.
  509. #
  510. BUFFER=$_history_substring_search_query
  511. _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
  512. }
  513. _history-substring-search-up-search() {
  514. _history_substring_search_refresh_display=1
  515. #
  516. # Select history entry during history-substring-down-search:
  517. #
  518. # The following variables have been initialized in
  519. # _history-substring-search-up/down-search():
  520. #
  521. # $_history_substring_search_matches is the current list of matches that
  522. # need to be displayed to the user.
  523. # $_history_substring_search_match_index is the index of the current match
  524. # that is being displayed to the user.
  525. #
  526. # The range of values that $_history_substring_search_match_index can take
  527. # is: [0, $#_history_substring_search_matches + 1]. A value of 0
  528. # indicates that we are beyond the beginning of
  529. # $_history_substring_search_matches. A value of
  530. # $#_history_substring_search_matches + 1 indicates that we are beyond
  531. # the end of $_history_substring_search_matches and that we have also
  532. # processed all entries in _history_substring_search_raw_matches.
  533. #
  534. # If $_history_substring_search_match_index equals
  535. # $#_history_substring_search_matches and
  536. # $_history_substring_search_raw_match_index is not greater than
  537. # $#_history_substring_search_raw_matches, then we need to further process
  538. # $_history_substring_search_raw_matches to see if there are any more
  539. # entries that need to be displayed to the user.
  540. #
  541. # In _history-substring-search-up-search() the initial value of
  542. # $_history_substring_search_match_index is 0. This value is set in
  543. # _history-substring-search-begin(). _history-substring-search-up-search()
  544. # will initially increment it to 1.
  545. #
  546. if [[ $_history_substring_search_match_index -gt $#_history_substring_search_matches ]]; then
  547. #
  548. # We are beyond the end of $_history_substring_search_matches. This
  549. # can only happen if we have also exhausted the unprocessed matches in
  550. # _history_substring_search_raw_matches.
  551. #
  552. # 1. Update display to indicate search not found.
  553. #
  554. _history-substring-search-not-found
  555. return
  556. fi
  557. if _history-substring-search-has-next; then
  558. #
  559. # We do have older matches.
  560. #
  561. # 1. Move index to point to the next match.
  562. # 2. Update display to indicate search found.
  563. #
  564. _history_substring_search_match_index+=1
  565. _history-substring-search-found
  566. else
  567. #
  568. # We do not have older matches.
  569. #
  570. # 1. Move the index beyond the end of
  571. # _history_substring_search_matches.
  572. # 2. Update display to indicate search not found.
  573. #
  574. _history_substring_search_match_index+=1
  575. _history-substring-search-not-found
  576. fi
  577. #
  578. # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
  579. # history should be matched, make sure the new and old results are different.
  580. #
  581. # However, if the HIST_IGNORE_ALL_DUPS shell option, or
  582. # HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set, then we already have a
  583. # unique history, so in this case we do not need to do anything.
  584. #
  585. if [[ -o HIST_IGNORE_ALL_DUPS || -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  586. return
  587. fi
  588. if [[ -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
  589. #
  590. # Repeat the current search so that a different (unique) match is found.
  591. #
  592. _history-substring-search-up-search
  593. fi
  594. }
  595. _history-substring-search-down-search() {
  596. _history_substring_search_refresh_display=1
  597. #
  598. # Select history entry during history-substring-down-search:
  599. #
  600. # The following variables have been initialized in
  601. # _history-substring-search-up/down-search():
  602. #
  603. # $_history_substring_search_matches is the current list of matches that
  604. # need to be displayed to the user.
  605. # $_history_substring_search_match_index is the index of the current match
  606. # that is being displayed to the user.
  607. #
  608. # The range of values that $_history_substring_search_match_index can take
  609. # is: [0, $#_history_substring_search_matches + 1]. A value of 0
  610. # indicates that we are beyond the beginning of
  611. # $_history_substring_search_matches. A value of
  612. # $#_history_substring_search_matches + 1 indicates that we are beyond
  613. # the end of $_history_substring_search_matches and that we have also
  614. # processed all entries in _history_substring_search_raw_matches.
  615. #
  616. # In _history-substring-search-down-search() the initial value of
  617. # $_history_substring_search_match_index is 1. This value is set in
  618. # _history-substring-search-begin(). _history-substring-search-down-search()
  619. # will initially decrement it to 0.
  620. #
  621. if [[ $_history_substring_search_match_index -lt 1 ]]; then
  622. #
  623. # We are beyond the beginning of $_history_substring_search_matches.
  624. #
  625. # 1. Update display to indicate search not found.
  626. #
  627. _history-substring-search-not-found
  628. return
  629. fi
  630. if _history-substring-search-has-prev; then
  631. #
  632. # We do have younger matches.
  633. #
  634. # 1. Move index to point to the previous match.
  635. # 2. Update display to indicate search found.
  636. #
  637. _history_substring_search_match_index+=-1
  638. _history-substring-search-found
  639. else
  640. #
  641. # We do not have younger matches.
  642. #
  643. # 1. Move the index beyond the beginning of
  644. # _history_substring_search_matches.
  645. # 2. Update display to indicate search not found.
  646. #
  647. _history_substring_search_match_index+=-1
  648. _history-substring-search-not-found
  649. fi
  650. #
  651. # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
  652. # history should be matched, make sure the new and old results are different.
  653. #
  654. # However, if the HIST_IGNORE_ALL_DUPS shell option, or
  655. # HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set, then we already have a
  656. # unique history, so in this case we do not need to do anything.
  657. #
  658. if [[ -o HIST_IGNORE_ALL_DUPS || -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  659. return
  660. fi
  661. if [[ -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
  662. #
  663. # Repeat the current search so that a different (unique) match is found.
  664. #
  665. _history-substring-search-down-search
  666. fi
  667. }
  668. # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
  669. # vim: ft=zsh sw=2 ts=2 et