Compare commits

..

41 Commits

Author SHA1 Message Date
Eric Freese
2a5791710a bump version v0.2.13 2016-02-23 18:14:12 -07:00
Eric Freese
03fac1f0d7 Revert "Use zle -w flag to set WIDGET appropriately when calling orig widget"
This reverts commit 70438d233d.
2016-02-23 18:13:03 -07:00
Eric Freese
aa859a282d bump version v0.2.12 2016-02-23 10:37:57 -07:00
Eric Freese
f08a5a1baa [Formatting] Remove extra space in test script. 2016-02-23 10:24:35 -07:00
Eric Freese
70438d233d Use zle -w flag to set WIDGET appropriately when calling orig widget 2016-02-23 10:21:35 -07:00
Eric Freese
ba029e83d0 bump version v0.2.11 2016-02-17 13:44:52 -07:00
Eric Freese
acc129de6c Fix error when using autosuggest widgets 2016-02-17 13:44:52 -07:00
Eric Freese
aa5ceee256 Make asciinema a bit smaller. 2016-02-16 21:55:56 -07:00
Eric Freese
113ca0ad10 Add asciinema recording to README 2016-02-16 21:35:44 -07:00
Eric Freese
2b449a62f8 bump version v0.2.10 2016-02-16 20:59:31 -07:00
Eric Freese
6d25df6864 Revert usage of fc for suggestions and fix for sh_word_split.
Force field splitting on \0 to support sh_word_split option.
2016-02-16 10:51:01 -07:00
Eric Freese
0faa2b6584 bump version v0.2.9 2016-02-16 09:34:35 -07:00
Eric Freese
a2d8d91196 Actually fix suggestions when sh_split_words option is enabled. 2016-02-16 09:33:26 -07:00
Eric Freese
dd9a8789a7 bump version v0.2.8 2016-02-16 07:59:36 -07:00
Eric Freese
1b98af5b33 Fix suggestions when sh_split_words option is enabled 2016-02-16 07:57:44 -07:00
Eric Freese
45ab49d1f2 bump version v0.2.7 2016-02-15 08:46:23 -07:00
Eric Freese
41f15d5c9f Forgot a pesky backslash 2016-02-15 08:45:52 -07:00
Eric Freese
3ce1adb55d bump version v0.2.6 2016-02-15 08:31:50 -07:00
Eric Freese
2461a98857 Fix segfaults once and for all? 2016-02-15 08:31:00 -07:00
Eric Freese
76f415bf43 bump version v0.2.5 2016-02-14 08:55:20 -07:00
Eric Freese
5e419da326 Remove list of modify widgets and make 'modify' the default behavior. 2016-02-14 08:54:34 -07:00
Eric Freese
cd71081303 bump version v0.2.4 2016-02-14 01:08:21 -07:00
Eric Freese
9788c2ee49 Fix deprecation warnings. 2016-02-14 01:07:05 -07:00
Eric Freese
ebcfc46b72 Comment formatting 2016-02-14 00:29:43 -07:00
Eric Freese
b49d002888 [README] Add example of minimal .zshrc. 2016-02-13 23:53:32 -07:00
Eric Freese
266437c98a bump version v0.2.3 2016-02-13 23:47:24 -07:00
Eric Freese
51b39e210e Bound widgets now handle arguments correctly 2016-02-13 23:46:34 -07:00
Eric Freese
011f5420fc Version bump v0.2.2 2016-02-13 21:15:33 -07:00
Eric Freese
1a38fbf6a5 [README] Add note about workaround for bpm bug. 2016-02-13 21:14:59 -07:00
Eric Freese
31452887d2 [Test] Add tests widgets 2016-02-13 14:13:21 -07:00
Eric Freese
cf146b6696 [Test] Clean up highlight tests 2016-02-13 14:13:21 -07:00
Eric Freese
74197498fc [Test] Add stub.sh from ericfreese/stub.sh for stubbing/mocking. 2016-02-13 14:13:21 -07:00
Eric Freese
abe577d519 Fix build 2016-02-13 14:13:21 -07:00
Eric Freese
0ae5907294 [README] Add note about Oh My Zsh paste bug. 2016-02-13 09:59:16 -07:00
Eric Freese
03bd381112 Add .plugin file and installation instructions for Oh My Zsh (#104). 2016-02-07 15:35:54 -07:00
Eric Freese
00bd0e9125 Use add-zsh-hook to remove need to call autosuggest_start. 2016-02-07 15:35:43 -07:00
Eric Freese
48d2dc1091 Remove absolute link in readme. 2016-02-07 14:50:00 -07:00
Eric Freese
f154d25fb3 Fix typo when setting up autosuggest-clear widget. 2016-02-07 08:58:09 -07:00
Eric Freese
e91db46ce0 Add Development section to README (#106) 2016-02-07 08:52:52 -07:00
Eric Freese
28836f15c5 Test script should be a .zsh file instead of .sh 2016-02-07 08:52:34 -07:00
Eric Freese
0dd1b7febb Add Troubleshooting section to README 2016-02-07 08:52:07 -07:00
18 changed files with 944 additions and 333 deletions

1
DESCRIPTION Normal file
View File

@@ -0,0 +1 @@
Fish-like fast/unobtrusive autosuggestions for zsh.

3
INFO
View File

@@ -1,3 +0,0 @@
Fish-like fast/unobtrusive autosuggestions for zsh.
https://github.com/tarruda/zsh-autosuggestions
v0.1.0

View File

@@ -1,8 +1,7 @@
DIST_DIR := ./dist
SRC_DIR := ./src
SCRIPT_DIR := ./script
SRC_TARGETS := \
SRC_FILES := \
$(SRC_DIR)/config.zsh \
$(SRC_DIR)/deprecated.zsh \
$(SRC_DIR)/bind.zsh \
@@ -11,17 +10,32 @@ SRC_TARGETS := \
$(SRC_DIR)/suggestion.zsh \
$(SRC_DIR)/start.zsh
$(DIST_DIR)/autosuggestions.zsh: $(SRC_TARGETS) LICENSE
mkdir -p $(DIST_DIR)
cat INFO | sed -e 's/^/# /g' > $@
echo "#" >> $@
cat LICENSE | sed -e 's/^/# /g' >> $@
cat >> $@ $(SRC_TARGETS)
HEADER_FILES := \
DESCRIPTION \
URL \
VERSION \
LICENSE
PLUGIN_TARGET := zsh-autosuggestions.zsh
OH_MY_ZSH_LINK_TARGET := zsh-autosuggestions.plugin.zsh
ALL_TARGETS := \
$(PLUGIN_TARGET) \
$(OH_MY_ZSH_LINK_TARGET)
all: $(ALL_TARGETS)
$(PLUGIN_TARGET): $(HEADER_FILES) $(SRC_FILES)
cat $(HEADER_FILES) | sed -e 's/^/# /g' > $@
cat $(SRC_FILES) >> $@
$(OH_MY_ZSH_LINK_TARGET): $(PLUGIN_TARGET)
ln -s $(PLUGIN_TARGET) $@
.PHONY: clean
clean:
rm -rf $(DIST_DIR)
rm $(ALL_TARGETS)
.PHONY: test
test: $(DIST_DIR)/autosuggestions.zsh $(SCRIPT_DIR)/test.sh
$(SCRIPT_DIR)/test.sh
test: all
$(SCRIPT_DIR)/test.zsh

103
README.md
View File

@@ -4,9 +4,12 @@ _[Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh._
It suggests commands as you type, based on command history.
<a href="https://asciinema.org/a/36578" target="_blank"><img src="https://asciinema.org/a/36578.png" width="520"/></a>
## Installation
### Manual
1. Clone this repository somewhere on your machine. This guide will assume `~/.zsh/zsh-autosuggestions`.
```sh
@@ -16,11 +19,27 @@ It suggests commands as you type, based on command history.
2. Add the following to your `.zshrc`:
```sh
source ~/.zsh/zsh-autosuggestions/dist/autosuggestions.zsh
autosuggest_start
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
```
**Note:** If you're using other zle plugins like `zsh-syntax-highlighting` or `zsh-history-substring-search`, check out the [section on compatibility](#compatibility-with-other-zle-plugins) below.
3. Start a new terminal session.
### Oh My Zsh
1. Clone this repository into `$ZSH_CUSTOM/plugins` (by default `~/.oh-my-zsh/custom/plugins`)
```sh
git clone git://github.com/tarruda/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
```
2. Add the plugin to the list of plugins for Oh My Zsh to load:
```sh
plugins=(zsh-autosuggestions)
```
3. Start a new terminal session.
## Usage
@@ -34,7 +53,9 @@ If you invoke the `forward-word` widget, it will partially accept the suggestion
## Configuration
You may want to override the default global config variables after sourcing the plugin. Default values of these variables can be found [here](https://github.com/tarruda/zsh-autosuggestions/blob/master/src/config.zsh).
You may want to override the default global config variables after sourcing the plugin. Default values of these variables can be found [here](src/config.zsh).
**Note:** If you are using Oh My Zsh, you can put this configuration in a file in the `$ZSH_CUSTOM` directory. See their comments on [overriding internals](https://github.com/robbyrussell/oh-my-zsh/wiki/Customization#overriding-internals).
### Suggestion Highlight Style
@@ -47,11 +68,10 @@ Set `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` to configure the style that the suggestion
This plugin works by triggering custom behavior when certain [zle widgets](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets) are invoked. You can add and remove widgets from these arrays to change the behavior of this plugin:
- `ZSH_AUTOSUGGEST_CLEAR_WIDGETS`: Widgets in this array will clear the suggestion when invoked.
- `ZSH_AUTOSUGGEST_MODIFY_WIDGETS`: Widgets in this array will modify the buffer and fetch a new suggestion when invoked.
- `ZSH_AUTOSUGGEST_ACCEPT_WIDGETS`: Widgets in this array will accept the suggestion when invoked.
- `ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS`: Widgets in this array will partially accept the suggestion when invoked.
**Note:** These arrays must be set before calling `autosuggest_start`.
Widgets not in any of these lists will update the suggestion when invoked.
**Note:** A widget shouldn't belong to more than one of the above arrays.
@@ -72,54 +92,30 @@ bindkey '^ ' autosuggest-accept
## Compatibility With Other ZLE Plugins
### [`zsh-syntax-highlighting`](https://github.com/zsh-users/zsh-syntax-highlighting)
Source `zsh-autosuggestions.zsh` *before* `zsh-syntax-highlighting`.
Call `autosuggest_start` *after* sourcing `zsh-syntax-highlighting`.
For example:
```sh
source ~/.zsh/zsh-autosuggestions/dist/autosuggestions.zsh
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
autosuggest_start
```
### [`zsh-history-substring-search`](https://github.com/zsh-users/zsh-history-substring-search)
When the buffer is empty and one of the `history-substring-search-up/down` widgets is invoked, it will call the `up/down-line-or-history` widget. If the `up/down-line-or-history` widgets are in `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` (the list of widgets that clear the suggestion), this can create an infinite recursion, crashing the shell session.
For best results, you'll want to remove `up-line-or-history` and `down-line-or-history` from `ZSH_AUTOSUGGEST_CLEAR_WIDGETS`:
```
# Remove *-line-or-history widgets from list of widgets that clear the autosuggestion to avoid conflict with history-substring-search-* widgets
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=("${(@)ZSH_AUTOSUGGEST_CLEAR_WIDGETS:#(up|down)-line-or-history}")
```
Additionally, the `history-substring-search-up` and `history-substring-search-down` widgets are not bound by default. You'll probably want to add them to `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` so that the suggestion will be cleared when you start searching through history:
The `history-substring-search-up` and `history-substring-search-down` widgets are not bound by default. You'll probably want to add them to `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` so that the suggestion will be cleared when you start searching through history:
```sh
# Add history-substring-search-* widgets to list of widgets that clear the autosuggestion
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down)
```
Make sure you add/remove these widgets *before* calling `autosuggest_start`.
For example:
## Troubleshooting
```sh
source ~/.zsh/zsh-autosuggestions/dist/autosuggestions.zsh
source ~/Code/zsh-history-substring-search/zsh-history-substring-search.zsh
If you have a problem, please search through [the list of issues on GitHub](https://github.com/tarruda/zsh-autosuggestions/issues) to see if someone else has already reported it.
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=("${(@)ZSH_AUTOSUGGEST_CLEAR_WIDGETS:#(up|down)-line-or-history}")
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down)
autosuggest_start
```
### Reporting an Issue
Before reporting an issue, please try temporarily disabling sections of your configuration and other plugins that may be conflicting with this plugin to isolate the problem.
When reporting an issue, please include:
- The smallest, simplest `.zshrc` configuration that will reproduce the problem. See [this comment](https://github.com/tarruda/zsh-autosuggestions/issues/102#issuecomment-180944764) for a good example of what this means.
- The version of zsh you're using (`zsh --version`)
- Which operating system you're running
## Uninstallation
@@ -133,6 +129,29 @@ autosuggest_start
```
## Development
### Build Process
Edit the source files in `src/`. Run `make` to build `zsh-autosuggestions.zsh` from those source files.
### Pull Requests
Pull requests are welcome! If you send a pull request, please:
- Match the existing coding conventions.
- Include helpful comments to keep the barrier-to-entry low for people new to the project.
- Write tests that cover your code as much as possible.
### Testing
Testing is performed with [`shunit2`](https://github.com/kward/shunit2) (v2.1.6). Documentation can be found [here](http://shunit2.googlecode.com/svn/trunk/source/2.1/doc/shunit2.html).
The test script lives at `script/test.zsh`. To run the tests, run `make test`.
## License
This project is licensed under [MIT license](http://opensource.org/licenses/MIT).

1
URL Normal file
View File

@@ -0,0 +1 @@
https://github.com/tarruda/zsh-autosuggestions

1
VERSION Normal file
View File

@@ -0,0 +1 @@
v0.2.13

View File

@@ -1,77 +0,0 @@
#!/usr/bin/env zsh
SCRIPT_DIR=$(dirname "$0")
TEST_DIR=$SCRIPT_DIR/../test
DIST_DIR=$SCRIPT_DIR/../dist
source $DIST_DIR/autosuggestions.zsh
testDefaultHighlightStyle() {
assertEquals \
"fg=8" \
"$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
}
testHighlightApplyWithSuggestion() {
orig_style=ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=4"
BUFFER="ec"
POSTDISPLAY="ho hello"
region_highlight=("0 2 fg=1")
_zsh_autosuggest_highlight_apply
assertEquals \
"adds to region_highlight with correct style" \
"0 2 fg=1 2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
"$region_highlight"
assertEquals \
"saves the higlight to be removed later" \
"2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=orig_style
}
testHighlightApplyWithoutSuggestion() {
BUFFER="echo hello"
POSTDISPLAY=""
region_highlight=("0 4 fg=1")
_zsh_autosuggest_highlight_apply
assertEquals \
"leaves region_highlight alone" \
"0 4 fg=1" \
"$region_highlight"
assertNull \
"clears the last highlight" \
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
}
testHighlightReset() {
BUFFER="ec"
POSTDISPLAY="ho hello"
region_highlight=("0 1 fg=1" "2 10 fg=8" "1 2 fg=1")
_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="2 10 fg=8"
_zsh_autosuggest_highlight_reset
assertEquals \
"removes last highlight region" \
"0 1 fg=1 1 2 fg=1" \
"$region_highlight"
assertNull \
"clears the last highlight" \
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
}
# For zsh compatibility
setopt shwordsplit
SHUNIT_PARENT=$0
source $TEST_DIR/shunit2-2.1.6/src/shunit2

288
script/test.zsh Executable file
View File

@@ -0,0 +1,288 @@
#!/usr/bin/env zsh
SCRIPT_DIR=$(dirname "$0")
TEST_DIR=$SCRIPT_DIR/../test
DIST_DIR=$SCRIPT_DIR/../
# Use stub.sh for stubbing/mocking
source $TEST_DIR/stub-1.0.2.sh
source $DIST_DIR/zsh-autosuggestions.zsh
#--------------------------------------------------------------------#
# Highlighting #
#--------------------------------------------------------------------#
testHighlightDefaultStyle() {
assertEquals \
"fg=8" \
"$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
}
testHighlightApplyWithSuggestion() {
orig_style=ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=4"
BUFFER="ec"
POSTDISPLAY="ho hello"
region_highlight=("0 2 fg=1")
_zsh_autosuggest_highlight_apply
assertEquals \
"highlight did not use correct style" \
"0 2 fg=1 2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
"$region_highlight"
assertEquals \
"higlight was not saved to be removed later" \
"2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=orig_style
}
testHighlightApplyWithoutSuggestion() {
BUFFER="echo hello"
POSTDISPLAY=""
region_highlight=("0 4 fg=1")
_zsh_autosuggest_highlight_apply
assertEquals \
"region_highlight was modified" \
"0 4 fg=1" \
"$region_highlight"
assertNull \
"last highlight region was not cleared" \
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
}
testHighlightReset() {
BUFFER="ec"
POSTDISPLAY="ho hello"
region_highlight=("0 1 fg=1" "2 10 fg=8" "1 2 fg=1")
_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="2 10 fg=8"
_zsh_autosuggest_highlight_reset
assertEquals \
"last highlight region was not removed" \
"0 1 fg=1 1 2 fg=1" \
"$region_highlight"
assertNull \
"last highlight variable was not cleared" \
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
}
#--------------------------------------------------------------------#
# Widgets #
#--------------------------------------------------------------------#
testWidgetFunctionClear() {
BUFFER="ec"
POSTDISPLAY="ho hello"
_zsh_autosuggest_clear "original-widget"
assertEquals \
"BUFFER was modified" \
"ec" \
"$BUFFER"
assertNull \
"POSTDISPLAY was not cleared" \
"$POSTDISPLAY"
}
testWidgetFunctionModify() {
BUFFER=""
POSTDISPLAY=""
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'BUFFER+="e"'
stub_and_echo \
_zsh_autosuggest_suggestion \
"echo hello"
_zsh_autosuggest_modify "original-widget"
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was not modified" \
"e" \
"$BUFFER"
assertEquals \
"POSTDISPLAY does not contain suggestion" \
"cho hello" \
"$POSTDISPLAY"
restore _zsh_autosuggest_invoke_original_widget
restore _zsh_autosuggest_suggestion
}
testWidgetFunctionAcceptCursorAtEnd() {
BUFFER="echo"
POSTDISPLAY=" hello"
CURSOR=4
stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_accept "original-widget"
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was not modified" \
"echo hello" \
"$BUFFER"
assertEquals \
"POSTDISPLAY was not cleared" \
"" \
"$POSTDISPLAY"
}
testWidgetFunctionAcceptCursorNotAtEnd() {
BUFFER="echo"
POSTDISPLAY=" hello"
CURSOR=2
stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_accept "original-widget"
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was modified" \
"echo" \
"$BUFFER"
assertEquals \
"POSTDISPLAY was modified" \
" hello" \
"$POSTDISPLAY"
}
testWidgetFunctionPartialAcceptCursorMovesOutOfBuffer() {
BUFFER="ec"
POSTDISPLAY="ho hello"
CURSOR=1
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
_zsh_autosuggest_partial_accept "original-widget"
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was not modified correctly" \
"echo " \
"$BUFFER"
assertEquals \
"POSTDISPLAY was not modified correctly" \
"hello" \
"$POSTDISPLAY"
}
testWidgetFunctionPartialAcceptCursorStaysInBuffer() {
BUFFER="echo hello"
POSTDISPLAY=" world"
CURSOR=1
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
_zsh_autosuggest_partial_accept "original-widget"
assertTrue \
"original widget not invoked" \
"stub_called _zsh_autosuggest_invoke_original_widget"
assertEquals \
"BUFFER was modified" \
"echo hello" \
"$BUFFER"
assertEquals \
"POSTDISPLAY was modified" \
" world" \
"$POSTDISPLAY"
}
testWidgetAccept() {
stub _zsh_autosuggest_highlight_reset
stub _zsh_autosuggest_accept
stub _zsh_autosuggest_highlight_apply
# Call the function pointed to by the widget since we can't call
# the widget itself when zle is not active
${widgets[autosuggest-accept]#*:} "original-widget"
assertTrue \
"autosuggest-accept widget does not exist" \
"zle -l autosuggest-accept"
assertTrue \
"highlight_reset was not called" \
"stub_called _zsh_autosuggest_highlight_reset"
assertTrue \
"widget function was not called" \
"stub_called _zsh_autosuggest_accept"
assertTrue \
"highlight_apply was not called" \
"stub_called _zsh_autosuggest_highlight_apply"
}
testWidgetClear() {
stub _zsh_autosuggest_highlight_reset
stub _zsh_autosuggest_clear
stub _zsh_autosuggest_highlight_apply
# Call the function pointed to by the widget since we can't call
# the widget itself when zle is not active
${widgets[autosuggest-clear]#*:} "original-widget"
assertTrue \
"autosuggest-clear widget does not exist" \
"zle -l autosuggest-clear"
assertTrue \
"highlight_reset was not called" \
"stub_called _zsh_autosuggest_highlight_reset"
assertTrue \
"widget function was not called" \
"stub_called _zsh_autosuggest_clear"
assertTrue \
"highlight_apply was not called" \
"stub_called _zsh_autosuggest_highlight_apply"
}
# For zsh compatibility
setopt shwordsplit
SHUNIT_PARENT=$0
source $TEST_DIR/shunit2-2.1.6/src/shunit2

View File

@@ -1,15 +1,15 @@
#----------------#
# Widget Helpers #
#----------------#
#--------------------------------------------------------------------#
# Widget Helpers #
#--------------------------------------------------------------------#
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
_zsh_autosuggest_bind_widget() {
local widget=$1
local autosuggest_function=$2
local autosuggest_action=$2
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
local action
# Save a reference to the original widget
case $widgets[$widget] in
# Already bound
user:_zsh_autosuggest_(bound|orig)_*);;
@@ -31,19 +31,14 @@ _zsh_autosuggest_bind_widget() {
;;
esac
# Set up widget to call $autosuggest_function if it exists
# Otherwise just call the original widget
if [ -n "$autosuggest_function" ]; then;
action=$autosuggest_function;
else;
action="zle $prefix$widget \$@"
fi
# Create new function for the widget that highlights and calls the action
# 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`).
eval "_zsh_autosuggest_bound_$widget() {
_zsh_autosuggest_highlight_reset
$action
_zsh_autosuggest_highlight_apply
_zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@
}"
# Create the bound widget
@@ -55,26 +50,30 @@ _zsh_autosuggest_bind_widgets() {
local widget;
# Find every widget we might want to bind and bind it appropriately
for widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|autosuggest-*|$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX*|run-help|which-command|beep|set-local-history|yank)}; do
if [ ${ZSH_AUTOSUGGEST_MODIFY_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_modify
elif [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_clear
for widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|autosuggest-*|$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX*|zle-line-*|run-help|which-command|beep|set-local-history|yank)}; do
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_accept
_zsh_autosuggest_bind_widget $widget accept
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_partial_accept
_zsh_autosuggest_bind_widget $widget partial_accept
else
_zsh_autosuggest_bind_widget $widget
# Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
fi
done
}
# Given the name of a widget, invoke the original we saved, if it exists
# Given the name of an original widget and args, invoke it, if it exists
_zsh_autosuggest_invoke_original_widget() {
local original_widget_name="$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX$1"
# Do nothing unless called with at least one arg
[ $# -gt 0 ] || return
local original_widget_name=$1
shift
if [ $widgets[$original_widget_name] ]; then
zle $original_widget_name
zle $original_widget_name -- $@
fi
}

View File

@@ -1,7 +1,7 @@
#--------------------------------#
# Global Configuration Variables #
#--------------------------------#
#--------------------------------------------------------------------#
# Global Configuration Variables #
#--------------------------------------------------------------------#
# Color to use when highlighting suggestion
# Uses format of `region_highlight`
@@ -22,27 +22,6 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
accept-line
)
# Widgets that modify the suggestion
ZSH_AUTOSUGGEST_MODIFY_WIDGETS=(
list-choices
complete-word
menu-complete
menu-expand-or-complete
reverse-menu-complete
expand-or-complete
expand-or-complete-prefix
self-insert
magic-space
bracketed-paste
expand-cmd-path
accept-and-menu-complete
backward-delete-char
vi-backward-delete-char
delete-char
vi-delete-char
delete-char-or-list
)
# Widgets that accept the entire suggestion
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
forward-char

View File

@@ -1,9 +1,11 @@
#-------------------------------------#
# Handle Deprecated Variables/Widgets #
#-------------------------------------#
#--------------------------------------------------------------------#
# Handle Deprecated Variables/Widgets #
#--------------------------------------------------------------------#
unset _ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN
_zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_check_deprecated_config() {
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then
@@ -23,17 +25,12 @@ _zsh_autosuggest_check_deprecated_config() {
fi
}
_zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_deprecated_start_widget() {
if [ -z "$_ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN" ]; then
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. Use the autosuggest_start function instead. For more info, see README at https://github.com/tarruda/zsh-autosuggestions."
_ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN=true
fi
autosuggest_start
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. For more info, see the README at https://github.com/tarruda/zsh-autosuggestions."
zle -D autosuggest-start
eval "zle-line-init() {
$(echo $functions[${widgets[zle-line-init]#*:}] | sed -e 's/zle autosuggest-start//g')
}"
}
zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget

View File

@@ -1,7 +1,7 @@
#--------------#
# Highlighting #
#--------------#
#--------------------------------------------------------------------#
# Highlighting #
#--------------------------------------------------------------------#
# If there was a highlight, remove it
_zsh_autosuggest_highlight_reset() {

View File

@@ -1,10 +1,13 @@
#-------#
# Start #
#-------#
#--------------------------------------------------------------------#
# Start #
#--------------------------------------------------------------------#
# Start the autosuggestion widgets
autosuggest_start() {
_zsh_autosuggest_start() {
_zsh_autosuggest_check_deprecated_config
_zsh_autosuggest_bind_widgets
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd _zsh_autosuggest_start

View File

@@ -1,19 +1,19 @@
#------------#
# Suggestion #
#------------#
#--------------------------------------------------------------------#
# Suggestion #
#--------------------------------------------------------------------#
# Get a suggestion from history that matches a given prefix
_zsh_autosuggest_suggestion() {
setopt localoptions extendedglob
# Escape the prefix (requires EXTENDED_GLOB)
local prefix=${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
local prefix="${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH}"
# Get all history items (reversed) that match pattern $prefix*
local history_matches
history_matches=(${history[(R)$prefix*]})
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
# Echo the first item that matches
echo ${history_matches[1]}
echo "$history_matches[1]"
}

View File

@@ -1,25 +1,25 @@
#------------------------------------#
# Autosuggest Widget Implementations #
#------------------------------------#
#--------------------------------------------------------------------#
# Autosuggest Widget Implementations #
#--------------------------------------------------------------------#
# Clear the suggestion
_zsh_autosuggest_clear() {
# Remove the suggestion
unset POSTDISPLAY
_zsh_autosuggest_invoke_original_widget $WIDGET
_zsh_autosuggest_invoke_original_widget $@
}
# Modify the buffer and get a new suggestion
_zsh_autosuggest_modify() {
# Original widget modifies the buffer
_zsh_autosuggest_invoke_original_widget $WIDGET
_zsh_autosuggest_invoke_original_widget $@
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
suggestion=$(_zsh_autosuggest_suggestion $BUFFER)
suggestion=$(_zsh_autosuggest_suggestion "$BUFFER")
fi
# Add the suggestion to the POSTDISPLAY
@@ -44,7 +44,7 @@ _zsh_autosuggest_accept() {
CURSOR=${#BUFFER}
fi
_zsh_autosuggest_invoke_original_widget $WIDGET
_zsh_autosuggest_invoke_original_widget $@
}
# Partially accept the suggestion
@@ -56,7 +56,7 @@ _zsh_autosuggest_partial_accept() {
BUFFER="$BUFFER$POSTDISPLAY"
# Original widget moves the cursor
_zsh_autosuggest_invoke_original_widget $WIDGET
_zsh_autosuggest_invoke_original_widget $@
# If we've moved past the end of the original buffer
if [ $CURSOR -gt $#original_buffer ]; then
@@ -71,17 +71,13 @@ _zsh_autosuggest_partial_accept() {
fi
}
_zsh_autosuggest_widget_accept() {
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_accept
_zsh_autosuggest_highlight_apply
}
_zsh_autosuggest_widget_clear() {
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_clear
_zsh_autosuggest_highlight_apply
}
for action in clear modify accept partial_accept; do
eval "_zsh_autosuggest_widget_$action() {
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_$action \$@
_zsh_autosuggest_highlight_apply
}"
done
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
zle -N autosuggest-clear _zsh_autosuggest_clear
zle -N autosuggest-clear _zsh_autosuggest_widget_clear

419
test/stub-1.0.2.sh Normal file
View File

@@ -0,0 +1,419 @@
# !/usr/bin/env bash
#
# stub.sh 1.0.2 - stubbing helpers for simplifying bash script tests.
# https://github.com/jimeh/stub.sh
#
# (The MIT License)
#
# Copyright (c) 2014 Jim Myhrberg.
#
# 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.
#
# Public: Stub given command.
#
# Arguments:
# - $1: Name of command to stub.
# - $2: (optional) When set to "STDOUT", echo a default message to STDOUT.
# When set to "STDERR", echo default message to STDERR.
#
# Echoes nothing.
# Returns nothing.
stub() {
local redirect="null"
if [ "$2" = "stdout" ] || [ "$2" = "STDOUT" ]; then redirect=""; fi
if [ "$2" = "stderr" ] || [ "$2" = "STDERR" ]; then redirect="stderr"; fi
stub_and_echo "$1" "$1 stub: \$@" "$redirect"
}
# Public: Stub given command, and echo given string.
#
# Arguments:
# - $1: Name of command to stub.
# - $2: String to echo when stub is called.
# - $3: (optional) When set to "STDERR", echo to STDERR instead of STDOUT.
# When set to "null", all output is redirected to /dev/null.
#
# Echoes nothing.
# Returns nothing.
stub_and_echo() {
local redirect=""
if [ "$3" = "stderr" ] || [ "$3" = "STDERR" ]; then redirect=" 1>&2"; fi
if [ "$3" = "null" ]; then redirect=" &>/dev/null"; fi
stub_and_eval "$1" "echo \"$2\"$redirect"
}
# Public: Stub given command, and execute given string with eval.
#
# Arguments:
# - $1: Name of command to stub.
# - $2: String to eval when stub is called.
#
# Echoes nothing.
# Returns nothing.
stub_and_eval() {
local cmd="$1"
# Setup empty list of active stubs.
if [ -z "$STUB_ACTIVE_STUBS" ]; then STUB_ACTIVE_STUBS=(); fi
# If stubbing a function, store non-stubbed copy of it required for restore.
if [ -n "$(command -v "$cmd")" ]; then
if [ -z "$(command -v "non_stubbed_${cmd}")" ]; then
if [[ "$(type "$cmd" | head -1)" == *"is a function" ]]; then
local source="$(type "$cmd" | tail -n +2)"
source="${source/$cmd/non_stubbed_${cmd}}"
eval "$source"
fi
fi
fi
# Prepare stub index and call list for this stub.
__stub_register "$cmd"
# Keep track of what is currently stubbed to ensure restore only acts on
# actual stubs.
if [[ " ${STUB_ACTIVE_STUBS[@]} " != *" $cmd "* ]]; then
STUB_ACTIVE_STUBS+=("$cmd")
fi
# Create the stub.
eval "$( printf "%s" "${cmd}() { __stub_call \"${cmd}\" \$@; $2;}")"
}
# Public: Find out if stub has been called. Returns 0 if yes, 1 if no.
#
# Arguments:
# - $1: Name of stubbed command.
#
# Echoes nothing.
# Returns 0 (success) is stub has been called, 1 (error) otherwise.
stub_called() {
if [ "$(stub_called_times "$1")" -lt 1 ]; then
return 1
fi
}
# Public: Find out if stub has been called with specific arguments.
#
# Arguments:
# - $1: Name of stubbed command.
# - $@: Any/all additional arguments are used to specify what stub was
# called with.
#
# Examples:
# stub uname
# uname
# uname -r -a
# stub_called_with uname # Returns 0 (success).
# stub_called_with uname -r # Returns 1 (error).
# stub_called_with uname -r -a # Returns 0 (success).
#
# Echoes nothing.
# Returns 0 (success) if specified stub has been called with given arguments,
# otherwise returns 1 (error).
stub_called_with() {
local cmd="$1"
shift 1
if [ "$(stub_called_with_times "$cmd" $@)" -lt 1 ]; then
return 1
fi
}
# Public: Find out how many times a stub has been called.
#
# Arguments:
# - $1: Name of stubbed command.
#
# Echoes number of times stub has been called if $2 is not given, otherwise
# echoes nothing.
# Returns 0 (success) if $2 is not given, or if it is given and it matches the
# number of times the stub has been called. Otherwise 1 (error) is returned if
# it doesn't match..
stub_called_times() {
local cmd="$1"
local index="$(__stub_index "$1")"
local count=0
if [ -n "$index" ]; then
eval "count=\"\${#STUB_${index}_CALLS[@]}\""
fi
echo $count
}
# Public: Find out if stub has been called exactly the given number of times
# with specified arguments.
#
# Arguments:
# - $1: Name of stubbed command.
# - $2: Exact number of times stub has been called.
#
# Echoes nothing.
# Returns 0 (success) if stub has been called at least the given number of
# times with specified arguments, otherwise 1 (error) is returned.
stub_called_exactly_times() {
if [ "$(stub_called_times "$1")" != "$2" ]; then
return 1
fi
}
# Public: Find out if stub has been called at least the given number of times.
#
# Arguments:
# - $1: Name of stubbed command.
# - $2: Minimum required number of times stub has been called.
#
# Echoes nothing.
# Returns 0 (success) if stub has been called at least the given number of
# times, otherwise 1 (error) is returned.
stub_called_at_least_times() {
if [ "$(stub_called_times "$1")" -lt "$2" ]; then
return 1
fi
}
# Public: Find out if stub has been called no more than the given number of
# times.
#
# Arguments:
# - $1: Name of stubbed command.
# - $2: Maximum allowed number of times stub has been called.
#
# Echoes nothing.
# Returns 0 (success) if stub has been called no more than the given number of
# times, otherwise 1 (error) is returned.
stub_called_at_most_times() {
if [ "$(stub_called_times "$1")" -gt "$2" ]; then
return 1
fi
}
# Public: Find out how many times a stub has been called with specific
# arguments.
#
# Arguments:
# - $1: Name of stubbed command.
# - $@: Any/all additional arguments are used to specify what stub was
# called with.
#
# Echoes number of times stub has been called with given arguments.
# Return 0 (success).
stub_called_with_times() {
local cmd="$1"
shift 1
local args="$@"
if [ "$args" = "" ]; then args="<none>"; fi
local count=0
local index="$(__stub_index "$cmd")"
if [ -n "$index" ]; then
eval "local calls=(\"\${STUB_${index}_CALLS[@]}\")"
for call in "${calls[@]}"; do
if [ "$call" = "$args" ]; then ((count++)); fi
done
fi
echo $count
}
# Public: Find out if stub has been called exactly the given number of times
# with specified arguments.
#
# Arguments:
# - $1: Name of stubbed command.
# - $2: Exact number of times stub has been called.
# - $@: Any/all additional arguments are used to specify what stub was
# called with.
#
# Echoes nothing.
# Returns 0 (success) if stub has been called at least the given number of
# times with specified arguments, otherwise 1 (error) is returned.
stub_called_with_exactly_times() {
local cmd="$1"
local count="$2"
shift 2
if [ "$(stub_called_with_times "$cmd" $@)" != "$count" ]; then
return 1
fi
}
# Public: Find out if stub has been called at least the given number of times
# with specified arguments.
#
# Arguments:
# - $1: Name of stubbed command.
# - $2: Minimum required number of times stub has been called.
# - $@: Any/all additional arguments are used to specify what stub was
# called with.
#
# Echoes nothing.
# Returns 0 (success) if stub has been called at least the given number of
# times with specified arguments, otherwise 1 (error) is returned.
stub_called_with_at_least_times() {
local cmd="$1"
local count="$2"
shift 2
if [ "$(stub_called_with_times "$cmd" $@)" -lt "$count" ]; then
return 1
fi
}
# Public: Find out if stub has been called no more than the given number of
# times.
#
# Arguments:
# - $1: Name of stubbed command.
# - $2: Maximum allowed number of times stub has been called.
# - $@: Any/all additional arguments are used to specify what stub was
# called with.
#
# Echoes nothing.
# Returns 0 (success) if stub has been called no more than the given number of
# times with specified arguments, otherwise 1 (error) is returned.
stub_called_with_at_most_times() {
local cmd="$1"
local count="$2"
shift 2
if [ "$(stub_called_with_times "$cmd" $@)" -gt "$count" ]; then
return 1
fi
}
# Public: Restore the original command/function that was stubbed.
#
# Arguments:
# - $1: Name of command to restore.
#
# Echoes nothing.
# Returns nothing.
restore() {
local cmd="$1"
# Don't do anything if the command isn't currently stubbed.
if [[ " ${STUB_ACTIVE_STUBS[@]} " != *" $1 "* ]]; then
return 0
fi
# Remove stub functions.
unset -f "$cmd"
# Remove stub from list of active stubs.
STUB_ACTIVE_STUBS=(${STUB_ACTIVE_STUBS[@]/$cmd/})
# If stub was for a function, restore the original function.
if type "non_stubbed_${cmd}" &>/dev/null; then
local original_type="$(type "non_stubbed_${cmd}" | head -1)"
if [[ "$original_type" == *"is a function" ]]; then
local source="$(type "non_stubbed_$cmd" | tail -n +2)"
source="${source/non_stubbed_${cmd}/$cmd}"
eval "$source"
unset -f "non_stubbed_${cmd}"
fi
fi
}
#
# Internal functions
#
# Private: Used to keep track of which stubs have been called and how many
# times.
__stub_call() {
local cmd="$1"
shift 1
local args="$@"
if [ "$args" = "" ]; then args="<none>"; fi
local index="$(__stub_index "$cmd")"
if [ -n "$index" ]; then
eval "STUB_${index}_CALLS+=(\"\$args\")"
fi
}
# Private: Get index value of stub. Required to access list of stub calls.
__stub_index() {
local cmd="$1"
for item in ${STUB_INDEX[@]}; do
if [[ "$item" = "${cmd}="* ]]; then
local index="$item"
index="${index/${cmd}=/}"
echo "$index"
fi
done
}
# Private: Prepare for the creation of a new stub. Adds stub to index and
# sets up an empty call list.
__stub_register() {
local cmd="$1"
if [ -z "$STUB_NEXT_INDEX" ]; then STUB_NEXT_INDEX=0; fi
if [ -z "$STUB_INDEX" ]; then STUB_INDEX=(); fi
# Clean up after any previous stub for the same command.
__stub_clean "$cmd"
# Add stub to index.
STUB_INDEX+=("${cmd}=${STUB_NEXT_INDEX}")
eval "STUB_${STUB_NEXT_INDEX}_CALLS=()"
# Increment stub count.
((STUB_NEXT_INDEX++))
}
# Private: Cleans out and removes a stub's call list, and removes stub from
# index.
__stub_clean() {
local cmd="$1"
local index="$(__stub_index "$cmd")"
# Remove all relevant details from any previously existing stub for the same
# command.
if [ -n "$index" ]; then
eval "unset STUB_${index}_CALLS"
STUB_INDEX=(${STUB_INDEX[@]/${cmd}=*/})
fi
}

View File

@@ -0,0 +1 @@
zsh-autosuggestions.zsh

View File

@@ -1,7 +1,6 @@
# Fish-like fast/unobtrusive autosuggestions for zsh.
# https://github.com/tarruda/zsh-autosuggestions
# v0.1.0
#
# v0.2.13
# Copyright (c) 2013 Thiago de Arruda
# Copyright (c) 2016 Eric Freese
#
@@ -26,9 +25,9 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#--------------------------------#
# Global Configuration Variables #
#--------------------------------#
#--------------------------------------------------------------------#
# Global Configuration Variables #
#--------------------------------------------------------------------#
# Color to use when highlighting suggestion
# Uses format of `region_highlight`
@@ -49,27 +48,6 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
accept-line
)
# Widgets that modify the suggestion
ZSH_AUTOSUGGEST_MODIFY_WIDGETS=(
list-choices
complete-word
menu-complete
menu-expand-or-complete
reverse-menu-complete
expand-or-complete
expand-or-complete-prefix
self-insert
magic-space
bracketed-paste
expand-cmd-path
accept-and-menu-complete
backward-delete-char
vi-backward-delete-char
delete-char
vi-delete-char
delete-char-or-list
)
# Widgets that accept the entire suggestion
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
forward-char
@@ -87,11 +65,13 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
vi-forward-blank-word-end
)
#-------------------------------------#
# Handle Deprecated Variables/Widgets #
#-------------------------------------#
#--------------------------------------------------------------------#
# Handle Deprecated Variables/Widgets #
#--------------------------------------------------------------------#
unset _ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN
_zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_check_deprecated_config() {
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then
@@ -111,32 +91,27 @@ _zsh_autosuggest_check_deprecated_config() {
fi
}
_zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_deprecated_start_widget() {
if [ -z "$_ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN" ]; then
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. Use the autosuggest_start function instead. For more info, see README at https://github.com/tarruda/zsh-autosuggestions."
_ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN=true
fi
autosuggest_start
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. For more info, see the README at https://github.com/tarruda/zsh-autosuggestions."
zle -D autosuggest-start
eval "zle-line-init() {
$(echo $functions[${widgets[zle-line-init]#*:}] | sed -e 's/zle autosuggest-start//g')
}"
}
zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget
#----------------#
# Widget Helpers #
#----------------#
#--------------------------------------------------------------------#
# Widget Helpers #
#--------------------------------------------------------------------#
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
_zsh_autosuggest_bind_widget() {
local widget=$1
local autosuggest_function=$2
local autosuggest_action=$2
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
local action
# Save a reference to the original widget
case $widgets[$widget] in
# Already bound
user:_zsh_autosuggest_(bound|orig)_*);;
@@ -158,19 +133,14 @@ _zsh_autosuggest_bind_widget() {
;;
esac
# Set up widget to call $autosuggest_function if it exists
# Otherwise just call the original widget
if [ -n "$autosuggest_function" ]; then;
action=$autosuggest_function;
else;
action="zle $prefix$widget \$@"
fi
# Create new function for the widget that highlights and calls the action
# 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`).
eval "_zsh_autosuggest_bound_$widget() {
_zsh_autosuggest_highlight_reset
$action
_zsh_autosuggest_highlight_apply
_zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@
}"
# Create the bound widget
@@ -182,33 +152,37 @@ _zsh_autosuggest_bind_widgets() {
local widget;
# Find every widget we might want to bind and bind it appropriately
for widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|autosuggest-*|$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX*|run-help|which-command|beep|set-local-history|yank)}; do
if [ ${ZSH_AUTOSUGGEST_MODIFY_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_modify
elif [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_clear
for widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|autosuggest-*|$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX*|zle-line-*|run-help|which-command|beep|set-local-history|yank)}; do
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_accept
_zsh_autosuggest_bind_widget $widget accept
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_partial_accept
_zsh_autosuggest_bind_widget $widget partial_accept
else
_zsh_autosuggest_bind_widget $widget
# Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
fi
done
}
# Given the name of a widget, invoke the original we saved, if it exists
# Given the name of an original widget and args, invoke it, if it exists
_zsh_autosuggest_invoke_original_widget() {
local original_widget_name="$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX$1"
# Do nothing unless called with at least one arg
[ $# -gt 0 ] || return
local original_widget_name=$1
shift
if [ $widgets[$original_widget_name] ]; then
zle $original_widget_name
zle $original_widget_name -- $@
fi
}
#--------------#
# Highlighting #
#--------------#
#--------------------------------------------------------------------#
# Highlighting #
#--------------------------------------------------------------------#
# If there was a highlight, remove it
_zsh_autosuggest_highlight_reset() {
@@ -228,27 +202,27 @@ _zsh_autosuggest_highlight_apply() {
fi
}
#------------------------------------#
# Autosuggest Widget Implementations #
#------------------------------------#
#--------------------------------------------------------------------#
# Autosuggest Widget Implementations #
#--------------------------------------------------------------------#
# Clear the suggestion
_zsh_autosuggest_clear() {
# Remove the suggestion
unset POSTDISPLAY
_zsh_autosuggest_invoke_original_widget $WIDGET
_zsh_autosuggest_invoke_original_widget $@
}
# Modify the buffer and get a new suggestion
_zsh_autosuggest_modify() {
# Original widget modifies the buffer
_zsh_autosuggest_invoke_original_widget $WIDGET
_zsh_autosuggest_invoke_original_widget $@
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
suggestion=$(_zsh_autosuggest_suggestion $BUFFER)
suggestion=$(_zsh_autosuggest_suggestion "$BUFFER")
fi
# Add the suggestion to the POSTDISPLAY
@@ -273,7 +247,7 @@ _zsh_autosuggest_accept() {
CURSOR=${#BUFFER}
fi
_zsh_autosuggest_invoke_original_widget $WIDGET
_zsh_autosuggest_invoke_original_widget $@
}
# Partially accept the suggestion
@@ -285,7 +259,7 @@ _zsh_autosuggest_partial_accept() {
BUFFER="$BUFFER$POSTDISPLAY"
# Original widget moves the cursor
_zsh_autosuggest_invoke_original_widget $WIDGET
_zsh_autosuggest_invoke_original_widget $@
# If we've moved past the end of the original buffer
if [ $CURSOR -gt $#original_buffer ]; then
@@ -300,46 +274,45 @@ _zsh_autosuggest_partial_accept() {
fi
}
_zsh_autosuggest_widget_accept() {
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_accept
_zsh_autosuggest_highlight_apply
}
_zsh_autosuggest_widget_clear() {
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_clear
_zsh_autosuggest_highlight_apply
}
for action in clear modify accept partial_accept; do
eval "_zsh_autosuggest_widget_$action() {
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_$action \$@
_zsh_autosuggest_highlight_apply
}"
done
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
zle -N autosuggest-clear _zsh_autosuggest_clear
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
#------------#
# Suggestion #
#------------#
#--------------------------------------------------------------------#
# Suggestion #
#--------------------------------------------------------------------#
# Get a suggestion from history that matches a given prefix
_zsh_autosuggest_suggestion() {
setopt localoptions extendedglob
# Escape the prefix (requires EXTENDED_GLOB)
local prefix=${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
local prefix="${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH}"
# Get all history items (reversed) that match pattern $prefix*
local history_matches
history_matches=(${history[(R)$prefix*]})
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
# Echo the first item that matches
echo ${history_matches[1]}
echo "$history_matches[1]"
}
#-------#
# Start #
#-------#
#--------------------------------------------------------------------#
# Start #
#--------------------------------------------------------------------#
# Start the autosuggestion widgets
autosuggest_start() {
_zsh_autosuggest_start() {
_zsh_autosuggest_check_deprecated_config
_zsh_autosuggest_bind_widgets
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd _zsh_autosuggest_start