2016-02-06 06:14:08 +08:00
|
|
|
# Fish-like fast/unobtrusive autosuggestions for zsh.
|
2016-02-26 04:04:32 +08:00
|
|
|
# https://github.com/zsh-users/zsh-autosuggestions
|
2018-05-22 01:38:41 +08:00
|
|
|
# v0.4.3
|
2016-02-06 06:14:08 +08:00
|
|
|
# Copyright (c) 2013 Thiago de Arruda
|
2018-05-22 00:52:41 +08:00
|
|
|
# Copyright (c) 2016-2018 Eric Freese
|
2016-02-06 06:14:08 +08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-07-20 11:04:18 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Setup #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
|
|
|
|
# Precmd hooks for initializing the library and starting pty's
|
|
|
|
autoload -Uz add-zsh-hook
|
|
|
|
|
|
|
|
# Asynchronous suggestions are generated in a pty
|
|
|
|
zmodload zsh/zpty
|
|
|
|
|
2016-02-14 15:29:43 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Global Configuration Variables #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-06 06:14:08 +08:00
|
|
|
|
|
|
|
# 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
|
2018-10-16 05:37:02 +08:00
|
|
|
: ${ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'}
|
2016-02-06 06:14:08 +08:00
|
|
|
|
|
|
|
# Prefix to use when saving original versions of bound widgets
|
2018-10-16 05:37:02 +08:00
|
|
|
: ${ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-}
|
2016-02-06 06:14:08 +08:00
|
|
|
|
2018-11-25 00:56:30 +08:00
|
|
|
# Strategies to use to fetch a suggestion
|
|
|
|
# Will try each strategy in order until a suggestion is returned
|
2018-10-16 05:37:02 +08:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_STRATEGY} )) && ZSH_AUTOSUGGEST_STRATEGY=(history)
|
2016-03-02 03:45:55 +08:00
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Widgets that clear the suggestion
|
2018-10-16 05:37:02 +08:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_CLEAR_WIDGETS} )) && ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
2016-02-06 06:14:08 +08:00
|
|
|
history-search-forward
|
|
|
|
history-search-backward
|
|
|
|
history-beginning-search-forward
|
|
|
|
history-beginning-search-backward
|
2016-03-02 04:13:01 +08:00
|
|
|
history-substring-search-up
|
|
|
|
history-substring-search-down
|
2018-03-24 05:04:21 +08:00
|
|
|
up-line-or-beginning-search
|
|
|
|
down-line-or-beginning-search
|
2016-02-06 06:14:08 +08:00
|
|
|
up-line-or-history
|
|
|
|
down-line-or-history
|
|
|
|
accept-line
|
|
|
|
)
|
|
|
|
|
|
|
|
# Widgets that accept the entire suggestion
|
2018-10-16 05:37:02 +08:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_ACCEPT_WIDGETS} )) && ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
2016-02-06 06:14:08 +08:00
|
|
|
forward-char
|
|
|
|
end-of-line
|
|
|
|
vi-forward-char
|
|
|
|
vi-end-of-line
|
2016-04-16 03:33:35 +08:00
|
|
|
vi-add-eol
|
2016-02-06 06:14:08 +08:00
|
|
|
)
|
|
|
|
|
2016-02-20 21:52:21 +08:00
|
|
|
# Widgets that accept the entire suggestion and execute it
|
2018-10-16 05:37:02 +08:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_EXECUTE_WIDGETS} )) && ZSH_AUTOSUGGEST_EXECUTE_WIDGETS=(
|
2016-02-20 21:52:21 +08:00
|
|
|
)
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Widgets that accept the suggestion as far as the cursor moves
|
2018-10-16 05:37:02 +08:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS} )) && ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
|
2016-02-06 06:14:08 +08:00
|
|
|
forward-word
|
2017-05-21 04:00:43 +08:00
|
|
|
emacs-forward-word
|
2016-02-06 06:14:08 +08:00
|
|
|
vi-forward-word
|
|
|
|
vi-forward-word-end
|
|
|
|
vi-forward-blank-word
|
|
|
|
vi-forward-blank-word-end
|
2018-05-15 00:59:08 +08:00
|
|
|
vi-find-next-char
|
|
|
|
vi-find-next-char-skip
|
2016-02-06 06:14:08 +08:00
|
|
|
)
|
|
|
|
|
2016-08-01 09:35:30 +08:00
|
|
|
# Widgets that should be ignored (globbing supported but must be escaped)
|
2018-10-16 05:37:02 +08:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_IGNORE_WIDGETS} )) && ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(
|
2016-08-01 09:35:30 +08:00
|
|
|
orig-\*
|
|
|
|
beep
|
|
|
|
run-help
|
|
|
|
set-local-history
|
|
|
|
which-command
|
|
|
|
yank
|
2017-12-25 05:46:20 +08:00
|
|
|
yank-pop
|
2016-08-01 09:35:30 +08:00
|
|
|
)
|
|
|
|
|
2018-10-16 05:37:02 +08:00
|
|
|
# Max size of buffer to trigger autosuggestion. Leave null for no upper bound.
|
|
|
|
: ${ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=}
|
2016-07-18 17:56:21 +08:00
|
|
|
|
2017-01-30 01:39:07 +08:00
|
|
|
# Pty name for calculating autosuggestions asynchronously
|
2018-10-16 05:37:02 +08:00
|
|
|
: ${ZSH_AUTOSUGGEST_ASYNC_PTY_NAME=zsh_autosuggest_pty}
|
2017-01-30 01:39:07 +08:00
|
|
|
|
2017-12-06 22:51:06 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Utility Functions #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
|
|
|
|
_zsh_autosuggest_escape_command() {
|
|
|
|
setopt localoptions EXTENDED_GLOB
|
|
|
|
|
|
|
|
# Escape special chars in the string (requires EXTENDED_GLOB)
|
|
|
|
echo -E "${1//(#m)[\"\'\\()\[\]|*?~]/\\$MATCH}"
|
|
|
|
}
|
|
|
|
|
2017-01-27 07:35:33 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Feature Detection #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
|
2017-02-19 02:25:27 +08:00
|
|
|
_zsh_autosuggest_feature_detect_zpty_returns_fd() {
|
2017-01-27 07:35:33 +08:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD
|
|
|
|
typeset -h REPLY
|
|
|
|
|
2017-02-18 13:07:48 +08:00
|
|
|
zpty zsh_autosuggest_feature_detect '{ zshexit() { kill -KILL $$; sleep 1 } }'
|
2017-01-27 07:35:33 +08:00
|
|
|
|
|
|
|
if (( REPLY )); then
|
|
|
|
_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD=1
|
|
|
|
else
|
|
|
|
_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD=0
|
|
|
|
fi
|
|
|
|
|
2017-02-18 13:07:48 +08:00
|
|
|
zpty -d zsh_autosuggest_feature_detect
|
2017-01-27 07:35:33 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 15:29:43 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Widget Helpers #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-06 06:14:08 +08:00
|
|
|
|
2017-03-01 02:14:16 +08:00
|
|
|
_zsh_autosuggest_incr_bind_count() {
|
|
|
|
if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then
|
|
|
|
((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]++))
|
|
|
|
else
|
|
|
|
_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=1
|
|
|
|
fi
|
|
|
|
|
2017-09-27 21:55:12 +08:00
|
|
|
typeset -gi bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]
|
2017-03-01 02:14:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_get_bind_count() {
|
|
|
|
if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then
|
2017-09-27 21:55:12 +08:00
|
|
|
typeset -gi bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]
|
2017-03-01 02:14:16 +08:00
|
|
|
else
|
2017-09-27 21:55:12 +08:00
|
|
|
typeset -gi bind_count=0
|
2017-03-01 02:14:16 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
|
|
|
|
_zsh_autosuggest_bind_widget() {
|
2017-03-01 02:14:16 +08:00
|
|
|
typeset -gA _ZSH_AUTOSUGGEST_BIND_COUNTS
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
local widget=$1
|
2016-02-14 23:54:34 +08:00
|
|
|
local autosuggest_action=$2
|
2016-02-06 06:14:08 +08:00
|
|
|
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
|
|
|
|
|
2017-03-01 02:14:16 +08:00
|
|
|
local -i bind_count
|
|
|
|
|
2016-02-14 23:54:34 +08:00
|
|
|
# Save a reference to the original widget
|
2016-02-06 06:14:08 +08:00
|
|
|
case $widgets[$widget] in
|
|
|
|
# Already bound
|
2016-02-15 23:31:00 +08:00
|
|
|
user:_zsh_autosuggest_(bound|orig)_*);;
|
2016-02-06 06:14:08 +08:00
|
|
|
|
|
|
|
# User-defined widget
|
|
|
|
user:*)
|
2017-03-01 02:14:16 +08:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
|
|
|
zle -N $prefix${bind_count}-$widget ${widgets[$widget]#*:}
|
2016-02-06 06:14:08 +08:00
|
|
|
;;
|
|
|
|
|
|
|
|
# Built-in widget
|
|
|
|
builtin)
|
2017-03-01 02:14:16 +08:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
2016-04-26 04:23:30 +08:00
|
|
|
eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
|
2017-03-01 02:14:16 +08:00
|
|
|
zle -N $prefix${bind_count}-$widget _zsh_autosuggest_orig_$widget
|
2016-02-06 06:14:08 +08:00
|
|
|
;;
|
|
|
|
|
|
|
|
# Completion widget
|
|
|
|
completion:*)
|
2017-03-01 02:14:16 +08:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
|
|
|
eval "zle -C $prefix${bind_count}-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
|
2016-02-06 06:14:08 +08:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2017-03-01 02:14:16 +08:00
|
|
|
_zsh_autosuggest_get_bind_count $widget
|
|
|
|
|
2016-02-15 23:31:00 +08:00
|
|
|
# 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`).
|
2017-03-01 02:14:16 +08:00
|
|
|
eval "_zsh_autosuggest_bound_${bind_count}_${(q)widget}() {
|
|
|
|
_zsh_autosuggest_widget_$autosuggest_action $prefix$bind_count-${(q)widget} \$@
|
2016-02-15 23:31:00 +08:00
|
|
|
}"
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Create the bound widget
|
2018-06-11 12:39:58 +08:00
|
|
|
zle -N -- $widget _zsh_autosuggest_bound_${bind_count}_$widget
|
2016-02-06 06:14:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Map all configured widgets to the right autosuggest widgets
|
|
|
|
_zsh_autosuggest_bind_widgets() {
|
2018-07-14 12:16:53 +08:00
|
|
|
emulate -L zsh
|
|
|
|
|
|
|
|
local widget
|
2016-08-01 09:35:30 +08:00
|
|
|
local ignore_widgets
|
|
|
|
|
|
|
|
ignore_widgets=(
|
|
|
|
.\*
|
|
|
|
_\*
|
2016-11-05 13:40:14 +08:00
|
|
|
zle-\*
|
2016-08-01 09:35:30 +08:00
|
|
|
autosuggest-\*
|
|
|
|
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
|
|
|
|
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
|
|
|
|
)
|
2016-02-06 06:14:08 +08:00
|
|
|
|
|
|
|
# Find every widget we might want to bind and bind it appropriately
|
2016-08-01 09:35:30 +08:00
|
|
|
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ -n ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 23:54:34 +08:00
|
|
|
_zsh_autosuggest_bind_widget $widget clear
|
2017-07-18 04:45:48 +08:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 23:54:34 +08:00
|
|
|
_zsh_autosuggest_bind_widget $widget accept
|
2017-07-18 04:45:48 +08:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]]; then
|
2016-02-20 21:52:21 +08:00
|
|
|
_zsh_autosuggest_bind_widget $widget execute
|
2017-07-18 04:45:48 +08:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 23:54:34 +08:00
|
|
|
_zsh_autosuggest_bind_widget $widget partial_accept
|
2016-02-06 06:14:08 +08:00
|
|
|
else
|
2016-02-14 23:54:34 +08:00
|
|
|
# Assume any unspecified widget might modify the buffer
|
|
|
|
_zsh_autosuggest_bind_widget $widget modify
|
2016-02-06 06:14:08 +08:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-02-15 23:31:00 +08:00
|
|
|
# Given the name of an original widget and args, invoke it, if it exists
|
2016-02-06 06:14:08 +08:00
|
|
|
_zsh_autosuggest_invoke_original_widget() {
|
2016-02-18 04:41:37 +08:00
|
|
|
# Do nothing unless called with at least one arg
|
2018-01-17 04:08:38 +08:00
|
|
|
(( $# )) || return 0
|
2016-02-18 04:41:37 +08:00
|
|
|
|
2016-03-06 12:03:14 +08:00
|
|
|
local original_widget_name="$1"
|
2016-02-15 23:31:00 +08:00
|
|
|
|
|
|
|
shift
|
2016-02-06 06:14:08 +08:00
|
|
|
|
2017-07-18 04:45:48 +08:00
|
|
|
if (( ${+widgets[$original_widget_name]} )); then
|
2016-02-24 09:13:03 +08:00
|
|
|
zle $original_widget_name -- $@
|
2016-02-06 06:14:08 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:29:43 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Highlighting #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-06 06:14:08 +08:00
|
|
|
|
|
|
|
# If there was a highlight, remove it
|
|
|
|
_zsh_autosuggest_highlight_reset() {
|
2016-03-15 11:50:04 +08:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
|
|
|
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ -n "$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT" ]]; then
|
2016-02-06 06:14:08 +08:00
|
|
|
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() {
|
2016-03-15 11:50:04 +08:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
|
|
|
|
2017-07-18 04:45:48 +08:00
|
|
|
if (( $#POSTDISPLAY )); then
|
2017-09-27 21:55:12 +08:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
|
2016-02-25 04:59:49 +08:00
|
|
|
region_highlight+=("$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT")
|
2016-02-06 06:14:08 +08:00
|
|
|
else
|
|
|
|
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:29:43 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Autosuggest Widget Implementations #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-06 06:14:08 +08:00
|
|
|
|
2017-03-04 02:59:30 +08:00
|
|
|
# Disable suggestions
|
|
|
|
_zsh_autosuggest_disable() {
|
|
|
|
typeset -g _ZSH_AUTOSUGGEST_DISABLED
|
|
|
|
_zsh_autosuggest_clear
|
|
|
|
}
|
|
|
|
|
|
|
|
# Enable suggestions
|
|
|
|
_zsh_autosuggest_enable() {
|
|
|
|
unset _ZSH_AUTOSUGGEST_DISABLED
|
2017-03-05 06:04:04 +08:00
|
|
|
|
2017-07-18 04:45:48 +08:00
|
|
|
if (( $#BUFFER )); then
|
2017-03-05 06:04:04 +08:00
|
|
|
_zsh_autosuggest_fetch
|
|
|
|
fi
|
2017-03-04 02:59:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Toggle suggestions (enable/disable)
|
|
|
|
_zsh_autosuggest_toggle() {
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]]; then
|
2017-03-04 02:59:30 +08:00
|
|
|
_zsh_autosuggest_enable
|
|
|
|
else
|
|
|
|
_zsh_autosuggest_disable
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Clear the suggestion
|
|
|
|
_zsh_autosuggest_clear() {
|
|
|
|
# Remove the suggestion
|
|
|
|
unset POSTDISPLAY
|
|
|
|
|
2016-02-14 14:46:34 +08:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-02-06 06:14:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Modify the buffer and get a new suggestion
|
|
|
|
_zsh_autosuggest_modify() {
|
2018-07-14 12:16:53 +08:00
|
|
|
emulate -L zsh
|
|
|
|
|
2016-04-07 07:10:34 +08:00
|
|
|
local -i retval
|
|
|
|
|
2017-09-28 05:04:42 +08:00
|
|
|
# Only available in zsh >= 5.4
|
2017-03-01 02:18:21 +08:00
|
|
|
local -i KEYS_QUEUED_COUNT
|
|
|
|
|
2016-08-01 10:10:22 +08:00
|
|
|
# Save the contents of the buffer/postdisplay
|
|
|
|
local orig_buffer="$BUFFER"
|
|
|
|
local orig_postdisplay="$POSTDISPLAY"
|
|
|
|
|
2016-07-20 11:04:18 +08:00
|
|
|
# Clear suggestion while waiting for next one
|
2016-04-26 04:42:09 +08:00
|
|
|
unset POSTDISPLAY
|
|
|
|
|
2016-08-01 10:10:22 +08:00
|
|
|
# Original widget may modify the buffer
|
2016-02-14 14:46:34 +08:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-04-07 07:10:34 +08:00
|
|
|
retval=$?
|
2016-02-06 06:14:08 +08:00
|
|
|
|
2017-03-01 02:18:21 +08:00
|
|
|
# Don't fetch a new suggestion if there's more input to be read immediately
|
2017-07-18 04:45:48 +08:00
|
|
|
if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then
|
2018-05-16 01:32:46 +08:00
|
|
|
POSTDISPLAY="$orig_postdisplay"
|
2017-03-01 02:18:21 +08:00
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
|
2017-01-25 15:00:13 +08:00
|
|
|
# Optimize if manually typing in the suggestion
|
2017-07-18 04:45:48 +08:00
|
|
|
if (( $#BUFFER > $#orig_buffer )); then
|
2017-01-25 15:00:13 +08:00
|
|
|
local added=${BUFFER#$orig_buffer}
|
|
|
|
|
|
|
|
# If the string added matches the beginning of the postdisplay
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ "$added" = "${orig_postdisplay:0:$#added}" ]]; then
|
2017-01-25 15:00:13 +08:00
|
|
|
POSTDISPLAY="${orig_postdisplay:$#added}"
|
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2016-08-01 10:10:22 +08:00
|
|
|
# Don't fetch a new suggestion if the buffer hasn't changed
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ "$BUFFER" = "$orig_buffer" ]]; then
|
2016-08-01 10:10:22 +08:00
|
|
|
POSTDISPLAY="$orig_postdisplay"
|
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
|
2017-03-04 02:59:30 +08:00
|
|
|
# Bail out if suggestions are disabled
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]]; then
|
2017-03-04 02:59:30 +08:00
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Get a new suggestion if the buffer is not empty after modification
|
2017-07-18 04:45:48 +08:00
|
|
|
if (( $#BUFFER > 0 )); then
|
|
|
|
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
|
2017-01-28 06:18:26 +08:00
|
|
|
_zsh_autosuggest_fetch
|
2016-07-15 16:39:33 +08:00
|
|
|
fi
|
2016-02-06 06:14:08 +08:00
|
|
|
fi
|
|
|
|
|
2016-04-07 07:10:34 +08:00
|
|
|
return $retval
|
2016-02-06 06:14:08 +08:00
|
|
|
}
|
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
# Fetch a new suggestion based on what's currently in the buffer
|
|
|
|
_zsh_autosuggest_fetch() {
|
2017-01-30 01:39:07 +08:00
|
|
|
if zpty -t "$ZSH_AUTOSUGGEST_ASYNC_PTY_NAME" &>/dev/null; then
|
2017-01-28 06:18:26 +08:00
|
|
|
_zsh_autosuggest_async_request "$BUFFER"
|
|
|
|
else
|
|
|
|
local suggestion
|
2018-11-25 00:56:30 +08:00
|
|
|
_zsh_autosuggest_fetch_suggestion "$BUFFER"
|
2017-01-28 06:18:26 +08:00
|
|
|
_zsh_autosuggest_suggest "$suggestion"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Offer a suggestion
|
|
|
|
_zsh_autosuggest_suggest() {
|
2018-07-14 12:16:53 +08:00
|
|
|
emulate -L zsh
|
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
local suggestion="$1"
|
|
|
|
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ -n "$suggestion" ]] && (( $#BUFFER )); then
|
2017-01-28 06:18:26 +08:00
|
|
|
POSTDISPLAY="${suggestion#$BUFFER}"
|
|
|
|
else
|
|
|
|
unset POSTDISPLAY
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Accept the entire suggestion
|
|
|
|
_zsh_autosuggest_accept() {
|
2016-03-15 11:41:14 +08:00
|
|
|
local -i max_cursor_pos=$#BUFFER
|
|
|
|
|
|
|
|
# When vicmd keymap is active, the cursor can't move all the way
|
|
|
|
# to the end of the buffer
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
2016-03-15 11:41:14 +08:00
|
|
|
max_cursor_pos=$((max_cursor_pos - 1))
|
|
|
|
fi
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Only accept if the cursor is at the end of the buffer
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ $CURSOR = $max_cursor_pos ]]; then
|
2016-02-06 06:14:08 +08:00
|
|
|
# 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
|
|
|
|
|
2016-02-14 14:46:34 +08:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-02-06 06:14:08 +08:00
|
|
|
}
|
|
|
|
|
2016-02-20 21:52:21 +08:00
|
|
|
# Accept the entire suggestion and execute it
|
|
|
|
_zsh_autosuggest_execute() {
|
|
|
|
# Add the suggestion to the buffer
|
|
|
|
BUFFER="$BUFFER$POSTDISPLAY"
|
|
|
|
|
|
|
|
# Remove the suggestion
|
|
|
|
unset POSTDISPLAY
|
|
|
|
|
2016-02-24 01:20:13 +08:00
|
|
|
# Call the original `accept-line` to handle syntax highlighting or
|
|
|
|
# other potential custom behavior
|
|
|
|
_zsh_autosuggest_invoke_original_widget "accept-line"
|
2016-02-20 21:52:21 +08:00
|
|
|
}
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Partially accept the suggestion
|
|
|
|
_zsh_autosuggest_partial_accept() {
|
2018-05-15 00:59:08 +08:00
|
|
|
local -i retval cursor_loc
|
2016-04-07 07:10:34 +08:00
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# Save the contents of the buffer so we can restore later if needed
|
2016-03-06 12:03:14 +08:00
|
|
|
local original_buffer="$BUFFER"
|
2016-02-06 06:14:08 +08:00
|
|
|
|
|
|
|
# Temporarily accept the suggestion.
|
|
|
|
BUFFER="$BUFFER$POSTDISPLAY"
|
|
|
|
|
|
|
|
# Original widget moves the cursor
|
2016-02-14 14:46:34 +08:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-04-07 07:10:34 +08:00
|
|
|
retval=$?
|
2016-02-06 06:14:08 +08:00
|
|
|
|
2018-05-15 00:59:08 +08:00
|
|
|
# Normalize cursor location across vi/emacs modes
|
|
|
|
cursor_loc=$CURSOR
|
|
|
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
|
|
|
cursor_loc=$((cursor_loc + 1))
|
|
|
|
fi
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
# If we've moved past the end of the original buffer
|
2018-05-15 00:59:08 +08:00
|
|
|
if (( $cursor_loc > $#original_buffer )); then
|
2016-02-06 06:14:08 +08:00
|
|
|
# Set POSTDISPLAY to text right of the cursor
|
2018-05-15 00:59:08 +08:00
|
|
|
POSTDISPLAY="${BUFFER[$(($cursor_loc + 1)),$#BUFFER]}"
|
2016-02-06 06:14:08 +08:00
|
|
|
|
|
|
|
# Clip the buffer at the cursor
|
2018-05-15 00:59:08 +08:00
|
|
|
BUFFER="${BUFFER[1,$cursor_loc]}"
|
2016-02-06 06:14:08 +08:00
|
|
|
else
|
|
|
|
# Restore the original buffer
|
2016-03-06 12:03:14 +08:00
|
|
|
BUFFER="$original_buffer"
|
2016-02-06 06:14:08 +08:00
|
|
|
fi
|
2016-04-07 07:10:34 +08:00
|
|
|
|
|
|
|
return $retval
|
2016-02-06 06:14:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-04 02:59:30 +08:00
|
|
|
for action in clear modify fetch suggest accept partial_accept execute enable disable toggle; do
|
2016-02-14 23:54:34 +08:00
|
|
|
eval "_zsh_autosuggest_widget_$action() {
|
2016-04-07 07:10:34 +08:00
|
|
|
local -i retval
|
|
|
|
|
2016-02-14 23:54:34 +08:00
|
|
|
_zsh_autosuggest_highlight_reset
|
2016-04-07 07:10:34 +08:00
|
|
|
|
2016-02-14 23:54:34 +08:00
|
|
|
_zsh_autosuggest_$action \$@
|
2016-04-07 07:10:34 +08:00
|
|
|
retval=\$?
|
|
|
|
|
2016-02-14 23:54:34 +08:00
|
|
|
_zsh_autosuggest_highlight_apply
|
2016-04-07 07:10:34 +08:00
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
zle -R
|
|
|
|
|
2016-04-07 07:10:34 +08:00
|
|
|
return \$retval
|
2016-02-14 23:54:34 +08:00
|
|
|
}"
|
|
|
|
done
|
2016-02-06 06:14:08 +08:00
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
zle -N autosuggest-fetch _zsh_autosuggest_widget_fetch
|
|
|
|
zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest
|
2016-02-06 06:14:08 +08:00
|
|
|
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
2016-02-07 23:58:09 +08:00
|
|
|
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
2016-02-20 21:52:21 +08:00
|
|
|
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
2017-03-04 02:59:30 +08:00
|
|
|
zle -N autosuggest-enable _zsh_autosuggest_widget_enable
|
|
|
|
zle -N autosuggest-disable _zsh_autosuggest_widget_disable
|
|
|
|
zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle
|
2016-02-06 06:14:08 +08:00
|
|
|
|
2016-03-02 03:45:55 +08:00
|
|
|
#--------------------------------------------------------------------#
|
2018-11-20 06:30:35 +08:00
|
|
|
# History Suggestion Strategy #
|
2016-03-02 03:45:55 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Suggests the most recent history item that matches the given
|
|
|
|
# prefix.
|
|
|
|
#
|
|
|
|
|
2018-11-20 06:30:35 +08:00
|
|
|
_zsh_autosuggest_strategy_history() {
|
2017-12-06 22:51:06 +08:00
|
|
|
# Reset options to defaults and enable LOCAL_OPTIONS
|
|
|
|
emulate -L zsh
|
|
|
|
|
|
|
|
# Enable globbing flags so that we can use (#m)
|
|
|
|
setopt EXTENDED_GLOB
|
|
|
|
|
|
|
|
# Escape backslashes and all of the glob operators so we can use
|
|
|
|
# this string as a pattern to search the $history associative array.
|
|
|
|
# - (#m) globbing flag enables setting references for match data
|
|
|
|
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
|
|
|
|
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
|
2017-01-25 14:04:07 +08:00
|
|
|
|
2017-02-17 10:18:03 +08:00
|
|
|
# Get the history items that match
|
|
|
|
# - (r) subscript flag makes the pattern match on values
|
2017-12-06 22:51:06 +08:00
|
|
|
typeset -g suggestion="${history[(r)${prefix}*]}"
|
2016-03-02 03:45:55 +08:00
|
|
|
}
|
|
|
|
|
2016-03-02 05:58:57 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Match Previous Command Suggestion Strategy #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Suggests the most recent history item that matches the given
|
2016-03-05 10:23:32 +08:00
|
|
|
# prefix and whose preceding history item also matches the most
|
2016-03-02 05:58:57 +08:00
|
|
|
# recently executed command.
|
|
|
|
#
|
2016-03-05 10:23:32 +08:00
|
|
|
# For example, suppose your history has the following entries:
|
|
|
|
# - pwd
|
|
|
|
# - ls foo
|
|
|
|
# - ls bar
|
|
|
|
# - pwd
|
|
|
|
#
|
|
|
|
# Given the history list above, when you type 'ls', the suggestion
|
|
|
|
# will be 'ls foo' rather than 'ls bar' because your most recently
|
|
|
|
# executed command (pwd) was previously followed by 'ls foo'.
|
2016-03-02 05:58:57 +08:00
|
|
|
#
|
2016-05-29 02:05:11 +08:00
|
|
|
# Note that this strategy won't work as expected with ZSH options that don't
|
|
|
|
# preserve the history order such as `HIST_IGNORE_ALL_DUPS` or
|
|
|
|
# `HIST_EXPIRE_DUPS_FIRST`.
|
2016-03-02 05:58:57 +08:00
|
|
|
|
|
|
|
_zsh_autosuggest_strategy_match_prev_cmd() {
|
2017-12-06 23:09:14 +08:00
|
|
|
# Reset options to defaults and enable LOCAL_OPTIONS
|
|
|
|
emulate -L zsh
|
|
|
|
|
|
|
|
# Enable globbing flags so that we can use (#m)
|
|
|
|
setopt EXTENDED_GLOB
|
|
|
|
|
2017-12-06 22:51:06 +08:00
|
|
|
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
|
|
|
|
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
|
2016-03-02 05:58:57 +08:00
|
|
|
|
|
|
|
# Get all history event numbers that correspond to history
|
|
|
|
# entries that match pattern $prefix*
|
|
|
|
local history_match_keys
|
2017-12-06 22:51:06 +08:00
|
|
|
history_match_keys=(${(k)history[(R)$prefix*]})
|
2016-03-02 05:58:57 +08:00
|
|
|
|
|
|
|
# By default we use the first history number (most recent history entry)
|
|
|
|
local histkey="${history_match_keys[1]}"
|
|
|
|
|
|
|
|
# Get the previously executed command
|
2017-12-06 22:51:06 +08:00
|
|
|
local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")"
|
2016-03-02 05:58:57 +08:00
|
|
|
|
|
|
|
# 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
|
2016-03-06 12:03:14 +08:00
|
|
|
if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then
|
2016-03-02 05:58:57 +08:00
|
|
|
histkey="$key"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
# Give back the matched history entry
|
2017-09-27 21:55:12 +08:00
|
|
|
typeset -g suggestion="$history[$histkey]"
|
2016-03-02 05:58:57 +08:00
|
|
|
}
|
|
|
|
|
2018-11-25 00:56:30 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Fetch Suggestion #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Loops through all specified strategies and returns a suggestion
|
|
|
|
# from the first strategy to provide one.
|
|
|
|
#
|
|
|
|
|
|
|
|
_zsh_autosuggest_fetch_suggestion() {
|
|
|
|
typeset -g suggestion
|
|
|
|
local -a strategies
|
|
|
|
|
|
|
|
# Ensure we are working with an array
|
|
|
|
strategies=(${=ZSH_AUTOSUGGEST_STRATEGY})
|
|
|
|
|
|
|
|
for strategy in $strategies; do
|
|
|
|
# Try to get a suggestion from this strategy
|
|
|
|
_zsh_autosuggest_strategy_$strategy "$1"
|
|
|
|
|
|
|
|
# Break once we've found a suggestion
|
|
|
|
[[ -n "$suggestion" ]] && break
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-07-20 11:04:18 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Async #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
|
2017-02-17 10:18:03 +08:00
|
|
|
# Zpty process is spawned running this function
|
2017-01-28 06:18:26 +08:00
|
|
|
_zsh_autosuggest_async_server() {
|
2017-01-25 10:58:25 +08:00
|
|
|
emulate -R zsh
|
|
|
|
|
2017-02-17 10:18:03 +08:00
|
|
|
# There is a bug in zpty module (fixed in zsh/master) by which a
|
|
|
|
# zpty that exits will kill all zpty processes that were forked
|
|
|
|
# before it. Here we set up a zsh exit hook to SIGKILL the zpty
|
|
|
|
# process immediately, before it has a chance to kill any other
|
|
|
|
# zpty processes.
|
|
|
|
zshexit() {
|
|
|
|
kill -KILL $$
|
|
|
|
sleep 1 # Block for long enough for the signal to come through
|
|
|
|
}
|
|
|
|
|
2018-05-27 04:01:03 +08:00
|
|
|
# Don't add any extra carriage returns
|
2017-01-25 13:27:09 +08:00
|
|
|
stty -onlcr
|
|
|
|
|
2018-05-27 04:01:03 +08:00
|
|
|
# Don't translate carriage returns to newlines
|
|
|
|
stty -icrnl
|
|
|
|
|
2017-02-17 10:27:32 +08:00
|
|
|
# Silence any error messages
|
|
|
|
exec 2>/dev/null
|
|
|
|
|
2017-01-25 15:00:53 +08:00
|
|
|
local last_pid
|
2017-01-25 13:48:30 +08:00
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
while IFS='' read -r -d $'\0' query; do
|
2016-07-20 11:04:18 +08:00
|
|
|
# Kill last bg process
|
2017-01-25 15:00:53 +08:00
|
|
|
kill -KILL $last_pid &>/dev/null
|
2016-07-20 11:04:18 +08:00
|
|
|
|
|
|
|
# Run suggestion search in the background
|
2017-01-28 06:18:26 +08:00
|
|
|
(
|
|
|
|
local suggestion
|
2018-11-25 00:56:30 +08:00
|
|
|
_zsh_autosuggest_fetch_suggestion "$query"
|
2017-01-28 06:18:26 +08:00
|
|
|
echo -n -E "$suggestion"$'\0'
|
|
|
|
) &
|
2017-01-25 15:00:53 +08:00
|
|
|
|
|
|
|
last_pid=$!
|
2016-07-20 11:04:18 +08:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
_zsh_autosuggest_async_request() {
|
2017-02-17 10:18:03 +08:00
|
|
|
# Write the query to the zpty process to fetch a suggestion
|
2017-01-30 01:39:07 +08:00
|
|
|
zpty -w -n $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME "${1}"$'\0'
|
2017-01-28 06:18:26 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:53:26 +08:00
|
|
|
# Called when new data is ready to be read from the pty
|
|
|
|
# First arg will be fd ready for reading
|
|
|
|
# Second arg will be passed in case of error
|
2017-01-28 06:18:26 +08:00
|
|
|
_zsh_autosuggest_async_response() {
|
2017-02-17 10:18:03 +08:00
|
|
|
setopt LOCAL_OPTIONS EXTENDED_GLOB
|
|
|
|
|
2017-01-25 10:58:52 +08:00
|
|
|
local suggestion
|
|
|
|
|
2017-01-30 01:39:07 +08:00
|
|
|
zpty -rt $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME suggestion '*'$'\0' 2>/dev/null
|
2017-02-19 07:51:53 +08:00
|
|
|
zle autosuggest-suggest -- "${suggestion%%$'\0'##}"
|
2016-07-20 11:04:18 +08:00
|
|
|
}
|
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
_zsh_autosuggest_async_pty_create() {
|
2017-01-27 07:50:19 +08:00
|
|
|
# With newer versions of zsh, REPLY stores the fd to read from
|
2016-07-20 11:04:18 +08:00
|
|
|
typeset -h REPLY
|
2017-01-25 10:53:26 +08:00
|
|
|
|
2017-01-27 07:50:19 +08:00
|
|
|
# If we won't get a fd back from zpty, try to guess it
|
2017-07-18 04:45:48 +08:00
|
|
|
if (( ! $_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD )); then
|
2017-01-27 07:50:19 +08:00
|
|
|
integer -l zptyfd
|
|
|
|
exec {zptyfd}>&1 # Open a new file descriptor (above 10).
|
|
|
|
exec {zptyfd}>&- # Close it so it's free to be used by zpty.
|
|
|
|
fi
|
|
|
|
|
2017-02-17 10:18:03 +08:00
|
|
|
# Fork a zpty process running the server function
|
2018-03-28 04:51:37 +08:00
|
|
|
zpty -b $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME _zsh_autosuggest_async_server
|
2017-01-25 10:53:26 +08:00
|
|
|
|
2017-01-27 08:00:56 +08:00
|
|
|
# Store the fd so we can remove the handler later
|
2017-01-27 07:50:19 +08:00
|
|
|
if (( REPLY )); then
|
|
|
|
_ZSH_AUTOSUGGEST_PTY_FD=$REPLY
|
|
|
|
else
|
|
|
|
_ZSH_AUTOSUGGEST_PTY_FD=$zptyfd
|
|
|
|
fi
|
|
|
|
|
2017-02-17 10:18:03 +08:00
|
|
|
# Set up input handler from the zpty
|
2017-01-28 06:18:26 +08:00
|
|
|
zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_response
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_async_pty_destroy() {
|
2018-05-16 03:55:37 +08:00
|
|
|
# Remove the input handler
|
|
|
|
zle -F $_ZSH_AUTOSUGGEST_PTY_FD &>/dev/null
|
2017-01-28 06:18:26 +08:00
|
|
|
|
2018-05-16 03:55:37 +08:00
|
|
|
# Destroy the zpty
|
|
|
|
zpty -d $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME &>/dev/null
|
2017-01-28 06:18:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_async_pty_recreate() {
|
|
|
|
_zsh_autosuggest_async_pty_destroy
|
|
|
|
_zsh_autosuggest_async_pty_create
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_async_start() {
|
|
|
|
typeset -g _ZSH_AUTOSUGGEST_PTY_FD
|
|
|
|
|
2017-02-19 02:25:27 +08:00
|
|
|
_zsh_autosuggest_feature_detect_zpty_returns_fd
|
2017-02-18 13:40:23 +08:00
|
|
|
_zsh_autosuggest_async_pty_recreate
|
2017-01-28 06:18:26 +08:00
|
|
|
|
|
|
|
# We recreate the pty to get a fresh list of history events
|
|
|
|
add-zsh-hook precmd _zsh_autosuggest_async_pty_recreate
|
2016-07-20 11:04:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 15:29:43 +08:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Start #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-06 06:14:08 +08:00
|
|
|
|
|
|
|
# Start the autosuggestion widgets
|
2016-02-08 05:21:57 +08:00
|
|
|
_zsh_autosuggest_start() {
|
2017-02-19 01:47:53 +08:00
|
|
|
add-zsh-hook -d precmd _zsh_autosuggest_start
|
|
|
|
|
2016-02-06 06:14:08 +08:00
|
|
|
_zsh_autosuggest_bind_widgets
|
2017-01-27 07:37:42 +08:00
|
|
|
|
2017-02-19 01:47:53 +08:00
|
|
|
# Re-bind widgets on every precmd to ensure we wrap other wrappers.
|
|
|
|
# Specifically, highlighting breaks if our widgets are wrapped by
|
|
|
|
# zsh-syntax-highlighting widgets. This also allows modifications
|
|
|
|
# to the widget list variables to take effect on the next precmd.
|
|
|
|
add-zsh-hook precmd _zsh_autosuggest_bind_widgets
|
|
|
|
|
2017-07-18 04:45:48 +08:00
|
|
|
if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then
|
2017-01-28 06:18:26 +08:00
|
|
|
_zsh_autosuggest_async_start
|
|
|
|
fi
|
2016-02-06 06:14:08 +08:00
|
|
|
}
|
2016-02-08 05:21:57 +08:00
|
|
|
|
2017-01-28 06:18:26 +08:00
|
|
|
# Start the autosuggestion widgets on the next precmd
|
2016-02-08 05:21:57 +08:00
|
|
|
add-zsh-hook precmd _zsh_autosuggest_start
|