Skip to content

Commit 200aa75

Browse files
committed
Refactor async mode to no longer use zpty
See technique used in `fast-syntax-highlighting`: - zdharma/fast-syntax-highlighting@ca2e18b - http://www.zsh.org/mla/users/2018/msg00424.html Also see http://www.zsh.org/mla/users/2018/msg00432.html In async response handler: - We only want to read data in case of POLLIN or POLLHUP. Not POLLNVAL or select error. - We always want to remove the handler, so it doesn't get called in an infinite loop when error is nval or err. There is an upstream bug that prevents ctrl-c from resetting the prompt immediately after a suggestion has been fetched asynchronously. A patch has been submitted, but a workaround for now is to add `command true` after the exec. See #364
1 parent 543f2b5 commit 200aa75

11 files changed

+109
-314
lines changed

Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ SRC_FILES := \
44
$(SRC_DIR)/setup.zsh \
55
$(SRC_DIR)/config.zsh \
66
$(SRC_DIR)/util.zsh \
7-
$(SRC_DIR)/features.zsh \
87
$(SRC_DIR)/bind.zsh \
98
$(SRC_DIR)/highlight.zsh \
109
$(SRC_DIR)/widgets.zsh \

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ This can be useful when pasting large amount of text in the terminal, to avoid t
6767

6868
### Enable Asynchronous Mode
6969

70-
As of `v0.4.0`, suggestions can be fetched asynchronously using the `zsh/zpty` module. To enable this behavior, set the `ZSH_AUTOSUGGEST_USE_ASYNC` variable (it can be set to anything).
70+
As of `v0.4.0`, suggestions can be fetched asynchronously. To enable this behavior, set the `ZSH_AUTOSUGGEST_USE_ASYNC` variable (it can be set to anything).
7171

7272

7373
### Key Bindings

spec/async_spec.rb

+11-48
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
context 'with asynchronous suggestions enabled' do
2-
before do
3-
skip 'Async mode not supported below v5.0.8' if session.zsh_version < Gem::Version.new('5.0.8')
4-
end
5-
62
let(:options) { ["ZSH_AUTOSUGGEST_USE_ASYNC="] }
73

84
describe '`up-line-or-beginning-search`' do
@@ -31,52 +27,19 @@
3127
end
3228
end
3329

34-
it 'should not add extra carriage returns before newlines' do
35-
session.
36-
send_string('echo "').
37-
send_keys('escape').
38-
send_keys('enter').
39-
send_string('"').
40-
send_keys('enter')
41-
42-
session.clear_screen
43-
44-
session.send_string('echo')
45-
wait_for { session.content }.to eq("echo \"\n\"")
46-
end
47-
48-
it 'should treat carriage returns and newlines as separate characters' do
49-
session.
50-
send_string('echo "').
51-
send_keys('C-v').
52-
send_keys('enter').
53-
send_string('foo"').
54-
send_keys('enter')
55-
56-
session.
57-
send_string('echo "').
58-
send_keys('control').
59-
send_keys('enter').
60-
send_string('bar"').
61-
send_keys('enter')
62-
63-
session.clear_screen
64-
65-
session.
66-
send_string('echo "').
67-
send_keys('C-v').
68-
send_keys('enter')
69-
70-
wait_for { session.content }.to eq('echo "^Mfoo"')
71-
end
72-
73-
describe 'exiting a subshell' do
74-
it 'should not cause error messages to be printed' do
75-
session.run_command('$(exit)')
30+
describe 'pressing ^C after fetching a suggestion' do
31+
before do
32+
skip 'Workaround does not work below v5.0.8' if session.zsh_version < Gem::Version.new('5.0.8')
33+
end
7634

77-
sleep 1
35+
it 'terminates the prompt and begins a new one' do
36+
session.send_keys('e')
37+
sleep 0.1
38+
session.send_keys('C-c')
39+
sleep 0.1
40+
session.send_keys('echo')
7841

79-
expect(session.content).to eq('$(exit)')
42+
wait_for { session.content }.to eq("e\necho")
8043
end
8144
end
8245
end

