comparison .zfun/zsh-autosuggestions/src/async.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 # Async #
4 #--------------------------------------------------------------------#
5
6 # Zpty process is spawned running this function
7 _zsh_autosuggest_async_server() {
8 emulate -R zsh
9
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
28
29 local last_pid
30
31 while IFS='' read -r -d $'\0' query; do
32 # Kill last bg process
33 kill -KILL $last_pid &>/dev/null
34
35 # Run suggestion search in the background
36 (
37 local suggestion
38 _zsh_autosuggest_fetch_suggestion "$query"
39 echo -n -E "$suggestion"$'\0'
40 ) &
41
42 last_pid=$!
43 done
44 }
45
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 }
50
51 # Called when new data is ready to be read from the pty
52 # First arg will be fd ready for reading
53 # Second arg will be passed in case of error
54 _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
66
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.
72 fi
73
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
109 }