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. local query_part
  286. for query_part in $_history_substring_search_query_parts; do
  287. local escaped_query_part=${query_part//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
  288. # (i) get index of pattern
  289. local query_part_match_index="${${BUFFER:$highlight_start_index}[(i)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${escaped_query_part}]}"
  290. if [[ $query_part_match_index -le ${#BUFFER:$highlight_start_index} ]]; then
  291. highlight_start_index=$(( $highlight_start_index + $query_part_match_index ))
  292. highlight_end_index=$(( $highlight_start_index + ${#query_part} ))
  293. region_highlight+=("$(($highlight_start_index - 1)) $(($highlight_end_index - 1)) $_history_substring_search_query_highlight")
  294. fi
  295. done
  296. fi
  297. # For debugging purposes:
  298. # zle -R "mn: "$_history_substring_search_match_index" m#: "${#_history_substring_search_matches}
  299. # read -k -t 200 && zle -U $REPLY
  300. # Exit successfully from the history-substring-search-* widgets.
  301. return 0
  302. }
  303. _history-substring-search-up-buffer() {
  304. #
  305. # Check if the UP arrow was pressed to move the cursor within a multi-line
  306. # buffer. This amounts to three tests:
  307. #
  308. # 1. $#buflines -gt 1.
  309. #
  310. # 2. $CURSOR -ne $#BUFFER.
  311. #
  312. # 3. Check if we are on the first line of the current multi-line buffer.
  313. # If so, pressing UP would amount to leaving the multi-line buffer.
  314. #
  315. # We check this by adding an extra "x" to $LBUFFER, which makes
  316. # sure that xlbuflines is always equal to the number of lines
  317. # until $CURSOR (including the line with the cursor on it).
  318. #
  319. local buflines XLBUFFER xlbuflines
  320. buflines=(${(f)BUFFER})
  321. XLBUFFER=$LBUFFER"x"
  322. xlbuflines=(${(f)XLBUFFER})
  323. if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xlbuflines -ne 1 ]]; then
  324. zle up-line-or-history
  325. return 0
  326. fi
  327. return 1
  328. }
  329. _history-substring-search-down-buffer() {
  330. #
  331. # Check if the DOWN arrow was pressed to move the cursor within a multi-line
  332. # buffer. This amounts to three tests:
  333. #
  334. # 1. $#buflines -gt 1.
  335. #
  336. # 2. $CURSOR -ne $#BUFFER.
  337. #
  338. # 3. Check if we are on the last line of the current multi-line buffer.
  339. # If so, pressing DOWN would amount to leaving the multi-line buffer.
  340. #
  341. # We check this by adding an extra "x" to $RBUFFER, which makes
  342. # sure that xrbuflines is always equal to the number of lines
  343. # from $CURSOR (including the line with the cursor on it).
  344. #
  345. local buflines XRBUFFER xrbuflines
  346. buflines=(${(f)BUFFER})
  347. XRBUFFER="x"$RBUFFER
  348. xrbuflines=(${(f)XRBUFFER})
  349. if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xrbuflines -ne 1 ]]; then
  350. zle down-line-or-history
  351. return 0
  352. fi
  353. return 1
  354. }
  355. _history-substring-search-up-history() {
  356. #
  357. # Behave like up in ZSH, except clear the $BUFFER
  358. # when beginning of history is reached like in Fish.
  359. #
  360. if [[ -z $_history_substring_search_query ]]; then
  361. # we have reached the absolute top of history
  362. if [[ $HISTNO -eq 1 ]]; then
  363. BUFFER=
  364. # going up from somewhere below the top of history
  365. else
  366. zle up-line-or-history
  367. fi
  368. return 0
  369. fi
  370. return 1
  371. }
  372. _history-substring-search-down-history() {
  373. #
  374. # Behave like down-history in ZSH, except clear the
  375. # $BUFFER when end of history is reached like in Fish.
  376. #
  377. if [[ -z $_history_substring_search_query ]]; then
  378. # going down from the absolute top of history
  379. if [[ $HISTNO -eq 1 && -z $BUFFER ]]; then
  380. BUFFER=${history[1]}
  381. _history_substring_search_refresh_display=1
  382. # going down from somewhere above the bottom of history
  383. else
  384. zle down-line-or-history
  385. fi
  386. return 0
  387. fi
  388. return 1
  389. }
  390. _history_substring_search_process_raw_matches() {
  391. #
  392. # Process more outstanding raw matches and append any matches that need to
  393. # be displayed to the user to _history_substring_search_matches.
  394. # Return whether there were any more results appended.
  395. #
  396. #
  397. # While we have more raw matches. Process them to see if there are any more
  398. # matches that need to be displayed to the user.
  399. #
  400. while [[ $_history_substring_search_raw_match_index -lt $#_history_substring_search_raw_matches ]]; do
  401. #
  402. # Move on to the next raw entry and get its history index.
  403. #
  404. _history_substring_search_raw_match_index+=1
  405. local index=${_history_substring_search_raw_matches[$_history_substring_search_raw_match_index]}
  406. #
  407. # If HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set to a non-empty value,
  408. # then ensure that only unique matches are presented to the user.
  409. # When HIST_IGNORE_ALL_DUPS is set, ZSH already ensures a unique history,
  410. # so in this case we do not need to do anything.
  411. #
  412. if [[ ! -o HIST_IGNORE_ALL_DUPS && -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  413. #
  414. # Get the actual history entry at the new index, and check if we have
  415. # already added it to _history_substring_search_matches.
  416. #
  417. local entry=${history[$index]}
  418. if [[ -z ${_history_substring_search_unique_filter[$entry]} ]]; then
  419. #
  420. # This is a new unique entry. Add it to the filter and append the
  421. # index to _history_substring_search_matches.
  422. #
  423. _history_substring_search_unique_filter[$entry]=1
  424. _history_substring_search_matches+=($index)
  425. #
  426. # Indicate that we did find a match.
  427. #
  428. return 0
  429. fi
  430. else
  431. #
  432. # Just append the new history index to the processed matches.
  433. #
  434. _history_substring_search_matches+=($index)
  435. #
  436. # Indicate that we did find a match.
  437. #
  438. return 0
  439. fi
  440. done
  441. #
  442. # We are beyond the end of the list of raw matches. Indicate that no
  443. # more matches are available.
  444. #
  445. return 1
  446. }
  447. _history-substring-search-has-next() {
  448. #
  449. # Predicate function that returns whether any more older matches are
  450. # available.
  451. #
  452. if [[ $_history_substring_search_match_index -lt $#_history_substring_search_matches ]]; then
  453. #
  454. # We did not reach the end of the processed list, so we do have further
  455. # matches.
  456. #
  457. return 0
  458. else
  459. #
  460. # We are at the end of the processed list. Try to process further
  461. # unprocessed matches. _history_substring_search_process_raw_matches
  462. # returns whether any more matches were available, so just return
  463. # that result.
  464. #
  465. _history_substring_search_process_raw_matches
  466. return $?
  467. fi
  468. }
  469. _history-substring-search-has-prev() {
  470. #
  471. # Predicate function that returns whether any more younger matches are
  472. # available.
  473. #
  474. if [[ $_history_substring_search_match_index -gt 1 ]]; then
  475. #
  476. # We did not reach the beginning of the processed list, so we do have
  477. # further matches.
  478. #
  479. return 0
  480. else
  481. #
  482. # We are at the beginning of the processed list. We do not have any more
  483. # matches.
  484. #
  485. return 1
  486. fi
  487. }
  488. _history-substring-search-found() {
  489. #
  490. # A match is available. The index of the match is held in
  491. # $_history_substring_search_match_index
  492. #
  493. # 1. Make $BUFFER equal to the matching history entry.
  494. #
  495. # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
  496. # to highlight the current buffer.
  497. #
  498. BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
  499. _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
  500. }
  501. _history-substring-search-not-found() {
  502. #
  503. # No more matches are available.
  504. #
  505. # 1. Make $BUFFER equal to $_history_substring_search_query so the user can
  506. # revise it and search again.
  507. #
  508. # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
  509. # to highlight the current buffer.
  510. #
  511. BUFFER=$_history_substring_search_query
  512. _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
  513. }
  514. _history-substring-search-up-search() {
  515. _history_substring_search_refresh_display=1
  516. #
  517. # Select history entry during history-substring-down-search:
  518. #
  519. # The following variables have been initialized in
  520. # _history-substring-search-up/down-search():
  521. #
  522. # $_history_substring_search_matches is the current list of matches that
  523. # need to be displayed to the user.
  524. # $_history_substring_search_match_index is the index of the current match
  525. # that is being displayed to the user.
  526. #
  527. # The range of values that $_history_substring_search_match_index can take
  528. # is: [0, $#_history_substring_search_matches + 1]. A value of 0
  529. # indicates that we are beyond the beginning of
  530. # $_history_substring_search_matches. A value of
  531. # $#_history_substring_search_matches + 1 indicates that we are beyond
  532. # the end of $_history_substring_search_matches and that we have also
  533. # processed all entries in _history_substring_search_raw_matches.
  534. #
  535. # If $_history_substring_search_match_index equals
  536. # $#_history_substring_search_matches and
  537. # $_history_substring_search_raw_match_index is not greater than
  538. # $#_history_substring_search_raw_matches, then we need to further process
  539. # $_history_substring_search_raw_matches to see if there are any more
  540. # entries that need to be displayed to the user.
  541. #
  542. # In _history-substring-search-up-search() the initial value of
  543. # $_history_substring_search_match_index is 0. This value is set in
  544. # _history-substring-search-begin(). _history-substring-search-up-search()
  545. # will initially increment it to 1.
  546. #
  547. if [[ $_history_substring_search_match_index -gt $#_history_substring_search_matches ]]; then
  548. #
  549. # We are beyond the end of $_history_substring_search_matches. This
  550. # can only happen if we have also exhausted the unprocessed matches in
  551. # _history_substring_search_raw_matches.
  552. #
  553. # 1. Update display to indicate search not found.
  554. #
  555. _history-substring-search-not-found
  556. return
  557. fi
  558. if _history-substring-search-has-next; then
  559. #
  560. # We do have older matches.
  561. #
  562. # 1. Move index to point to the next match.
  563. # 2. Update display to indicate search found.
  564. #
  565. _history_substring_search_match_index+=1
  566. _history-substring-search-found
  567. else
  568. #
  569. # We do not have older matches.
  570. #
  571. # 1. Move the index beyond the end of
  572. # _history_substring_search_matches.
  573. # 2. Update display to indicate search not found.
  574. #
  575. _history_substring_search_match_index+=1
  576. _history-substring-search-not-found
  577. fi
  578. #
  579. # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
  580. # history should be matched, make sure the new and old results are different.
  581. #
  582. # However, if the HIST_IGNORE_ALL_DUPS shell option, or
  583. # HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set, then we already have a
  584. # unique history, so in this case we do not need to do anything.
  585. #
  586. if [[ -o HIST_IGNORE_ALL_DUPS || -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  587. return
  588. fi
  589. if [[ -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
  590. #
  591. # Repeat the current search so that a different (unique) match is found.
  592. #
  593. _history-substring-search-up-search
  594. fi
  595. }
  596. _history-substring-search-down-search() {
  597. _history_substring_search_refresh_display=1
  598. #
  599. # Select history entry during history-substring-down-search:
  600. #
  601. # The following variables have been initialized in
  602. # _history-substring-search-up/down-search():
  603. #
  604. # $_history_substring_search_matches is the current list of matches that
  605. # need to be displayed to the user.
  606. # $_history_substring_search_match_index is the index of the current match
  607. # that is being displayed to the user.
  608. #
  609. # The range of values that $_history_substring_search_match_index can take
  610. # is: [0, $#_history_substring_search_matches + 1]. A value of 0
  611. # indicates that we are beyond the beginning of
  612. # $_history_substring_search_matches. A value of
  613. # $#_history_substring_search_matches + 1 indicates that we are beyond
  614. # the end of $_history_substring_search_matches and that we have also
  615. # processed all entries in _history_substring_search_raw_matches.
  616. #
  617. # In _history-substring-search-down-search() the initial value of
  618. # $_history_substring_search_match_index is 1. This value is set in
  619. # _history-substring-search-begin(). _history-substring-search-down-search()
  620. # will initially decrement it to 0.
  621. #
  622. if [[ $_history_substring_search_match_index -lt 1 ]]; then
  623. #
  624. # We are beyond the beginning of $_history_substring_search_matches.
  625. #
  626. # 1. Update display to indicate search not found.
  627. #
  628. _history-substring-search-not-found
  629. return
  630. fi
  631. if _history-substring-search-has-prev; then
  632. #
  633. # We do have younger matches.
  634. #
  635. # 1. Move index to point to the previous match.
  636. # 2. Update display to indicate search found.
  637. #
  638. _history_substring_search_match_index+=-1
  639. _history-substring-search-found
  640. else
  641. #
  642. # We do not have younger matches.
  643. #
  644. # 1. Move the index beyond the beginning of
  645. # _history_substring_search_matches.
  646. # 2. Update display to indicate search not found.
  647. #
  648. _history_substring_search_match_index+=-1
  649. _history-substring-search-not-found
  650. fi
  651. #
  652. # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
  653. # history should be matched, make sure the new and old results are different.
  654. #
  655. # However, if the HIST_IGNORE_ALL_DUPS shell option, or
  656. # HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set, then we already have a
  657. # unique history, so in this case we do not need to do anything.
  658. #
  659. if [[ -o HIST_IGNORE_ALL_DUPS || -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  660. return
  661. fi
  662. if [[ -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
  663. #
  664. # Repeat the current search so that a different (unique) match is found.
  665. #
  666. _history-substring-search-down-search
  667. fi
  668. }
  669. # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
  670. # vim: ft=zsh sw=2 ts=2 et