Merge pull request #298 from zsh-users/fixes/support_older_versions

Fixes/support older versions
This commit is contained in:
Eric Freese 2017-12-06 08:27:18 -07:00 committed by GitHub
commit 9f1046727a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 13 deletions

View file

@ -3,6 +3,7 @@ SRC_DIR := ./src
SRC_FILES := \ SRC_FILES := \
$(SRC_DIR)/setup.zsh \ $(SRC_DIR)/setup.zsh \
$(SRC_DIR)/config.zsh \ $(SRC_DIR)/config.zsh \
$(SRC_DIR)/util.zsh \
$(SRC_DIR)/features.zsh \ $(SRC_DIR)/features.zsh \
$(SRC_DIR)/bind.zsh \ $(SRC_DIR)/bind.zsh \
$(SRC_DIR)/highlight.zsh \ $(SRC_DIR)/highlight.zsh \

View file

@ -11,6 +11,8 @@ It suggests commands as you type, based on command history.
## Installation ## Installation
Requirements: Zsh v4.3.11 or later
### Manual ### Manual
1. Clone this repository somewhere on your machine. This guide will assume `~/.zsh/zsh-autosuggestions`. 1. Clone this repository somewhere on your machine. This guide will assume `~/.zsh/zsh-autosuggestions`.

View file

