2016-02-05 23:14:08 +01:00
|
|
|
# Fish-like fast/unobtrusive autosuggestions for zsh.
|
2016-02-25 21:04:32 +01:00
|
|
|
# https://github.com/zsh-users/zsh-autosuggestions
|
2021-06-05 00:00:37 +02:00
|
|
|
# v0.7.0
|
2016-02-05 23:14:08 +01:00
|
|
|
# Copyright (c) 2013 Thiago de Arruda
|
2021-06-05 00:01:04 +02:00
|
|
|
# Copyright (c) 2016-2021 Eric Freese
|
2016-02-05 23:14:08 +01: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-02-14 08:29:43 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Global Configuration Variables #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-05 23:14:08 +01: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-12-19 08:20:57 +01:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE} )) &&
|
|
|
|
typeset -g ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Prefix to use when saving original versions of bound widgets
|
2018-12-19 08:20:57 +01:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX} )) &&
|
|
|
|
typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2018-06-07 05:49:03 +02:00
|
|
|
# Strategies to use to fetch a suggestion
|
|
|
|
# Will try each strategy in order until a suggestion is returned
|
2018-12-19 08:20:57 +01:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_STRATEGY} )) && {
|
|
|
|
typeset -ga ZSH_AUTOSUGGEST_STRATEGY
|
|
|
|
ZSH_AUTOSUGGEST_STRATEGY=(history)
|
|
|
|
}
|
2016-03-01 20:45:55 +01:00
|
|
|
|
2021-07-14 23:57:23 +02:00
|
|
|
# Maximum number of commands to consider for match_prev_cmd strategy
|
|
|
|
# Set to -1 to always use all matches
|
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS} )) &&
|
|
|
|
typeset -g ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS=200
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Widgets that clear the suggestion
|
2018-12-19 08:20:57 +01:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_CLEAR_WIDGETS} )) && {
|
|
|
|
typeset -ga ZSH_AUTOSUGGEST_CLEAR_WIDGETS
|
|
|
|
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
|
|
|
history-search-forward
|
|
|
|
history-search-backward
|
|
|
|
history-beginning-search-forward
|
|
|
|
history-beginning-search-backward
|
|
|
|
history-substring-search-up
|
|
|
|
history-substring-search-down
|
|
|
|
up-line-or-beginning-search
|
|
|
|
down-line-or-beginning-search
|
|
|
|
up-line-or-history
|
|
|
|
down-line-or-history
|
|
|
|
accept-line
|
2019-06-14 17:48:31 +02:00
|
|
|
copy-earlier-word
|
2018-12-19 08:20:57 +01:00
|
|
|
)
|
|
|
|
}
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Widgets that accept the entire suggestion
|
2018-12-19 08:20:57 +01:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_ACCEPT_WIDGETS} )) && {
|
|
|
|
typeset -ga ZSH_AUTOSUGGEST_ACCEPT_WIDGETS
|
|
|
|
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
|
|
|
forward-char
|
|
|
|
end-of-line
|
|
|
|
vi-forward-char
|
|
|
|
vi-end-of-line
|
|
|
|
vi-add-eol
|
|
|
|
)
|
|
|
|
}
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2016-02-20 14:52:21 +01:00
|
|
|
# Widgets that accept the entire suggestion and execute it
|
2018-12-19 08:20:57 +01:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_EXECUTE_WIDGETS} )) && {
|
|
|
|
typeset -ga ZSH_AUTOSUGGEST_EXECUTE_WIDGETS
|
|
|
|
ZSH_AUTOSUGGEST_EXECUTE_WIDGETS=(
|
|
|
|
)
|
|
|
|
}
|
2016-02-20 14:52:21 +01:00
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Widgets that accept the suggestion as far as the cursor moves
|
2018-12-19 08:20:57 +01:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS} )) && {
|
|
|
|
typeset -ga ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS
|
|
|
|
ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
|
|
|
|
forward-word
|
|
|
|
emacs-forward-word
|
|
|
|
vi-forward-word
|
|
|
|
vi-forward-word-end
|
|
|
|
vi-forward-blank-word
|
|
|
|
vi-forward-blank-word-end
|
|
|
|
vi-find-next-char
|
|
|
|
vi-find-next-char-skip
|
|
|
|
)
|
|
|
|
}
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2016-08-01 03:35:30 +02:00
|
|
|
# Widgets that should be ignored (globbing supported but must be escaped)
|
2018-12-19 08:20:57 +01:00
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_IGNORE_WIDGETS} )) && {
|
|
|
|
typeset -ga ZSH_AUTOSUGGEST_IGNORE_WIDGETS
|
|
|
|
ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(
|
|
|
|
orig-\*
|
|
|
|
beep
|
|
|
|
run-help
|
|
|
|
set-local-history
|
|
|
|
which-command
|
|
|
|
yank
|
|
|
|
yank-pop
|
2019-05-27 21:37:25 +02:00
|
|
|
zle-\*
|
2018-12-19 08:20:57 +01:00
|
|
|
)
|
|
|
|
}
|
2016-08-01 03:35:30 +02:00
|
|
|
|
2018-11-23 18:18:15 +01:00
|
|
|
# Pty name for capturing completions for completion suggestion strategy
|
|
|
|
(( ! ${+ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME} )) &&
|
|
|
|
typeset -g ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME=zsh_autosuggest_completion_pty
|
|
|
|
|
2017-12-06 15:51:06 +01: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}"
|
|
|
|
}
|
|
|
|
|
2016-02-14 08:29:43 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Widget Helpers #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2017-02-28 19:14:16 +01:00
|
|
|
_zsh_autosuggest_incr_bind_count() {
|
2019-02-25 12:59:31 +01:00
|
|
|
typeset -gi bind_count=$((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]+1))
|
|
|
|
_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=$bind_count
|
2017-02-28 19:14:16 +01:00
|
|
|
}
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
|
|
|
|
_zsh_autosuggest_bind_widget() {
|
2017-02-28 19:14:16 +01:00
|
|
|
typeset -gA _ZSH_AUTOSUGGEST_BIND_COUNTS
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
local widget=$1
|
2016-02-14 16:54:34 +01:00
|
|
|
local autosuggest_action=$2
|
2016-02-05 23:14:08 +01:00
|
|
|
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
|
|
|
|
|
2017-02-28 19:14:16 +01:00
|
|
|
local -i bind_count
|
|
|
|
|
2016-02-14 16:54:34 +01:00
|
|
|
# Save a reference to the original widget
|
2016-02-05 23:14:08 +01:00
|
|
|
case $widgets[$widget] in
|
|
|
|
# Already bound
|
2019-02-25 12:59:31 +01:00
|
|
|
user:_zsh_autosuggest_(bound|orig)_*)
|
|
|
|
bind_count=$((_ZSH_AUTOSUGGEST_BIND_COUNTS[$widget]))
|
|
|
|
;;
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# User-defined widget
|
|
|
|
user:*)
|
2017-02-28 19:14:16 +01:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
2019-04-01 22:36:31 +02:00
|
|
|
zle -N $prefix$bind_count-$widget ${widgets[$widget]#*:}
|
2016-02-05 23:14:08 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
# Built-in widget
|
|
|
|
builtin)
|
2017-02-28 19:14:16 +01:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
2016-04-25 22:23:30 +02:00
|
|
|
eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
|
2019-04-01 22:36:31 +02:00
|
|
|
zle -N $prefix$bind_count-$widget _zsh_autosuggest_orig_$widget
|
2016-02-05 23:14:08 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
# Completion widget
|
|
|
|
completion:*)
|
2017-02-28 19:14:16 +01:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
2019-04-01 22:36:31 +02:00
|
|
|
eval "zle -C $prefix$bind_count-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
|
2016-02-05 23:14:08 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2016-02-15 16:31:00 +01: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-02-28 19:14:16 +01:00
|
|
|
eval "_zsh_autosuggest_bound_${bind_count}_${(q)widget}() {
|
|
|
|
_zsh_autosuggest_widget_$autosuggest_action $prefix$bind_count-${(q)widget} \$@
|
2016-02-15 16:31:00 +01:00
|
|
|
}"
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Create the bound widget
|
2018-06-11 06:39:58 +02:00
|
|
|
zle -N -- $widget _zsh_autosuggest_bound_${bind_count}_$widget
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Map all configured widgets to the right autosuggest widgets
|
|
|
|
_zsh_autosuggest_bind_widgets() {
|
2018-07-14 06:16:53 +02:00
|
|
|
emulate -L zsh
|
|
|
|
|
|
|
|
local widget
|
2016-08-01 03:35:30 +02:00
|
|
|
local ignore_widgets
|
|
|
|
|
|
|
|
ignore_widgets=(
|
|
|
|
.\*
|
|
|
|
_\*
|
2020-01-25 15:47:58 +01:00
|
|
|
${_ZSH_AUTOSUGGEST_BUILTIN_ACTIONS/#/autosuggest-}
|
2016-08-01 03:35:30 +02:00
|
|
|
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
|
|
|
|
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
|
|
|
|
)
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Find every widget we might want to bind and bind it appropriately
|
2016-08-01 03:35:30 +02:00
|
|
|
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ -n ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_bind_widget $widget clear
|
2017-07-17 22:45:48 +02:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_bind_widget $widget accept
|
2017-07-17 22:45:48 +02:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]]; then
|
2016-02-20 14:52:21 +01:00
|
|
|
_zsh_autosuggest_bind_widget $widget execute
|
2017-07-17 22:45:48 +02:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_bind_widget $widget partial_accept
|
2016-02-05 23:14:08 +01:00
|
|
|
else
|
2016-02-14 16:54:34 +01:00
|
|
|
# Assume any unspecified widget might modify the buffer
|
|
|
|
_zsh_autosuggest_bind_widget $widget modify
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-02-15 16:31:00 +01:00
|
|
|
# Given the name of an original widget and args, invoke it, if it exists
|
2016-02-05 23:14:08 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget() {
|
2016-02-17 21:41:37 +01:00
|
|
|
# Do nothing unless called with at least one arg
|
2018-01-16 21:08:38 +01:00
|
|
|
(( $# )) || return 0
|
2016-02-17 21:41:37 +01:00
|
|
|
|
2016-03-06 05:03:14 +01:00
|
|
|
local original_widget_name="$1"
|
2016-02-15 16:31:00 +01:00
|
|
|
|
|
|
|
shift
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( ${+widgets[$original_widget_name]} )); then
|
2016-02-24 02:13:03 +01:00
|
|
|
zle $original_widget_name -- $@
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-14 08:29:43 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Highlighting #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# If there was a highlight, remove it
|
|
|
|
_zsh_autosuggest_highlight_reset() {
|
2016-03-15 04:50:04 +01:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
|
|
|
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ -n "$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT" ]]; then
|
2016-02-05 23:14:08 +01: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 04:50:04 +01:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
|
|
|
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( $#POSTDISPLAY )); then
|
2017-09-27 15:55:12 +02:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
|
2016-02-24 21:59:49 +01:00
|
|
|
region_highlight+=("$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT")
|
2016-02-05 23:14:08 +01:00
|
|
|
else
|
|
|
|
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-14 08:29:43 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Autosuggest Widget Implementations #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2017-03-03 19:59:30 +01: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-04 23:04:04 +01:00
|
|
|
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( $#BUFFER )); then
|
2017-03-04 23:04:04 +01:00
|
|
|
_zsh_autosuggest_fetch
|
|
|
|
fi
|
2017-03-03 19:59:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Toggle suggestions (enable/disable)
|
|
|
|
_zsh_autosuggest_toggle() {
|
2020-01-25 15:44:44 +01:00
|
|
|
if (( ${+_ZSH_AUTOSUGGEST_DISABLED} )); then
|
2017-03-03 19:59:30 +01:00
|
|
|
_zsh_autosuggest_enable
|
|
|
|
else
|
|
|
|
_zsh_autosuggest_disable
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Clear the suggestion
|
|
|
|
_zsh_autosuggest_clear() {
|
|
|
|
# Remove the suggestion
|
|
|
|
unset POSTDISPLAY
|
|
|
|
|
2016-02-14 07:46:34 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Modify the buffer and get a new suggestion
|
|
|
|
_zsh_autosuggest_modify() {
|
2016-04-07 01:10:34 +02:00
|
|
|
local -i retval
|
|
|
|
|
2017-09-27 23:04:42 +02:00
|
|
|
# Only available in zsh >= 5.4
|
2017-02-28 19:18:21 +01:00
|
|
|
local -i KEYS_QUEUED_COUNT
|
|
|
|
|
2016-08-01 04:10:22 +02:00
|
|
|
# Save the contents of the buffer/postdisplay
|
|
|
|
local orig_buffer="$BUFFER"
|
|
|
|
local orig_postdisplay="$POSTDISPLAY"
|
|
|
|
|
2016-07-20 05:04:18 +02:00
|
|
|
# Clear suggestion while waiting for next one
|
2016-04-25 22:42:09 +02:00
|
|
|
unset POSTDISPLAY
|
|
|
|
|
2016-08-01 04:10:22 +02:00
|
|
|
# Original widget may modify the buffer
|
2016-02-14 07:46:34 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-04-07 01:10:34 +02:00
|
|
|
retval=$?
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2018-12-09 18:14:19 +01:00
|
|
|
emulate -L zsh
|
|
|
|
|
2017-02-28 19:18:21 +01:00
|
|
|
# Don't fetch a new suggestion if there's more input to be read immediately
|
2019-12-15 15:01:12 +01:00
|
|
|
if (( $PENDING > 0 || $KEYS_QUEUED_COUNT > 0 )); then
|
2018-05-15 19:32:46 +02:00
|
|
|
POSTDISPLAY="$orig_postdisplay"
|
2017-02-28 19:18:21 +01:00
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
|
2020-05-29 09:39:26 +02:00
|
|
|
# Optimize if manually typing in the suggestion or if buffer hasn't changed
|
|
|
|
if [[ "$BUFFER" = "$orig_buffer"* && "$orig_postdisplay" = "${BUFFER:$#orig_buffer}"* ]]; then
|
|
|
|
POSTDISPLAY="${orig_postdisplay:$(($#BUFFER - $#orig_buffer))}"
|
2016-08-01 04:10:22 +02:00
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
|
2017-03-03 19:59:30 +01:00
|
|
|
# Bail out if suggestions are disabled
|
2020-01-25 15:44:44 +01:00
|
|
|
if (( ${+_ZSH_AUTOSUGGEST_DISABLED} )); then
|
2017-03-03 19:59:30 +01:00
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Get a new suggestion if the buffer is not empty after modification
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( $#BUFFER > 0 )); then
|
2019-05-27 22:18:21 +02:00
|
|
|
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_fetch
|
2016-07-15 10:39:33 +02:00
|
|
|
fi
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
|
|
|
|
2016-04-07 01:10:34 +02:00
|
|
|
return $retval
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
# Fetch a new suggestion based on what's currently in the buffer
|
|
|
|
_zsh_autosuggest_fetch() {
|
2019-04-10 19:20:08 +02:00
|
|
|
if (( ${+ZSH_AUTOSUGGEST_USE_ASYNC} )); then
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_async_request "$BUFFER"
|
|
|
|
else
|
|
|
|
local suggestion
|
2018-06-07 05:49:03 +02:00
|
|
|
_zsh_autosuggest_fetch_suggestion "$BUFFER"
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_suggest "$suggestion"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Offer a suggestion
|
|
|
|
_zsh_autosuggest_suggest() {
|
2018-07-14 06:16:53 +02:00
|
|
|
emulate -L zsh
|
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
local suggestion="$1"
|
|
|
|
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ -n "$suggestion" ]] && (( $#BUFFER )); then
|
2017-01-27 23:18:26 +01:00
|
|
|
POSTDISPLAY="${suggestion#$BUFFER}"
|
|
|
|
else
|
|
|
|
unset POSTDISPLAY
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Accept the entire suggestion
|
|
|
|
_zsh_autosuggest_accept() {
|
2019-12-15 14:56:10 +01:00
|
|
|
local -i retval max_cursor_pos=$#BUFFER
|
2016-03-15 04:41:14 +01:00
|
|
|
|
|
|
|
# When vicmd keymap is active, the cursor can't move all the way
|
|
|
|
# to the end of the buffer
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
2016-03-15 04:41:14 +01:00
|
|
|
max_cursor_pos=$((max_cursor_pos - 1))
|
|
|
|
fi
|
|
|
|
|
2019-12-15 14:56:10 +01:00
|
|
|
# If we're not in a valid state to accept a suggestion, just run the
|
|
|
|
# original widget and bail out
|
|
|
|
if (( $CURSOR != $max_cursor_pos || !$#POSTDISPLAY )); then
|
2019-12-15 14:55:14 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Only accept if the cursor is at the end of the buffer
|
2019-12-15 14:55:14 +01:00
|
|
|
# Add the suggestion to the buffer
|
|
|
|
BUFFER="$BUFFER$POSTDISPLAY"
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2019-12-15 14:55:14 +01:00
|
|
|
# Remove the suggestion
|
|
|
|
unset POSTDISPLAY
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2019-12-15 14:56:10 +01:00
|
|
|
# Run the original widget before manually moving the cursor so that the
|
|
|
|
# cursor movement doesn't make the widget do something unexpected
|
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
|
|
|
retval=$?
|
|
|
|
|
2019-12-15 14:55:14 +01:00
|
|
|
# Move the cursor to the end of the buffer
|
|
|
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
|
|
|
CURSOR=$(($#BUFFER - 1))
|
|
|
|
else
|
|
|
|
CURSOR=$#BUFFER
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
|
|
|
|
2019-12-15 14:56:10 +01:00
|
|
|
return $retval
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
2016-02-20 14:52:21 +01: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 18:20:13 +01: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 14:52:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Partially accept the suggestion
|
|
|
|
_zsh_autosuggest_partial_accept() {
|
2018-05-14 18:59:08 +02:00
|
|
|
local -i retval cursor_loc
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Save the contents of the buffer so we can restore later if needed
|
2016-03-06 05:03:14 +01:00
|
|
|
local original_buffer="$BUFFER"
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Temporarily accept the suggestion.
|
|
|
|
BUFFER="$BUFFER$POSTDISPLAY"
|
|
|
|
|
|
|
|
# Original widget moves the cursor
|
2016-02-14 07:46:34 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-04-07 01:10:34 +02:00
|
|
|
retval=$?
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2018-05-14 18:59:08 +02:00
|
|
|
# Normalize cursor location across vi/emacs modes
|
|
|
|
cursor_loc=$CURSOR
|
|
|
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
|
|
|
cursor_loc=$((cursor_loc + 1))
|
|
|
|
fi
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# If we've moved past the end of the original buffer
|
2018-05-14 18:59:08 +02:00
|
|
|
if (( $cursor_loc > $#original_buffer )); then
|
2016-02-05 23:14:08 +01:00
|
|
|
# Set POSTDISPLAY to text right of the cursor
|
2018-05-14 18:59:08 +02:00
|
|
|
POSTDISPLAY="${BUFFER[$(($cursor_loc + 1)),$#BUFFER]}"
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Clip the buffer at the cursor
|
2018-05-14 18:59:08 +02:00
|
|
|
BUFFER="${BUFFER[1,$cursor_loc]}"
|
2016-02-05 23:14:08 +01:00
|
|
|
else
|
|
|
|
# Restore the original buffer
|
2016-03-06 05:03:14 +01:00
|
|
|
BUFFER="$original_buffer"
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
2016-04-07 01:10:34 +02:00
|
|
|
|
|
|
|
return $retval
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
() {
|
2020-01-25 15:47:23 +01:00
|
|
|
typeset -ga _ZSH_AUTOSUGGEST_BUILTIN_ACTIONS
|
|
|
|
|
|
|
|
_ZSH_AUTOSUGGEST_BUILTIN_ACTIONS=(
|
|
|
|
clear
|
|
|
|
fetch
|
|
|
|
suggest
|
|
|
|
accept
|
|
|
|
execute
|
|
|
|
enable
|
|
|
|
disable
|
|
|
|
toggle
|
|
|
|
)
|
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
local action
|
2020-01-25 15:47:23 +01:00
|
|
|
for action in $_ZSH_AUTOSUGGEST_BUILTIN_ACTIONS modify partial_accept; do
|
2018-12-19 08:20:57 +01:00
|
|
|
eval "_zsh_autosuggest_widget_$action() {
|
|
|
|
local -i retval
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
_zsh_autosuggest_highlight_reset
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
_zsh_autosuggest_$action \$@
|
|
|
|
retval=\$?
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
_zsh_autosuggest_highlight_apply
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
zle -R
|
2017-01-27 23:18:26 +01:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
return \$retval
|
|
|
|
}"
|
|
|
|
done
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2020-01-25 15:47:23 +01:00
|
|
|
for action in $_ZSH_AUTOSUGGEST_BUILTIN_ACTIONS; do
|
|
|
|
zle -N autosuggest-$action _zsh_autosuggest_widget_$action
|
|
|
|
done
|
2018-12-22 07:20:08 +01:00
|
|
|
}
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2018-11-23 18:18:15 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Completion Suggestion Strategy #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Fetches a suggestion from the completion engine
|
|
|
|
#
|
|
|
|
|
|
|
|
_zsh_autosuggest_capture_postcompletion() {
|
|
|
|
# Always insert the first completion into the buffer
|
|
|
|
compstate[insert]=1
|
|
|
|
|
|
|
|
# Don't list completions
|
2019-04-18 07:25:43 +02:00
|
|
|
unset 'compstate[list]'
|
2018-11-23 18:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_capture_completion_widget() {
|
2019-04-18 07:28:20 +02:00
|
|
|
# Add a post-completion hook to be called after all completions have been
|
|
|
|
# gathered. The hook can modify compstate to affect what is done with the
|
|
|
|
# gathered completions.
|
2018-11-23 18:18:15 +01:00
|
|
|
local -a +h comppostfuncs
|
|
|
|
comppostfuncs=(_zsh_autosuggest_capture_postcompletion)
|
|
|
|
|
|
|
|
# Only capture completions at the end of the buffer
|
|
|
|
CURSOR=$#BUFFER
|
|
|
|
|
|
|
|
# Run the original widget wrapping `.complete-word` so we don't
|
|
|
|
# recursively try to fetch suggestions, since our pty is forked
|
|
|
|
# after autosuggestions is initialized.
|
|
|
|
zle -- ${(k)widgets[(r)completion:.complete-word:_main_complete]}
|
|
|
|
|
2019-04-18 07:30:48 +02:00
|
|
|
if is-at-least 5.0.3; then
|
|
|
|
# Don't do any cr/lf transformations. We need to do this immediately before
|
|
|
|
# output because if we do it in setup, onlcr will be re-enabled when we enter
|
|
|
|
# vared in the async code path. There is a bug in zpty module in older versions
|
|
|
|
# where the tty is not properly attached to the pty slave, resulting in stty
|
|
|
|
# getting stopped with a SIGTTOU. See zsh-workers thread 31660 and upstream
|
|
|
|
# commit f75904a38
|
|
|
|
stty -onlcr -ocrnl -F /dev/tty
|
|
|
|
fi
|
|
|
|
|
2018-11-23 18:18:15 +01:00
|
|
|
# The completion has been added, print the buffer as the suggestion
|
|
|
|
echo -nE - $'\0'$BUFFER$'\0'
|
|
|
|
}
|
|
|
|
|
|
|
|
zle -N autosuggest-capture-completion _zsh_autosuggest_capture_completion_widget
|
|
|
|
|
|
|
|
_zsh_autosuggest_capture_setup() {
|
|
|
|
# There is a bug in zpty module in older zsh versions 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.
|
|
|
|
if ! is-at-least 5.4; then
|
|
|
|
zshexit() {
|
2019-06-15 05:27:21 +02:00
|
|
|
# The zsh builtin `kill` fails sometimes in older versions
|
|
|
|
# https://unix.stackexchange.com/a/477647/156673
|
|
|
|
kill -KILL $$ 2>&- || command kill -KILL $$
|
|
|
|
|
|
|
|
# Block for long enough for the signal to come through
|
|
|
|
sleep 1
|
2018-11-23 18:18:15 +01:00
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Try to avoid any suggestions that wouldn't match the prefix
|
|
|
|
zstyle ':completion:*' matcher-list ''
|
|
|
|
zstyle ':completion:*' path-completion false
|
|
|
|
zstyle ':completion:*' max-errors 0 not-numeric
|
|
|
|
|
|
|
|
bindkey '^I' autosuggest-capture-completion
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_capture_completion_sync() {
|
|
|
|
_zsh_autosuggest_capture_setup
|
|
|
|
|
|
|
|
zle autosuggest-capture-completion
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_capture_completion_async() {
|
|
|
|
_zsh_autosuggest_capture_setup
|
|
|
|
|
|
|
|
zmodload zsh/parameter 2>/dev/null || return # For `$functions`
|
|
|
|
|
|
|
|
# Make vared completion work as if for a normal command line
|
|
|
|
# https://stackoverflow.com/a/7057118/154703
|
|
|
|
autoload +X _complete
|
|
|
|
functions[_original_complete]=$functions[_complete]
|
2019-06-18 06:04:31 +02:00
|
|
|
function _complete() {
|
2018-11-23 18:18:15 +01:00
|
|
|
unset 'compstate[vared]'
|
|
|
|
_original_complete "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Open zle with buffer set so we can capture completions for it
|
|
|
|
vared 1
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_strategy_completion() {
|
2019-12-15 16:07:59 +01:00
|
|
|
# Reset options to defaults and enable LOCAL_OPTIONS
|
|
|
|
emulate -L zsh
|
|
|
|
|
|
|
|
# Enable extended glob for completion ignore pattern
|
|
|
|
setopt EXTENDED_GLOB
|
|
|
|
|
2018-11-23 18:18:15 +01:00
|
|
|
typeset -g suggestion
|
|
|
|
local line REPLY
|
|
|
|
|
|
|
|
# Exit if we don't have completions
|
|
|
|
whence compdef >/dev/null || return
|
|
|
|
|
|
|
|
# Exit if we don't have zpty
|
|
|
|
zmodload zsh/zpty 2>/dev/null || return
|
|
|
|
|
2019-12-15 16:07:59 +01:00
|
|
|
# Exit if our search string matches the ignore pattern
|
|
|
|
[[ -n "$ZSH_AUTOSUGGEST_COMPLETION_IGNORE" ]] && [[ "$1" == $~ZSH_AUTOSUGGEST_COMPLETION_IGNORE ]] && return
|
|
|
|
|
2018-11-23 18:18:15 +01:00
|
|
|
# Zle will be inactive if we are in async mode
|
|
|
|
if zle; then
|
|
|
|
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_sync
|
|
|
|
else
|
|
|
|
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_async "\$1"
|
|
|
|
zpty -w $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME $'\t'
|
|
|
|
fi
|
|
|
|
|
|
|
|
{
|
|
|
|
# The completion result is surrounded by null bytes, so read the
|
|
|
|
# content between the first two null bytes.
|
|
|
|
zpty -r $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME line '*'$'\0''*'$'\0'
|
|
|
|
|
2019-04-18 07:29:52 +02:00
|
|
|
# Extract the suggestion from between the null bytes. On older
|
|
|
|
# versions of zsh (older than 5.3), we sometimes get extra bytes after
|
|
|
|
# the second null byte, so trim those off the end.
|
|
|
|
# See http://www.zsh.org/mla/workers/2015/msg03290.html
|
2019-09-25 23:36:07 +02:00
|
|
|
suggestion="${${(@0)line}[2]}"
|
2018-11-23 18:18:15 +01:00
|
|
|
} always {
|
|
|
|
# Destroy the pty
|
|
|
|
zpty -d $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 23:52:46 +02:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# History Suggestion Strategy #
|
2016-03-01 20:45:55 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Suggests the most recent history item that matches the given
|
|
|
|
# prefix.
|
|
|
|
#
|
|
|
|
|
2018-05-16 23:52:46 +02:00
|
|
|
_zsh_autosuggest_strategy_history() {
|
2017-12-06 15:51:06 +01:00
|
|
|
# Reset options to defaults and enable LOCAL_OPTIONS
|
|
|
|
emulate -L zsh
|
|
|
|
|
2019-06-30 05:44:29 +02:00
|
|
|
# Enable globbing flags so that we can use (#m) and (x~y) glob operator
|
2017-12-06 15:51:06 +01:00
|
|
|
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 07:04:07 +01:00
|
|
|
|
2019-06-30 05:44:29 +02:00
|
|
|
# Get the history items that match the prefix, excluding those that match
|
|
|
|
# the ignore pattern
|
|
|
|
local pattern="$prefix*"
|
|
|
|
if [[ -n $ZSH_AUTOSUGGEST_HISTORY_IGNORE ]]; then
|
|
|
|
pattern="($pattern)~($ZSH_AUTOSUGGEST_HISTORY_IGNORE)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Give the first history item matching the pattern as the suggestion
|
2017-02-17 03:18:03 +01:00
|
|
|
# - (r) subscript flag makes the pattern match on values
|
2019-06-30 05:44:29 +02:00
|
|
|
typeset -g suggestion="${history[(r)$pattern]}"
|
2016-03-01 20:45:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 22:58:57 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Match Previous Command Suggestion Strategy #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Suggests the most recent history item that matches the given
|
2016-03-05 03:23:32 +01:00
|
|
|
# prefix and whose preceding history item also matches the most
|
2016-03-01 22:58:57 +01:00
|
|
|
# recently executed command.
|
|
|
|
#
|
2016-03-05 03:23:32 +01: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 22:58:57 +01:00
|
|
|
#
|
2016-05-28 20:05:11 +02: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 22:58:57 +01:00
|
|
|
|
|
|
|
_zsh_autosuggest_strategy_match_prev_cmd() {
|
2017-12-06 16:09:14 +01:00
|
|
|
# Reset options to defaults and enable LOCAL_OPTIONS
|
|
|
|
emulate -L zsh
|
|
|
|
|
2019-06-30 05:44:29 +02:00
|
|
|
# Enable globbing flags so that we can use (#m) and (x~y) glob operator
|
2017-12-06 16:09:14 +01:00
|
|
|
setopt EXTENDED_GLOB
|
|
|
|
|
2017-12-06 15:51:06 +01:00
|
|
|
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
|
|
|
|
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
|
2016-03-01 22:58:57 +01:00
|
|
|
|
2019-06-30 05:44:29 +02:00
|
|
|
# Get the history items that match the prefix, excluding those that match
|
|
|
|
# the ignore pattern
|
|
|
|
local pattern="$prefix*"
|
|
|
|
if [[ -n $ZSH_AUTOSUGGEST_HISTORY_IGNORE ]]; then
|
|
|
|
pattern="($pattern)~($ZSH_AUTOSUGGEST_HISTORY_IGNORE)"
|
|
|
|
fi
|
|
|
|
|
2016-03-01 22:58:57 +01:00
|
|
|
# Get all history event numbers that correspond to history
|
2019-06-30 05:44:29 +02:00
|
|
|
# entries that match the pattern
|
2016-03-01 22:58:57 +01:00
|
|
|
local history_match_keys
|
2019-06-30 05:44:29 +02:00
|
|
|
history_match_keys=(${(k)history[(R)$~pattern]})
|
2016-03-01 22:58:57 +01: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 15:51:06 +01:00
|
|
|
local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")"
|
2016-03-01 22:58:57 +01:00
|
|
|
|
2021-07-14 23:57:23 +02:00
|
|
|
# Iterate over the most recent history event numbers that match $prefix.
|
2021-07-14 16:56:00 +02:00
|
|
|
local key
|
2021-07-14 23:57:23 +02:00
|
|
|
for key in "${(@)history_match_keys[1,$ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS]}"; do
|
2016-03-01 22:58:57 +01:00
|
|
|
# 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 05:03:14 +01:00
|
|
|
if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then
|
2016-03-01 22:58:57 +01:00
|
|
|
histkey="$key"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
# Give back the matched history entry
|
2017-09-27 15:55:12 +02:00
|
|
|
typeset -g suggestion="$history[$histkey]"
|
2016-03-01 22:58:57 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 05:49:03 +02: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
|
2018-06-18 19:47:27 +02:00
|
|
|
local strategy
|
2018-06-07 05:49:03 +02:00
|
|
|
|
|
|
|
# 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"
|
|
|
|
|
2018-11-23 18:18:15 +01:00
|
|
|
# Ensure the suggestion matches the prefix
|
|
|
|
[[ "$suggestion" != "$1"* ]] && unset suggestion
|
|
|
|
|
|
|
|
# Break once we've found a valid suggestion
|
2018-06-07 05:49:03 +02:00
|
|
|
[[ -n "$suggestion" ]] && break
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-07-20 05:04:18 +02:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Async #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
_zsh_autosuggest_async_request() {
|
2019-06-19 21:35:54 +02:00
|
|
|
zmodload zsh/system 2>/dev/null # For `$sysparams`
|
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_ASYNC_FD _ZSH_AUTOSUGGEST_CHILD_PID
|
|
|
|
|
|
|
|
# If we've got a pending request, cancel it
|
|
|
|
if [[ -n "$_ZSH_AUTOSUGGEST_ASYNC_FD" ]] && { true <&$_ZSH_AUTOSUGGEST_ASYNC_FD } 2>/dev/null; then
|
|
|
|
# Close the file descriptor and remove the handler
|
|
|
|
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&-
|
|
|
|
zle -F $_ZSH_AUTOSUGGEST_ASYNC_FD
|
|
|
|
|
2019-06-19 21:35:54 +02:00
|
|
|
# We won't know the pid unless the user has zsh/system module installed
|
|
|
|
if [[ -n "$_ZSH_AUTOSUGGEST_CHILD_PID" ]]; then
|
|
|
|
# Zsh will make a new process group for the child process only if job
|
|
|
|
# control is enabled (MONITOR option)
|
|
|
|
if [[ -o MONITOR ]]; then
|
|
|
|
# Send the signal to the process group to kill any processes that may
|
|
|
|
# have been forked by the suggestion strategy
|
|
|
|
kill -TERM -$_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null
|
|
|
|
else
|
|
|
|
# Kill just the child process since it wasn't placed in a new process
|
|
|
|
# group. If the suggestion strategy forked any child processes they may
|
|
|
|
# be orphaned and left behind.
|
|
|
|
kill -TERM $_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null
|
|
|
|
fi
|
2018-06-11 10:06:18 +02:00
|
|
|
fi
|
|
|
|
fi
|
2018-12-17 04:43:25 +01:00
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# Fork a process to fetch a suggestion and open a pipe to read from it
|
|
|
|
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}< <(
|
|
|
|
# Tell parent process our pid
|
|
|
|
echo $sysparams[pid]
|
2018-12-17 04:43:25 +01:00
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# Fetch and print the suggestion
|
|
|
|
local suggestion
|
|
|
|
_zsh_autosuggest_fetch_suggestion "$1"
|
|
|
|
echo -nE "$suggestion"
|
|
|
|
)
|
2018-12-17 04:43:25 +01:00
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# There's a weird bug here where ^C stops working unless we force a fork
|
|
|
|
# See https://github.com/zsh-users/zsh-autosuggestions/issues/364
|
2021-05-15 07:22:14 +02:00
|
|
|
autoload -Uz is-at-least
|
|
|
|
is-at-least 5.8 || command true
|
2016-07-20 05:04:18 +02:00
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# Read the pid from the child process
|
|
|
|
read _ZSH_AUTOSUGGEST_CHILD_PID <&$_ZSH_AUTOSUGGEST_ASYNC_FD
|
2018-06-13 07:45:29 +02:00
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# When the fd is readable, call the response handler
|
|
|
|
zle -F "$_ZSH_AUTOSUGGEST_ASYNC_FD" _zsh_autosuggest_async_response
|
2017-01-27 23:18:26 +01:00
|
|
|
}
|
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# Called when new data is ready to be read from the pipe
|
2017-01-25 03:53:26 +01:00
|
|
|
# First arg will be fd ready for reading
|
|
|
|
# Second arg will be passed in case of error
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_async_response() {
|
2019-04-10 17:51:33 +02:00
|
|
|
emulate -L zsh
|
|
|
|
|
2019-04-15 22:12:55 +02:00
|
|
|
local suggestion
|
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
if [[ -z "$2" || "$2" == "hup" ]]; then
|
|
|
|
# Read everything from the fd and give it as a suggestion
|
2019-04-15 22:12:55 +02:00
|
|
|
IFS='' read -rd '' -u $1 suggestion
|
|
|
|
zle autosuggest-suggest -- "$suggestion"
|
2018-12-17 04:43:25 +01:00
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# Close the fd
|
|
|
|
exec {1}<&-
|
2018-12-17 04:43:25 +01:00
|
|
|
fi
|
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# Always remove the handler
|
|
|
|
zle -F "$1"
|
2016-07-20 05:04:18 +02:00
|
|
|
}
|
|
|
|
|
2016-02-14 08:29:43 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Start #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Start the autosuggestion widgets
|
2016-02-07 22:21:57 +01:00
|
|
|
_zsh_autosuggest_start() {
|
2019-04-10 19:41:20 +02:00
|
|
|
# By default we 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. However this has
|
|
|
|
# a decent performance hit, so users can set ZSH_AUTOSUGGEST_MANUAL_REBIND
|
|
|
|
# to disable the automatic re-binding.
|
|
|
|
if (( ${+ZSH_AUTOSUGGEST_MANUAL_REBIND} )); then
|
|
|
|
add-zsh-hook -d precmd _zsh_autosuggest_start
|
|
|
|
fi
|
2017-02-18 18:47:53 +01:00
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
_zsh_autosuggest_bind_widgets
|
|
|
|
}
|
2016-02-07 22:21:57 +01:00
|
|
|
|
2020-01-27 05:15:47 +01:00
|
|
|
# Mark for auto-loading the functions that we use
|
|
|
|
autoload -Uz add-zsh-hook is-at-least
|
|
|
|
|
2020-01-27 05:18:01 +01:00
|
|
|
# Automatically enable asynchronous mode in newer versions of zsh. Disable for
|
|
|
|
# older versions because there is a bug when using async mode where ^C does not
|
|
|
|
# work immediately after fetching a suggestion.
|
|
|
|
# See https://github.com/zsh-users/zsh-autosuggestions/issues/364
|
|
|
|
if is-at-least 5.0.8; then
|
|
|
|
typeset -g ZSH_AUTOSUGGEST_USE_ASYNC=
|
|
|
|
fi
|
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
# Start the autosuggestion widgets on the next precmd
|
2016-02-07 22:21:57 +01:00
|
|
|
add-zsh-hook precmd _zsh_autosuggest_start
|