spec/integrations/client_zpty_spec.rb

-10
This file was deleted.

spec/options/async_zpty_name_spec.rb

-19
This file was deleted.

src/async.zsh

+47-89
Original file line numberDiff line numberDiff line change
@@ -3,107 +3,65 @@
33
# Async #
44
#--------------------------------------------------------------------#
55

6-
# Zpty process is spawned running this function
7-
_zsh_autosuggest_async_server() {
8-
emulate -R zsh
6+
zmodload zsh/system
97

10-
# There is a bug in zpty module (fixed in zsh/master) by which a
11-
# zpty that exits will kill all zpty processes that were forked
12-
# before it. Here we set up a zsh exit hook to SIGKILL the zpty
13-
# process immediately, before it has a chance to kill any other
14-
# zpty processes.
15-
zshexit() {
16-
kill -KILL $$
17-
sleep 1 # Block for long enough for the signal to come through
18-
}
19-
20-
# Don't add any extra carriage returns
21-
stty -onlcr
22-
23-
# Don't translate carriage returns to newlines
24-
stty -icrnl
25-
26-
# Silence any error messages
27-
exec 2>/dev/null
8+
_zsh_autosuggest_async_request() {
9+
typeset -g _ZSH_AUTOSUGGEST_ASYNC_FD _ZSH_AUTOSUGGEST_CHILD_PID
10+
11+
# If we've got a pending request, cancel it
12+
if [[ -n "$_ZSH_AUTOSUGGEST_ASYNC_FD" ]] && { true <&$_ZSH_AUTOSUGGEST_ASYNC_FD } 2>/dev/null; then
13+
# Close the file descriptor and remove the handler
14+
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&-
15+
zle -F $_ZSH_AUTOSUGGEST_ASYNC_FD
16+
17+
# Zsh will make a new process group for the child process only if job
18+
# control is enabled (MONITOR option)
19+
if [[ -o MONITOR ]]; then
20+
# Send the signal to the process group to kill any processes that may
21+
# have been forked by the suggestion strategy
22+
kill -TERM -$_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null
23+
else
24+
# Kill just the child process since it wasn't placed in a new process
25+
# group. If the suggestion strategy forked any child processes they may
26+
# be orphaned and left behind.
27+
kill -TERM $_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null
28+
fi
29+
fi
2830

29-
local last_pid
31+
# Fork a process to fetch a suggestion and open a pipe to read from it
32+
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}< <(
33+
# Tell parent process our pid
34+
echo $sysparams[pid]
3035
31-
while IFS='' read -r -d $'\0' query; do
32-
# Kill last bg process
33-
kill -KILL $last_pid &>/dev/null
36+
# Fetch and print the suggestion
37+
local suggestion
38+
_zsh_autosuggest_fetch_suggestion "$1"
39+
echo -nE "$suggestion"
40+
)
3441

35-
# Run suggestion search in the background
36-
(
37-
local suggestion
38-
_zsh_autosuggest_fetch_suggestion "$query"
39-
echo -n -E "$suggestion"$'\0'
40-
) &
42+
# There's a weird bug here where ^C stops working unless we force a fork
43+
# See https://github.com/zsh-users/zsh-autosuggestions/issues/364
44+
command true
4145

42-
last_pid=$!
43-
done
44-
}
46+
# Read the pid from the child process
47+
read _ZSH_AUTOSUGGEST_CHILD_PID <&$_ZSH_AUTOSUGGEST_ASYNC_FD
4548

46-
_zsh_autosuggest_async_request() {
47-
# Write the query to the zpty process to fetch a suggestion
48-
zpty -w -n $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME "${1}"$'\0'
49+
# When the fd is readable, call the response handler
50+
zle -F "$_ZSH_AUTOSUGGEST_ASYNC_FD" _zsh_autosuggest_async_response
4951
}
5052

