151
|
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 # All rights reserved.
|
|
10 #
|
|
11 # Redistribution and use in source and binary forms, with or without
|
|
12 # modification, are permitted provided that the following conditions are met:
|
|
13 #
|
|
14 # * Redistributions of source code must retain the above copyright
|
|
15 # notice, this list of conditions and the following disclaimer.
|
|
16 #
|
|
17 # * Redistributions in binary form must reproduce the above
|
|
18 # copyright notice, this list of conditions and the following
|
|
19 # disclaimer in the documentation and/or other materials provided
|
|
20 # with the distribution.
|
|
21 #
|
|
22 # * Neither the name of the FIZSH nor the names of its contributors
|
|
23 # may be used to endorse or promote products derived from this
|
|
24 # software without specific prior written permission.
|
|
25 #
|
|
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
27 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
28 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
29 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
30 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
31 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
32 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
33 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
34 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
35 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
36 # POSSIBILITY OF SUCH DAMAGE.
|
|
37 #
|
|
38 ##############################################################################
|
|
39
|
|
40 #-----------------------------------------------------------------------------
|
|
41 # configuration variables
|
|
42 #-----------------------------------------------------------------------------
|
|
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
|
|
48 #-----------------------------------------------------------------------------
|
|
49 # the main ZLE widgets
|
|
50 #-----------------------------------------------------------------------------
|
|
51
|
|
52 function history-substring-search-up() {
|
|
53 _history-substring-search-begin
|
|
54
|
|
55 _history-substring-search-up-history ||
|
|
56 _history-substring-search-up-buffer ||
|
|
57 _history-substring-search-up-search
|
|
58
|
|
59 _history-substring-search-end
|
|
60 }
|
|
61
|
|
62 function history-substring-search-down() {
|
|
63 _history-substring-search-begin
|
|
64
|
|
65 _history-substring-search-down-history ||
|
|
66 _history-substring-search-down-buffer ||
|
|
67 _history-substring-search-down-search
|
|
68
|
|
69 _history-substring-search-end
|
|
70 }
|
|
71
|
|
72 zle -N history-substring-search-up
|
|
73 zle -N history-substring-search-down
|
|
74
|
|
75 #-----------------------------------------------------------------------------
|
|
76 # implementation details
|
|
77 #-----------------------------------------------------------------------------
|
|
78
|
|
79 zmodload -F zsh/parameter
|
|
80
|
|
81 #
|
|
82 # We have to "override" some keys and widgets if the
|
|
83 # zsh-syntax-highlighting plugin has not been loaded:
|
|
84 #
|
|
85 # https://github.com/nicoulaj/zsh-syntax-highlighting
|
|
86 #
|
|
87 if [[ $+functions[_zsh_highlight] -eq 0 ]]; then
|
|
88 #
|
|
89 # Dummy implementation of _zsh_highlight() that
|
|
90 # simply removes any existing highlights when the
|
|
91 # user inserts printable characters into $BUFFER.
|
|
92 #
|
|
93 function _zsh_highlight() {
|
|
94 if [[ $KEYS == [[:print:]] ]]; then
|
|
95 region_highlight=()
|
|
96 fi
|
|
97 }
|
|
98
|
|
99 #
|
|
100 # The following snippet was taken from the zsh-syntax-highlighting project:
|
|
101 #
|
|
102 # https://github.com/zsh-users/zsh-syntax-highlighting/blob/56b134f5d62ae3d4e66c7f52bd0cc2595f9b305b/zsh-syntax-highlighting.zsh#L126-161
|
|
103 #
|
|
104 # Copyright (c) 2010-2011 zsh-syntax-highlighting contributors
|
|
105 # All rights reserved.
|
|
106 #
|
|
107 # Redistribution and use in source and binary forms, with or without
|
|
108 # modification, are permitted provided that the following conditions are
|
|
109 # met:
|
|
110 #
|
|
111 # * Redistributions of source code must retain the above copyright
|
|
112 # notice, this list of conditions and the following disclaimer.
|
|
113 #
|
|
114 # * Redistributions in binary form must reproduce the above copyright
|
|
115 # notice, this list of conditions and the following disclaimer in the
|
|
116 # documentation and/or other materials provided with the distribution.
|
|
117 #
|
|
118 # * Neither the name of the zsh-syntax-highlighting contributors nor the
|
|
119 # names of its contributors may be used to endorse or promote products
|
|
120 # derived from this software without specific prior written permission.
|
|
121 #
|
|
122 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
123 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
124 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
125 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
126 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
127 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
128 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
129 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
130 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
131 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
132 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
133 #
|
|
134 #--------------8<-------------------8<-------------------8<-----------------
|
|
135 # Rebind all ZLE widgets to make them invoke _zsh_highlights.
|
|
136 _zsh_highlight_bind_widgets()
|
|
137 {
|
|
138 # Load ZSH module zsh/zleparameter, needed to override user defined widgets.
|
|
139 zmodload zsh/zleparameter 2>/dev/null || {
|
|
140 echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
|
|
141 return 1
|
|
142 }
|
|
143
|
|
144 # Override ZLE widgets to make them invoke _zsh_highlight.
|
|
145 local cur_widget
|
|
146 for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep)}; do
|
|
147 case $widgets[$cur_widget] in
|
|
148
|
|
149 # Already rebound event: do nothing.
|
|
150 user:$cur_widget|user:_zsh_highlight_widget_*);;
|
|
151
|
|
152 # User defined widget: override and rebind old one with prefix "orig-".
|
|
153 user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
|
|
154 _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
|
|
155 zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
|
|
156
|
|
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
|
|
162 # Builtin widget: override and make it call the builtin ".widget".
|
|
163 builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget -- \"\$@\" && _zsh_highlight }; \
|
|
164 zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
|
|
165
|
|
166 # Default: unhandled case.
|
|
167 *) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
|
|
168 esac
|
|
169 done
|
|
170 }
|
|
171 #-------------->8------------------->8------------------->8-----------------
|
|
172
|
|
173 _zsh_highlight_bind_widgets
|
|
174 fi
|
|
175
|
|
176 function _history-substring-search-begin() {
|
|
177 setopt localoptions extendedglob
|
|
178
|
|
179 _history_substring_search_refresh_display=
|
|
180 _history_substring_search_query_highlight=
|
|
181
|
|
182 #
|
|
183 # Continue using the previous $_history_substring_search_result by default,
|
|
184 # unless the current query was cleared or a new/different query was entered.
|
|
185 #
|
|
186 if [[ -z $BUFFER || $BUFFER != $_history_substring_search_result ]]; then
|
|
187 #
|
|
188 # For the purpose of highlighting we will also keep
|
|
189 # a version without doubly-escaped meta characters.
|
|
190 #
|
|
191 _history_substring_search_query=$BUFFER
|
|
192
|
|
193 #
|
|
194 # $BUFFER contains the text that is in the command-line currently.
|
|
195 # we put an extra "\\" before meta characters such as "\(" and "\)",
|
|
196 # so that they become "\\\(" and "\\\)".
|
|
197 #
|
|
198 _history_substring_search_query_escaped=${BUFFER//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
|
|
199
|
|
200 #
|
|
201 # Find all occurrences of the search query in the history file.
|
|
202 #
|
|
203 # (k) turns it an array of line numbers.
|
|
204 #
|
|
205 # (on) seems to remove duplicates, which are default
|
|
206 # options. They can be turned off by (ON).
|
|
207 #
|
|
208 _history_substring_search_matches=(${(kon)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]})
|
|
209
|
|
210 #
|
|
211 # Define the range of values that $_history_substring_search_match_index
|
|
212 # can take: [0, $_history_substring_search_matches_count_plus].
|
|
213 #
|
|
214 _history_substring_search_matches_count=$#_history_substring_search_matches
|
|
215 _history_substring_search_matches_count_plus=$(( _history_substring_search_matches_count + 1 ))
|
|
216 _history_substring_search_matches_count_sans=$(( _history_substring_search_matches_count - 1 ))
|
|
217
|
|
218 #
|
|
219 # If $_history_substring_search_match_index is equal to
|
|
220 # $_history_substring_search_matches_count_plus, this indicates that we
|
|
221 # are beyond the beginning of $_history_substring_search_matches.
|
|
222 #
|
|
223 # If $_history_substring_search_match_index is equal to 0, this indicates
|
|
224 # that we are beyond the end of $_history_substring_search_matches.
|
|
225 #
|
|
226 # If we have initially pressed "up" we have to initialize
|
|
227 # $_history_substring_search_match_index to
|
|
228 # $_history_substring_search_matches_count_plus so that it will be
|
|
229 # decreased to $_history_substring_search_matches_count.
|
|
230 #
|
|
231 # If we have initially pressed "down" we have to initialize
|
|
232 # $_history_substring_search_match_index to
|
|
233 # $_history_substring_search_matches_count so that it will be increased to
|
|
234 # $_history_substring_search_matches_count_plus.
|
|
235 #
|
|
236 if [[ $WIDGET == history-substring-search-down ]]; then
|
|
237 _history_substring_search_match_index=$_history_substring_search_matches_count
|
|
238 else
|
|
239 _history_substring_search_match_index=$_history_substring_search_matches_count_plus
|
|
240 fi
|
|
241 fi
|
|
242 }
|
|
243
|
|
244 function _history-substring-search-end() {
|
|
245 setopt localoptions extendedglob
|
|
246
|
|
247 _history_substring_search_result=$BUFFER
|
|
248
|
|
249 # the search was succesful so display the result properly by clearing away
|
|
250 # existing highlights and moving the cursor to the end of the result buffer
|
|
251 if [[ $_history_substring_search_refresh_display -eq 1 ]]; then
|
|
252 region_highlight=()
|
|
253 CURSOR=${#BUFFER}
|
|
254 fi
|
|
255
|
|
256 # highlight command line using zsh-syntax-highlighting
|
|
257 _zsh_highlight
|
|
258
|
|
259 # highlight the search query inside the command line
|
|
260 if [[ -n $_history_substring_search_query_highlight && -n $_history_substring_search_query ]]; then
|
|
261 #
|
|
262 # The following expression yields a variable $MBEGIN, which
|
|
263 # indicates the begin position + 1 of the first occurrence
|
|
264 # of _history_substring_search_query_escaped in $BUFFER.
|
|
265 #
|
|
266 : ${(S)BUFFER##(#m$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)($_history_substring_search_query##)}
|
|
267 local begin=$(( MBEGIN - 1 ))
|
|
268 local end=$(( begin + $#_history_substring_search_query ))
|
|
269 region_highlight+=("$begin $end $_history_substring_search_query_highlight")
|
|
270 fi
|
|
271
|
|
272 # For debugging purposes:
|
|
273 # zle -R "mn: "$_history_substring_search_match_index" m#: "${#_history_substring_search_matches}
|
|
274 # read -k -t 200 && zle -U $REPLY
|
|
275
|
|
276 # Exit successfully from the history-substring-search-* widgets.
|
|
277 return 0
|
|
278 }
|
|
279
|
|
280 function _history-substring-search-up-buffer() {
|
|
281 #
|
|
282 # Check if the UP arrow was pressed to move the cursor within a multi-line
|
|
283 # buffer. This amounts to three tests:
|
|
284 #
|
|
285 # 1. $#buflines -gt 1.
|
|
286 #
|
|
287 # 2. $CURSOR -ne $#BUFFER.
|
|
288 #
|
|
289 # 3. Check if we are on the first line of the current multi-line buffer.
|
|
290 # If so, pressing UP would amount to leaving the multi-line buffer.
|
|
291 #
|
|
292 # We check this by adding an extra "x" to $LBUFFER, which makes
|
|
293 # sure that xlbuflines is always equal to the number of lines
|
|
294 # until $CURSOR (including the line with the cursor on it).
|
|
295 #
|
|
296 local buflines XLBUFFER xlbuflines
|
|
297 buflines=(${(f)BUFFER})
|
|
298 XLBUFFER=$LBUFFER"x"
|
|
299 xlbuflines=(${(f)XLBUFFER})
|
|
300
|
|
301 if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xlbuflines -ne 1 ]]; then
|
|
302 zle up-line-or-history
|
|
303 return 0
|
|
304 fi
|
|
305
|
|
306 return 1
|
|
307 }
|
|
308
|
|
309 function _history-substring-search-down-buffer() {
|
|
310 #
|
|
311 # Check if the DOWN arrow was pressed to move the cursor within a multi-line
|
|
312 # buffer. This amounts to three tests:
|
|
313 #
|
|
314 # 1. $#buflines -gt 1.
|
|
315 #
|
|
316 # 2. $CURSOR -ne $#BUFFER.
|
|
317 #
|
|
318 # 3. Check if we are on the last line of the current multi-line buffer.
|
|
319 # If so, pressing DOWN would amount to leaving the multi-line buffer.
|
|
320 #
|
|
321 # We check this by adding an extra "x" to $RBUFFER, which makes
|
|
322 # sure that xrbuflines is always equal to the number of lines
|
|
323 # from $CURSOR (including the line with the cursor on it).
|
|
324 #
|
|
325 local buflines XRBUFFER xrbuflines
|
|
326 buflines=(${(f)BUFFER})
|
|
327 XRBUFFER="x"$RBUFFER
|
|
328 xrbuflines=(${(f)XRBUFFER})
|
|
329
|
|
330 if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xrbuflines -ne 1 ]]; then
|
|
331 zle down-line-or-history
|
|
332 return 0
|
|
333 fi
|
|
334
|
|
335 return 1
|
|
336 }
|
|
337
|
|
338 function _history-substring-search-up-history() {
|
|
339 #
|
|
340 # Behave like up in ZSH, except clear the $BUFFER
|
|
341 # when beginning of history is reached like in Fish.
|
|
342 #
|
|
343 if [[ -z $_history_substring_search_query ]]; then
|
|
344
|
|
345 # we have reached the absolute top of history
|
|
346 if [[ $HISTNO -eq 1 ]]; then
|
|
347 BUFFER=
|
|
348
|
|
349 # going up from somewhere below the top of history
|
|
350 else
|
|
351 zle up-line-or-history
|
|
352 fi
|
|
353
|
|
354 return 0
|
|
355 fi
|
|
356
|
|
357 return 1
|
|
358 }
|
|
359
|
|
360 function _history-substring-search-down-history() {
|
|
361 #
|
|
362 # Behave like down-history in ZSH, except clear the
|
|
363 # $BUFFER when end of history is reached like in Fish.
|
|
364 #
|
|
365 if [[ -z $_history_substring_search_query ]]; then
|
|
366
|
|
367 # going down from the absolute top of history
|
|
368 if [[ $HISTNO -eq 1 && -z $BUFFER ]]; then
|
|
369 BUFFER=${history[1]}
|
|
370 _history_substring_search_refresh_display=1
|
|
371
|
|
372 # going down from somewhere above the bottom of history
|
|
373 else
|
|
374 zle down-line-or-history
|
|
375 fi
|
|
376
|
|
377 return 0
|
|
378 fi
|
|
379
|
|
380 return 1
|
|
381 }
|
|
382
|
|
383 function _history-substring-search-not-found() {
|
|
384 #
|
|
385 # Nothing matched the search query, so put it back into the $BUFFER while
|
|
386 # highlighting it accordingly so the user can revise it and search again.
|
|
387 #
|
|
388 _history_substring_search_old_buffer=$BUFFER
|
|
389 BUFFER=$_history_substring_search_query
|
|
390 _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
|
|
391 }
|
|
392
|
|
393 function _history-substring-search-up-search() {
|
|
394 _history_substring_search_refresh_display=1
|
|
395
|
|
396 #
|
|
397 # Highlight matches during history-substring-up-search:
|
|
398 #
|
|
399 # The following constants have been initialized in
|
|
400 # _history-substring-search-up/down-search():
|
|
401 #
|
|
402 # $_history_substring_search_matches is the current list of matches
|
|
403 # $_history_substring_search_matches_count is the current number of matches
|
|
404 # $_history_substring_search_matches_count_plus is the current number of matches + 1
|
|
405 # $_history_substring_search_matches_count_sans is the current number of matches - 1
|
|
406 # $_history_substring_search_match_index is the index of the current match
|
|
407 #
|
|
408 # The range of values that $_history_substring_search_match_index can take
|
|
409 # is: [0, $_history_substring_search_matches_count_plus]. A value of 0
|
|
410 # indicates that we are beyond the end of
|
|
411 # $_history_substring_search_matches. A value of
|
|
412 # $_history_substring_search_matches_count_plus indicates that we are beyond
|
|
413 # the beginning of $_history_substring_search_matches.
|
|
414 #
|
|
415 # In _history-substring-search-up-search() the initial value of
|
|
416 # $_history_substring_search_match_index is
|
|
417 # $_history_substring_search_matches_count_plus. This value is set in
|
|
418 # _history-substring-search-begin(). _history-substring-search-up-search()
|
|
419 # will initially decrease it to $_history_substring_search_matches_count.
|
|
420 #
|
|
421 if [[ $_history_substring_search_match_index -ge 2 ]]; then
|
|
422 #
|
|
423 # Highlight the next match:
|
|
424 #
|
|
425 # 1. Decrease the value of $_history_substring_search_match_index.
|
|
426 #
|
|
427 # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
428 # to highlight the current buffer.
|
|
429 #
|
|
430 (( _history_substring_search_match_index-- ))
|
|
431 BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
|
|
432 _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
433
|
|
434 elif [[ $_history_substring_search_match_index -eq 1 ]]; then
|
|
435 #
|
|
436 # We will move beyond the end of $_history_substring_search_matches:
|
|
437 #
|
|
438 # 1. Decrease the value of $_history_substring_search_match_index.
|
|
439 #
|
|
440 # 2. Save the current buffer in $_history_substring_search_old_buffer,
|
|
441 # so that it can be retrieved by
|
|
442 # _history-substring-search-down-search() later.
|
|
443 #
|
|
444 # 3. Make $BUFFER equal to $_history_substring_search_query.
|
|
445 #
|
|
446 # 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
|
|
447 # to highlight the current buffer.
|
|
448 #
|
|
449 (( _history_substring_search_match_index-- ))
|
|
450 _history-substring-search-not-found
|
|
451
|
|
452 elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count_plus ]]; then
|
|
453 #
|
|
454 # We were beyond the beginning of $_history_substring_search_matches but
|
|
455 # UP makes us move back to $_history_substring_search_matches:
|
|
456 #
|
|
457 # 1. Decrease the value of $_history_substring_search_match_index.
|
|
458 #
|
|
459 # 2. Restore $BUFFER from $_history_substring_search_old_buffer.
|
|
460 #
|
|
461 # 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
462 # to highlight the current buffer.
|
|
463 #
|
|
464 (( _history_substring_search_match_index-- ))
|
|
465 BUFFER=$_history_substring_search_old_buffer
|
|
466 _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
467
|
|
468 else
|
|
469 #
|
|
470 # We are at the beginning of history and there are no further matches.
|
|
471 #
|
|
472 _history-substring-search-not-found
|
|
473 fi
|
|
474 }
|
|
475
|
|
476 function _history-substring-search-down-search() {
|
|
477 _history_substring_search_refresh_display=1
|
|
478
|
|
479 #
|
|
480 # Highlight matches during history-substring-up-search:
|
|
481 #
|
|
482 # The following constants have been initialized in
|
|
483 # _history-substring-search-up/down-search():
|
|
484 #
|
|
485 # $_history_substring_search_matches is the current list of matches
|
|
486 # $_history_substring_search_matches_count is the current number of matches
|
|
487 # $_history_substring_search_matches_count_plus is the current number of matches + 1
|
|
488 # $_history_substring_search_matches_count_sans is the current number of matches - 1
|
|
489 # $_history_substring_search_match_index is the index of the current match
|
|
490 #
|
|
491 # The range of values that $_history_substring_search_match_index can take
|
|
492 # is: [0, $_history_substring_search_matches_count_plus]. A value of 0
|
|
493 # indicates that we are beyond the end of
|
|
494 # $_history_substring_search_matches. A value of
|
|
495 # $_history_substring_search_matches_count_plus indicates that we are beyond
|
|
496 # the beginning of $_history_substring_search_matches.
|
|
497 #
|
|
498 # In _history-substring-search-down-search() the initial value of
|
|
499 # $_history_substring_search_match_index is
|
|
500 # $_history_substring_search_matches_count. This value is set in
|
|
501 # _history-substring-search-begin().
|
|
502 # _history-substring-search-down-search() will initially increase it to
|
|
503 # $_history_substring_search_matches_count_plus.
|
|
504 #
|
|
505 if [[ $_history_substring_search_match_index -le $_history_substring_search_matches_count_sans ]]; then
|
|
506 #
|
|
507 # Highlight the next match:
|
|
508 #
|
|
509 # 1. Increase $_history_substring_search_match_index by 1.
|
|
510 #
|
|
511 # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
512 # to highlight the current buffer.
|
|
513 #
|
|
514 (( _history_substring_search_match_index++ ))
|
|
515 BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
|
|
516 _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
517
|
|
518 elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count ]]; then
|
|
519 #
|
|
520 # We will move beyond the beginning of $_history_substring_search_matches:
|
|
521 #
|
|
522 # 1. Increase $_history_substring_search_match_index by 1.
|
|
523 #
|
|
524 # 2. Save the current buffer in $_history_substring_search_old_buffer, so
|
|
525 # that it can be retrieved by _history-substring-search-up-search()
|
|
526 # later.
|
|
527 #
|
|
528 # 3. Make $BUFFER equal to $_history_substring_search_query.
|
|
529 #
|
|
530 # 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
|
|
531 # to highlight the current buffer.
|
|
532 #
|
|
533 (( _history_substring_search_match_index++ ))
|
|
534 _history-substring-search-not-found
|
|
535
|
|
536 elif [[ $_history_substring_search_match_index -eq 0 ]]; then
|
|
537 #
|
|
538 # We were beyond the end of $_history_substring_search_matches but DOWN
|
|
539 # makes us move back to the $_history_substring_search_matches:
|
|
540 #
|
|
541 # 1. Increase $_history_substring_search_match_index by 1.
|
|
542 #
|
|
543 # 2. Restore $BUFFER from $_history_substring_search_old_buffer.
|
|
544 #
|
|
545 # 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
546 # to highlight the current buffer.
|
|
547 #
|
|
548 (( _history_substring_search_match_index++ ))
|
|
549 BUFFER=$_history_substring_search_old_buffer
|
|
550 _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
551
|
|
552 else
|
|
553 #
|
|
554 # We are at the end of history and there are no further matches.
|
|
555 #
|
|
556 _history-substring-search-not-found
|
|
557 fi
|
|
558 }
|
|
559
|
|
560 # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
|
|
561 # vim: ft=zsh sw=2 ts=2 et
|