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