51-
# Called when new data is ready to be read from the pty
53+
# Called when new data is ready to be read from the pipe
5254
# First arg will be fd ready for reading
5355
# Second arg will be passed in case of error
5456
_zsh_autosuggest_async_response() {
55-
setopt LOCAL_OPTIONS EXTENDED_GLOB
56-
57-
local suggestion
58-
59-
zpty -rt $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME suggestion '*'$'\0' 2>/dev/null
60-
zle autosuggest-suggest -- "${suggestion%%$'\0'##}"
61-
}
62-
63-
_zsh_autosuggest_async_pty_create() {
64-
# With newer versions of zsh, REPLY stores the fd to read from
65-
typeset -h REPLY
57+
if [[ -z "$2" || "$2" == "hup" ]]; then
58+
# Read everything from the fd and give it as a suggestion
59+
zle autosuggest-suggest -- "$(cat <&$1)"
6660

67-
# If we won't get a fd back from zpty, try to guess it
68-
if (( ! $_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD )); then
69-
integer -l zptyfd
70-
exec {zptyfd}>&1 # Open a new file descriptor (above 10).
71-
exec {zptyfd}>&- # Close it so it's free to be used by zpty.
61+
# Close the fd
62+
exec {1}<&-
7263
fi
7364

74-
# Fork a zpty process running the server function
75-
zpty -b $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME _zsh_autosuggest_async_server
76-
77-
# Store the fd so we can remove the handler later
78-
if (( REPLY )); then
79-
_ZSH_AUTOSUGGEST_PTY_FD=$REPLY
80-
else
81-
_ZSH_AUTOSUGGEST_PTY_FD=$zptyfd
82-
fi
83-
84-
# Set up input handler from the zpty
85-
zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_response
86-
}
87-
88-
_zsh_autosuggest_async_pty_destroy() {
89-
# Remove the input handler
90-
zle -F $_ZSH_AUTOSUGGEST_PTY_FD &>/dev/null
91-
92-
# Destroy the zpty
93-
zpty -d $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME &>/dev/null
94-
}
95-
96-
_zsh_autosuggest_async_pty_recreate() {
97-
_zsh_autosuggest_async_pty_destroy
98-
_zsh_autosuggest_async_pty_create
99-
}
100-
101-
_zsh_autosuggest_async_start() {
102-
typeset -g _ZSH_AUTOSUGGEST_PTY_FD
103-
104-
_zsh_autosuggest_feature_detect_zpty_returns_fd
105-
_zsh_autosuggest_async_pty_recreate
106-
107-
# We recreate the pty to get a fresh list of history events
108-
add-zsh-hook precmd _zsh_autosuggest_async_pty_recreate
65+
# Always remove the handler
66+
zle -F "$1"
10967
}

src/config.zsh

-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,3 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
8989
# Max size of buffer to trigger autosuggestion. Leave null for no upper bound.
9090
(( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) &&
9191
typeset -g ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=
92-
93-
# Pty name for calculating autosuggestions asynchronously
94-
(( ! ${+ZSH_AUTOSUGGEST_ASYNC_PTY_NAME} )) &&
95-
typeset -g ZSH_AUTOSUGGEST_ASYNC_PTY_NAME=zsh_autosuggest_pty

src/features.zsh

-19
This file was deleted.

src/start.zsh

-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ _zsh_autosuggest_start() {
1414
# zsh-syntax-highlighting widgets. This also allows modifications
1515
# to the widget list variables to take effect on the next precmd.
1616
add-zsh-hook precmd _zsh_autosuggest_bind_widgets
17-
18-
if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then
19-
_zsh_autosuggest_async_start
20-
fi
2117
}
2218

2319
# Start the autosuggestion widgets on the next precmd

src/widgets.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ _zsh_autosuggest_modify() {
9595

9696
# Fetch a new suggestion based on what's currently in the buffer
9797
_zsh_autosuggest_fetch() {
98-
if zpty -t "$ZSH_AUTOSUGGEST_ASYNC_PTY_NAME" &>/dev/null; then
98+
if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then
9999
_zsh_autosuggest_async_request "$BUFFER"
100100
else
101101
local suggestion

0 commit comments

Comments
 (0)