Mercurial > dotfiles
comparison .zfun/zsh-autosuggestions/src/widgets.zsh @ 467:e1ce8897030d
zsh: import df6f6f9ff41 of zsh-autosuggestions
author | Augie Fackler <raf@durin42.com> |
---|---|
date | Mon, 03 Dec 2018 22:37:29 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
466:f248cf012d9a | 467:e1ce8897030d |
---|---|
1 | |
2 #--------------------------------------------------------------------# | |
3 # Autosuggest Widget Implementations # | |
4 #--------------------------------------------------------------------# | |
5 | |
6 # Disable suggestions | |
7 _zsh_autosuggest_disable() { | |
8 typeset -g _ZSH_AUTOSUGGEST_DISABLED | |
9 _zsh_autosuggest_clear | |
10 } | |
11 | |
12 # Enable suggestions | |
13 _zsh_autosuggest_enable() { | |
14 unset _ZSH_AUTOSUGGEST_DISABLED | |
15 | |
16 if (( $#BUFFER )); then | |
17 _zsh_autosuggest_fetch | |
18 fi | |
19 } | |
20 | |
21 # Toggle suggestions (enable/disable) | |
22 _zsh_autosuggest_toggle() { | |
23 if [[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]]; then | |
24 _zsh_autosuggest_enable | |
25 else | |
26 _zsh_autosuggest_disable | |
27 fi | |
28 } | |
29 | |
30 # Clear the suggestion | |
31 _zsh_autosuggest_clear() { | |
32 # Remove the suggestion | |
33 unset POSTDISPLAY | |
34 | |
35 _zsh_autosuggest_invoke_original_widget $@ | |
36 } | |
37 | |
38 # Modify the buffer and get a new suggestion | |
39 _zsh_autosuggest_modify() { | |
40 emulate -L zsh | |
41 | |
42 local -i retval | |
43 | |
44 # Only available in zsh >= 5.4 | |
45 local -i KEYS_QUEUED_COUNT | |
46 | |
47 # Save the contents of the buffer/postdisplay | |
48 local orig_buffer="$BUFFER" | |
49 local orig_postdisplay="$POSTDISPLAY" | |
50 | |
51 # Clear suggestion while waiting for next one | |
52 unset POSTDISPLAY | |
53 | |
54 # Original widget may modify the buffer | |
55 _zsh_autosuggest_invoke_original_widget $@ | |
56 retval=$? | |
57 | |
58 # Don't fetch a new suggestion if there's more input to be read immediately | |
59 if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then | |
60 POSTDISPLAY="$orig_postdisplay" | |
61 return $retval | |
62 fi | |
63 | |
64 # Optimize if manually typing in the suggestion | |
65 if (( $#BUFFER > $#orig_buffer )); then | |
66 local added=${BUFFER#$orig_buffer} | |
67 | |
68 # If the string added matches the beginning of the postdisplay | |
69 if [[ "$added" = "${orig_postdisplay:0:$#added}" ]]; then | |
70 POSTDISPLAY="${orig_postdisplay:$#added}" | |
71 return $retval | |
72 fi | |
73 fi | |
74 | |
75 # Don't fetch a new suggestion if the buffer hasn't changed | |
76 if [[ "$BUFFER" = "$orig_buffer" ]]; then | |
77 POSTDISPLAY="$orig_postdisplay" | |
78 return $retval | |
79 fi | |
80 | |
81 # Bail out if suggestions are disabled | |
82 if [[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]]; then | |
83 return $? | |
84 fi | |
85 | |
86 # Get a new suggestion if the buffer is not empty after modification | |
87 if (( $#BUFFER > 0 )); then | |
88 if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then | |
89 _zsh_autosuggest_fetch | |
90 fi | |
91 fi | |
92 | |
93 return $retval | |
94 } | |
95 | |
96 # Fetch a new suggestion based on what's currently in the buffer | |
97 _zsh_autosuggest_fetch() { | |
98 if zpty -t "$ZSH_AUTOSUGGEST_ASYNC_PTY_NAME" &>/dev/null; then | |
99 _zsh_autosuggest_async_request "$BUFFER" | |
100 else | |
101 local suggestion | |
102 _zsh_autosuggest_fetch_suggestion "$BUFFER" | |
103 _zsh_autosuggest_suggest "$suggestion" | |
104 fi | |
105 } | |
106 | |
107 # Offer a suggestion | |
108 _zsh_autosuggest_suggest() { | |
109 emulate -L zsh | |
110 | |
111 local suggestion="$1" | |
112 | |
113 if [[ -n "$suggestion" ]] && (( $#BUFFER )); then | |
114 POSTDISPLAY="${suggestion#$BUFFER}" | |
115 else | |
116 unset POSTDISPLAY | |
117 fi | |
118 } | |
119 | |
120 # Accept the entire suggestion | |
121 _zsh_autosuggest_accept() { | |
122 local -i max_cursor_pos=$#BUFFER | |
123 | |
124 # When vicmd keymap is active, the cursor can't move all the way | |
125 # to the end of the buffer | |
126 if [[ "$KEYMAP" = "vicmd" ]]; then | |
127 max_cursor_pos=$((max_cursor_pos - 1)) | |
128 fi | |
129 | |
130 # Only accept if the cursor is at the end of the buffer | |
131 if [[ $CURSOR = $max_cursor_pos ]]; then | |
132 # Add the suggestion to the buffer | |
133 BUFFER="$BUFFER$POSTDISPLAY" | |
134 | |
135 # Remove the suggestion | |
136 unset POSTDISPLAY | |
137 | |
138 # Move the cursor to the end of the buffer | |
139 CURSOR=${#BUFFER} | |
140 fi | |
141 | |
142 _zsh_autosuggest_invoke_original_widget $@ | |
143 } | |
144 | |
145 # Accept the entire suggestion and execute it | |
146 _zsh_autosuggest_execute() { | |
147 # Add the suggestion to the buffer | |
148 BUFFER="$BUFFER$POSTDISPLAY" | |
149 | |
150 # Remove the suggestion | |
151 unset POSTDISPLAY | |
152 | |
153 # Call the original `accept-line` to handle syntax highlighting or | |
154 # other potential custom behavior | |
155 _zsh_autosuggest_invoke_original_widget "accept-line" | |
156 } | |
157 | |
158 # Partially accept the suggestion | |
159 _zsh_autosuggest_partial_accept() { | |
160 local -i retval cursor_loc | |
161 | |
162 # Save the contents of the buffer so we can restore later if needed | |
163 local original_buffer="$BUFFER" | |
164 | |
165 # Temporarily accept the suggestion. | |
166 BUFFER="$BUFFER$POSTDISPLAY" | |
167 | |
168 # Original widget moves the cursor | |
169 _zsh_autosuggest_invoke_original_widget $@ | |
170 retval=$? | |
171 | |
172 # Normalize cursor location across vi/emacs modes | |
173 cursor_loc=$CURSOR | |
174 if [[ "$KEYMAP" = "vicmd" ]]; then | |
175 cursor_loc=$((cursor_loc + 1)) | |
176 fi | |
177 | |
178 # If we've moved past the end of the original buffer | |
179 if (( $cursor_loc > $#original_buffer )); then | |
180 # Set POSTDISPLAY to text right of the cursor | |
181 POSTDISPLAY="${BUFFER[$(($cursor_loc + 1)),$#BUFFER]}" | |
182 | |
183 # Clip the buffer at the cursor | |
184 BUFFER="${BUFFER[1,$cursor_loc]}" | |
185 else | |
186 # Restore the original buffer | |
187 BUFFER="$original_buffer" | |
188 fi | |
189 | |
190 return $retval | |
191 } | |
192 | |
193 for action in clear modify fetch suggest accept partial_accept execute enable disable toggle; do | |
194 eval "_zsh_autosuggest_widget_$action() { | |
195 local -i retval | |
196 | |
197 _zsh_autosuggest_highlight_reset | |
198 | |
199 _zsh_autosuggest_$action \$@ | |
200 retval=\$? | |
201 | |
202 _zsh_autosuggest_highlight_apply | |
203 | |
204 zle -R | |
205 | |
206 return \$retval | |
207 }" | |
208 done | |
209 | |
210 zle -N autosuggest-fetch _zsh_autosuggest_widget_fetch | |
211 zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest | |
212 zle -N autosuggest-accept _zsh_autosuggest_widget_accept | |
213 zle -N autosuggest-clear _zsh_autosuggest_widget_clear | |
214 zle -N autosuggest-execute _zsh_autosuggest_widget_execute | |
215 zle -N autosuggest-enable _zsh_autosuggest_widget_enable | |
216 zle -N autosuggest-disable _zsh_autosuggest_widget_disable | |
217 zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle |