2016-02-05 22:14:08 +00:00
# Fish-like fast/unobtrusive autosuggestions for zsh.
2016-02-25 20:04:32 +00:00
# https://github.com/zsh-users/zsh-autosuggestions
2016-10-17 13:45:09 +00:00
# v0.3.3
2016-02-05 22:14:08 +00:00
# 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.
2016-07-20 03:04:18 +00: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 07:29:43 +00:00
#--------------------------------------------------------------------#
# Global Configuration Variables #
#--------------------------------------------------------------------#
2016-02-05 22:14:08 +00: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
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE = 'fg=8'
# Prefix to use when saving original versions of bound widgets
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX = autosuggest-orig-
2016-07-20 03:04:18 +00:00
# Pty name for calculating autosuggestions asynchronously
ZSH_AUTOSUGGEST_PTY_NAME = zsh_autosuggest_pty
2016-03-01 19:45:55 +00:00
ZSH_AUTOSUGGEST_STRATEGY = default
2016-02-05 22:14:08 +00:00
# Widgets that clear the suggestion
ZSH_AUTOSUGGEST_CLEAR_WIDGETS = (
history-search-forward
history-search-backward
history-beginning-search-forward
history-beginning-search-backward
2016-03-01 20:13:01 +00:00
history-substring-search-up
history-substring-search-down
2016-02-05 22:14:08 +00:00
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
2016-04-15 19:33:35 +00:00
vi-add-eol
2016-02-05 22:14:08 +00:00
)
2016-02-20 13:52:21 +00:00
# Widgets that accept the entire suggestion and execute it
ZSH_AUTOSUGGEST_EXECUTE_WIDGETS = (
)
2016-02-05 22:14:08 +00:00
# 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
)
2016-08-01 01:35:30 +00:00
# Widgets that should be ignored (globbing supported but must be escaped)
ZSH_AUTOSUGGEST_IGNORE_WIDGETS = (
orig-\*
beep
run-help
set-local-history
which-command
yank
)
2016-07-18 09:56:21 +00:00
# Max size of buffer to trigger autosuggestion. Leave undefined for no upper bound.
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE =
2016-07-20 03:04:18 +00: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-26 23:35:33 +00:00
#--------------------------------------------------------------------#
# Feature Detection #
#--------------------------------------------------------------------#
_zsh_autosuggest_feature_detect( ) {
typeset -g _ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD
typeset -h REPLY
zpty $ZSH_AUTOSUGGEST_PTY_NAME :
if ( ( REPLY ) ) ; then
_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD = 1
else
_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD = 0
fi
zpty -d $ZSH_AUTOSUGGEST_PTY_NAME
}
2016-02-14 07:29:43 +00:00
#--------------------------------------------------------------------#
# Handle Deprecated Variables/Widgets #
#--------------------------------------------------------------------#
2016-02-05 22:14:08 +00:00
2016-02-14 07:32:25 +00:00
_zsh_autosuggest_deprecated_warning( ) {
>& 2 echo " zsh-autosuggestions: $@ "
}
2016-02-05 22:14:08 +00:00
_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( ) {
2016-02-25 20:04:32 +00:00
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. For more info, see the README at https://github.com/zsh-users/zsh-autosuggestions."
2016-02-14 07:32:25 +00:00
zle -D autosuggest-start
eval " zle-line-init() {
$( echo $functions [ ${ widgets [zle-line-init]#* : } ] | sed -e 's/zle autosuggest-start//g' )
} "
2016-02-05 22:14:08 +00:00
}
zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget
2016-02-14 07:29:43 +00:00
#--------------------------------------------------------------------#
# Widget Helpers #
#--------------------------------------------------------------------#
2016-02-05 22:14:08 +00:00
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
_zsh_autosuggest_bind_widget( ) {
local widget = $1
2016-02-14 15:54:34 +00:00
local autosuggest_action = $2
2016-02-05 22:14:08 +00:00
local prefix = $ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
2016-02-14 15:54:34 +00:00
# Save a reference to the original widget
2016-02-05 22:14:08 +00:00
case $widgets [ $widget ] in
# Already bound
2016-02-15 15:31:00 +00:00
user:_zsh_autosuggest_( bound| orig) _*) ; ;
2016-02-05 22:14:08 +00:00
# User-defined widget
user:*)
zle -N $prefix $widget ${ widgets [ $widget ]#* : }
; ;
# Built-in widget
builtin )
2016-04-25 20:23:30 +00:00
eval " _zsh_autosuggest_orig_ ${ (q)widget } () { zle . ${ (q)widget } } "
2016-02-05 22:14:08 +00:00
zle -N $prefix $widget _zsh_autosuggest_orig_$widget
; ;
# Completion widget
completion:*)
2016-04-25 20:23:56 +00:00
eval " zle -C $prefix ${ (q)widget } ${ ${ (s. : .)widgets[ $widget ] } [2,3] } "
2016-02-05 22:14:08 +00:00
; ;
esac
2016-02-15 15:31:00 +00: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`).
2016-04-25 20:23:30 +00:00
eval " _zsh_autosuggest_bound_ ${ (q)widget } () {
_zsh_autosuggest_widget_$autosuggest_action $prefix ${ (q)widget } \$ @
2016-02-15 15:31:00 +00:00
} "
2016-02-05 22:14:08 +00:00
# Create the bound widget
2016-02-15 15:31:00 +00:00
zle -N $widget _zsh_autosuggest_bound_$widget
2016-02-05 22:14:08 +00:00
}
# Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_widgets( ) {
2016-08-01 01:35:30 +00:00
local widget
local ignore_widgets
ignore_widgets = (
.\*
_\*
zle-line-\*
autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX \*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
)
2016-02-05 22:14:08 +00:00
# Find every widget we might want to bind and bind it appropriately
2016-08-01 01:35:30 +00:00
for widget in ${ ${ (f) " $( builtin zle -la) " } : # ${ (j : | : )~ignore_widgets } } ; do
2016-02-14 15:54:34 +00:00
if [ ${ ZSH_AUTOSUGGEST_CLEAR_WIDGETS [(r) $widget ] } ] ; then
_zsh_autosuggest_bind_widget $widget clear
2016-02-05 22:14:08 +00:00
elif [ ${ ZSH_AUTOSUGGEST_ACCEPT_WIDGETS [(r) $widget ] } ] ; then
2016-02-14 15:54:34 +00:00
_zsh_autosuggest_bind_widget $widget accept
2016-02-20 13:52:21 +00:00
elif [ ${ ZSH_AUTOSUGGEST_EXECUTE_WIDGETS [(r) $widget ] } ] ; then
_zsh_autosuggest_bind_widget $widget execute
2016-02-05 22:14:08 +00:00
elif [ ${ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS [(r) $widget ] } ] ; then
2016-02-14 15:54:34 +00:00
_zsh_autosuggest_bind_widget $widget partial_accept
2016-02-05 22:14:08 +00:00
else
2016-02-14 15:54:34 +00:00
# Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
2016-02-05 22:14:08 +00:00
fi
done
}
2016-02-15 15:31:00 +00:00
# Given the name of an original widget and args, invoke it, if it exists
2016-02-05 22:14:08 +00:00
_zsh_autosuggest_invoke_original_widget( ) {
2016-02-17 20:41:37 +00:00
# Do nothing unless called with at least one arg
[ $# -gt 0 ] || return
2016-03-06 04:03:14 +00:00
local original_widget_name = " $1 "
2016-02-15 15:31:00 +00:00
shift
2016-02-05 22:14:08 +00:00
if [ $widgets [ $original_widget_name ] ] ; then
2016-02-24 01:13:03 +00:00
zle $original_widget_name -- $@
2016-02-05 22:14:08 +00:00
fi
}
2016-02-14 07:29:43 +00:00
#--------------------------------------------------------------------#
# Highlighting #
#--------------------------------------------------------------------#
2016-02-05 22:14:08 +00:00
# If there was a highlight, remove it
_zsh_autosuggest_highlight_reset( ) {
2016-03-15 03:50:04 +00:00
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
2016-02-05 22:14:08 +00:00
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( ) {
2016-03-15 03:50:04 +00:00
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
2016-02-05 22:14:08 +00:00
if [ $# POSTDISPLAY -gt 0 ] ; then
_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT = " $# BUFFER $(( $# BUFFER + $# POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE "
2016-02-24 20:59:49 +00:00
region_highlight += ( " $_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT " )
2016-02-05 22:14:08 +00:00
else
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
fi
}
2016-02-14 07:29:43 +00:00
#--------------------------------------------------------------------#
# Autosuggest Widget Implementations #
#--------------------------------------------------------------------#
2016-02-05 22:14:08 +00:00
# Clear the suggestion
_zsh_autosuggest_clear( ) {
# Remove the suggestion
unset POSTDISPLAY
2016-02-14 06:46:34 +00:00
_zsh_autosuggest_invoke_original_widget $@
2016-02-05 22:14:08 +00:00
}
# Modify the buffer and get a new suggestion
_zsh_autosuggest_modify( ) {
2016-04-06 23:10:34 +00:00
local -i retval
2016-08-01 02:10:22 +00:00
# Save the contents of the buffer/postdisplay
local orig_buffer = " $BUFFER "
local orig_postdisplay = " $POSTDISPLAY "
2016-07-20 03:04:18 +00:00
# Clear suggestion while waiting for next one
2016-04-25 20:42:09 +00:00
unset POSTDISPLAY
2016-08-01 02:10:22 +00:00
# Original widget may modify the buffer
2016-02-14 06:46:34 +00:00
_zsh_autosuggest_invoke_original_widget $@
2016-04-06 23:10:34 +00:00
retval = $?
2016-02-05 22:14:08 +00:00
2017-01-25 07:00:13 +00:00
# Optimize if manually typing in the suggestion
if [ $# BUFFER -gt $# orig_buffer ] ; then
local added = ${ BUFFER # $orig_buffer }
# If the string added matches the beginning of the postdisplay
if [ " $added " = " ${ orig_postdisplay : 0 : $# added } " ] ; then
POSTDISPLAY = " ${ orig_postdisplay : $# added } "
return $retval
fi
fi
2016-08-01 02:10:22 +00:00
# Don't fetch a new suggestion if the buffer hasn't changed
if [ " $BUFFER " = " $orig_buffer " ] ; then
POSTDISPLAY = " $orig_postdisplay "
return $retval
fi
2016-02-05 22:14:08 +00:00
# Get a new suggestion if the buffer is not empty after modification
if [ $# BUFFER -gt 0 ] ; then
2016-07-18 09:56:21 +00:00
if [ -z " $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE " -o $# BUFFER -lt " $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE " ] ; then
2016-07-20 03:04:18 +00:00
_zsh_autosuggest_async_fetch_suggestion " $BUFFER "
2016-07-15 08:39:33 +00:00
fi
2016-02-05 22:14:08 +00:00
fi
2016-04-06 23:10:34 +00:00
return $retval
2016-02-05 22:14:08 +00:00
}
# Accept the entire suggestion
_zsh_autosuggest_accept( ) {
2016-03-15 03:41:14 +00: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
if [ " $KEYMAP " = "vicmd" ] ; then
max_cursor_pos = $(( max_cursor_pos - 1 ))
fi
2016-02-05 22:14:08 +00:00
# Only accept if the cursor is at the end of the buffer
2016-03-15 03:41:14 +00:00
if [ $CURSOR -eq $max_cursor_pos ] ; then
2016-02-05 22:14:08 +00: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 06:46:34 +00:00
_zsh_autosuggest_invoke_original_widget $@
2016-02-05 22:14:08 +00:00
}
2016-02-20 13:52:21 +00: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-23 17:20:13 +00: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 13:52:21 +00:00
}
2016-02-05 22:14:08 +00:00
# Partially accept the suggestion
_zsh_autosuggest_partial_accept( ) {
2016-04-06 23:10:34 +00:00
local -i retval
2016-02-05 22:14:08 +00:00
# Save the contents of the buffer so we can restore later if needed
2016-03-06 04:03:14 +00:00
local original_buffer = " $BUFFER "
2016-02-05 22:14:08 +00:00
# Temporarily accept the suggestion.
BUFFER = " $BUFFER $POSTDISPLAY "
# Original widget moves the cursor
2016-02-14 06:46:34 +00:00
_zsh_autosuggest_invoke_original_widget $@
2016-04-06 23:10:34 +00:00
retval = $?
2016-02-05 22:14:08 +00:00
# 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
2016-03-06 04:03:14 +00:00
POSTDISPLAY = " $RBUFFER "
2016-02-05 22:14:08 +00:00
# Clip the buffer at the cursor
2016-03-06 04:03:14 +00:00
BUFFER = " $LBUFFER "
2016-02-05 22:14:08 +00:00
else
# Restore the original buffer
2016-03-06 04:03:14 +00:00
BUFFER = " $original_buffer "
2016-02-05 22:14:08 +00:00
fi
2016-04-06 23:10:34 +00:00
return $retval
2016-02-05 22:14:08 +00:00
}
2016-02-20 13:52:21 +00:00
for action in clear modify accept partial_accept execute; do
2016-02-14 15:54:34 +00:00
eval " _zsh_autosuggest_widget_ $action () {
2016-04-06 23:10:34 +00:00
local -i retval
2016-02-14 15:54:34 +00:00
_zsh_autosuggest_highlight_reset
2016-04-06 23:10:34 +00:00
2016-02-14 15:54:34 +00:00
_zsh_autosuggest_$action \$ @
2016-04-06 23:10:34 +00:00
retval = \$ ?
2016-02-14 15:54:34 +00:00
_zsh_autosuggest_highlight_apply
2016-04-06 23:10:34 +00:00
return \$ retval
2016-02-14 15:54:34 +00:00
} "
done
2016-02-05 22:14:08 +00:00
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
2016-02-07 15:58:09 +00:00
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
2016-02-20 13:52:21 +00:00
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
2016-02-05 22:14:08 +00:00
2016-07-20 03:04:18 +00:00
_zsh_autosuggest_show_suggestion( ) {
2017-01-25 05:27:47 +00:00
local suggestion = " $1 "
2016-02-05 22:14:08 +00:00
2016-07-20 03:04:18 +00:00
_zsh_autosuggest_highlight_reset
2016-02-16 17:30:34 +00:00
2016-07-20 03:04:18 +00:00
if [ -n " $suggestion " ] ; then
POSTDISPLAY = " ${ suggestion # $BUFFER } "
else
unset POSTDISPLAY
2016-03-01 19:45:55 +00:00
fi
2016-02-24 03:11:56 +00:00
2016-07-20 03:04:18 +00:00
_zsh_autosuggest_highlight_apply
2016-02-24 03:11:56 +00:00
2016-07-20 03:04:18 +00:00
zle -R
2016-02-05 22:14:08 +00:00
}
2016-07-20 03:04:18 +00:00
zle -N _autosuggest-show-suggestion _zsh_autosuggest_show_suggestion
2016-03-01 19:45:55 +00:00
#--------------------------------------------------------------------#
# Default Suggestion Strategy #
#--------------------------------------------------------------------#
# Suggests the most recent history item that matches the given
# prefix.
#
_zsh_autosuggest_strategy_default( ) {
2017-01-25 02:58:06 +00:00
setopt localoptions EXTENDED_GLOB
2017-01-25 06:04:07 +00:00
local prefix = " ${ 1 //(#m)[ \\ () \[ \] |*?~]/ \\ $MATCH } "
# Get the keys of the history items that match
local -a histkeys
histkeys = ( ${ (k)history[(r) $prefix *] } )
# Echo the value of the first key
echo -E " ${ history [ $histkeys [1]] } "
2016-03-01 19:45:55 +00:00
}
2016-03-01 21:58:57 +00:00
#--------------------------------------------------------------------#
# Match Previous Command Suggestion Strategy #
#--------------------------------------------------------------------#
# Suggests the most recent history item that matches the given
2016-03-05 02:23:32 +00:00
# prefix and whose preceding history item also matches the most
2016-03-01 21:58:57 +00:00
# recently executed command.
#
2016-03-05 02:23:32 +00: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-01 21:58:57 +00:00
#
2016-05-28 18:05:11 +00: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-01 21:58:57 +00:00
_zsh_autosuggest_strategy_match_prev_cmd( ) {
2017-01-25 06:05:50 +00:00
local prefix = " ${ 1 //(#m)[ \\ () \[ \] |*?~]/ \\ $MATCH } "
2016-03-01 21:58:57 +00:00
# Get all history event numbers 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 histkey = " ${ history_match_keys [1] } "
# Get the previously executed command
2016-04-25 20:19:26 +00:00
local prev_cmd = " $( _zsh_autosuggest_escape_command " ${ history [ $(( HISTCMD-1)) ] } " ) "
2016-03-01 21:58:57 +00: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 04:03:14 +00:00
if [ [ " ${ history [ $(( key - 1 )) ] } " = = " $prev_cmd " ] ] ; then
2016-03-01 21:58:57 +00:00
histkey = " $key "
break
fi
done
# Echo the matched history entry
echo -E " $history [ $histkey ] "
}
2016-07-20 03:04:18 +00:00
#--------------------------------------------------------------------#
# Async #
#--------------------------------------------------------------------#
_zsh_autosuggest_async_fetch_suggestion( ) {
2017-01-25 02:58:06 +00:00
# Send the prefix to the pty to fetch a suggestion
2017-01-25 04:58:17 +00:00
zpty -w -n $ZSH_AUTOSUGGEST_PTY_NAME " ${ 1 } " $'\0'
2016-07-20 03:04:18 +00:00
}
2017-01-25 02:53:26 +00:00
# Pty is spawned running this function
2017-01-25 02:55:38 +00:00
_zsh_autosuggest_async_suggestion_server( ) {
2017-01-25 02:58:25 +00:00
emulate -R zsh
2017-01-25 05:27:09 +00:00
# Output only newlines (not carriage return + newline)
stty -onlcr
2017-01-25 05:48:30 +00:00
local strategy = $1
2017-01-25 07:00:53 +00:00
local last_pid
2017-01-25 05:48:30 +00:00
2017-01-25 04:58:17 +00:00
while IFS = '' read -r -d $'\0' prefix; do
2016-07-20 03:04:18 +00:00
# Kill last bg process
2017-01-25 07:00:53 +00:00
kill -KILL $last_pid & >/dev/null
2016-07-20 03:04:18 +00:00
# Run suggestion search in the background
2017-01-25 06:59:38 +00:00
( echo -n -E " $( $strategy " $prefix " ) " $'\0' ) &
2017-01-25 07:00:53 +00:00
last_pid = $!
2016-07-20 03:04:18 +00:00
done
}
2017-01-25 02:53:26 +00: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
2016-07-20 03:04:18 +00:00
_zsh_autosuggest_async_suggestion_ready( ) {
2017-01-25 02:58:52 +00:00
local suggestion
zpty -rt $ZSH_AUTOSUGGEST_PTY_NAME suggestion '*' $'\0' 2>/dev/null
zle _autosuggest-show-suggestion " ${ suggestion % $'\0' } "
2016-07-20 03:04:18 +00:00
}
# Recreate the pty to get a fresh list of history events
_zsh_autosuggest_async_recreate_pty( ) {
typeset -g _ZSH_AUTOSUGGEST_PTY_FD
# Kill the old pty
if [ -n " $_ZSH_AUTOSUGGEST_PTY_FD " ] ; then
2017-01-25 02:53:26 +00:00
# Remove the input handler
2016-07-20 03:04:18 +00:00
zle -F $_ZSH_AUTOSUGGEST_PTY_FD
2017-01-25 02:53:26 +00:00
# Destroy the pty
2016-07-20 03:04:18 +00:00
zpty -d $ZSH_AUTOSUGGEST_PTY_NAME & >/dev/null
fi
2017-01-26 23:50:19 +00:00
# With newer versions of zsh, REPLY stores the fd to read from
2016-07-20 03:04:18 +00:00
typeset -h REPLY
2017-01-25 02:53:26 +00:00
2017-01-26 23:50:19 +00:00
# If we won't get a fd back from zpty, try to guess it
if [ $_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD -eq 0 ] ; then
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-01-25 02:53:26 +00:00
# Start a new pty running the server function
2017-01-25 05:48:30 +00:00
zpty -b $ZSH_AUTOSUGGEST_PTY_NAME " _zsh_autosuggest_async_suggestion_server _zsh_autosuggest_strategy_ $ZSH_AUTOSUGGEST_STRATEGY "
2017-01-25 02:53:26 +00:00
# Store the fd so we can destroy this pty later
2017-01-26 23:50:19 +00:00
if ( ( REPLY ) ) ; then
_ZSH_AUTOSUGGEST_PTY_FD = $REPLY
else
_ZSH_AUTOSUGGEST_PTY_FD = $zptyfd
fi
2017-01-25 02:53:26 +00:00
# Set up input handler from the pty
2016-07-20 03:04:18 +00:00
zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_suggestion_ready
}
2016-02-14 07:29:43 +00:00
#--------------------------------------------------------------------#
# Start #
#--------------------------------------------------------------------#
2016-02-05 22:14:08 +00:00
# Start the autosuggestion widgets
2016-02-07 21:21:57 +00:00
_zsh_autosuggest_start( ) {
2017-01-26 23:11:01 +00:00
add-zsh-hook -d precmd _zsh_autosuggest_start
2017-01-26 23:35:33 +00:00
_zsh_autosuggest_feature_detect
2016-02-05 22:14:08 +00:00
_zsh_autosuggest_check_deprecated_config
_zsh_autosuggest_bind_widgets
2017-01-26 23:37:42 +00:00
_zsh_autosuggest_async_recreate_pty
add-zsh-hook precmd _zsh_autosuggest_async_recreate_pty
2016-02-05 22:14:08 +00:00
}
2016-02-07 21:21:57 +00:00
add-zsh-hook precmd _zsh_autosuggest_start