forked from zsh-users/zsh-autosuggestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-autosuggestions.zsh
363 lines (301 loc) · 11.8 KB
/
zsh-autosuggestions.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Fish-like fast/unobtrusive autosuggestions for zsh.
# https://github.com/zsh-users/zsh-autosuggestions
# v0.2.16
# Copyright (c) 2013 Thiago de Arruda
# Copyright (c) 2016 Eric Freese
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#--------------------------------------------------------------------#
# Global Configuration Variables #
#--------------------------------------------------------------------#
# Color to use when highlighting suggestion
# Uses format of `region_highlight`
# More info: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
# Set this to enable matching the history entry preceding the suggestion
# against the previously executed command.
# For example, if your have just executed:
# pwd
# ls foo
# ls bar
# pwd
# And then you start typing 'ls', then the suggestion will be 'ls foo',
# rather than 'ls bar', as your most recently executed command (pwd)
# was succeeded by 'ls foo' on it's previous invocation.
unset ZSH_AUTOSUGGEST_MATCH_PREV_CMD
# Prefix to use when saving original versions of bound widgets
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
# Widgets that clear the suggestion
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
history-search-forward
history-search-backward
history-beginning-search-forward
history-beginning-search-backward
up-line-or-history
down-line-or-history
accept-line
)
# Widgets that accept the entire suggestion
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
forward-char
end-of-line
vi-forward-char
vi-end-of-line
)
# Widgets that accept the suggestion as far as the cursor moves
ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
forward-word
vi-forward-word
vi-forward-word-end
vi-forward-blank-word
vi-forward-blank-word-end
)
#--------------------------------------------------------------------#
# Handle Deprecated Variables/Widgets #
#--------------------------------------------------------------------#
_zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_check_deprecated_config() {
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_HIGHLIGHT_COLOR is deprecated. Use ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE instead."
[ -z "$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" ] && ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=$AUTOSUGGESTION_HIGHLIGHT_STYLE
unset AUTOSUGGESTION_HIGHLIGHT_STYLE
fi
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_CURSOR" ]; then
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_HIGHLIGHT_CURSOR is deprecated."
unset AUTOSUGGESTION_HIGHLIGHT_CURSOR
fi
if [ -n "$AUTOSUGGESTION_ACCEPT_RIGHT_ARROW" ]; then
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_ACCEPT_RIGHT_ARROW is deprecated. The right arrow now accepts the suggestion by default."
unset AUTOSUGGESTION_ACCEPT_RIGHT_ARROW
fi
}
_zsh_autosuggest_deprecated_start_widget() {
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. For more info, see the README at https://github.com/zsh-users/zsh-autosuggestions."
zle -D autosuggest-start
eval "zle-line-init() {
$(echo $functions[${widgets[zle-line-init]#*:}] | sed -e 's/zle autosuggest-start//g')
}"
}
zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget
#--------------------------------------------------------------------#
# Widget Helpers #
#--------------------------------------------------------------------#
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
_zsh_autosuggest_bind_widget() {
local widget=$1
local autosuggest_action=$2
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
# Save a reference to the original widget
case $widgets[$widget] in
# Already bound
user:_zsh_autosuggest_(bound|orig)_*);;
# User-defined widget
user:*)
zle -N $prefix$widget ${widgets[$widget]#*:}
;;
# Built-in widget
builtin)
eval "_zsh_autosuggest_orig_$widget() { zle .$widget }"
zle -N $prefix$widget _zsh_autosuggest_orig_$widget
;;
# Completion widget
completion:*)
eval "zle -C $prefix$widget ${${widgets[$widget]#*:}/:/ }"
;;
esac
# Pass the original widget's name explicitly into the autosuggest
# function. Use this passed in widget name to call the original
# widget instead of relying on the $WIDGET variable being set
# correctly. $WIDGET cannot be trusted because other plugins call
# zle without the `-w` flag (e.g. `zle self-insert` instead of
# `zle self-insert -w`).
eval "_zsh_autosuggest_bound_$widget() {
_zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@
}"
# Create the bound widget
zle -N $widget _zsh_autosuggest_bound_$widget
}
# Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_widgets() {
local widget;
# Find every widget we might want to bind and bind it appropriately
for widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|autosuggest-*|$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX*|zle-line-*|run-help|which-command|beep|set-local-history|yank)}; do
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget accept
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget partial_accept
else
# Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
fi
done
}
# Given the name of an original widget and args, invoke it, if it exists
_zsh_autosuggest_invoke_original_widget() {
# Do nothing unless called with at least one arg
[ $# -gt 0 ] || return
local original_widget_name=$1
shift
if [ $widgets[$original_widget_name] ]; then
zle $original_widget_name -- $@
fi
}
#--------------------------------------------------------------------#
# Highlighting #
#--------------------------------------------------------------------#
# If there was a highlight, remove it
_zsh_autosuggest_highlight_reset() {
if [ -n "$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT" ]; then
region_highlight=("${(@)region_highlight:#$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT}")
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
fi
}
# If there's a suggestion, highlight it
_zsh_autosuggest_highlight_apply() {
if [ $#POSTDISPLAY -gt 0 ]; then
_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
region_highlight+=("$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT")
else
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
fi
}
#--------------------------------------------------------------------#
# Autosuggest Widget Implementations #
#--------------------------------------------------------------------#
# Clear the suggestion
_zsh_autosuggest_clear() {
# Remove the suggestion
unset POSTDISPLAY
_zsh_autosuggest_invoke_original_widget $@
}
# Modify the buffer and get a new suggestion
_zsh_autosuggest_modify() {
# Original widget modifies the buffer
_zsh_autosuggest_invoke_original_widget $@
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
suggestion=$(_zsh_autosuggest_suggestion "$BUFFER")
fi
# Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then
POSTDISPLAY=${suggestion#$BUFFER}
else
unset POSTDISPLAY
fi
}
# Accept the entire suggestion
_zsh_autosuggest_accept() {
# Only accept if the cursor is at the end of the buffer
if [ $CURSOR -eq $#BUFFER ]; then
# Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY"
# Remove the suggestion
unset POSTDISPLAY
# Move the cursor to the end of the buffer
CURSOR=${#BUFFER}
fi
_zsh_autosuggest_invoke_original_widget $@
}
# Partially accept the suggestion
_zsh_autosuggest_partial_accept() {
# Save the contents of the buffer so we can restore later if needed
local original_buffer=$BUFFER
# Temporarily accept the suggestion.
BUFFER="$BUFFER$POSTDISPLAY"
# Original widget moves the cursor
_zsh_autosuggest_invoke_original_widget $@
# If we've moved past the end of the original buffer
if [ $CURSOR -gt $#original_buffer ]; then
# Set POSTDISPLAY to text right of the cursor
POSTDISPLAY=$RBUFFER
# Clip the buffer at the cursor
BUFFER=$LBUFFER
else
# Restore the original buffer
BUFFER=$original_buffer
fi
}
for action in clear modify accept partial_accept; do
eval "_zsh_autosuggest_widget_$action() {
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_$action \$@
_zsh_autosuggest_highlight_apply
}"
done
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
#--------------------------------------------------------------------#
# Suggestion #
#--------------------------------------------------------------------#
# Get the peviously executed command (hookable for testing)
_zsh_autosuggest_prev_cmd() {
echo -E "${history[$((HISTCMD-1))]}"
}
# Get a suggestion from history that matches a given prefix
_zsh_autosuggest_suggestion() {
local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")"
# Get all history event numbers (reversed) that correspond to history
# entries that match pattern $prefix*
local history_match_keys
history_match_keys=(${(k)history[(R)$prefix*]})
# By default we use the first history number (most recent history entry)
local history_key="$history_match_keys[1]"
# If matching on the previous command is enabled ...
if (( ${+ZSH_AUTOSUGGEST_MATCH_PREV_CMD} )); then
# Get the previously executed command
local prev_cmd="$(_zsh_autosuggest_prev_cmd)"
prev_cmd="$(_zsh_autosuggest_escape_command_prefix $prev_cmd)"
# Iterate up to the first 200 history event numbers that match $prefix
for key in "${(@)history_match_keys[1,200]}"; do
# Stop if we ran out of history
[[ $key -gt 1 ]] || break
# See if the history entry preceding the suggestion matches the
# previous command, and use it if it does
if [[ "${history[$((key - 1))]}" == $prev_cmd ]]; then
history_key=$key
break
fi
done
fi
# Echo the matched history entry
echo -E "$history[$history_key]"
}
_zsh_autosuggest_escape_command_prefix() {
setopt localoptions EXTENDED_GLOB
# Escape special chars in the string (requires EXTENDED_GLOB)
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
}
#--------------------------------------------------------------------#
# Start #
#--------------------------------------------------------------------#
# Start the autosuggestion widgets
_zsh_autosuggest_start() {
_zsh_autosuggest_check_deprecated_config
_zsh_autosuggest_bind_widgets
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd _zsh_autosuggest_start