@ -1,12 +1,12 @@
machine: machine:
environment: environment:
ZSH_VERSIONS: 5.0.8 5.1.1 5.2 5.3.1 ZSH_VERSIONS: zsh-dev/4.3.11 zsh/5.0.2 zsh/5.0.8 zsh/5.1.1 zsh/5.2 zsh/5.3.1 zsh/5.4.2
dependencies: dependencies:
pre: pre:
- for v in $(echo $ZSH_VERSIONS | awk "{ for (i=$((1+CIRCLE_NODE_INDEX));i<=NF;i+=$CIRCLE_NODE_TOTAL) print \$i }"); do wget https://sourceforge.net/projects/zsh/files/zsh/$v/zsh-$v.tar.gz && tar xzf zsh-$v.tar.gz && cd zsh-$v && ./configure && sudo make install || exit 1; done - for v in $(echo $ZSH_VERSIONS | awk "{ for (i=$((1+CIRCLE_NODE_INDEX));i<=NF;i+=$CIRCLE_NODE_TOTAL) print \$i }"); do wget https://sourceforge.net/projects/zsh/files/$v/zsh-${v#*/}.tar.gz && tar xzf zsh-${v#*/}.tar.gz && pushd zsh-${v#*/} && ./configure && sudo make install && popd || exit 1; done
test: test:
override: override:
- for v in $(echo $ZSH_VERSIONS | awk "{ for (i=$((1+CIRCLE_NODE_INDEX));i<=NF;i+=$CIRCLE_NODE_TOTAL) print \$i }"); do TEST_ZSH_BIN=/usr/local/bin/zsh-$v make test || exit 1; done: - for v in $(echo $ZSH_VERSIONS | awk "{ for (i=$((1+CIRCLE_NODE_INDEX));i<=NF;i+=$CIRCLE_NODE_TOTAL) print \$i }"); do TEST_ZSH_BIN=/usr/local/bin/zsh-${v#*/} make test || exit 1; done:
parallel: true parallel: true

View file

@ -7,9 +7,19 @@
# #
_zsh_autosuggest_strategy_default() { _zsh_autosuggest_strategy_default() {
local prefix="$1" # 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}"
# Get the history items that match # Get the history items that match
# - (r) subscript flag makes the pattern match on values # - (r) subscript flag makes the pattern match on values
typeset -g suggestion="${history[(r)${(b)prefix}*]}" typeset -g suggestion="${history[(r)${prefix}*]}"
} }

View file

@ -21,18 +21,25 @@
# `HIST_EXPIRE_DUPS_FIRST`. # `HIST_EXPIRE_DUPS_FIRST`.
_zsh_autosuggest_strategy_match_prev_cmd() { _zsh_autosuggest_strategy_match_prev_cmd() {
local prefix="$1" # Reset options to defaults and enable LOCAL_OPTIONS
emulate -L zsh
# Enable globbing flags so that we can use (#m)
setopt EXTENDED_GLOB
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# Get all history event numbers that correspond to history # Get all history event numbers that correspond to history
# entries that match pattern $prefix* # entries that match pattern $prefix*
local history_match_keys local history_match_keys
history_match_keys=(${(k)history[(R)${(b)prefix}*]}) history_match_keys=(${(k)history[(R)$prefix*]})
# By default we use the first history number (most recent history entry) # By default we use the first history number (most recent history entry)
local histkey="${history_match_keys[1]}" local histkey="${history_match_keys[1]}"
# Get the previously executed command # Get the previously executed command
local prev_cmd="${history[$((HISTCMD-1))]}" local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")"
# Iterate up to the first 200 history event numbers that match $prefix # Iterate up to the first 200 history event numbers that match $prefix
for key in "${(@)history_match_keys[1,200]}"; do for key in "${(@)history_match_keys[1,200]}"; do

11
src/util.zsh Normal file
View file

@ -0,0 +1,11 @@
#--------------------------------------------------------------------#
# Utility Functions #
#--------------------------------------------------------------------#
_zsh_autosuggest_escape_command() {
setopt localoptions EXTENDED_GLOB
# Escape special chars in the string (requires EXTENDED_GLOB)
echo -E "${1//(#m)[\"\'\\()\[\]|*?~]/\\$MATCH}"
}

View file

@ -101,6 +101,17 @@ ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=
# Pty name for calculating autosuggestions asynchronously # Pty name for calculating autosuggestions asynchronously
ZSH_AUTOSUGGEST_ASYNC_PTY_NAME=zsh_autosuggest_pty ZSH_AUTOSUGGEST_ASYNC_PTY_NAME=zsh_autosuggest_pty
#--------------------------------------------------------------------#
# Utility Functions #
#--------------------------------------------------------------------#
_zsh_autosuggest_escape_command() {
setopt localoptions EXTENDED_GLOB
# Escape special chars in the string (requires EXTENDED_GLOB)
echo -E "${1//(#m)[\"\'\\()\[\]|*?~]/\\$MATCH}"
}
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Feature Detection # # Feature Detection #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
@ -478,11 +489,21 @@ zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle
# #
_zsh_autosuggest_strategy_default() { _zsh_autosuggest_strategy_default() {
local prefix="$1" # 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}"
# Get the history items that match # Get the history items that match
# - (r) subscript flag makes the pattern match on values # - (r) subscript flag makes the pattern match on values
typeset -g suggestion="${history[(r)${(b)prefix}*]}" typeset -g suggestion="${history[(r)${prefix}*]}"
} }
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
@ -507,18 +528,25 @@ _zsh_autosuggest_strategy_default() {
# `HIST_EXPIRE_DUPS_FIRST`. # `HIST_EXPIRE_DUPS_FIRST`.
_zsh_autosuggest_strategy_match_prev_cmd() { _zsh_autosuggest_strategy_match_prev_cmd() {
local prefix="$1" # Reset options to defaults and enable LOCAL_OPTIONS
emulate -L zsh
# Enable globbing flags so that we can use (#m)
setopt EXTENDED_GLOB
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# Get all history event numbers that correspond to history # Get all history event numbers that correspond to history
# entries that match pattern $prefix* # entries that match pattern $prefix*
local history_match_keys local history_match_keys
history_match_keys=(${(k)history[(R)${(b)prefix}*]}) history_match_keys=(${(k)history[(R)$prefix*]})
# By default we use the first history number (most recent history entry) # By default we use the first history number (most recent history entry)
local histkey="${history_match_keys[1]}" local histkey="${history_match_keys[1]}"
# Get the previously executed command # Get the previously executed command
local prev_cmd="${history[$((HISTCMD-1))]}" local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")"
# Iterate up to the first 200 history event numbers that match $prefix # Iterate up to the first 200 history event numbers that match $prefix
for key in "${(@)history_match_keys[1,200]}"; do for key in "${(@)history_match_keys[1,200]}"; do