1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,7 +0,0 @@ |
1 |
-To activate this script, load it into an interactive ZSH session: |
|
2 |
- |
|
3 |
- % source history-substring-search.zsh |
|
4 |
- |
|
5 |
-See the "history-substring-search.zsh" file for more information: |
|
6 |
- |
|
7 |
- % sed -n '2,/^$/s/^#//p' history-substring-search.zsh | more |
8 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,12 +0,0 @@ |
1 |
-# This file integrates the history-substring-search script into oh-my-zsh. |
|
2 |
- |
|
3 |
-source "$ZSH/plugins/history-substring-search/history-substring-search.zsh" |
|
4 |
- |
|
5 |
-if test "$CASE_SENSITIVE" = true; then |
|
6 |
- unset HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS |
|
7 |
-fi |
|
8 |
- |
|
9 |
-if test "$DISABLE_COLOR" = true; then |
|
10 |
- unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
11 |
- unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
12 |
-fi |
13 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,642 +0,0 @@ |
1 |
-#!/usr/bin/env zsh |
|
2 |
-# |
|
3 |
-# This is a clean-room implementation of the Fish[1] shell's history search |
|
4 |
-# feature, where you can type in any part of any previously entered command |
|
5 |
-# and press the UP and DOWN arrow keys to cycle through the matching commands. |
|
6 |
-# |
|
7 |
-#----------------------------------------------------------------------------- |
|
8 |
-# Usage |
|
9 |
-#----------------------------------------------------------------------------- |
|
10 |
-# |
|
11 |
-# 1. Load this script into your interactive ZSH session: |
|
12 |
-# |
|
13 |
-# % source history-substring-search.zsh |
|
14 |
-# |
|
15 |
-# If you want to use the zsh-syntax-highlighting[6] script along with this |
|
16 |
-# script, then make sure that you load it *before* you load this script: |
|
17 |
-# |
|
18 |
-# % source zsh-syntax-highlighting.zsh |
|
19 |
-# % source history-substring-search.zsh |
|
20 |
-# |
|
21 |
-# 2. Type any part of any previous command and then: |
|
22 |
-# |
|
23 |
-# * Press the UP arrow key to select the nearest command that (1) contains |
|
24 |
-# your query and (2) is older than the current command in the command |
|
25 |
-# history. |
|
26 |
-# |
|
27 |
-# * Press the DOWN arrow key to select the nearest command that (1) |
|
28 |
-# contains your query and (2) is newer than the current command in the |
|
29 |
-# command history. |
|
30 |
-# |
|
31 |
-# * Press ^U (the Control and U keys simultaneously) to abort the search. |
|
32 |
-# |
|
33 |
-# 3. If a matching command spans more than one line of text, press the LEFT |
|
34 |
-# arrow key to move the cursor away from the end of the command, and then: |
|
35 |
-# |
|
36 |
-# * Press the UP arrow key to move the cursor to the line above. When the |
|
37 |
-# cursor reaches the first line of the command, pressing the UP arrow |
|
38 |
-# key again will cause this script to perform another search. |
|
39 |
-# |
|
40 |
-# * Press the DOWN arrow key to move the cursor to the line below. When |
|
41 |
-# the cursor reaches the last line of the command, pressing the DOWN |
|
42 |
-# arrow key again will cause this script to perform another search. |
|
43 |
-# |
|
44 |
-#----------------------------------------------------------------------------- |
|
45 |
-# Configuration |
|
46 |
-#----------------------------------------------------------------------------- |
|
47 |
-# |
|
48 |
-# This script defines the following global variables. You may override their |
|
49 |
-# default values only after having loaded this script into your ZSH session. |
|
50 |
-# |
|
51 |
-# * HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND is a global variable that defines |
|
52 |
-# how the query should be highlighted inside a matching command. Its default |
|
53 |
-# value causes this script to highlight using bold, white text on a magenta |
|
54 |
-# background. See the "Character Highlighting" section in the zshzle(1) man |
|
55 |
-# page to learn about the kinds of values you may assign to this variable. |
|
56 |
-# |
|
57 |
-# * HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND is a global variable that |
|
58 |
-# defines how the query should be highlighted when no commands in the |
|
59 |
-# history match it. Its default value causes this script to highlight using |
|
60 |
-# bold, white text on a red background. See the "Character Highlighting" |
|
61 |
-# section in the zshzle(1) man page to learn about the kinds of values you |
|
62 |
-# may assign to this variable. |
|
63 |
-# |
|
64 |
-# * HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS is a global variable that defines |
|
65 |
-# how the command history will be searched for your query. Its default value |
|
66 |
-# causes this script to perform a case-insensitive search. See the "Globbing |
|
67 |
-# Flags" section in the zshexpn(1) man page to learn about the kinds of |
|
68 |
-# values you may assign to this variable. |
|
69 |
-# |
|
70 |
-#----------------------------------------------------------------------------- |
|
71 |
-# History |
|
72 |
-#----------------------------------------------------------------------------- |
|
73 |
-# |
|
74 |
-# This script was originally written by Peter Stephenson[2], who published it |
|
75 |
-# to the ZSH users mailing list (thereby making it public domain) in September |
|
76 |
-# 2009. It was later revised by Guido van Steen and released under the BSD |
|
77 |
-# license (see below) as part of the fizsh[3] project in January 2011. |
|
78 |
-# |
|
79 |
-# It was later extracted from fizsh[3] release 1.0.1, refactored heavily, and |
|
80 |
-# repackaged as both an oh-my-zsh plugin[4] and as an independently loadable |
|
81 |
-# ZSH script[5] by Suraj N. Kurapati in 2011. |
|
82 |
-# |
|
83 |
-# It was further developed[4] by Guido van Steen, Suraj N. Kurapati, Sorin |
|
84 |
-# Ionescu, and Vincent Guerci in 2011. |
|
85 |
-# |
|
86 |
-# [1]: http://fishshell.com |
|
87 |
-# [2]: http://www.zsh.org/mla/users/2009/msg00818.html |
|
88 |
-# [3]: http://sourceforge.net/projects/fizsh/ |
|
89 |
-# [4]: https://github.com/robbyrussell/oh-my-zsh/pull/215 |
|
90 |
-# [5]: https://github.com/sunaku/zsh-history-substring-search |
|
91 |
-# [6]: https://github.com/nicoulaj/zsh-syntax-highlighting |
|
92 |
-# |
|
93 |
-############################################################################## |
|
94 |
-# |
|
95 |
-# Copyright (c) 2009 Peter Stephenson |
|
96 |
-# Copyright (c) 2011 Guido van Steen |
|
97 |
-# Copyright (c) 2011 Suraj N. Kurapati |
|
98 |
-# Copyright (c) 2011 Sorin Ionescu |
|
99 |
-# Copyright (c) 2011 Vincent Guerci |
|
100 |
-# All rights reserved. |
|
101 |
-# |
|
102 |
-# Redistribution and use in source and binary forms, with or without |
|
103 |
-# modification, are permitted provided that the following conditions are met: |
|
104 |
-# |
|
105 |
-# * Redistributions of source code must retain the above copyright |
|
106 |
-# notice, this list of conditions and the following disclaimer. |
|
107 |
-# |
|
108 |
-# * Redistributions in binary form must reproduce the above |
|
109 |
-# copyright notice, this list of conditions and the following |
|
110 |
-# disclaimer in the documentation and/or other materials provided |
|
111 |
-# with the distribution. |
|
112 |
-# |
|
113 |
-# * Neither the name of the FIZSH nor the names of its contributors |
|
114 |
-# may be used to endorse or promote products derived from this |
|
115 |
-# software without specific prior written permission. |
|
116 |
-# |
|
117 |
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
118 |
-# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
119 |
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
120 |
-# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
121 |
-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
122 |
-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
123 |
-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
124 |
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
125 |
-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
126 |
-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
127 |
-# POSSIBILITY OF SUCH DAMAGE. |
|
128 |
-# |
|
129 |
-############################################################################## |
|
130 |
- |
|
131 |
-#----------------------------------------------------------------------------- |
|
132 |
-# configuration variables |
|
133 |
-#----------------------------------------------------------------------------- |
|
134 |
- |
|
135 |
-HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold' |
|
136 |
-HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold' |
|
137 |
-HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i' |
|
138 |
- |
|
139 |
-#----------------------------------------------------------------------------- |
|
140 |
-# the main ZLE widgets |
|
141 |
-#----------------------------------------------------------------------------- |
|
142 |
- |
|
143 |
-function history-substring-search-up() { |
|
144 |
- _history-substring-search-begin |
|
145 |
- |
|
146 |
- _history-substring-search-up-history || |
|
147 |
- _history-substring-search-up-buffer || |
|
148 |
- _history-substring-search-up-search |
|
149 |
- |
|
150 |
- _history-substring-search-end |
|
151 |
-} |
|
152 |
- |
|
153 |
-function history-substring-search-down() { |
|
154 |
- _history-substring-search-begin |
|
155 |
- |
|
156 |
- _history-substring-search-down-history || |
|
157 |
- _history-substring-search-down-buffer || |
|
158 |
- _history-substring-search-down-search |
|
159 |
- |
|
160 |
- _history-substring-search-end |
|
161 |
-} |
|
162 |
- |
|
163 |
-zle -N history-substring-search-up |
|
164 |
-zle -N history-substring-search-down |
|
165 |
- |
|
166 |
-bindkey '\e[A' history-substring-search-up |
|
167 |
-bindkey '\e[B' history-substring-search-down |
|
168 |
- |
|
169 |
-#----------------------------------------------------------------------------- |
|
170 |
-# implementation details |
|
171 |
-#----------------------------------------------------------------------------- |
|
172 |
- |
|
173 |
-setopt extendedglob |
|
174 |
-zmodload -F zsh/parameter |
|
175 |
- |
|
176 |
-# |
|
177 |
-# We have to "override" some keys and widgets if the |
|
178 |
-# zsh-syntax-highlighting plugin has not been loaded: |
|
179 |
-# |
|
180 |
-# https://github.com/nicoulaj/zsh-syntax-highlighting |
|
181 |
-# |
|
182 |
-if [[ $+functions[_zsh_highlight] -eq 0 ]]; then |
|
183 |
- # |
|
184 |
- # Dummy implementation of _zsh_highlight() |
|
185 |
- # that simply removes existing highlights |
|
186 |
- # |
|
187 |
- function _zsh_highlight() { |
|
188 |
- region_highlight=() |
|
189 |
- } |
|
190 |
- |
|
191 |
- # |
|
192 |
- # Remove existing highlights when the user |
|
193 |
- # inserts printable characters into $BUFFER |
|
194 |
- # |
|
195 |
- function ordinary-key-press() { |
|
196 |
- if [[ $KEYS == [[:print:]] ]]; then |
|
197 |
- region_highlight=() |
|
198 |
- fi |
|
199 |
- zle .self-insert |
|
200 |
- } |
|
201 |
- zle -N self-insert ordinary-key-press |
|
202 |
- |
|
203 |
- # |
|
204 |
- # Override ZLE widgets to invoke _zsh_highlight() |
|
205 |
- # |
|
206 |
- # https://github.com/nicoulaj/zsh-syntax-highlighting/blob/ |
|
207 |
- # bb7fcb79fad797a40077bebaf6f4e4a93c9d8163/zsh-syntax-highlighting.zsh#L121 |
|
208 |
- # |
|
209 |
- #--------------8<-------------------8<-------------------8<----------------- |
|
210 |
- # |
|
211 |
- # Copyright (c) 2010-2011 zsh-syntax-highlighting contributors |
|
212 |
- # All rights reserved. |
|
213 |
- # |
|
214 |
- # Redistribution and use in source and binary forms, with or without |
|
215 |
- # modification, are permitted provided that the following conditions are |
|
216 |
- # met: |
|
217 |
- # |
|
218 |
- # * Redistributions of source code must retain the above copyright |
|
219 |
- # notice, this list of conditions and the following disclaimer. |
|
220 |
- # |
|
221 |
- # * Redistributions in binary form must reproduce the above copyright |
|
222 |
- # notice, this list of conditions and the following disclaimer in the |
|
223 |
- # documentation and/or other materials provided with the distribution. |
|
224 |
- # |
|
225 |
- # * Neither the name of the zsh-syntax-highlighting contributors nor the |
|
226 |
- # names of its contributors may be used to endorse or promote products |
|
227 |
- # derived from this software without specific prior written permission. |
|
228 |
- # |
|
229 |
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
230 |
- # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
231 |
- # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
232 |
- # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
|
233 |
- # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
234 |
- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
235 |
- # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
236 |
- # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
237 |
- # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
238 |
- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
239 |
- # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
240 |
- |
|
241 |
- # Load ZSH module zsh/zleparameter, needed to override user defined widgets. |
|
242 |
- zmodload zsh/zleparameter 2>/dev/null || { |
|
243 |
- echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter, exiting.' >&2 |
|
244 |
- return -1 |
|
245 |
- } |
|
246 |
- |
|
247 |
- # Override ZLE widgets to make them invoke _zsh_highlight. |
|
248 |
- for event in ${${(f)"$(zle -la)"}:#(_*|orig-*|.run-help|.which-command)}; do |
|
249 |
- if [[ "$widgets[$event]" == completion:* ]]; then |
|
250 |
- eval "zle -C orig-$event ${${${widgets[$event]}#*:}/:/ } ; $event() { builtin zle orig-$event && _zsh_highlight } ; zle -N $event" |
|
251 |
- else |
|
252 |
- case $event in |
|
253 |
- accept-and-menu-complete) |
|
254 |
- eval "$event() { builtin zle .$event && _zsh_highlight } ; zle -N $event" |
|
255 |
- ;; |
|
256 |
- |
|
257 |
- # The following widgets should NOT remove any previously |
|
258 |
- # applied highlighting. Therefore we do not remap them. |
|
259 |
- .forward-char|.backward-char|.up-line-or-history|.down-line-or-history) |
|
260 |
- ;; |
|
261 |
- |
|
262 |
- .*) |
|
263 |
- clean_event=$event[2,${#event}] # Remove the leading dot in the event name |
|
264 |
- case ${widgets[$clean_event]-} in |
|
265 |
- (completion|user):*) |
|
266 |
- ;; |
|
267 |
- *) |
|
268 |
- eval "$clean_event() { builtin zle $event && _zsh_highlight } ; zle -N $clean_event" |
|
269 |
- ;; |
|
270 |
- esac |
|
271 |
- ;; |
|
272 |
- *) |
|
273 |
- ;; |
|
274 |
- esac |
|
275 |
- fi |
|
276 |
- done |
|
277 |
- unset event clean_event |
|
278 |
- #-------------->8------------------->8------------------->8----------------- |
|
279 |
-fi |
|
280 |
- |
|
281 |
-function _history-substring-search-begin() { |
|
282 |
- _history_substring_search_move_cursor_eol=false |
|
283 |
- _history_substring_search_query_highlight= |
|
284 |
- |
|
285 |
- # |
|
286 |
- # Continue using the previous $_history_substring_search_result by default, |
|
287 |
- # unless the current query was cleared or a new/different query was entered. |
|
288 |
- # |
|
289 |
- if [[ -z $BUFFER || $BUFFER != $_history_substring_search_result ]]; then |
|
290 |
- # |
|
291 |
- # For the purpose of highlighting we will also keep |
|
292 |
- # a version without doubly-escaped meta characters. |
|
293 |
- # |
|
294 |
- _history_substring_search_query=$BUFFER |
|
295 |
- |
|
296 |
- # |
|
297 |
- # $BUFFER contains the text that is in the command-line currently. |
|
298 |
- # we put an extra "\\" before meta characters such as "\(" and "\)", |
|
299 |
- # so that they become "\\\(" and "\\\)". |
|
300 |
- # |
|
301 |
- _history_substring_search_query_escaped=${BUFFER//(#m)[\][()|\\*?#<>~^]/\\$MATCH} |
|
302 |
- |
|
303 |
- # |
|
304 |
- # Find all occurrences of the search query in the history file. |
|
305 |
- # |
|
306 |
- # (k) turns it an array of line numbers. |
|
307 |
- # |
|
308 |
- # (on) seems to remove duplicates, which are default |
|
309 |
- # options. They can be turned off by (ON). |
|
310 |
- # |
|
311 |
- _history_substring_search_matches=(${(kon)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]}) |
|
312 |
- |
|
313 |
- # |
|
314 |
- # Define the range of values that $_history_substring_search_match_index |
|
315 |
- # can take: [0, $_history_substring_search_matches_count_plus]. |
|
316 |
- # |
|
317 |
- _history_substring_search_matches_count=$#_history_substring_search_matches |
|
318 |
- _history_substring_search_matches_count_plus=$(( _history_substring_search_matches_count + 1 )) |
|
319 |
- _history_substring_search_matches_count_sans=$(( _history_substring_search_matches_count - 1 )) |
|
320 |
- |
|
321 |
- # |
|
322 |
- # If $_history_substring_search_match_index is equal to |
|
323 |
- # $_history_substring_search_matches_count_plus, this indicates that we |
|
324 |
- # are beyond the beginning of $_history_substring_search_matches. |
|
325 |
- # |
|
326 |
- # If $_history_substring_search_match_index is equal to 0, this indicates |
|
327 |
- # that we are beyond the end of $_history_substring_search_matches. |
|
328 |
- # |
|
329 |
- # If we have initially pressed "up" we have to initialize |
|
330 |
- # $_history_substring_search_match_index to |
|
331 |
- # $_history_substring_search_matches_count_plus so that it will be |
|
332 |
- # decreased to $_history_substring_search_matches_count. |
|
333 |
- # |
|
334 |
- # If we have initially pressed "down" we have to initialize |
|
335 |
- # $_history_substring_search_match_index to |
|
336 |
- # $_history_substring_search_matches_count so that it will be increased to |
|
337 |
- # $_history_substring_search_matches_count_plus. |
|
338 |
- # |
|
339 |
- if [[ $WIDGET == history-substring-search-down ]]; then |
|
340 |
- _history_substring_search_match_index=$_history_substring_search_matches_count |
|
341 |
- else |
|
342 |
- _history_substring_search_match_index=$_history_substring_search_matches_count_plus |
|
343 |
- fi |
|
344 |
- fi |
|
345 |
-} |
|
346 |
- |
|
347 |
-function _history-substring-search-end() { |
|
348 |
- _history_substring_search_result=$BUFFER |
|
349 |
- |
|
350 |
- # move the cursor to the end of the command line |
|
351 |
- if [[ $_history_substring_search_move_cursor_eol == true ]]; then |
|
352 |
- CURSOR=${#BUFFER} |
|
353 |
- fi |
|
354 |
- |
|
355 |
- # highlight command line using zsh-syntax-highlighting |
|
356 |
- _zsh_highlight |
|
357 |
- |
|
358 |
- # highlight the search query inside the command line |
|
359 |
- if [[ -n $_history_substring_search_query_highlight && -n $_history_substring_search_query ]]; then |
|
360 |
- # |
|
361 |
- # The following expression yields a variable $MBEGIN, which |
|
362 |
- # indicates the begin position + 1 of the first occurrence |
|
363 |
- # of _history_substring_search_query_escaped in $BUFFER. |
|
364 |
- # |
|
365 |
- : ${(S)BUFFER##(#m$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)($_history_substring_search_query##)} |
|
366 |
- local begin=$(( MBEGIN - 1 )) |
|
367 |
- local end=$(( begin + $#_history_substring_search_query )) |
|
368 |
- region_highlight+=("$begin $end $_history_substring_search_query_highlight") |
|
369 |
- fi |
|
370 |
- |
|
371 |
- # For debugging purposes: |
|
372 |
- # zle -R "mn: "$_history_substring_search_match_index" m#: "${#_history_substring_search_matches} |
|
373 |
- # read -k -t 200 && zle -U $REPLY |
|
374 |
- |
|
375 |
- # Exit successfully from the history-substring-search-* widgets. |
|
376 |
- true |
|
377 |
-} |
|
378 |
- |
|
379 |
-function _history-substring-search-up-buffer() { |
|
380 |
- # |
|
381 |
- # Check if the UP arrow was pressed to move the cursor within a multi-line |
|
382 |
- # buffer. This amounts to three tests: |
|
383 |
- # |
|
384 |
- # 1. $#buflines -gt 1. |
|
385 |
- # |
|
386 |
- # 2. $CURSOR -ne $#BUFFER. |
|
387 |
- # |
|
388 |
- # 3. Check if we are on the first line of the current multi-line buffer. |
|
389 |
- # If so, pressing UP would amount to leaving the multi-line buffer. |
|
390 |
- # |
|
391 |
- # We check this by adding an extra "x" to $LBUFFER, which makes |
|
392 |
- # sure that xlbuflines is always equal to the number of lines |
|
393 |
- # until $CURSOR (including the line with the cursor on it). |
|
394 |
- # |
|
395 |
- local buflines XLBUFFER xlbuflines |
|
396 |
- buflines=(${(f)BUFFER}) |
|
397 |
- XLBUFFER=$LBUFFER"x" |
|
398 |
- xlbuflines=(${(f)XLBUFFER}) |
|
399 |
- |
|
400 |
- if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xlbuflines -ne 1 ]]; then |
|
401 |
- zle up-line-or-history |
|
402 |
- return true |
|
403 |
- fi |
|
404 |
- |
|
405 |
- false |
|
406 |
-} |
|
407 |
- |
|
408 |
-function _history-substring-search-down-buffer() { |
|
409 |
- # |
|
410 |
- # Check if the DOWN arrow was pressed to move the cursor within a multi-line |
|
411 |
- # buffer. This amounts to three tests: |
|
412 |
- # |
|
413 |
- # 1. $#buflines -gt 1. |
|
414 |
- # |
|
415 |
- # 2. $CURSOR -ne $#BUFFER. |
|
416 |
- # |
|
417 |
- # 3. Check if we are on the last line of the current multi-line buffer. |
|
418 |
- # If so, pressing DOWN would amount to leaving the multi-line buffer. |
|
419 |
- # |
|
420 |
- # We check this by adding an extra "x" to $RBUFFER, which makes |
|
421 |
- # sure that xrbuflines is always equal to the number of lines |
|
422 |
- # from $CURSOR (including the line with the cursor on it). |
|
423 |
- # |
|
424 |
- local buflines XRBUFFER xrbuflines |
|
425 |
- buflines=(${(f)BUFFER}) |
|
426 |
- XRBUFFER="x"$RBUFFER |
|
427 |
- xrbuflines=(${(f)XRBUFFER}) |
|
428 |
- |
|
429 |
- if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xrbuflines -ne 1 ]]; then |
|
430 |
- zle down-line-or-history |
|
431 |
- return true |
|
432 |
- fi |
|
433 |
- |
|
434 |
- false |
|
435 |
-} |
|
436 |
- |
|
437 |
-function _history-substring-search-up-history() { |
|
438 |
- # |
|
439 |
- # Behave like up in ZSH, except clear the $BUFFER |
|
440 |
- # when beginning of history is reached like in Fish. |
|
441 |
- # |
|
442 |
- if [[ -z $_history_substring_search_query ]]; then |
|
443 |
- |
|
444 |
- # we have reached the absolute top of history |
|
445 |
- if [[ $HISTNO -eq 1 ]]; then |
|
446 |
- BUFFER= |
|
447 |
- |
|
448 |
- # going up from somewhere below the top of history |
|
449 |
- else |
|
450 |
- zle up-history |
|
451 |
- fi |
|
452 |
- |
|
453 |
- return true |
|
454 |
- fi |
|
455 |
- |
|
456 |
- false |
|
457 |
-} |
|
458 |
- |
|
459 |
-function _history-substring-search-down-history() { |
|
460 |
- # |
|
461 |
- # Behave like down-history in ZSH, except clear the |
|
462 |
- # $BUFFER when end of history is reached like in Fish. |
|
463 |
- # |
|
464 |
- if [[ -z $_history_substring_search_query ]]; then |
|
465 |
- |
|
466 |
- # going down from the absolute top of history |
|
467 |
- if [[ $HISTNO -eq 1 && -z $BUFFER ]]; then |
|
468 |
- BUFFER=${history[1]} |
|
469 |
- _history_substring_search_move_cursor_eol=true |
|
470 |
- |
|
471 |
- # going down from somewhere above the bottom of history |
|
472 |
- else |
|
473 |
- zle down-history |
|
474 |
- fi |
|
475 |
- |
|
476 |
- return true |
|
477 |
- fi |
|
478 |
- |
|
479 |
- false |
|
480 |
-} |
|
481 |
- |
|
482 |
-function _history-substring-search-up-search() { |
|
483 |
- _history_substring_search_move_cursor_eol=true |
|
484 |
- |
|
485 |
- # |
|
486 |
- # Highlight matches during history-substring-up-search: |
|
487 |
- # |
|
488 |
- # The following constants have been initialized in |
|
489 |
- # _history-substring-search-up/down-search(): |
|
490 |
- # |
|
491 |
- # $_history_substring_search_matches is the current list of matches |
|
492 |
- # $_history_substring_search_matches_count is the current number of matches |
|
493 |
- # $_history_substring_search_matches_count_plus is the current number of matches + 1 |
|
494 |
- # $_history_substring_search_matches_count_sans is the current number of matches - 1 |
|
495 |
- # $_history_substring_search_match_index is the index of the current match |
|
496 |
- # |
|
497 |
- # The range of values that $_history_substring_search_match_index can take |
|
498 |
- # is: [0, $_history_substring_search_matches_count_plus]. A value of 0 |
|
499 |
- # indicates that we are beyond the end of |
|
500 |
- # $_history_substring_search_matches. A value of |
|
501 |
- # $_history_substring_search_matches_count_plus indicates that we are beyond |
|
502 |
- # the beginning of $_history_substring_search_matches. |
|
503 |
- # |
|
504 |
- # In _history-substring-search-up-search() the initial value of |
|
505 |
- # $_history_substring_search_match_index is |
|
506 |
- # $_history_substring_search_matches_count_plus. This value is set in |
|
507 |
- # _history-substring-search-begin(). _history-substring-search-up-search() |
|
508 |
- # will initially decrease it to $_history_substring_search_matches_count. |
|
509 |
- # |
|
510 |
- if [[ $_history_substring_search_match_index -ge 2 ]]; then |
|
511 |
- # |
|
512 |
- # Highlight the next match: |
|
513 |
- # |
|
514 |
- # 1. Decrease the value of $_history_substring_search_match_index. |
|
515 |
- # |
|
516 |
- # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
517 |
- # to highlight the current buffer. |
|
518 |
- # |
|
519 |
- (( _history_substring_search_match_index-- )) |
|
520 |
- BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]] |
|
521 |
- _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
522 |
- |
|
523 |
- elif [[ $_history_substring_search_match_index -eq 1 ]]; then |
|
524 |
- # |
|
525 |
- # We will move beyond the end of $_history_substring_search_matches: |
|
526 |
- # |
|
527 |
- # 1. Decrease the value of $_history_substring_search_match_index. |
|
528 |
- # |
|
529 |
- # 2. Save the current buffer in $_history_substring_search_old_buffer, |
|
530 |
- # so that it can be retrieved by |
|
531 |
- # _history-substring-search-down-search() later. |
|
532 |
- # |
|
533 |
- # 3. Make $BUFFER equal to $_history_substring_search_query. |
|
534 |
- # |
|
535 |
- # 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
536 |
- # to highlight the current buffer. |
|
537 |
- # |
|
538 |
- (( _history_substring_search_match_index-- )) |
|
539 |
- _history_substring_search_old_buffer=$BUFFER |
|
540 |
- BUFFER=$_history_substring_search_query |
|
541 |
- _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
542 |
- |
|
543 |
- elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count_plus ]]; then |
|
544 |
- # |
|
545 |
- # We were beyond the beginning of $_history_substring_search_matches but |
|
546 |
- # UP makes us move back to $_history_substring_search_matches: |
|
547 |
- # |
|
548 |
- # 1. Decrease the value of $_history_substring_search_match_index. |
|
549 |
- # |
|
550 |
- # 2. Restore $BUFFER from $_history_substring_search_old_buffer. |
|
551 |
- # |
|
552 |
- # 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
553 |
- # to highlight the current buffer. |
|
554 |
- # |
|
555 |
- (( _history_substring_search_match_index-- )) |
|
556 |
- BUFFER=$_history_substring_search_old_buffer |
|
557 |
- _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
558 |
- fi |
|
559 |
-} |
|
560 |
- |
|
561 |
-function _history-substring-search-down-search() { |
|
562 |
- _history_substring_search_move_cursor_eol=true |
|
563 |
- |
|
564 |
- # |
|
565 |
- # Highlight matches during history-substring-up-search: |
|
566 |
- # |
|
567 |
- # The following constants have been initialized in |
|
568 |
- # _history-substring-search-up/down-search(): |
|
569 |
- # |
|
570 |
- # $_history_substring_search_matches is the current list of matches |
|
571 |
- # $_history_substring_search_matches_count is the current number of matches |
|
572 |
- # $_history_substring_search_matches_count_plus is the current number of matches + 1 |
|
573 |
- # $_history_substring_search_matches_count_sans is the current number of matches - 1 |
|
574 |
- # $_history_substring_search_match_index is the index of the current match |
|
575 |
- # |
|
576 |
- # The range of values that $_history_substring_search_match_index can take |
|
577 |
- # is: [0, $_history_substring_search_matches_count_plus]. A value of 0 |
|
578 |
- # indicates that we are beyond the end of |
|
579 |
- # $_history_substring_search_matches. A value of |
|
580 |
- # $_history_substring_search_matches_count_plus indicates that we are beyond |
|
581 |
- # the beginning of $_history_substring_search_matches. |
|
582 |
- # |
|
583 |
- # In _history-substring-search-down-search() the initial value of |
|
584 |
- # $_history_substring_search_match_index is |
|
585 |
- # $_history_substring_search_matches_count. This value is set in |
|
586 |
- # _history-substring-search-begin(). |
|
587 |
- # _history-substring-search-down-search() will initially increase it to |
|
588 |
- # $_history_substring_search_matches_count_plus. |
|
589 |
- # |
|
590 |
- if [[ $_history_substring_search_match_index -le $_history_substring_search_matches_count_sans ]]; then |
|
591 |
- # |
|
592 |
- # Highlight the next match: |
|
593 |
- # |
|
594 |
- # 1. Increase $_history_substring_search_match_index by 1. |
|
595 |
- # |
|
596 |
- # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
597 |
- # to highlight the current buffer. |
|
598 |
- # |
|
599 |
- (( _history_substring_search_match_index++ )) |
|
600 |
- BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]] |
|
601 |
- _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
602 |
- |
|
603 |
- elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count ]]; then |
|
604 |
- # |
|
605 |
- # We will move beyond the beginning of $_history_substring_search_matches: |
|
606 |
- # |
|
607 |
- # 1. Increase $_history_substring_search_match_index by 1. |
|
608 |
- # |
|
609 |
- # 2. Save the current buffer in $_history_substring_search_old_buffer, so |
|
610 |
- # that it can be retrieved by _history-substring-search-up-search() |
|
611 |
- # later. |
|
612 |
- # |
|
613 |
- # 3. Make $BUFFER equal to $_history_substring_search_query. |
|
614 |
- # |
|
615 |
- # 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
616 |
- # to highlight the current buffer. |
|
617 |
- # |
|
618 |
- (( _history_substring_search_match_index++ )) |
|
619 |
- _history_substring_search_old_buffer=$BUFFER |
|
620 |
- BUFFER=$_history_substring_search_query |
|
621 |
- _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
622 |
- |
|
623 |
- elif [[ $_history_substring_search_match_index -eq 0 ]]; then |
|
624 |
- # |
|
625 |
- # We were beyond the end of $_history_substring_search_matches but DOWN |
|
626 |
- # makes us move back to the $_history_substring_search_matches: |
|
627 |
- # |
|
628 |
- # 1. Increase $_history_substring_search_match_index by 1. |
|
629 |
- # |
|
630 |
- # 2. Restore $BUFFER from $_history_substring_search_old_buffer. |
|
631 |
- # |
|
632 |
- # 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
633 |
- # to highlight the current buffer. |
|
634 |
- # |
|
635 |
- (( _history_substring_search_match_index++ )) |
|
636 |
- BUFFER=$_history_substring_search_old_buffer |
|
637 |
- _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
638 |
- fi |
|
639 |
-} |
|
640 |
- |
|
641 |
-# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- |
|
642 |
-# vim: ft=zsh sw=2 ts=2 et |
643 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,7 @@ |
0 |
+To activate this script, load it into an interactive ZSH session: |
|
1 |
+ |
|
2 |
+ % source history-substring-search.zsh |
|
3 |
+ |
|
4 |
+See the "history-substring-search.zsh" file for more information: |
|
5 |
+ |
|
6 |
+ % sed -n '2,/^$/s/^#//p' history-substring-search.zsh | more |
0 | 7 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,12 @@ |
0 |
+# This file integrates the history-substring-search script into oh-my-zsh. |
|
1 |
+ |
|
2 |
+source "$ZSH/plugins/history-substring-search/history-substring-search.zsh" |
|
3 |
+ |
|
4 |
+if test "$CASE_SENSITIVE" = true; then |
|
5 |
+ unset HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS |
|
6 |
+fi |
|
7 |
+ |
|
8 |
+if test "$DISABLE_COLOR" = true; then |
|
9 |
+ unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
10 |
+ unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
11 |
+fi |
0 | 12 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,642 @@ |
0 |
+#!/usr/bin/env zsh |
|
1 |
+# |
|
2 |
+# This is a clean-room implementation of the Fish[1] shell's history search |
|
3 |
+# feature, where you can type in any part of any previously entered command |
|
4 |
+# and press the UP and DOWN arrow keys to cycle through the matching commands. |
|
5 |
+# |
|
6 |
+#----------------------------------------------------------------------------- |
|
7 |
+# Usage |
|
8 |
+#----------------------------------------------------------------------------- |
|
9 |
+# |
|
10 |
+# 1. Load this script into your interactive ZSH session: |
|
11 |
+# |
|
12 |
+# % source history-substring-search.zsh |
|
13 |
+# |
|
14 |
+# If you want to use the zsh-syntax-highlighting[6] script along with this |
|
15 |
+# script, then make sure that you load it *before* you load this script: |
|
16 |
+# |
|
17 |
+# % source zsh-syntax-highlighting.zsh |
|
18 |
+# % source history-substring-search.zsh |
|
19 |
+# |
|
20 |
+# 2. Type any part of any previous command and then: |
|
21 |
+# |
|
22 |
+# * Press the UP arrow key to select the nearest command that (1) contains |
|
23 |
+# your query and (2) is older than the current command in the command |
|
24 |
+# history. |
|
25 |
+# |
|
26 |
+# * Press the DOWN arrow key to select the nearest command that (1) |
|
27 |
+# contains your query and (2) is newer than the current command in the |
|
28 |
+# command history. |
|
29 |
+# |
|
30 |
+# * Press ^U (the Control and U keys simultaneously) to abort the search. |
|
31 |
+# |
|
32 |
+# 3. If a matching command spans more than one line of text, press the LEFT |
|
33 |
+# arrow key to move the cursor away from the end of the command, and then: |
|
34 |
+# |
|
35 |
+# * Press the UP arrow key to move the cursor to the line above. When the |
|
36 |
+# cursor reaches the first line of the command, pressing the UP arrow |
|
37 |
+# key again will cause this script to perform another search. |
|
38 |
+# |
|
39 |
+# * Press the DOWN arrow key to move the cursor to the line below. When |
|
40 |
+# the cursor reaches the last line of the command, pressing the DOWN |
|
41 |
+# arrow key again will cause this script to perform another search. |
|
42 |
+# |
|
43 |
+#----------------------------------------------------------------------------- |
|
44 |
+# Configuration |
|
45 |
+#----------------------------------------------------------------------------- |
|
46 |
+# |
|
47 |
+# This script defines the following global variables. You may override their |
|
48 |
+# default values only after having loaded this script into your ZSH session. |
|
49 |
+# |
|
50 |
+# * HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND is a global variable that defines |
|
51 |
+# how the query should be highlighted inside a matching command. Its default |
|
52 |
+# value causes this script to highlight using bold, white text on a magenta |
|
53 |
+# background. See the "Character Highlighting" section in the zshzle(1) man |
|
54 |
+# page to learn about the kinds of values you may assign to this variable. |
|
55 |
+# |
|
56 |
+# * HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND is a global variable that |
|
57 |
+# defines how the query should be highlighted when no commands in the |
|
58 |
+# history match it. Its default value causes this script to highlight using |
|
59 |
+# bold, white text on a red background. See the "Character Highlighting" |
|
60 |
+# section in the zshzle(1) man page to learn about the kinds of values you |
|
61 |
+# may assign to this variable. |
|
62 |
+# |
|
63 |
+# * HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS is a global variable that defines |
|
64 |
+# how the command history will be searched for your query. Its default value |
|
65 |
+# causes this script to perform a case-insensitive search. See the "Globbing |
|
66 |
+# Flags" section in the zshexpn(1) man page to learn about the kinds of |
|
67 |
+# values you may assign to this variable. |
|
68 |
+# |
|
69 |
+#----------------------------------------------------------------------------- |
|
70 |
+# History |
|
71 |
+#----------------------------------------------------------------------------- |
|
72 |
+# |
|
73 |
+# This script was originally written by Peter Stephenson[2], who published it |
|
74 |
+# to the ZSH users mailing list (thereby making it public domain) in September |
|
75 |
+# 2009. It was later revised by Guido van Steen and released under the BSD |
|
76 |
+# license (see below) as part of the fizsh[3] project in January 2011. |
|
77 |
+# |
|
78 |
+# It was later extracted from fizsh[3] release 1.0.1, refactored heavily, and |
|
79 |
+# repackaged as both an oh-my-zsh plugin[4] and as an independently loadable |
|
80 |
+# ZSH script[5] by Suraj N. Kurapati in 2011. |
|
81 |
+# |
|
82 |
+# It was further developed[4] by Guido van Steen, Suraj N. Kurapati, Sorin |
|
83 |
+# Ionescu, and Vincent Guerci in 2011. |
|
84 |
+# |
|
85 |
+# [1]: http://fishshell.com |
|
86 |
+# [2]: http://www.zsh.org/mla/users/2009/msg00818.html |
|
87 |
+# [3]: http://sourceforge.net/projects/fizsh/ |
|
88 |
+# [4]: https://github.com/robbyrussell/oh-my-zsh/pull/215 |
|
89 |
+# [5]: https://github.com/sunaku/zsh-history-substring-search |
|
90 |
+# [6]: https://github.com/nicoulaj/zsh-syntax-highlighting |
|
91 |
+# |
|
92 |
+############################################################################## |
|
93 |
+# |
|
94 |
+# Copyright (c) 2009 Peter Stephenson |
|
95 |
+# Copyright (c) 2011 Guido van Steen |
|
96 |
+# Copyright (c) 2011 Suraj N. Kurapati |
|
97 |
+# Copyright (c) 2011 Sorin Ionescu |
|
98 |
+# Copyright (c) 2011 Vincent Guerci |
|
99 |
+# All rights reserved. |
|
100 |
+# |
|
101 |
+# Redistribution and use in source and binary forms, with or without |
|
102 |
+# modification, are permitted provided that the following conditions are met: |
|
103 |
+# |
|
104 |
+# * Redistributions of source code must retain the above copyright |
|
105 |
+# notice, this list of conditions and the following disclaimer. |
|
106 |
+# |
|
107 |
+# * Redistributions in binary form must reproduce the above |
|
108 |
+# copyright notice, this list of conditions and the following |
|
109 |
+# disclaimer in the documentation and/or other materials provided |
|
110 |
+# with the distribution. |
|
111 |
+# |
|
112 |
+# * Neither the name of the FIZSH nor the names of its contributors |
|
113 |
+# may be used to endorse or promote products derived from this |
|
114 |
+# software without specific prior written permission. |
|
115 |
+# |
|
116 |
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
117 |
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
118 |
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
119 |
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
120 |
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
121 |
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
122 |
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
123 |
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
124 |
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
125 |
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
126 |
+# POSSIBILITY OF SUCH DAMAGE. |
|
127 |
+# |
|
128 |
+############################################################################## |
|
129 |
+ |
|
130 |
+#----------------------------------------------------------------------------- |
|
131 |
+# configuration variables |
|
132 |
+#----------------------------------------------------------------------------- |
|
133 |
+ |
|
134 |
+HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold' |
|
135 |
+HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold' |
|
136 |
+HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i' |
|
137 |
+ |
|
138 |
+#----------------------------------------------------------------------------- |
|
139 |
+# the main ZLE widgets |
|
140 |
+#----------------------------------------------------------------------------- |
|
141 |
+ |
|
142 |
+function history-substring-search-up() { |
|
143 |
+ _history-substring-search-begin |
|
144 |
+ |
|
145 |
+ _history-substring-search-up-history || |
|
146 |
+ _history-substring-search-up-buffer || |
|
147 |
+ _history-substring-search-up-search |
|
148 |
+ |
|
149 |
+ _history-substring-search-end |
|
150 |
+} |
|
151 |
+ |
|
152 |
+function history-substring-search-down() { |
|
153 |
+ _history-substring-search-begin |
|
154 |
+ |
|
155 |
+ _history-substring-search-down-history || |
|
156 |
+ _history-substring-search-down-buffer || |
|
157 |
+ _history-substring-search-down-search |
|
158 |
+ |
|
159 |
+ _history-substring-search-end |
|
160 |
+} |
|
161 |
+ |
|
162 |
+zle -N history-substring-search-up |
|
163 |
+zle -N history-substring-search-down |
|
164 |
+ |
|
165 |
+bindkey '\e[A' history-substring-search-up |
|
166 |
+bindkey '\e[B' history-substring-search-down |
|
167 |
+ |
|
168 |
+#----------------------------------------------------------------------------- |
|
169 |
+# implementation details |
|
170 |
+#----------------------------------------------------------------------------- |
|
171 |
+ |
|
172 |
+setopt extendedglob |
|
173 |
+zmodload -F zsh/parameter |
|
174 |
+ |
|
175 |
+# |
|
176 |
+# We have to "override" some keys and widgets if the |
|
177 |
+# zsh-syntax-highlighting plugin has not been loaded: |
|
178 |
+# |
|
179 |
+# https://github.com/nicoulaj/zsh-syntax-highlighting |
|
180 |
+# |
|
181 |
+if [[ $+functions[_zsh_highlight] -eq 0 ]]; then |
|
182 |
+ # |
|
183 |
+ # Dummy implementation of _zsh_highlight() |
|
184 |
+ # that simply removes existing highlights |
|
185 |
+ # |
|
186 |
+ function _zsh_highlight() { |
|
187 |
+ region_highlight=() |
|
188 |
+ } |
|
189 |
+ |
|
190 |
+ # |
|
191 |
+ # Remove existing highlights when the user |
|
192 |
+ # inserts printable characters into $BUFFER |
|
193 |
+ # |
|
194 |
+ function ordinary-key-press() { |
|
195 |
+ if [[ $KEYS == [[:print:]] ]]; then |
|
196 |
+ region_highlight=() |
|
197 |
+ fi |
|
198 |
+ zle .self-insert |
|
199 |
+ } |
|
200 |
+ zle -N self-insert ordinary-key-press |
|
201 |
+ |
|
202 |
+ # |
|
203 |
+ # Override ZLE widgets to invoke _zsh_highlight() |
|
204 |
+ # |
|
205 |
+ # https://github.com/nicoulaj/zsh-syntax-highlighting/blob/ |
|
206 |
+ # bb7fcb79fad797a40077bebaf6f4e4a93c9d8163/zsh-syntax-highlighting.zsh#L121 |
|
207 |
+ # |
|
208 |
+ #--------------8<-------------------8<-------------------8<----------------- |
|
209 |
+ # |
|
210 |
+ # Copyright (c) 2010-2011 zsh-syntax-highlighting contributors |
|
211 |
+ # All rights reserved. |
|
212 |
+ # |
|
213 |
+ # Redistribution and use in source and binary forms, with or without |
|
214 |
+ # modification, are permitted provided that the following conditions are |
|
215 |
+ # met: |
|
216 |
+ # |
|
217 |
+ # * Redistributions of source code must retain the above copyright |
|
218 |
+ # notice, this list of conditions and the following disclaimer. |
|
219 |
+ # |
|
220 |
+ # * Redistributions in binary form must reproduce the above copyright |
|
221 |
+ # notice, this list of conditions and the following disclaimer in the |
|
222 |
+ # documentation and/or other materials provided with the distribution. |
|
223 |
+ # |
|
224 |
+ # * Neither the name of the zsh-syntax-highlighting contributors nor the |
|
225 |
+ # names of its contributors may be used to endorse or promote products |
|
226 |
+ # derived from this software without specific prior written permission. |
|
227 |
+ # |
|
228 |
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
229 |
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
230 |
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
231 |
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
|
232 |
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
233 |
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
234 |
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
235 |
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
236 |
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
237 |
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
238 |
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
239 |
+ |
|
240 |
+ # Load ZSH module zsh/zleparameter, needed to override user defined widgets. |
|
241 |
+ zmodload zsh/zleparameter 2>/dev/null || { |
|
242 |
+ echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter, exiting.' >&2 |
|
243 |
+ return -1 |
|
244 |
+ } |
|
245 |
+ |
|
246 |
+ # Override ZLE widgets to make them invoke _zsh_highlight. |
|
247 |
+ for event in ${${(f)"$(zle -la)"}:#(_*|orig-*|.run-help|.which-command)}; do |
|
248 |
+ if [[ "$widgets[$event]" == completion:* ]]; then |
|
249 |
+ eval "zle -C orig-$event ${${${widgets[$event]}#*:}/:/ } ; $event() { builtin zle orig-$event && _zsh_highlight } ; zle -N $event" |
|
250 |
+ else |
|
251 |
+ case $event in |
|
252 |
+ accept-and-menu-complete) |
|
253 |
+ eval "$event() { builtin zle .$event && _zsh_highlight } ; zle -N $event" |
|
254 |
+ ;; |
|
255 |
+ |
|
256 |
+ # The following widgets should NOT remove any previously |
|
257 |
+ # applied highlighting. Therefore we do not remap them. |
|
258 |
+ .forward-char|.backward-char|.up-line-or-history|.down-line-or-history) |
|
259 |
+ ;; |
|
260 |
+ |
|
261 |
+ .*) |
|
262 |
+ clean_event=$event[2,${#event}] # Remove the leading dot in the event name |
|
263 |
+ case ${widgets[$clean_event]-} in |
|
264 |
+ (completion|user):*) |
|
265 |
+ ;; |
|
266 |
+ *) |
|
267 |
+ eval "$clean_event() { builtin zle $event && _zsh_highlight } ; zle -N $clean_event" |
|
268 |
+ ;; |
|
269 |
+ esac |
|
270 |
+ ;; |
|
271 |
+ *) |
|
272 |
+ ;; |
|
273 |
+ esac |
|
274 |
+ fi |
|
275 |
+ done |
|
276 |
+ unset event clean_event |
|
277 |
+ #-------------->8------------------->8------------------->8----------------- |
|
278 |
+fi |
|
279 |
+ |
|
280 |
+function _history-substring-search-begin() { |
|
281 |
+ _history_substring_search_move_cursor_eol=false |
|
282 |
+ _history_substring_search_query_highlight= |
|
283 |
+ |
|
284 |
+ # |
|
285 |
+ # Continue using the previous $_history_substring_search_result by default, |
|
286 |
+ # unless the current query was cleared or a new/different query was entered. |
|
287 |
+ # |
|
288 |
+ if [[ -z $BUFFER || $BUFFER != $_history_substring_search_result ]]; then |
|
289 |
+ # |
|
290 |
+ # For the purpose of highlighting we will also keep |
|
291 |
+ # a version without doubly-escaped meta characters. |
|
292 |
+ # |
|
293 |
+ _history_substring_search_query=$BUFFER |
|
294 |
+ |
|
295 |
+ # |
|
296 |
+ # $BUFFER contains the text that is in the command-line currently. |
|
297 |
+ # we put an extra "\\" before meta characters such as "\(" and "\)", |
|
298 |
+ # so that they become "\\\(" and "\\\)". |
|
299 |
+ # |
|
300 |
+ _history_substring_search_query_escaped=${BUFFER//(#m)[\][()|\\*?#<>~^]/\\$MATCH} |
|
301 |
+ |
|
302 |
+ # |
|
303 |
+ # Find all occurrences of the search query in the history file. |
|
304 |
+ # |
|
305 |
+ # (k) turns it an array of line numbers. |
|
306 |
+ # |
|
307 |
+ # (on) seems to remove duplicates, which are default |
|
308 |
+ # options. They can be turned off by (ON). |
|
309 |
+ # |
|
310 |
+ _history_substring_search_matches=(${(kon)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]}) |
|
311 |
+ |
|
312 |
+ # |
|
313 |
+ # Define the range of values that $_history_substring_search_match_index |
|
314 |
+ # can take: [0, $_history_substring_search_matches_count_plus]. |
|
315 |
+ # |
|
316 |
+ _history_substring_search_matches_count=$#_history_substring_search_matches |
|
317 |
+ _history_substring_search_matches_count_plus=$(( _history_substring_search_matches_count + 1 )) |
|
318 |
+ _history_substring_search_matches_count_sans=$(( _history_substring_search_matches_count - 1 )) |
|
319 |
+ |
|
320 |
+ # |
|
321 |
+ # If $_history_substring_search_match_index is equal to |
|
322 |
+ # $_history_substring_search_matches_count_plus, this indicates that we |
|
323 |
+ # are beyond the beginning of $_history_substring_search_matches. |
|
324 |
+ # |
|
325 |
+ # If $_history_substring_search_match_index is equal to 0, this indicates |
|
326 |
+ # that we are beyond the end of $_history_substring_search_matches. |
|
327 |
+ # |
|
328 |
+ # If we have initially pressed "up" we have to initialize |
|
329 |
+ # $_history_substring_search_match_index to |
|
330 |
+ # $_history_substring_search_matches_count_plus so that it will be |
|
331 |
+ # decreased to $_history_substring_search_matches_count. |
|
332 |
+ # |
|
333 |
+ # If we have initially pressed "down" we have to initialize |
|
334 |
+ # $_history_substring_search_match_index to |
|
335 |
+ # $_history_substring_search_matches_count so that it will be increased to |
|
336 |
+ # $_history_substring_search_matches_count_plus. |
|
337 |
+ # |
|
338 |
+ if [[ $WIDGET == history-substring-search-down ]]; then |
|
339 |
+ _history_substring_search_match_index=$_history_substring_search_matches_count |
|
340 |
+ else |
|
341 |
+ _history_substring_search_match_index=$_history_substring_search_matches_count_plus |
|
342 |
+ fi |
|
343 |
+ fi |
|
344 |
+} |
|
345 |
+ |
|
346 |
+function _history-substring-search-end() { |
|
347 |
+ _history_substring_search_result=$BUFFER |
|
348 |
+ |
|
349 |
+ # move the cursor to the end of the command line |
|
350 |
+ if [[ $_history_substring_search_move_cursor_eol == true ]]; then |
|
351 |
+ CURSOR=${#BUFFER} |
|
352 |
+ fi |
|
353 |
+ |
|
354 |
+ # highlight command line using zsh-syntax-highlighting |
|
355 |
+ _zsh_highlight |
|
356 |
+ |
|
357 |
+ # highlight the search query inside the command line |
|
358 |
+ if [[ -n $_history_substring_search_query_highlight && -n $_history_substring_search_query ]]; then |
|
359 |
+ # |
|
360 |
+ # The following expression yields a variable $MBEGIN, which |
|
361 |
+ # indicates the begin position + 1 of the first occurrence |
|
362 |
+ # of _history_substring_search_query_escaped in $BUFFER. |
|
363 |
+ # |
|
364 |
+ : ${(S)BUFFER##(#m$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)($_history_substring_search_query##)} |
|
365 |
+ local begin=$(( MBEGIN - 1 )) |
|
366 |
+ local end=$(( begin + $#_history_substring_search_query )) |
|
367 |
+ region_highlight+=("$begin $end $_history_substring_search_query_highlight") |
|
368 |
+ fi |
|
369 |
+ |
|
370 |
+ # For debugging purposes: |
|
371 |
+ # zle -R "mn: "$_history_substring_search_match_index" m#: "${#_history_substring_search_matches} |
|
372 |
+ # read -k -t 200 && zle -U $REPLY |
|
373 |
+ |
|
374 |
+ # Exit successfully from the history-substring-search-* widgets. |
|
375 |
+ true |
|
376 |
+} |
|
377 |
+ |
|
378 |
+function _history-substring-search-up-buffer() { |
|
379 |
+ # |
|
380 |
+ # Check if the UP arrow was pressed to move the cursor within a multi-line |
|
381 |
+ # buffer. This amounts to three tests: |
|
382 |
+ # |
|
383 |
+ # 1. $#buflines -gt 1. |
|
384 |
+ # |
|
385 |
+ # 2. $CURSOR -ne $#BUFFER. |
|
386 |
+ # |
|
387 |
+ # 3. Check if we are on the first line of the current multi-line buffer. |
|
388 |
+ # If so, pressing UP would amount to leaving the multi-line buffer. |
|
389 |
+ # |
|
390 |
+ # We check this by adding an extra "x" to $LBUFFER, which makes |
|
391 |
+ # sure that xlbuflines is always equal to the number of lines |
|
392 |
+ # until $CURSOR (including the line with the cursor on it). |
|
393 |
+ # |
|
394 |
+ local buflines XLBUFFER xlbuflines |
|
395 |
+ buflines=(${(f)BUFFER}) |
|
396 |
+ XLBUFFER=$LBUFFER"x" |
|
397 |
+ xlbuflines=(${(f)XLBUFFER}) |
|
398 |
+ |
|
399 |
+ if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xlbuflines -ne 1 ]]; then |
|
400 |
+ zle up-line-or-history |
|
401 |
+ return true |
|
402 |
+ fi |
|
403 |
+ |
|
404 |
+ false |
|
405 |
+} |
|
406 |
+ |
|
407 |
+function _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 |
+ |
|
428 |
+ if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xrbuflines -ne 1 ]]; then |
|
429 |
+ zle down-line-or-history |
|
430 |
+ return true |
|
431 |
+ fi |
|
432 |
+ |
|
433 |
+ false |
|
434 |
+} |
|
435 |
+ |
|
436 |
+function _history-substring-search-up-history() { |
|
437 |
+ # |
|
438 |
+ # Behave like up in ZSH, except clear the $BUFFER |
|
439 |
+ # when beginning of history is reached like in Fish. |
|
440 |
+ # |
|
441 |
+ if [[ -z $_history_substring_search_query ]]; then |
|
442 |
+ |
|
443 |
+ # we have reached the absolute top of history |
|
444 |
+ if [[ $HISTNO -eq 1 ]]; then |
|
445 |
+ BUFFER= |
|
446 |
+ |
|
447 |
+ # going up from somewhere below the top of history |
|
448 |
+ else |
|
449 |
+ zle up-history |
|
450 |
+ fi |
|
451 |
+ |
|
452 |
+ return true |
|
453 |
+ fi |
|
454 |
+ |
|
455 |
+ false |
|
456 |
+} |
|
457 |
+ |
|
458 |
+function _history-substring-search-down-history() { |
|
459 |
+ # |
|
460 |
+ # Behave like down-history in ZSH, except clear the |
|
461 |
+ # $BUFFER when end of history is reached like in Fish. |
|
462 |
+ # |
|
463 |
+ if [[ -z $_history_substring_search_query ]]; then |
|
464 |
+ |
|
465 |
+ # going down from the absolute top of history |
|
466 |
+ if [[ $HISTNO -eq 1 && -z $BUFFER ]]; then |
|
467 |
+ BUFFER=${history[1]} |
|
468 |
+ _history_substring_search_move_cursor_eol=true |
|
469 |
+ |
|
470 |
+ # going down from somewhere above the bottom of history |
|
471 |
+ else |
|
472 |
+ zle down-history |
|
473 |
+ fi |
|
474 |
+ |
|
475 |
+ return true |
|
476 |
+ fi |
|
477 |
+ |
|
478 |
+ false |
|
479 |
+} |
|
480 |
+ |
|
481 |
+function _history-substring-search-up-search() { |
|
482 |
+ _history_substring_search_move_cursor_eol=true |
|
483 |
+ |
|
484 |
+ # |
|
485 |
+ # Highlight matches during history-substring-up-search: |
|
486 |
+ # |
|
487 |
+ # The following constants have been initialized in |
|
488 |
+ # _history-substring-search-up/down-search(): |
|
489 |
+ # |
|
490 |
+ # $_history_substring_search_matches is the current list of matches |
|
491 |
+ # $_history_substring_search_matches_count is the current number of matches |
|
492 |
+ # $_history_substring_search_matches_count_plus is the current number of matches + 1 |
|
493 |
+ # $_history_substring_search_matches_count_sans is the current number of matches - 1 |
|
494 |
+ # $_history_substring_search_match_index is the index of the current match |
|
495 |
+ # |
|
496 |
+ # The range of values that $_history_substring_search_match_index can take |
|
497 |
+ # is: [0, $_history_substring_search_matches_count_plus]. A value of 0 |
|
498 |
+ # indicates that we are beyond the end of |
|
499 |
+ # $_history_substring_search_matches. A value of |
|
500 |
+ # $_history_substring_search_matches_count_plus indicates that we are beyond |
|
501 |
+ # the beginning of $_history_substring_search_matches. |
|
502 |
+ # |
|
503 |
+ # In _history-substring-search-up-search() the initial value of |
|
504 |
+ # $_history_substring_search_match_index is |
|
505 |
+ # $_history_substring_search_matches_count_plus. This value is set in |
|
506 |
+ # _history-substring-search-begin(). _history-substring-search-up-search() |
|
507 |
+ # will initially decrease it to $_history_substring_search_matches_count. |
|
508 |
+ # |
|
509 |
+ if [[ $_history_substring_search_match_index -ge 2 ]]; then |
|
510 |
+ # |
|
511 |
+ # Highlight the next match: |
|
512 |
+ # |
|
513 |
+ # 1. Decrease the value of $_history_substring_search_match_index. |
|
514 |
+ # |
|
515 |
+ # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
516 |
+ # to highlight the current buffer. |
|
517 |
+ # |
|
518 |
+ (( _history_substring_search_match_index-- )) |
|
519 |
+ BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]] |
|
520 |
+ _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
521 |
+ |
|
522 |
+ elif [[ $_history_substring_search_match_index -eq 1 ]]; then |
|
523 |
+ # |
|
524 |
+ # We will move beyond the end of $_history_substring_search_matches: |
|
525 |
+ # |
|
526 |
+ # 1. Decrease the value of $_history_substring_search_match_index. |
|
527 |
+ # |
|
528 |
+ # 2. Save the current buffer in $_history_substring_search_old_buffer, |
|
529 |
+ # so that it can be retrieved by |
|
530 |
+ # _history-substring-search-down-search() later. |
|
531 |
+ # |
|
532 |
+ # 3. Make $BUFFER equal to $_history_substring_search_query. |
|
533 |
+ # |
|
534 |
+ # 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
535 |
+ # to highlight the current buffer. |
|
536 |
+ # |
|
537 |
+ (( _history_substring_search_match_index-- )) |
|
538 |
+ _history_substring_search_old_buffer=$BUFFER |
|
539 |
+ BUFFER=$_history_substring_search_query |
|
540 |
+ _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
541 |
+ |
|
542 |
+ elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count_plus ]]; then |
|
543 |
+ # |
|
544 |
+ # We were beyond the beginning of $_history_substring_search_matches but |
|
545 |
+ # UP makes us move back to $_history_substring_search_matches: |
|
546 |
+ # |
|
547 |
+ # 1. Decrease the value of $_history_substring_search_match_index. |
|
548 |
+ # |
|
549 |
+ # 2. Restore $BUFFER from $_history_substring_search_old_buffer. |
|
550 |
+ # |
|
551 |
+ # 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
552 |
+ # to highlight the current buffer. |
|
553 |
+ # |
|
554 |
+ (( _history_substring_search_match_index-- )) |
|
555 |
+ BUFFER=$_history_substring_search_old_buffer |
|
556 |
+ _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
557 |
+ fi |
|
558 |
+} |
|
559 |
+ |
|
560 |
+function _history-substring-search-down-search() { |
|
561 |
+ _history_substring_search_move_cursor_eol=true |
|
562 |
+ |
|
563 |
+ # |
|
564 |
+ # Highlight matches during history-substring-up-search: |
|
565 |
+ # |
|
566 |
+ # The following constants have been initialized in |
|
567 |
+ # _history-substring-search-up/down-search(): |
|
568 |
+ # |
|
569 |
+ # $_history_substring_search_matches is the current list of matches |
|
570 |
+ # $_history_substring_search_matches_count is the current number of matches |
|
571 |
+ # $_history_substring_search_matches_count_plus is the current number of matches + 1 |
|
572 |
+ # $_history_substring_search_matches_count_sans is the current number of matches - 1 |
|
573 |
+ # $_history_substring_search_match_index is the index of the current match |
|
574 |
+ # |
|
575 |
+ # The range of values that $_history_substring_search_match_index can take |
|
576 |
+ # is: [0, $_history_substring_search_matches_count_plus]. A value of 0 |
|
577 |
+ # indicates that we are beyond the end of |
|
578 |
+ # $_history_substring_search_matches. A value of |
|
579 |
+ # $_history_substring_search_matches_count_plus indicates that we are beyond |
|
580 |
+ # the beginning of $_history_substring_search_matches. |
|
581 |
+ # |
|
582 |
+ # In _history-substring-search-down-search() the initial value of |
|
583 |
+ # $_history_substring_search_match_index is |
|
584 |
+ # $_history_substring_search_matches_count. This value is set in |
|
585 |
+ # _history-substring-search-begin(). |
|
586 |
+ # _history-substring-search-down-search() will initially increase it to |
|
587 |
+ # $_history_substring_search_matches_count_plus. |
|
588 |
+ # |
|
589 |
+ if [[ $_history_substring_search_match_index -le $_history_substring_search_matches_count_sans ]]; then |
|
590 |
+ # |
|
591 |
+ # Highlight the next match: |
|
592 |
+ # |
|
593 |
+ # 1. Increase $_history_substring_search_match_index by 1. |
|
594 |
+ # |
|
595 |
+ # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
596 |
+ # to highlight the current buffer. |
|
597 |
+ # |
|
598 |
+ (( _history_substring_search_match_index++ )) |
|
599 |
+ BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]] |
|
600 |
+ _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
601 |
+ |
|
602 |
+ elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count ]]; then |
|
603 |
+ # |
|
604 |
+ # We will move beyond the beginning of $_history_substring_search_matches: |
|
605 |
+ # |
|
606 |
+ # 1. Increase $_history_substring_search_match_index by 1. |
|
607 |
+ # |
|
608 |
+ # 2. Save the current buffer in $_history_substring_search_old_buffer, so |
|
609 |
+ # that it can be retrieved by _history-substring-search-up-search() |
|
610 |
+ # later. |
|
611 |
+ # |
|
612 |
+ # 3. Make $BUFFER equal to $_history_substring_search_query. |
|
613 |
+ # |
|
614 |
+ # 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
615 |
+ # to highlight the current buffer. |
|
616 |
+ # |
|
617 |
+ (( _history_substring_search_match_index++ )) |
|
618 |
+ _history_substring_search_old_buffer=$BUFFER |
|
619 |
+ BUFFER=$_history_substring_search_query |
|
620 |
+ _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND |
|
621 |
+ |
|
622 |
+ elif [[ $_history_substring_search_match_index -eq 0 ]]; then |
|
623 |
+ # |
|
624 |
+ # We were beyond the end of $_history_substring_search_matches but DOWN |
|
625 |
+ # makes us move back to the $_history_substring_search_matches: |
|
626 |
+ # |
|
627 |
+ # 1. Increase $_history_substring_search_match_index by 1. |
|
628 |
+ # |
|
629 |
+ # 2. Restore $BUFFER from $_history_substring_search_old_buffer. |
|
630 |
+ # |
|
631 |
+ # 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
632 |
+ # to highlight the current buffer. |
|
633 |
+ # |
|
634 |
+ (( _history_substring_search_match_index++ )) |
|
635 |
+ BUFFER=$_history_substring_search_old_buffer |
|
636 |
+ _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
|
637 |
+ fi |
|
638 |
+} |
|
639 |
+ |
|
640 |
+# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- |
|
641 |
+# vim: ft=zsh sw=2 ts=2 et |