history-substring-search.zsh 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. : ${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'}
  45. : ${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'}
  46. : ${HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'}
  47. : ${HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''}
  48. : ${HISTORY_SUBSTRING_SEARCH_FUZZY=''}
  49. : ${HISTORY_SUBSTRING_SEARCH_PREFIXED=''}
  50. #-----------------------------------------------------------------------------
  51. # declare internal global variables
  52. #-----------------------------------------------------------------------------
  53. typeset -g BUFFER MATCH MBEGIN MEND CURSOR
  54. typeset -g _history_substring_search_refresh_display
  55. typeset -g _history_substring_search_query_highlight
  56. typeset -g _history_substring_search_result
  57. typeset -g _history_substring_search_query
  58. typeset -g -a _history_substring_search_query_parts
  59. typeset -g -a _history_substring_search_raw_matches
  60. typeset -g -i _history_substring_search_raw_match_index
  61. typeset -g -a _history_substring_search_matches
  62. typeset -g -i _history_substring_search_match_index
  63. typeset -g -A _history_substring_search_unique_filter
  64. typeset -g -i _history_substring_search_zsh_5_9
  65. #-----------------------------------------------------------------------------
  66. # the main ZLE widgets
  67. #-----------------------------------------------------------------------------
  68. history-substring-search-up() {
  69. _history-substring-search-begin
  70. _history-substring-search-up-history ||
  71. _history-substring-search-up-buffer ||
  72. _history-substring-search-up-search
  73. _history-substring-search-end
  74. }
  75. history-substring-search-down() {
  76. _history-substring-search-begin
  77. _history-substring-search-down-history ||
  78. _history-substring-search-down-buffer ||
  79. _history-substring-search-down-search
  80. _history-substring-search-end
  81. }
  82. zle -N history-substring-search-up
  83. zle -N history-substring-search-down
  84. #-----------------------------------------------------------------------------
  85. # implementation details
  86. #-----------------------------------------------------------------------------
  87. zmodload -F zsh/parameter
  88. autoload -Uz is-at-least
  89. if is-at-least 5.9 $ZSH_VERSION; then
  90. _history_substring_search_zsh_5_9=1
  91. fi
  92. #
  93. # We have to "override" some keys and widgets if the
  94. # zsh-syntax-highlighting plugin has not been loaded:
  95. #
  96. # https://github.com/nicoulaj/zsh-syntax-highlighting
  97. #
  98. if [[ $+functions[_zsh_highlight] -eq 0 ]]; then
  99. #
  100. # Dummy implementation of _zsh_highlight() that
  101. # simply removes any existing highlights when the
  102. # user inserts printable characters into $BUFFER.
  103. #
  104. _zsh_highlight() {
  105. if [[ $KEYS == [[:print:]] ]]; then
  106. region_highlight=()
  107. fi
  108. }
  109. #
  110. # Check if $1 denotes the name of a callable function, i.e. it is fully
  111. # defined or it is marked for autoloading and autoloading it at the first
  112. # call to it will succeed. In particular, if $1 has been marked for
  113. # autoloading but is not available in $fpath, then it will return 1 (false).
  114. #
  115. # This is based on the zsh-syntax-highlighting plugin.
  116. #
  117. _history-substring-search-function-callable() {
  118. if (( ${+functions[$1]} )) && ! [[ "$functions[$1]" == *"builtin autoload -X"* ]]; then
  119. return 0 # already fully loaded
  120. else
  121. # "$1" is either an autoload stub, or not a function at all.
  122. # We expect 'autoload +X' to return non-zero if it fails to fully load
  123. # the function.
  124. ( autoload -U +X -- "$1" 2>/dev/null )
  125. return $?
  126. fi
  127. }
  128. #
  129. # The zsh-syntax-highlighting plugin uses zle-line-pre-redraw hook instead
  130. # of the legacy "bind all widgets" if 1) zsh has the memo= feature (added in
  131. # version 5.9) and 2) add-zle-hook-widget is available.
  132. #
  133. if [[ $_history_substring_search_zsh_5_9 -eq 1 ]] && _history-substring-search-function-callable add-zle-hook-widget; then
  134. #
  135. # The following code is based on the zsh-syntax-highlighting plugin.
  136. #
  137. autoload -U add-zle-hook-widget
  138. _history-substring-search-zle-line-finish() {
  139. #
  140. # Reset $WIDGET since the 'main' highlighter depends on it.
  141. #
  142. # Since $WIDGET is declared by zle as read-only in this function's scope,
  143. # a nested function is required in order to shadow its built-in value;
  144. # see "User-defined widgets" in zshall.
  145. #
  146. () {
  147. local -h -r WIDGET=zle-line-finish
  148. _zsh_highlight
  149. }
  150. }
  151. _history-substring-search-zle-line-pre-redraw() {
  152. #
  153. # If the zsh-syntax-highlighting plugin has been loaded (after our plugin
  154. # plugin, otherwise this hook wouldn't be called), remove our hooks.
  155. #
  156. if [[ $+ZSH_HIGHLIGHT_VERSION -eq 1 ]]; then
  157. autoload -U add-zle-hook-widget
  158. add-zle-hook-widget -d zle-line-pre-redraw _history-substring-search-zle-line-pre-redraw
  159. add-zle-hook-widget -d zle-line-finish _history-substring-search-zle-line-finish
  160. return 0
  161. fi
  162. #
  163. # Set $? to 0 for _zsh_highlight. Without this, subsequent
  164. # zle-line-pre-redraw hooks won't run, since add-zle-hook-widget happens to
  165. # call us with $? == 1 in the common case.
  166. #
  167. true && _zsh_highlight "$@"
  168. }
  169. if [[ -o zle ]]; then
  170. add-zle-hook-widget zle-line-pre-redraw _history-substring-search-zle-line-pre-redraw
  171. add-zle-hook-widget zle-line-finish _history-substring-search-zle-line-finish
  172. fi
  173. else
  174. #
  175. # The following snippet was taken from the zsh-syntax-highlighting project:
  176. # https://github.com/zsh-users/zsh-syntax-highlighting/blob/56b134f5d62ae3d4e66c7f52bd0cc2595f9b305b/zsh-syntax-highlighting.zsh#L126-161
  177. #
  178. # SPDX-SnippetBegin
  179. # SPDX-License-Identifier: BSD-3-Clause
  180. # SPDX-SnippetCopyrightText: 2010-2011 zsh-syntax-highlighting contributors
  181. #--------------8<-------------------8<-------------------8<-----------------
  182. # Rebind all ZLE widgets to make them invoke _zsh_highlights.
  183. _zsh_highlight_bind_widgets()
  184. {
  185. # Load ZSH module zsh/zleparameter, needed to override user defined widgets.
  186. zmodload zsh/zleparameter 2>/dev/null || {
  187. echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
  188. return 1
  189. }
  190. # Override ZLE widgets to make them invoke _zsh_highlight.
  191. local cur_widget
  192. for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep|yank*)}; do
  193. case $widgets[$cur_widget] in
  194. # Already rebound event: do nothing.
  195. user:$cur_widget|user:_zsh_highlight_widget_*);;
  196. # User defined widget: override and rebind old one with prefix "orig-".
  197. user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
  198. _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
  199. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  200. # Completion widget: override and rebind old one with prefix "orig-".
  201. completion:*) eval "zle -C orig-$cur_widget ${${widgets[$cur_widget]#*:}/:/ }; \
  202. _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
  203. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  204. # Builtin widget: override and make it call the builtin ".widget".
  205. builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget -- \"\$@\" && _zsh_highlight }; \
  206. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  207. # Default: unhandled case.
  208. *) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
  209. esac
  210. done
  211. }
  212. #-------------->8------------------->8------------------->8-----------------
  213. # SPDX-SnippetEnd
  214. _zsh_highlight_bind_widgets
  215. fi
  216. unfunction _history-substring-search-function-callable
  217. fi
  218. _history-substring-search-begin() {
  219. setopt localoptions extendedglob
  220. _history_substring_search_refresh_display=
  221. _history_substring_search_query_highlight=
  222. #
  223. # If the buffer is the same as the previously displayed history substring
  224. # search result, then just keep stepping through the match list. Otherwise
  225. # start a new search.
  226. #
  227. if [[ -n $BUFFER && $BUFFER == ${_history_substring_search_result:-} ]]; then
  228. return;
  229. fi
  230. #
  231. # Clear the previous result.
  232. #
  233. _history_substring_search_result=''
  234. if [[ -z $BUFFER ]]; then
  235. #
  236. # If the buffer is empty, we will just act like up-history/down-history
  237. # in ZSH, so we do not need to actually search the history. This should
  238. # speed things up a little.
  239. #
  240. _history_substring_search_query=
  241. _history_substring_search_query_parts=()
  242. _history_substring_search_raw_matches=()
  243. else
  244. #
  245. # For the purpose of highlighting we keep a copy of the original
  246. # query string.
  247. #
  248. _history_substring_search_query=$BUFFER
  249. #
  250. # compose search pattern
  251. #
  252. if [[ -n $HISTORY_SUBSTRING_SEARCH_FUZZY ]]; then
  253. #
  254. # `=` split string in arguments
  255. #
  256. _history_substring_search_query_parts=(${=_history_substring_search_query})
  257. else
  258. _history_substring_search_query_parts=(${==_history_substring_search_query})
  259. fi
  260. #
  261. # Escape and join query parts with wildcard character '*' as seperator
  262. # `(j:CHAR:)` join array to string with CHAR as seperator
  263. #
  264. local search_pattern="${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*"
  265. #
  266. # Support anchoring history search to the beginning of the command
  267. #
  268. if [[ -z $HISTORY_SUBSTRING_SEARCH_PREFIXED ]]; then
  269. search_pattern="*${search_pattern}"
  270. fi
  271. #
  272. # Find all occurrences of the search pattern in the history file.
  273. #
  274. # (k) returns the "keys" (history index numbers) instead of the values
  275. # (R) returns values in reverse older, so the index of the youngest
  276. # matching history entry is at the head of the list.
  277. #
  278. _history_substring_search_raw_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${search_pattern}]})
  279. fi
  280. #
  281. # In order to stay as responsive as possible, we will process the raw
  282. # matches lazily (when the user requests the next match) to choose items
  283. # that need to be displayed to the user.
  284. # _history_substring_search_raw_match_index holds the index of the last
  285. # unprocessed entry in _history_substring_search_raw_matches. Any items
  286. # that need to be displayed will be added to
  287. # _history_substring_search_matches.
  288. #
  289. # We use an associative array (_history_substring_search_unique_filter) as
  290. # a 'set' data structure to ensure uniqueness of the results if desired.
  291. # If an entry (key) is in the set (non-empty value), then we have already
  292. # added that entry to _history_substring_search_matches.
  293. #
  294. _history_substring_search_raw_match_index=0
  295. _history_substring_search_matches=()
  296. _history_substring_search_unique_filter=()
  297. #
  298. # If $_history_substring_search_match_index is equal to
  299. # $#_history_substring_search_matches + 1, this indicates that we
  300. # are beyond the end of $_history_substring_search_matches and that we
  301. # have also processed all entries in
  302. # _history_substring_search_raw_matches.
  303. #
  304. # If $#_history_substring_search_match_index is equal to 0, this indicates
  305. # that we are beyond the beginning of $_history_substring_search_matches.
  306. #
  307. # If we have initially pressed "up" we have to initialize
  308. # $_history_substring_search_match_index to 0 so that it will be
  309. # incremented to 1.
  310. #
  311. # If we have initially pressed "down" we have to initialize
  312. # $_history_substring_search_match_index to 1 so that it will be
  313. # decremented to 0.
  314. #
  315. if [[ $WIDGET == history-substring-search-down ]]; then
  316. _history_substring_search_match_index=1
  317. else
  318. _history_substring_search_match_index=0
  319. fi
  320. }
  321. _history-substring-search-end() {
  322. setopt localoptions extendedglob
  323. local highlight_memo=
  324. _history_substring_search_result=$BUFFER
  325. if [[ $_history_substring_search_zsh_5_9 -eq 1 ]]; then
  326. highlight_memo='memo=history-substring-search'
  327. fi
  328. # the search was successful so display the result properly by clearing away
  329. # existing highlights and moving the cursor to the end of the result buffer
  330. if [[ $_history_substring_search_refresh_display -eq 1 ]]; then
  331. if [[ -n $highlight_memo ]]; then
  332. region_highlight=( "${(@)region_highlight:#*${highlight_memo}*}" )
  333. else
  334. region_highlight=()
  335. fi
  336. CURSOR=${#BUFFER}
  337. fi
  338. # highlight command line using zsh-syntax-highlighting
  339. _zsh_highlight
  340. # highlight the search query inside the command line
  341. if [[ -n $_history_substring_search_query_highlight ]]; then
  342. # highlight first matching query parts
  343. local highlight_start_index=0
  344. local highlight_end_index=0
  345. local query_part
  346. for query_part in $_history_substring_search_query_parts; do
  347. local escaped_query_part=${query_part//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
  348. # (i) get index of pattern
  349. local query_part_match_index="${${BUFFER:$highlight_start_index}[(i)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${escaped_query_part}]}"
  350. if [[ $query_part_match_index -le ${#BUFFER:$highlight_start_index} ]]; then
  351. highlight_start_index=$(( $highlight_start_index + $query_part_match_index ))
  352. highlight_end_index=$(( $highlight_start_index + ${#query_part} ))
  353. region_highlight+=(
  354. "$(($highlight_start_index - 1)) $(($highlight_end_index - 1)) ${_history_substring_search_query_highlight}${highlight_memo:+,$highlight_memo}"
  355. )
  356. fi
  357. done
  358. fi
  359. # For debugging purposes:
  360. # zle -R "mn: "$_history_substring_search_match_index" m#: "${#_history_substring_search_matches}
  361. # read -k -t 200 && zle -U $REPLY
  362. #
  363. # When this function returns, z-sy-h runs its line-pre-redraw hook. It has no
  364. # logic for determining highlight priority, when two different memo= marked
  365. # region highlights overlap; instead, it always prioritises itself. Below is
  366. # a workaround for dealing with it.
  367. #
  368. if [[ $_history_substring_search_zsh_5_9 -eq 1 ]]; then
  369. zle -R
  370. #
  371. # After line redraw with desired highlight, wait for timeout or user input
  372. # before removing search highlight and exiting. This ensures no highlights
  373. # are left lingering after search is finished.
  374. #
  375. read -k -t ${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_TIMEOUT:-1} && zle -U $REPLY
  376. region_highlight=( "${(@)region_highlight:#*${highlight_memo}*}" )
  377. fi
  378. # Exit successfully from the history-substring-search-* widgets.
  379. return 0
  380. }
  381. _history-substring-search-up-buffer() {
  382. #
  383. # Check if the UP arrow was pressed to move the cursor within a multi-line
  384. # buffer. This amounts to three tests:
  385. #
  386. # 1. $#buflines -gt 1.
  387. #
  388. # 2. $CURSOR -ne $#BUFFER.
  389. #
  390. # 3. Check if we are on the first line of the current multi-line buffer.
  391. # If so, pressing UP would amount to leaving the multi-line buffer.
  392. #
  393. # We check this by adding an extra "x" to $LBUFFER, which makes
  394. # sure that xlbuflines is always equal to the number of lines
  395. # until $CURSOR (including the line with the cursor on it).
  396. #
  397. local buflines XLBUFFER xlbuflines
  398. buflines=(${(f)BUFFER})
  399. XLBUFFER=$LBUFFER"x"
  400. xlbuflines=(${(f)XLBUFFER})
  401. if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xlbuflines -ne 1 ]]; then
  402. zle up-line-or-history
  403. return 0
  404. fi
  405. return 1
  406. }
  407. _history-substring-search-down-buffer() {
  408. #
  409. # Check if the DOWN arrow was pressed to move the cursor within a multi-line
  410. # buffer. This amounts to three tests:
  411. #
  412. # 1. $#buflines -gt 1.
  413. #
  414. # 2. $CURSOR -ne $#BUFFER.
  415. #
  416. # 3. Check if we are on the last line of the current multi-line buffer.
  417. # If so, pressing DOWN would amount to leaving the multi-line buffer.
  418. #
  419. # We check this by adding an extra "x" to $RBUFFER, which makes
  420. # sure that xrbuflines is always equal to the number of lines
  421. # from $CURSOR (including the line with the cursor on it).
  422. #
  423. local buflines XRBUFFER xrbuflines
  424. buflines=(${(f)BUFFER})
  425. XRBUFFER="x"$RBUFFER
  426. xrbuflines=(${(f)XRBUFFER})
  427. if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xrbuflines -ne 1 ]]; then
  428. zle down-line-or-history
  429. return 0
  430. fi
  431. return 1
  432. }
  433. _history-substring-search-up-history() {
  434. #
  435. # Behave like up in ZSH, except clear the $BUFFER
  436. # when beginning of history is reached like in Fish.
  437. #
  438. if [[ -z $_history_substring_search_query ]]; then
  439. # we have reached the absolute top of history
  440. if [[ $HISTNO -eq 1 ]]; then
  441. BUFFER=
  442. # going up from somewhere below the top of history
  443. else
  444. zle up-line-or-history
  445. fi
  446. return 0
  447. fi
  448. return 1
  449. }
  450. _history-substring-search-down-history() {
  451. #
  452. # Behave like down-history in ZSH, except clear the
  453. # $BUFFER when end of history is reached like in Fish.
  454. #
  455. if [[ -z $_history_substring_search_query ]]; then
  456. # going down from the absolute top of history
  457. if [[ $HISTNO -eq 1 && -z $BUFFER ]]; then
  458. BUFFER=${history[1]}
  459. _history_substring_search_refresh_display=1
  460. # going down from somewhere above the bottom of history
  461. else
  462. zle down-line-or-history
  463. fi
  464. return 0
  465. fi
  466. return 1
  467. }
  468. _history_substring_search_process_raw_matches() {
  469. #
  470. # Process more outstanding raw matches and append any matches that need to
  471. # be displayed to the user to _history_substring_search_matches.
  472. # Return whether there were any more results appended.
  473. #
  474. #
  475. # While we have more raw matches. Process them to see if there are any more
  476. # matches that need to be displayed to the user.
  477. #
  478. while [[ $_history_substring_search_raw_match_index -lt $#_history_substring_search_raw_matches ]]; do
  479. #
  480. # Move on to the next raw entry and get its history index.
  481. #
  482. _history_substring_search_raw_match_index+=1
  483. local index=${_history_substring_search_raw_matches[$_history_substring_search_raw_match_index]}
  484. #
  485. # If HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set to a non-empty value,
  486. # then ensure that only unique matches are presented to the user.
  487. # When HIST_IGNORE_ALL_DUPS is set, ZSH already ensures a unique history,
  488. # so in this case we do not need to do anything.
  489. #
  490. if [[ ! -o HIST_IGNORE_ALL_DUPS && -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  491. #
  492. # Get the actual history entry at the new index, and check if we have
  493. # already added it to _history_substring_search_matches.
  494. #
  495. local entry=${history[$index]}
  496. if [[ -z ${_history_substring_search_unique_filter[$entry]} ]]; then
  497. #
  498. # This is a new unique entry. Add it to the filter and append the
  499. # index to _history_substring_search_matches.
  500. #
  501. _history_substring_search_unique_filter[$entry]=1
  502. _history_substring_search_matches+=($index)
  503. #
  504. # Indicate that we did find a match.
  505. #
  506. return 0
  507. fi
  508. else
  509. #
  510. # Just append the new history index to the processed matches.
  511. #
  512. _history_substring_search_matches+=($index)
  513. #
  514. # Indicate that we did find a match.
  515. #
  516. return 0
  517. fi
  518. done
  519. #
  520. # We are beyond the end of the list of raw matches. Indicate that no
  521. # more matches are available.
  522. #
  523. return 1
  524. }
  525. _history-substring-search-has-next() {
  526. #
  527. # Predicate function that returns whether any more older matches are
  528. # available.
  529. #
  530. if [[ $_history_substring_search_match_index -lt $#_history_substring_search_matches ]]; then
  531. #
  532. # We did not reach the end of the processed list, so we do have further
  533. # matches.
  534. #
  535. return 0
  536. else
  537. #
  538. # We are at the end of the processed list. Try to process further
  539. # unprocessed matches. _history_substring_search_process_raw_matches
  540. # returns whether any more matches were available, so just return
  541. # that result.
  542. #
  543. _history_substring_search_process_raw_matches
  544. return $?
  545. fi
  546. }
  547. _history-substring-search-has-prev() {
  548. #
  549. # Predicate function that returns whether any more younger matches are
  550. # available.
  551. #
  552. if [[ $_history_substring_search_match_index -gt 1 ]]; then
  553. #
  554. # We did not reach the beginning of the processed list, so we do have
  555. # further matches.
  556. #
  557. return 0
  558. else
  559. #
  560. # We are at the beginning of the processed list. We do not have any more
  561. # matches.
  562. #
  563. return 1
  564. fi
  565. }
  566. _history-substring-search-found() {
  567. #
  568. # A match is available. The index of the match is held in
  569. # $_history_substring_search_match_index
  570. #
  571. # 1. Make $BUFFER equal to the matching history entry.
  572. #
  573. # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
  574. # to highlight the current buffer.
  575. #
  576. BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
  577. _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
  578. }
  579. _history-substring-search-not-found() {
  580. #
  581. # No more matches are available.
  582. #
  583. # 1. Make $BUFFER equal to $_history_substring_search_query so the user can
  584. # revise it and search again.
  585. #
  586. # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
  587. # to highlight the current buffer.
  588. #
  589. BUFFER=$_history_substring_search_query
  590. _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
  591. }
  592. _history-substring-search-up-search() {
  593. _history_substring_search_refresh_display=1
  594. #
  595. # Select history entry during history-substring-down-search:
  596. #
  597. # The following variables have been initialized in
  598. # _history-substring-search-up/down-search():
  599. #
  600. # $_history_substring_search_matches is the current list of matches that
  601. # need to be displayed to the user.
  602. # $_history_substring_search_match_index is the index of the current match
  603. # that is being displayed to the user.
  604. #
  605. # The range of values that $_history_substring_search_match_index can take
  606. # is: [0, $#_history_substring_search_matches + 1]. A value of 0
  607. # indicates that we are beyond the beginning of
  608. # $_history_substring_search_matches. A value of
  609. # $#_history_substring_search_matches + 1 indicates that we are beyond
  610. # the end of $_history_substring_search_matches and that we have also
  611. # processed all entries in _history_substring_search_raw_matches.
  612. #
  613. # If $_history_substring_search_match_index equals
  614. # $#_history_substring_search_matches and
  615. # $_history_substring_search_raw_match_index is not greater than
  616. # $#_history_substring_search_raw_matches, then we need to further process
  617. # $_history_substring_search_raw_matches to see if there are any more
  618. # entries that need to be displayed to the user.
  619. #
  620. # In _history-substring-search-up-search() the initial value of
  621. # $_history_substring_search_match_index is 0. This value is set in
  622. # _history-substring-search-begin(). _history-substring-search-up-search()
  623. # will initially increment it to 1.
  624. #
  625. if [[ $_history_substring_search_match_index -gt $#_history_substring_search_matches ]]; then
  626. #
  627. # We are beyond the end of $_history_substring_search_matches. This
  628. # can only happen if we have also exhausted the unprocessed matches in
  629. # _history_substring_search_raw_matches.
  630. #
  631. # 1. Update display to indicate search not found.
  632. #
  633. _history-substring-search-not-found
  634. return
  635. fi
  636. if _history-substring-search-has-next; then
  637. #
  638. # We do have older matches.
  639. #
  640. # 1. Move index to point to the next match.
  641. # 2. Update display to indicate search found.
  642. #
  643. _history_substring_search_match_index+=1
  644. _history-substring-search-found
  645. else
  646. #
  647. # We do not have older matches.
  648. #
  649. # 1. Move the index beyond the end of
  650. # _history_substring_search_matches.
  651. # 2. Update display to indicate search not found.
  652. #
  653. _history_substring_search_match_index+=1
  654. _history-substring-search-not-found
  655. fi
  656. #
  657. # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
  658. # history should be matched, make sure the new and old results are different.
  659. #
  660. # However, if the HIST_IGNORE_ALL_DUPS shell option, or
  661. # HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set, then we already have a
  662. # unique history, so in this case we do not need to do anything.
  663. #
  664. if [[ -o HIST_IGNORE_ALL_DUPS || -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  665. return
  666. fi
  667. if [[ -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
  668. #
  669. # Repeat the current search so that a different (unique) match is found.
  670. #
  671. _history-substring-search-up-search
  672. fi
  673. }
  674. _history-substring-search-down-search() {
  675. _history_substring_search_refresh_display=1
  676. #
  677. # Select history entry during history-substring-down-search:
  678. #
  679. # The following variables have been initialized in
  680. # _history-substring-search-up/down-search():
  681. #
  682. # $_history_substring_search_matches is the current list of matches that
  683. # need to be displayed to the user.
  684. # $_history_substring_search_match_index is the index of the current match
  685. # that is being displayed to the user.
  686. #
  687. # The range of values that $_history_substring_search_match_index can take
  688. # is: [0, $#_history_substring_search_matches + 1]. A value of 0
  689. # indicates that we are beyond the beginning of
  690. # $_history_substring_search_matches. A value of
  691. # $#_history_substring_search_matches + 1 indicates that we are beyond
  692. # the end of $_history_substring_search_matches and that we have also
  693. # processed all entries in _history_substring_search_raw_matches.
  694. #
  695. # In _history-substring-search-down-search() the initial value of
  696. # $_history_substring_search_match_index is 1. This value is set in
  697. # _history-substring-search-begin(). _history-substring-search-down-search()
  698. # will initially decrement it to 0.
  699. #
  700. if [[ $_history_substring_search_match_index -lt 1 ]]; then
  701. #
  702. # We are beyond the beginning of $_history_substring_search_matches.
  703. #
  704. # 1. Update display to indicate search not found.
  705. #
  706. _history-substring-search-not-found
  707. return
  708. fi
  709. if _history-substring-search-has-prev; then
  710. #
  711. # We do have younger matches.
  712. #
  713. # 1. Move index to point to the previous match.
  714. # 2. Update display to indicate search found.
  715. #
  716. _history_substring_search_match_index+=-1
  717. _history-substring-search-found
  718. else
  719. #
  720. # We do not have younger matches.
  721. #
  722. # 1. Move the index beyond the beginning of
  723. # _history_substring_search_matches.
  724. # 2. Update display to indicate search not found.
  725. #
  726. _history_substring_search_match_index+=-1
  727. _history-substring-search-not-found
  728. fi
  729. #
  730. # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
  731. # history should be matched, make sure the new and old results are different.
  732. #
  733. # However, if the HIST_IGNORE_ALL_DUPS shell option, or
  734. # HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set, then we already have a
  735. # unique history, so in this case we do not need to do anything.
  736. #
  737. if [[ -o HIST_IGNORE_ALL_DUPS || -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
  738. return
  739. fi
  740. if [[ -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
  741. #
  742. # Repeat the current search so that a different (unique) match is found.
  743. #
  744. _history-substring-search-down-search
  745. fi
  746. }
  747. # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
  748. # vim: ft=zsh sw=2 ts=2 et