mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2025-12-06 15:20:40 +01:00
Compare commits
97 Commits
pre-v0.1.0
...
v0.3.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cce68de46d | ||
|
|
4a2d9f9049 | ||
|
|
7c688ec20c | ||
|
|
96eb0fae77 | ||
|
|
5e5bfdb659 | ||
|
|
0a6c34947c | ||
|
|
e87bc74654 | ||
|
|
964773aa75 | ||
|
|
945c660856 | ||
|
|
011d8bdfd1 | ||
|
|
c5f57da2b8 | ||
|
|
c477db2696 | ||
|
|
d7001f2c34 | ||
|
|
6d6e7820f3 | ||
|
|
9bef50fac1 | ||
|
|
1d4f7e157e | ||
|
|
2acf25e065 | ||
|
|
ba7109169d | ||
|
|
a28e72e84a | ||
|
|
87facd9b85 | ||
|
|
dc822c54f8 | ||
|
|
15c5db898f | ||
|
|
46d5fe174d | ||
|
|
6c31a02892 | ||
|
|
9ec62a1a23 | ||
|
|
1ed9155f89 | ||
|
|
97b51a2c57 | ||
|
|
b54c8a15c6 | ||
|
|
2a6d401106 | ||
|
|
a314a01a6a | ||
|
|
e5cdbb6c33 | ||
|
|
dffd9beae1 | ||
|
|
d202b32ae9 | ||
|
|
ab0f4c0bd0 | ||
|
|
ee6dde9ee8 | ||
|
|
73f774bd5d | ||
|
|
976acc708c | ||
|
|
83f78d0760 | ||
|
|
9df362f783 | ||
|
|
125f48c7f2 | ||
|
|
8c3fdea75d | ||
|
|
aa597eea6d | ||
|
|
f0a745576f | ||
|
|
8935a39e9b | ||
|
|
c7c9929490 | ||
|
|
985de56f6e | ||
|
|
0a42f872b8 | ||
|
|
dd54925b06 | ||
|
|
c761dc8150 | ||
|
|
cc921994e6 | ||
|
|
0242c7eff1 | ||
|
|
9d100f4f32 | ||
|
|
ddb7284852 | ||
|
|
2a5791710a | ||
|
|
03fac1f0d7 | ||
|
|
aa859a282d | ||
|
|
f08a5a1baa | ||
|
|
70438d233d | ||
|
|
ba029e83d0 | ||
|
|
acc129de6c | ||
|
|
aa5ceee256 | ||
|
|
113ca0ad10 | ||
|
|
2b449a62f8 | ||
|
|
6d25df6864 | ||
|
|
0faa2b6584 | ||
|
|
a2d8d91196 | ||
|
|
dd9a8789a7 | ||
|
|
1b98af5b33 | ||
|
|
45ab49d1f2 | ||
|
|
41f15d5c9f | ||
|
|
3ce1adb55d | ||
|
|
2461a98857 | ||
|
|
76f415bf43 | ||
|
|
5e419da326 | ||
|
|
cd71081303 | ||
|
|
9788c2ee49 | ||
|
|
ebcfc46b72 | ||
|
|
b49d002888 | ||
|
|
266437c98a | ||
|
|
51b39e210e | ||
|
|
011f5420fc | ||
|
|
1a38fbf6a5 | ||
|
|
31452887d2 | ||
|
|
cf146b6696 | ||
|
|
74197498fc | ||
|
|
abe577d519 | ||
|
|
0ae5907294 | ||
|
|
03bd381112 | ||
|
|
00bd0e9125 | ||
|
|
48d2dc1091 | ||
|
|
f154d25fb3 | ||
|
|
e91db46ce0 | ||
|
|
28836f15c5 | ||
|
|
0dd1b7febb | ||
|
|
6a4b2b3865 | ||
|
|
3449ae505c | ||
|
|
775dd20706 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,4 +0,0 @@
|
|||||||
/node_modules/
|
|
||||||
/build/
|
|
||||||
*.log
|
|
||||||
*~
|
|
||||||
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[submodule "vendor/shunit2"]
|
||||||
|
path = vendor/shunit2
|
||||||
|
url = https://github.com/kward/shunit2
|
||||||
|
[submodule "vendor/stub.sh"]
|
||||||
|
path = vendor/stub.sh
|
||||||
|
url = https://github.com/ericfreese/stub.sh
|
||||||
29
CHANGELOG.md
Normal file
29
CHANGELOG.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## v0.3.2
|
||||||
|
- Test runner now supports running specific tests and choosing zsh binary
|
||||||
|
- Return code from original widget is now correctly passed through (#135)
|
||||||
|
- Add `vi-add-eol` to list of accept widgets (#143)
|
||||||
|
- Escapes widget names within evals to fix problems with irregular widget names (#152)
|
||||||
|
- Plugin now clears suggestion while within a completion menu (#149)
|
||||||
|
- .plugin file no longer relies on symbolic link support, fixing issues on Windows (#156)
|
||||||
|
|
||||||
|
## v0.3.1
|
||||||
|
|
||||||
|
- Fixes issue with `vi-next-char` not accepting suggestion (#137).
|
||||||
|
- Fixes global variable warning when WARN_CREATE_GLOBAL option enabled (#133).
|
||||||
|
- Split out a separate test file for each widget.
|
||||||
|
|
||||||
|
## v0.3.0
|
||||||
|
|
||||||
|
- Adds `autosuggest-execute` widget (PR #124).
|
||||||
|
- Adds concept of suggestion "strategies" for different ways of fetching suggestions.
|
||||||
|
- Adds "match_prev_cmd" strategy (PR #131).
|
||||||
|
- Uses git submodules for testing dependencies.
|
||||||
|
- Lots of test cleanup.
|
||||||
|
- Various bug fixes for zsh 5.0.x and `sh_word_split` option.
|
||||||
|
|
||||||
|
|
||||||
|
## v0.2.17
|
||||||
|
|
||||||
|
Start of changelog.
|
||||||
1
DESCRIPTION
Normal file
1
DESCRIPTION
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Fish-like fast/unobtrusive autosuggestions for zsh.
|
||||||
1
LICENSE
1
LICENSE
@@ -1,4 +1,5 @@
|
|||||||
Copyright (c) 2013 Thiago de Arruda
|
Copyright (c) 2013 Thiago de Arruda
|
||||||
|
Copyright (c) 2016 Eric Freese
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person
|
Permission is hereby granted, free of charge, to any person
|
||||||
obtaining a copy of this software and associated documentation
|
obtaining a copy of this software and associated documentation
|
||||||
|
|||||||
47
Makefile
Normal file
47
Makefile
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
SRC_DIR := ./src
|
||||||
|
VENDOR_DIR := ./vendor
|
||||||
|
|
||||||
|
SRC_FILES := \
|
||||||
|
$(SRC_DIR)/config.zsh \
|
||||||
|
$(SRC_DIR)/deprecated.zsh \
|
||||||
|
$(SRC_DIR)/bind.zsh \
|
||||||
|
$(SRC_DIR)/highlight.zsh \
|
||||||
|
$(SRC_DIR)/widgets.zsh \
|
||||||
|
$(SRC_DIR)/suggestion.zsh \
|
||||||
|
$(SRC_DIR)/strategies/*.zsh \
|
||||||
|
$(SRC_DIR)/start.zsh
|
||||||
|
|
||||||
|
HEADER_FILES := \
|
||||||
|
DESCRIPTION \
|
||||||
|
URL \
|
||||||
|
VERSION \
|
||||||
|
LICENSE
|
||||||
|
|
||||||
|
PLUGIN_TARGET := zsh-autosuggestions.zsh
|
||||||
|
|
||||||
|
SHUNIT2 := $(VENDOR_DIR)/shunit2/2.1.6
|
||||||
|
STUB_SH := $(VENDOR_DIR)/stub.sh/stub.sh
|
||||||
|
|
||||||
|
TEST_PREREQS := \
|
||||||
|
$(SHUNIT2) \
|
||||||
|
$(STUB_SH)
|
||||||
|
|
||||||
|
all: $(PLUGIN_TARGET)
|
||||||
|
|
||||||
|
$(PLUGIN_TARGET): $(HEADER_FILES) $(SRC_FILES)
|
||||||
|
cat $(HEADER_FILES) | sed -e 's/^/# /g' > $@
|
||||||
|
cat $(SRC_FILES) >> $@
|
||||||
|
|
||||||
|
$(SHUNIT2):
|
||||||
|
git submodule update --init vendor/shunit2
|
||||||
|
|
||||||
|
$(STUB_SH):
|
||||||
|
git submodule update --init vendor/stub.sh
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm $(PLUGIN_TARGET)
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test: all $(TEST_PREREQS)
|
||||||
|
script/test_runner.zsh $(TESTS)
|
||||||
201
README.md
201
README.md
@@ -4,139 +4,152 @@ _[Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh._
|
|||||||
|
|
||||||
It suggests commands as you type, based on command history.
|
It suggests commands as you type, based on command history.
|
||||||
|
|
||||||
|
<a href="https://asciinema.org/a/37390" target="_blank"><img src="https://asciinema.org/a/37390.png" width="400" /></a>
|
||||||
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
If you already use [zsh-syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting) plugin, then make sure to be loaded **before** zsh-autosuggestions.
|
### Manual
|
||||||
|
|
||||||
Note: _.zshrc_ is a file that contains user-specific ZSH configuration.
|
1. Clone this repository somewhere on your machine. This guide will assume `~/.zsh/zsh-autosuggestions`.
|
||||||
ZSH assumes this file in your home directory (i.e. `~/.zshrc`), but the location can be changed using `ZDOTDIR` variable.
|
|
||||||
|
|
||||||
### Using zgen
|
|
||||||
|
|
||||||
[Zgen](https://github.com/tarjoilija/zgen) is a simple and fast plugin manager for ZSH.
|
|
||||||
If you don’t use zgen, then use instructions for the manual installation.
|
|
||||||
|
|
||||||
1. Load `tarruda/zsh-autosuggestions` and `zsh-users/zsh-syntax-highlighting` using zgen in your .zshrc file, for example:
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
if ! zgen saved; then
|
git clone git://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
|
||||||
echo "Creating a zgen save"
|
|
||||||
|
|
||||||
zgen load zsh-users/zsh-syntax-highlighting
|
|
||||||
|
|
||||||
# autosuggestions should be loaded last
|
|
||||||
zgen load tarruda/zsh-autosuggestions
|
|
||||||
|
|
||||||
zgen save
|
|
||||||
fi
|
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Enable zsh-autosuggestions; copy the following snippet and put it after the zgen config section in your .zshrc file:
|
2. Add the following to your `.zshrc`:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Enable autosuggestions automatically.
|
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
|
||||||
zle-line-init() {
|
|
||||||
zle autosuggest-start
|
|
||||||
}
|
|
||||||
zle -N zle-line-init
|
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Run `zgen reset` and reopen your terminal.
|
3. Start a new terminal session.
|
||||||
|
|
||||||
|
|
||||||
### Manually
|
### Oh My Zsh
|
||||||
|
|
||||||
1. Clone this repository to `~/.zsh/zsh-autosuggestions` (or anywhere else):
|
1. Clone this repository into `$ZSH_CUSTOM/plugins` (by default `~/.oh-my-zsh/custom/plugins`)
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone git://github.com/tarruda/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
|
git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Clone zsh-syntax-highlighting repository to `~/.zsh/zsh-syntax-highlighting` (or anywhere else):
|
2. Add the plugin to the list of plugins for Oh My Zsh to load:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone git://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/zsh-syntax-highlighting
|
plugins=(zsh-autosuggestions)
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Load and enable autosuggestions; copy the following snippet and put it to your .zshrc file:
|
3. Start a new terminal session.
|
||||||
|
|
||||||
```sh
|
|
||||||
# Load zsh-syntax-highlighting.
|
|
||||||
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
|
|
||||||
|
|
||||||
# Load zsh-autosuggestions.
|
|
||||||
source ~/.zsh/zsh-autosuggestions/autosuggestions.zsh
|
|
||||||
|
|
||||||
# Enable autosuggestions automatically.
|
|
||||||
zle-line-init() {
|
|
||||||
zle autosuggest-start
|
|
||||||
}
|
|
||||||
zle -N zle-line-init
|
|
||||||
```
|
|
||||||
|
|
||||||
4. Reopen your terminal.
|
|
||||||
|
|
||||||
|
|
||||||
## Uninstallation
|
## Usage
|
||||||
|
|
||||||
Just remove the config lines from .zshrc that you’ve added during “installation.”
|
As you type commands, you will see a completion offered after the cursor in a muted gray color. This color can be changed by setting the `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` variable. See [configuration](#configuration).
|
||||||
If you don’t use zgen, then also delete `~/.zsh/zsh-autosuggestions` and `~/.zsh/zsh-syntax-highlighting`.
|
|
||||||
|
|
||||||
|
If you press the <kbd>→</kbd> key (`forward-char` widget) or <kbd>End</kbd> (`end-of-line` widget) with the cursor at the end of the buffer, it will accept the suggestion, replacing the contents of the command line buffer with the suggestion.
|
||||||
|
|
||||||
## How to use
|
If you invoke the `forward-word` widget, it will partially accept the suggestion up to the point that the cursor moves to.
|
||||||
|
|
||||||
As you type commands, you will see a completion offered after the cursor, in a muted gray color (which can be changed, see [Configuration](#configuration)).
|
|
||||||
To accept the autosuggestion (replacing the command line contents), hit <kbd>End</kbd>, <kbd>Alt+F</kbd>, <kbd>Ctrl+F</kbd>, or any other key that moves the cursor to the right.
|
|
||||||
If the autosuggestion is not what you want, just ignore it: it won’t execute unless you accept it.
|
|
||||||
|
|
||||||
Any widget that moves the cursor to the right (forward-word, forward-char, end-of-line…) will accept parts of the suggested text.
|
|
||||||
For example, vi-mode users can do this:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# Accept suggestions without leaving insert mode
|
|
||||||
bindkey '^f' vi-forward-word
|
|
||||||
# or
|
|
||||||
bindkey '^f' vi-forward-blank-word
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also use right arrow key to accept the suggested text as in Fish shell; see [Configuration](#configuration) section to enable it.
|
|
||||||
|
|
||||||
### Exposed widgets
|
|
||||||
|
|
||||||
This plugin defines some ZLE widgets (think about them as functions) which you can bind to some key using [bindkey](http://zshwiki.org/home/zle/bindkeys).
|
|
||||||
For example, to toggle autosuggestions using <kbd>Ctrl+T</kbd> add this to your .zshrc:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
bindkey '^T' autosuggest-toggle
|
|
||||||
```
|
|
||||||
|
|
||||||
List of widgets:
|
|
||||||
|
|
||||||
- `autosuggest-toggle` – disable/enable autosuggestions.
|
|
||||||
- `autosuggest-execute-suggestion` – accept the suggestion and execute it.
|
|
||||||
|
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
You may override default global config variables after plugin load, i.e. put it to your .zshrc after the code that loads plugins.
|
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).
|
||||||
|
|
||||||
- `AUTOSUGGESTION_HIGHLIGHT_COLOR` – suggestion highlight color, default is `'fg=8'`.
|
**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).
|
||||||
- `AUTOSUGGESTION_HIGHLIGHT_CURSOR` – highlight word after cursor, or not. Must be integer value `1` or `0`, default is `1`.
|
|
||||||
- `AUTOSUGGESTION_ACCEPT_RIGHT_ARROW` – complete entire suggestion with right arrow. Must be integer value `1` or `0`, default is `0` (right arrow completes one letter at a time).
|
|
||||||
|
|
||||||
|
|
||||||
## Known Issues
|
### Suggestion Highlight Style
|
||||||
|
|
||||||
> When I hit <kbd>Tab</kbd> and autosuggestions is enabled, it deletes the previous line, and scrolls up the terminal.
|
Set `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` to configure the style that the suggestion is shown with. The default is `fg=8`.
|
||||||
|
|
||||||
This usually happens when autosuggestions is used along with something like [“completion waiting dots.”](http://michael.thegrebs.com/2012/09/04/zsh-completion-waiting-dots/)
|
|
||||||
Check which widget is bind to the Tab key; run `bindkey "^I"`.
|
|
||||||
If it prints something other than `"^I" expand-or-complete`, then this may be the problem.
|
|
||||||
|
|
||||||
If you use [Oh My Zsh](https://github.com/robbyrussell/oh-my-zsh), then make sure that the variable `COMPLETION_WAITING_DOTS` is not set (it enables [this](https://github.com/robbyrussell/oh-my-zsh/blob/e55c715508a2f652fed741f2047c66dda2c6e5b0/lib/completion.zsh#L56-L64) problematic code).
|
### Suggestion Strategy
|
||||||
|
|
||||||
If you use module [editor](https://github.com/sorin-ionescu/prezto/tree/master/modules/editor) from [Prezto](https://github.com/sorin-ionescu/prezto), then you must comment out [these lines](https://github.com/sorin-ionescu/prezto/blob/a84ac5b0023d71c98bb28a68c550dc13f6c51945/modules/editor/init.zsh#L303-L304).
|
Set `ZSH_AUTOSUGGEST_STRATEGY` to choose the strategy for generating suggestions. There are currently two to choose from:
|
||||||
|
|
||||||
|
- `default`: Chooses the most recent match.
|
||||||
|
- `match_prev_cmd`: Chooses the most recent match whose preceding history item matches the most recently executed command ([more info](src/strategies/match_prev_cmd.zsh)).
|
||||||
|
|
||||||
|
|
||||||
|
### Widget Mapping
|
||||||
|
|
||||||
|
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_ACCEPT_WIDGETS`: Widgets in this array will accept the suggestion when invoked.
|
||||||
|
- `ZSH_AUTOSUGGEST_EXECUTE_WIDGETS`: Widgets in this array will execute the suggestion when invoked.
|
||||||
|
- `ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS`: Widgets in this array will partially accept the suggestion when invoked.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
### Key Bindings
|
||||||
|
|
||||||
|
This plugin provides three widgets that you can use with `bindkey`:
|
||||||
|
|
||||||
|
1. `autosuggest-accept`: Accepts the current suggestion.
|
||||||
|
2. `autosuggest-execute`: Accepts and executes the current suggestion.
|
||||||
|
3. `autosuggest-clear`: Clears the current suggestion.
|
||||||
|
|
||||||
|
For example, this would bind <kbd>ctrl</kbd> + <kbd>space</kbd> to accept the current suggestion.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
bindkey '^ ' autosuggest-accept
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
If you have a problem, please search through [the list of issues on GitHub](https://github.com/zsh-users/zsh-autosuggestions/issues) to see if someone else has already reported it.
|
||||||
|
|
||||||
|
|
||||||
|
### 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/zsh-users/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
|
||||||
|
|
||||||
|
1. Remove the code referencing this plugin from `~/.zshrc`.
|
||||||
|
|
||||||
|
2. Remove the git repository from your hard drive
|
||||||
|
|
||||||
|
```sh
|
||||||
|
rm -rf ~/.zsh/zsh-autosuggestions # Or wherever you installed
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 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:
|
||||||
|
|
||||||
|
- Request to merge into the `develop` branch (*NOT* `master`)
|
||||||
|
- 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_runner.zsh`. To run the tests, run `make test`.
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
autosuggestions.zsh
|
|
||||||
@@ -1,294 +0,0 @@
|
|||||||
# Fish-like autosuggestions for zsh. Some of the code was based on the code
|
|
||||||
# for 'predict-on'
|
|
||||||
#
|
|
||||||
# ```zsh
|
|
||||||
# zle-line-init() {
|
|
||||||
# autosuggest-enable
|
|
||||||
# }
|
|
||||||
# zle -N zle-line-init
|
|
||||||
# ```
|
|
||||||
zmodload zsh/net/socket
|
|
||||||
|
|
||||||
source "${0:a:h}/completion-client.zsh"
|
|
||||||
|
|
||||||
# configuration variables
|
|
||||||
AUTOSUGGESTION_HIGHLIGHT_COLOR='fg=8'
|
|
||||||
AUTOSUGGESTION_HIGHLIGHT_CURSOR=1
|
|
||||||
|
|
||||||
function {
|
|
||||||
if [[ -n $ZLE_DISABLE_AUTOSUGGEST ]]; then
|
|
||||||
ZSH_HIGHLIGHT_HIGHLIGHTERS=()
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
autoload -U is-at-least
|
|
||||||
|
|
||||||
# if is-at-least 5.0.3; then
|
|
||||||
# autosuggest-ensure-server
|
|
||||||
# fi
|
|
||||||
}
|
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_SUSPEND_WIDGETS=(
|
|
||||||
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
|
|
||||||
history-search-forward history-search-backward up-line-or-history
|
|
||||||
history-beginning-search-forward history-beginning-search-backward
|
|
||||||
down-line-or-history history-substring-search-up history-substring-search-down
|
|
||||||
backward-kill-word
|
|
||||||
)
|
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_COMPLETION_WIDGETS=(
|
|
||||||
complete-word expand-or-complete expand-or-complete-prefix list-choices
|
|
||||||
menu-complete reverse-menu-complete menu-expand-or-complete menu-select
|
|
||||||
accept-and-menu-complete
|
|
||||||
)
|
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
|
||||||
vi-forward-char forward-char vi-forward-word forward-word vi-add-eol
|
|
||||||
vi-add-next vi-forward-blank-word vi-end-of-line end-of-line
|
|
||||||
)
|
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_ALL_WIDGETS=(
|
|
||||||
self-insert magic-space backward-delete-char accept-line
|
|
||||||
$ZLE_AUTOSUGGEST_ACCEPT_WIDGETS
|
|
||||||
$ZLE_AUTOSUGGEST_SUSPEND_WIDGETS
|
|
||||||
$ZLE_AUTOSUGGEST_COMPLETION_WIDGETS
|
|
||||||
)
|
|
||||||
|
|
||||||
autosuggest-pause() {
|
|
||||||
[[ -z $ZLE_AUTOSUGGESTING ]] && return
|
|
||||||
unset ZLE_AUTOSUGGESTING
|
|
||||||
|
|
||||||
# Restore standard widgets except for self-insert, which triggers resume
|
|
||||||
autosuggest-restore-widgets
|
|
||||||
zle -A autosuggest-paused-self-insert self-insert
|
|
||||||
|
|
||||||
# When autosuggestions are disabled, kill the unmaterialized part
|
|
||||||
RBUFFER=''
|
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
|
|
||||||
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
|
||||||
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-resume() {
|
|
||||||
[[ -n $ZLE_AUTOSUGGESTING ]] && return
|
|
||||||
ZLE_AUTOSUGGESTING=1
|
|
||||||
autosuggest-hook-widgets
|
|
||||||
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
|
||||||
# install listen for suggestions asynchronously
|
|
||||||
zle -Fw $ZLE_AUTOSUGGEST_CONNECTION autosuggest-pop-suggestion
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-start() {
|
|
||||||
if [[ -z $ZLE_DISABLE_AUTOSUGGEST && -n $functions[_zsh_highlight] ]]; then
|
|
||||||
if [[ ${ZSH_HIGHLIGHT_HIGHLIGHTERS[(i)autosuggest]} -gt ${#ZSH_HIGHLIGHT_HIGHLIGHTERS} ]]; then
|
|
||||||
ZSH_HIGHLIGHT_HIGHLIGHTERS+=(autosuggest)
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
autosuggest-resume
|
|
||||||
}
|
|
||||||
|
|
||||||
# Toggles autosuggestions on/off
|
|
||||||
autosuggest-toggle() {
|
|
||||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
|
||||||
autosuggest-pause
|
|
||||||
zle -A .self-insert self-insert
|
|
||||||
else
|
|
||||||
autosuggest-resume
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-highlight-suggested-text() {
|
|
||||||
if (( $+functions[_zsh_highlight_buffer_modified] > 0 )); then
|
|
||||||
_zsh_highlight
|
|
||||||
else
|
|
||||||
region_highlight=()
|
|
||||||
_zsh_highlight_autosuggest_highlighter
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
_zsh_highlight_autosuggest_highlighter_predicate() {
|
|
||||||
[[ -n $ZLE_AUTOSUGGESTING ]] && (( $#RBUFFER > 0 ))
|
|
||||||
}
|
|
||||||
|
|
||||||
_zsh_highlight_autosuggest_highlighter() {
|
|
||||||
region_highlight+=("$(( $CURSOR + $AUTOSUGGESTION_HIGHLIGHT_CURSOR )) $(( $CURSOR + $#RBUFFER )) $AUTOSUGGESTION_HIGHLIGHT_COLOR")
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-insert-or-space() {
|
|
||||||
setopt localoptions noshwordsplit noksharrays
|
|
||||||
if [[ $LBUFFER == *$'\012'* ]] || (( PENDING )); then
|
|
||||||
# Editing multiline buffer or pasting a chunk of text, pause
|
|
||||||
autosuggest-suspend
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
|
|
||||||
# Same as what's typed, just move on
|
|
||||||
((++CURSOR))
|
|
||||||
autosuggest-invalidate-highlight-cache
|
|
||||||
else
|
|
||||||
LBUFFER="$LBUFFER$KEYS"
|
|
||||||
if [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) || $LASTWIDGET == (complete-word|accept-*|zle-line-init) ]]; then
|
|
||||||
if ! zle .history-beginning-search-backward; then
|
|
||||||
RBUFFER=''
|
|
||||||
if [[ ${KEYS[-1]} != ' ' ]]; then
|
|
||||||
autosuggest-send-request ${LBUFFER}
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-backward-delete-char() {
|
|
||||||
if (( $#LBUFFER > 1 )); then
|
|
||||||
setopt localoptions noshwordsplit noksharrays
|
|
||||||
|
|
||||||
if [[ $LBUFFER = *$'\012'* || $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
|
|
||||||
LBUFFER="$LBUFFER[1,-2]"
|
|
||||||
else
|
|
||||||
((--CURSOR))
|
|
||||||
autosuggest-invalidate-highlight-cache
|
|
||||||
zle .history-beginning-search-forward || RBUFFER=''
|
|
||||||
fi
|
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
else
|
|
||||||
zle .kill-whole-line
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# When autosuggesting, ignore RBUFFER which corresponds to the 'unmaterialized'
|
|
||||||
# section when the user accepts the line
|
|
||||||
autosuggest-accept-line() {
|
|
||||||
RBUFFER=''
|
|
||||||
if ! (( $+functions[_zsh_highlight_buffer_modified] )); then
|
|
||||||
# Only clear the colors if the user doesn't have zsh-highlight installed
|
|
||||||
region_highlight=()
|
|
||||||
fi
|
|
||||||
zle .accept-line
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-paused-self-insert() {
|
|
||||||
if [[ $RBUFFER == '' ]]; then
|
|
||||||
# Resume autosuggestions when inserting at the end of the line
|
|
||||||
autosuggest-resume
|
|
||||||
zle self-insert
|
|
||||||
else
|
|
||||||
zle .self-insert
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-pop-suggestion() {
|
|
||||||
local words last_word suggestion
|
|
||||||
if ! IFS= read -r -u $ZLE_AUTOSUGGEST_CONNECTION suggestion; then
|
|
||||||
# server closed the connection, stop listenting
|
|
||||||
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
|
||||||
unset ZLE_AUTOSUGGEST_CONNECTION
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
if [[ -n $suggestion ]]; then
|
|
||||||
local prefix=${suggestion%$'\2'*}
|
|
||||||
suggestion=${suggestion#*$'\2'}
|
|
||||||
# only use the suggestion if the prefix is still compatible with
|
|
||||||
# the suggestion(prefix should be contained in LBUFFER)
|
|
||||||
if [[ ${LBUFFER#$prefix*} != ${LBUFFER} ]]; then
|
|
||||||
words=(${(z)LBUFFER})
|
|
||||||
last_word=${words[-1]}
|
|
||||||
suggestion=${suggestion:$#last_word}
|
|
||||||
RBUFFER="$suggestion"
|
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
else
|
|
||||||
RBUFFER=''
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
RBUFFER=''
|
|
||||||
fi
|
|
||||||
zle -Rc
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-suspend() {
|
|
||||||
autosuggest-pause
|
|
||||||
zle .${WIDGET} "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-tab() {
|
|
||||||
RBUFFER=''
|
|
||||||
zle .${WIDGET} "$@"
|
|
||||||
autosuggest-invalidate-highlight-cache
|
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-accept-suggestion() {
|
|
||||||
if [[ AUTOSUGGESTION_ACCEPT_RIGHT_ARROW -eq 1 && ("$WIDGET" == 'forward-char' || "$WIDGET" == 'vi-forward-char') ]]; then
|
|
||||||
zle .end-of-line "$@"
|
|
||||||
else
|
|
||||||
zle .${WIDGET} "$@"
|
|
||||||
fi
|
|
||||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
|
||||||
autosuggest-invalidate-highlight-cache
|
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-execute-suggestion() {
|
|
||||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
|
||||||
zle .end-of-line
|
|
||||||
autosuggest-invalidate-highlight-cache
|
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
fi
|
|
||||||
zle .accept-line
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-invalidate-highlight-cache() {
|
|
||||||
# invalidate the buffer for zsh-syntax-highlighting
|
|
||||||
_zsh_highlight_autosuggest_highlighter_cache=()
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-restore-widgets() {
|
|
||||||
for widget in $ZLE_AUTOSUGGEST_ALL_WIDGETS; do
|
|
||||||
[[ -z $widgets[$widget] ]] && continue
|
|
||||||
zle -A .${widget} ${widget}
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-hook-widgets() {
|
|
||||||
local widget
|
|
||||||
# Replace prediction widgets by versions that will also highlight RBUFFER
|
|
||||||
zle -A autosuggest-insert-or-space self-insert
|
|
||||||
zle -A autosuggest-insert-or-space magic-space
|
|
||||||
zle -A autosuggest-backward-delete-char backward-delete-char
|
|
||||||
zle -A autosuggest-accept-line accept-line
|
|
||||||
# Hook into some default widgets that should suspend autosuggestion
|
|
||||||
# automatically
|
|
||||||
for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS; do
|
|
||||||
[[ -z $widgets[$widget] ]] && continue
|
|
||||||
eval "zle -A autosuggest-accept-suggestion $widget"
|
|
||||||
done
|
|
||||||
for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do
|
|
||||||
[[ -z $widgets[$widget] ]] && continue
|
|
||||||
eval "zle -A autosuggest-suspend $widget"
|
|
||||||
done
|
|
||||||
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
|
||||||
[[ -z $widgets[$widget] ]] && continue
|
|
||||||
eval "zle -A autosuggest-tab $widget"
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
zle -N autosuggest-toggle
|
|
||||||
zle -N autosuggest-start
|
|
||||||
zle -N autosuggest-accept-suggested-small-word
|
|
||||||
zle -N autosuggest-accept-suggested-word
|
|
||||||
zle -N autosuggest-execute-suggestion
|
|
||||||
|
|
||||||
zle -N autosuggest-paused-self-insert
|
|
||||||
zle -N autosuggest-insert-or-space
|
|
||||||
zle -N autosuggest-backward-delete-char
|
|
||||||
zle -N autosuggest-accept-line
|
|
||||||
|
|
||||||
zle -N autosuggest-tab
|
|
||||||
zle -N autosuggest-suspend
|
|
||||||
zle -N autosuggest-accept-suggestion
|
|
||||||
|
|
||||||
autosuggest-restore-widgets
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
#!/usr/bin/env zsh
|
|
||||||
zmodload zsh/net/socket
|
|
||||||
|
|
||||||
AUTOSUGGEST_SERVER_SCRIPT="${0:a:h}/completion-server.zsh"
|
|
||||||
|
|
||||||
autosuggest-ensure-server() {
|
|
||||||
setopt local_options no_hup
|
|
||||||
local server_dir="/tmp/zsh-autosuggest-$USER"
|
|
||||||
local pid_file="$server_dir/pid"
|
|
||||||
local socket_path="$server_dir/socket"
|
|
||||||
|
|
||||||
if [[ ! -d $server_dir || ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
|
|
||||||
if which setsid &> /dev/null; then
|
|
||||||
setsid zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
|
|
||||||
else
|
|
||||||
zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
autosuggest-server-connect
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-server-connect() {
|
|
||||||
unset ZLE_AUTOSUGGEST_CONNECTION
|
|
||||||
|
|
||||||
integer remaining_tries=10
|
|
||||||
while (( --remaining_tries )) && ! zsocket $socket_path &>/dev/null; do
|
|
||||||
sleep 0.3
|
|
||||||
done
|
|
||||||
|
|
||||||
[[ -z $REPLY ]] && return 1
|
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_CONNECTION=$REPLY
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-send-request() {
|
|
||||||
[[ -z $ZLE_AUTOSUGGEST_CONNECTION ]] && return 1
|
|
||||||
setopt local_options noglob
|
|
||||||
print -u $ZLE_AUTOSUGGEST_CONNECTION - $1 &> /dev/null || return 1
|
|
||||||
}
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
# Based on:
|
|
||||||
# https://github.com/Valodim/zsh-capture-completion/blob/master/.zshrc
|
|
||||||
|
|
||||||
ZLE_DISABLE_AUTOSUGGEST=1
|
|
||||||
# no prompt!
|
|
||||||
PROMPT=
|
|
||||||
|
|
||||||
# load completion system
|
|
||||||
autoload compinit
|
|
||||||
compinit
|
|
||||||
|
|
||||||
# never run a command
|
|
||||||
bindkey '\C-m' .kill-buffer
|
|
||||||
bindkey '\C-j' .kill-buffer
|
|
||||||
bindkey '\C-i' complete-word
|
|
||||||
|
|
||||||
# send an emtpy line before completions are output
|
|
||||||
empty-line() {
|
|
||||||
print
|
|
||||||
# handler needs to reinsert itself after being called
|
|
||||||
compprefuncs+=empty-line
|
|
||||||
}
|
|
||||||
compprefuncs+=empty-line
|
|
||||||
|
|
||||||
# send a line with null-byte after completions are output
|
|
||||||
null-line() {
|
|
||||||
print $'\0'
|
|
||||||
# handler needs to reinsert itself after being called
|
|
||||||
comppostfuncs+=null-line
|
|
||||||
}
|
|
||||||
comppostfuncs+=null-line
|
|
||||||
|
|
||||||
zstyle ':completion:*' completer _complete
|
|
||||||
# never group stuff!
|
|
||||||
zstyle ':completion:*' list-grouped false
|
|
||||||
# don't insert tab when attempting completion on empty line
|
|
||||||
zstyle ':completion:*' insert-tab false
|
|
||||||
# no list separator, this saves some stripping later on
|
|
||||||
zstyle ':completion:*' list-separator ''
|
|
||||||
# dont use matchers
|
|
||||||
zstyle -d ':completion:*' matcher-list
|
|
||||||
# dont format
|
|
||||||
zstyle -d ':completion:*' format
|
|
||||||
# no color formatting
|
|
||||||
zstyle -d ':completion:*' list-colors
|
|
||||||
|
|
||||||
# we use zparseopts
|
|
||||||
zmodload zsh/zutil
|
|
||||||
|
|
||||||
# override compadd (this our hook)
|
|
||||||
compadd() {
|
|
||||||
|
|
||||||
# check if any of -O, -A or -D are given
|
|
||||||
if [[ ${@[1,(i)(-|--)]} == *-(O|A|D)\ * ]]; then
|
|
||||||
# if that is the case, just delegate and leave
|
|
||||||
builtin compadd "$@"
|
|
||||||
return $?
|
|
||||||
fi
|
|
||||||
|
|
||||||
# be careful with namespacing here, we don't want to mess with stuff that
|
|
||||||
# should be passed to compadd!
|
|
||||||
typeset -a __hits __dscr __tmp
|
|
||||||
|
|
||||||
# do we have a description parameter?
|
|
||||||
# note we don't use zparseopts here because of combined option parameters
|
|
||||||
# with arguments like -default- confuse it.
|
|
||||||
if (( $@[(I)-d] )); then # kind of a hack, $+@[(r)-d] doesn't work because of line noise overload
|
|
||||||
# next param after -d
|
|
||||||
__tmp=${@[$[${@[(i)-d]}+1]]}
|
|
||||||
# description can be given as an array parameter name, or inline () array
|
|
||||||
if [[ $__tmp == \(* ]]; then
|
|
||||||
eval "__dscr=$__tmp"
|
|
||||||
else
|
|
||||||
__dscr=( "${(@P)__tmp}" )
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# capture completions by injecting -A parameter into the compadd call.
|
|
||||||
# this takes care of matching for us.
|
|
||||||
builtin compadd -A __hits -D __dscr "$@"
|
|
||||||
|
|
||||||
# JESUS CHRIST IT TOOK ME FOREVER TO FIGURE OUT THIS OPTION WAS SET AND WAS MESSING WITH MY SHIT HERE
|
|
||||||
setopt localoptions norcexpandparam extendedglob
|
|
||||||
|
|
||||||
# extract prefixes and suffixes from compadd call. we can't do zsh's cool
|
|
||||||
# -r remove-func magic, but it's better than nothing.
|
|
||||||
typeset -A apre hpre hsuf asuf
|
|
||||||
zparseopts -E P:=apre p:=hpre S:=asuf s:=hsuf
|
|
||||||
|
|
||||||
# append / to directories? we are only emulating -f in a half-assed way
|
|
||||||
# here, but it's better than nothing.
|
|
||||||
integer dirsuf=0
|
|
||||||
# don't be fooled by -default- >.>
|
|
||||||
if [[ -z $hsuf && "${${@//-default-/}% -# *}" == *-[[:alnum:]]#f* ]]; then
|
|
||||||
dirsuf=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# just drop
|
|
||||||
[[ -n $__hits ]] || return
|
|
||||||
|
|
||||||
# this is the point where we have all matches in $__hits and all
|
|
||||||
# descriptions in $__dscr!
|
|
||||||
|
|
||||||
# display all matches
|
|
||||||
local dsuf dscr
|
|
||||||
for i in {1..$#__hits}; do
|
|
||||||
|
|
||||||
# add a dir suffix?
|
|
||||||
(( dirsuf )) && [[ -d $__hits[$i] ]] && dsuf=/ || dsuf=
|
|
||||||
# description to be displayed afterwards
|
|
||||||
# (( $#__dscr >= $i )) && dscr=" -- ${${__dscr[$i]}##$__hits[$i] #}" || dscr=
|
|
||||||
|
|
||||||
print - $'\1'$IPREFIX$apre$hpre$__hits[$i]$dsuf$hsuf$asuf$dscr
|
|
||||||
|
|
||||||
done
|
|
||||||
|
|
||||||
unset __hits __dscr __tmp
|
|
||||||
}
|
|
||||||
|
|
||||||
# signal the daemon we are ready for input
|
|
||||||
print $'\0'
|
|
||||||
@@ -1,128 +0,0 @@
|
|||||||
#!/usr/bin/env zsh
|
|
||||||
# Based on:
|
|
||||||
# https://github.com/Valodim/zsh-capture-completion/blob/master/capture.zsh
|
|
||||||
|
|
||||||
# read everything until a line containing the byte 0 is found
|
|
||||||
read-to-null() {
|
|
||||||
while zpty -r z chunk; do
|
|
||||||
[[ $chunk == *$'\0'* ]] && break
|
|
||||||
[[ $chunk != $'\1'* ]] && continue # ignore what doesnt start with '1'
|
|
||||||
print -n - ${chunk:1}
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
accept-connection() {
|
|
||||||
zsocket -a $server
|
|
||||||
fds[$REPLY]=1
|
|
||||||
print "connection accepted, fd: $REPLY" >&2
|
|
||||||
}
|
|
||||||
|
|
||||||
handle-request() {
|
|
||||||
local connection=$1 current line
|
|
||||||
integer read_something=0
|
|
||||||
print "request received from fd $connection"
|
|
||||||
while read -u $connection prefix &> /dev/null; do
|
|
||||||
read_something=1
|
|
||||||
# send the prefix to be completed followed by a TAB to force
|
|
||||||
# completion
|
|
||||||
zpty -w -n z $prefix$'\t'
|
|
||||||
zpty -r z chunk &> /dev/null # read empty line before completions
|
|
||||||
current=''
|
|
||||||
# read completions one by one, storing the longest match
|
|
||||||
read-to-null | while IFS= read -r line; do
|
|
||||||
(( $#line > $#current )) && current=$line
|
|
||||||
done
|
|
||||||
# send the longest completion back to the client, strip the last
|
|
||||||
# non-printable character
|
|
||||||
if (( $#current )); then
|
|
||||||
print -u $connection - $prefix$'\2'${current:0:-1}
|
|
||||||
else
|
|
||||||
print -u $connection ''
|
|
||||||
fi
|
|
||||||
# clear input buffer
|
|
||||||
zpty -w z $'\n'
|
|
||||||
break # handle more requests/return to zselect
|
|
||||||
done
|
|
||||||
if ! (( read_something )); then
|
|
||||||
print "connection with fd $connection closed" >&2
|
|
||||||
unset fds[$connection]
|
|
||||||
exec {connection}>&- # free the file descriptor
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if [[ -n $ZLE_AUTOSUGGEST_SERVER_LOG ]]; then
|
|
||||||
exec >> "$HOME/.autosuggest-server.log"
|
|
||||||
else
|
|
||||||
exec > /dev/null
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n $ZLE_AUTOSUGGEST_SERVER_LOG_ERRORS ]]; then
|
|
||||||
exec 2>> "$HOME/.autosuggest-server-errors.log"
|
|
||||||
else
|
|
||||||
exec 2> /dev/null
|
|
||||||
fi
|
|
||||||
|
|
||||||
exec < /dev/null
|
|
||||||
|
|
||||||
zmodload zsh/zpty
|
|
||||||
zmodload zsh/zselect
|
|
||||||
zmodload zsh/net/socket
|
|
||||||
setopt noglob
|
|
||||||
print "autosuggestion server started, pid: $$" >&2
|
|
||||||
|
|
||||||
# Start an interactive zsh connected to a zpty
|
|
||||||
zpty z ZLE_DISABLE_AUTOSUGGEST=1 zsh -i
|
|
||||||
print 'interactive shell started'
|
|
||||||
# Source the init script
|
|
||||||
zpty -w z "source '${0:a:h}/completion-server-init.zsh'"
|
|
||||||
|
|
||||||
# wait for ok from shell
|
|
||||||
read-to-null &> /dev/null
|
|
||||||
print 'interactive shell ready'
|
|
||||||
|
|
||||||
# listen on a socket for completion requests
|
|
||||||
server_dir=$1
|
|
||||||
pid_file=$2
|
|
||||||
socket_path=$3
|
|
||||||
|
|
||||||
|
|
||||||
cleanup() {
|
|
||||||
print 'removing socket and pid file...'
|
|
||||||
rm -f $socket_path $pid_file
|
|
||||||
print "autosuggestion server stopped, pid: $$"
|
|
||||||
exit
|
|
||||||
}
|
|
||||||
|
|
||||||
trap cleanup TERM INT HUP EXIT
|
|
||||||
|
|
||||||
mkdir -m 700 $server_dir
|
|
||||||
|
|
||||||
while ! zsocket -l $socket_path; do
|
|
||||||
if [[ ! -r $pid_file ]] || ! kill -0 $(<$pid_file); then
|
|
||||||
rm -f $socket_path
|
|
||||||
else
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
print "will retry listening on '$socket_path'"
|
|
||||||
done
|
|
||||||
|
|
||||||
server=$REPLY
|
|
||||||
|
|
||||||
print "server listening on '$socket_path'"
|
|
||||||
|
|
||||||
print $$ > $pid_file
|
|
||||||
|
|
||||||
typeset -A fds ready
|
|
||||||
fds[$server]=1
|
|
||||||
|
|
||||||
while zselect -A ready ${(k)fds}; do
|
|
||||||
queue=(${(k)ready})
|
|
||||||
for fd in $queue; do
|
|
||||||
if (( fd == server )); then
|
|
||||||
accept-connection
|
|
||||||
else
|
|
||||||
handle-request $fd
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
done
|
|
||||||
44
install
44
install
@@ -1,44 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Install script for zsh-autocomplete
|
|
||||||
|
|
||||||
config="$HOME/.zshrc"
|
|
||||||
for config in "$HOME/.zshrc" "$ZDOTDIR/.zshrc" "$1"; do
|
|
||||||
echo $config
|
|
||||||
#first checks if ~/.zshrc file exists and is readable
|
|
||||||
if [ -r "$config" ]; then
|
|
||||||
break
|
|
||||||
elif [ "$config" = "$1" ]; then
|
|
||||||
echo "\nError: Please specify as first argument the file in which to load zsh-autosuggestions (usually ~/.zshrc)!\n"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
SOURCE="${BASH_SOURCE[0]}"
|
|
||||||
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
|
|
||||||
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
|
||||||
SOURCE="$(readlink "$SOURCE")"
|
|
||||||
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
|
|
||||||
done
|
|
||||||
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
|
||||||
|
|
||||||
# appends the string to $config (usually ~/.zshrc) file
|
|
||||||
cat >> "$config" <<-EOF
|
|
||||||
|
|
||||||
# Setup zsh-autosuggestions
|
|
||||||
source $DIR/autosuggestions.zsh
|
|
||||||
|
|
||||||
# Enable autosuggestions automatically
|
|
||||||
zle-line-init() {
|
|
||||||
zle autosuggest-start
|
|
||||||
}
|
|
||||||
|
|
||||||
zle -N zle-line-init
|
|
||||||
|
|
||||||
# use ctrl+t to toggle autosuggestions(hopefully this wont be needed as
|
|
||||||
# zsh-autosuggestions is designed to be unobtrusive)
|
|
||||||
bindkey '^T' autosuggest-toggle
|
|
||||||
EOF
|
|
||||||
|
|
||||||
echo "\nSetup completed successfully!\n"
|
|
||||||
exit 0
|
|
||||||
54
script/test_runner.zsh
Executable file
54
script/test_runner.zsh
Executable file
@@ -0,0 +1,54 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
DIR="${0:a:h}"
|
||||||
|
ROOT_DIR="$DIR/.."
|
||||||
|
TEST_DIR="$ROOT_DIR/test"
|
||||||
|
|
||||||
|
header() {
|
||||||
|
local message="$1"
|
||||||
|
|
||||||
|
cat <<-EOF
|
||||||
|
|
||||||
|
#====================================================================#
|
||||||
|
# $message
|
||||||
|
#====================================================================#
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# ZSH binary to use
|
||||||
|
local zsh_bin="zsh"
|
||||||
|
|
||||||
|
while getopts ":z:" opt; do
|
||||||
|
case $opt in
|
||||||
|
z)
|
||||||
|
zsh_bin="$OPTARG"
|
||||||
|
;;
|
||||||
|
\?)
|
||||||
|
echo "Invalid option: -$OPTARG" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
:)
|
||||||
|
echo "Option -$OPTARG requires an argument" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
shift $((OPTIND -1))
|
||||||
|
|
||||||
|
# Test suites to run
|
||||||
|
local -a tests
|
||||||
|
if [ $#@ -gt 0 ]; then
|
||||||
|
tests=($@)
|
||||||
|
else
|
||||||
|
tests=($TEST_DIR/**/*_test.zsh)
|
||||||
|
fi
|
||||||
|
|
||||||
|
local -i retval=0
|
||||||
|
|
||||||
|
for suite in $tests; do
|
||||||
|
header "${suite#"$ROOT_DIR/"}"
|
||||||
|
"$zsh_bin" -f "$suite" || retval=$?
|
||||||
|
done
|
||||||
|
|
||||||
|
exit $retval
|
||||||
81
src/bind.zsh
Normal file
81
src/bind.zsh
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# 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_action=$2
|
||||||
|
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
|
||||||
|
|
||||||
|
# Save a reference to the original widget
|
||||||
|
case $widgets[$widget] in
|
||||||
|
# Already bound
|
||||||
|
user:_zsh_autosuggest_(bound|orig)_*);;
|
||||||
|
|
||||||
|
# User-defined widget
|
||||||
|
user:*)
|
||||||
|
zle -N $prefix$widget ${widgets[$widget]#*:}
|
||||||
|
;;
|
||||||
|
|
||||||
|
# Built-in widget
|
||||||
|
builtin)
|
||||||
|
eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
|
||||||
|
zle -N $prefix$widget _zsh_autosuggest_orig_$widget
|
||||||
|
;;
|
||||||
|
|
||||||
|
# Completion widget
|
||||||
|
completion:*)
|
||||||
|
eval "zle -C $prefix${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# 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_${(q)widget}() {
|
||||||
|
_zsh_autosuggest_widget_$autosuggest_action $prefix${(q)widget} \$@
|
||||||
|
}"
|
||||||
|
|
||||||
|
# Create the bound widget
|
||||||
|
zle -N $widget _zsh_autosuggest_bound_$widget
|
||||||
|
}
|
||||||
|
|
||||||
|
# Map all configured widgets to the right autosuggest widgets
|
||||||
|
_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*|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 accept
|
||||||
|
elif [ ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]; then
|
||||||
|
_zsh_autosuggest_bind_widget $widget execute
|
||||||
|
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
|
||||||
|
_zsh_autosuggest_bind_widget $widget partial_accept
|
||||||
|
else
|
||||||
|
# Assume any unspecified widget might modify the buffer
|
||||||
|
_zsh_autosuggest_bind_widget $widget modify
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# Given the name of an original widget and args, invoke it, if it exists
|
||||||
|
_zsh_autosuggest_invoke_original_widget() {
|
||||||
|
# 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 -- $@
|
||||||
|
fi
|
||||||
|
}
|
||||||
49
src/config.zsh
Normal file
49
src/config.zsh
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Global Configuration Variables #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Color to use when highlighting suggestion
|
||||||
|
# Uses format of `region_highlight`
|
||||||
|
# More info: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets
|
||||||
|
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
|
||||||
|
|
||||||
|
# Prefix to use when saving original versions of bound widgets
|
||||||
|
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
||||||
|
|
||||||
|
ZSH_AUTOSUGGEST_STRATEGY=default
|
||||||
|
|
||||||
|
# Widgets that clear the suggestion
|
||||||
|
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-history
|
||||||
|
down-line-or-history
|
||||||
|
accept-line
|
||||||
|
)
|
||||||
|
|
||||||
|
# Widgets that accept the entire suggestion
|
||||||
|
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
||||||
|
forward-char
|
||||||
|
end-of-line
|
||||||
|
vi-forward-char
|
||||||
|
vi-end-of-line
|
||||||
|
vi-add-eol
|
||||||
|
)
|
||||||
|
|
||||||
|
# Widgets that accept the entire suggestion and execute it
|
||||||
|
ZSH_AUTOSUGGEST_EXECUTE_WIDGETS=(
|
||||||
|
)
|
||||||
|
|
||||||
|
# Widgets that accept the suggestion as far as the cursor moves
|
||||||
|
ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
|
||||||
|
forward-word
|
||||||
|
vi-forward-word
|
||||||
|
vi-forward-word-end
|
||||||
|
vi-forward-blank-word
|
||||||
|
vi-forward-blank-word-end
|
||||||
|
)
|
||||||
36
src/deprecated.zsh
Normal file
36
src/deprecated.zsh
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Handle Deprecated Variables/Widgets #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
_zsh_autosuggest_deprecated_warning() {
|
||||||
|
>&2 echo "zsh-autosuggestions: $@"
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_check_deprecated_config() {
|
||||||
|
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then
|
||||||
|
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_HIGHLIGHT_COLOR is deprecated. Use ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE instead."
|
||||||
|
[ -z "$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" ] && ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=$AUTOSUGGESTION_HIGHLIGHT_STYLE
|
||||||
|
unset AUTOSUGGESTION_HIGHLIGHT_STYLE
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_CURSOR" ]; then
|
||||||
|
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_HIGHLIGHT_CURSOR is deprecated."
|
||||||
|
unset AUTOSUGGESTION_HIGHLIGHT_CURSOR
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$AUTOSUGGESTION_ACCEPT_RIGHT_ARROW" ]; then
|
||||||
|
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_ACCEPT_RIGHT_ARROW is deprecated. The right arrow now accepts the suggestion by default."
|
||||||
|
unset AUTOSUGGESTION_ACCEPT_RIGHT_ARROW
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_deprecated_start_widget() {
|
||||||
|
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. For more info, see the README at https://github.com/zsh-users/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
|
||||||
26
src/highlight.zsh
Normal file
26
src/highlight.zsh
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Highlighting #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# If there was a highlight, remove it
|
||||||
|
_zsh_autosuggest_highlight_reset() {
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
||||||
|
|
||||||
|
if [ -n "$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT" ]; then
|
||||||
|
region_highlight=("${(@)region_highlight:#$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT}")
|
||||||
|
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# If there's a suggestion, highlight it
|
||||||
|
_zsh_autosuggest_highlight_apply() {
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
||||||
|
|
||||||
|
if [ $#POSTDISPLAY -gt 0 ]; then
|
||||||
|
_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
|
||||||
|
region_highlight+=("$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT")
|
||||||
|
else
|
||||||
|
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
||||||
|
fi
|
||||||
|
}
|
||||||
13
src/start.zsh
Normal file
13
src/start.zsh
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Start #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Start the autosuggestion widgets
|
||||||
|
_zsh_autosuggest_start() {
|
||||||
|
_zsh_autosuggest_check_deprecated_config
|
||||||
|
_zsh_autosuggest_bind_widgets
|
||||||
|
}
|
||||||
|
|
||||||
|
autoload -Uz add-zsh-hook
|
||||||
|
add-zsh-hook precmd _zsh_autosuggest_start
|
||||||
18
src/strategies/default.zsh
Normal file
18
src/strategies/default.zsh
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Default Suggestion Strategy #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Suggests the most recent history item that matches the given
|
||||||
|
# prefix.
|
||||||
|
#
|
||||||
|
|
||||||
|
_zsh_autosuggest_strategy_default() {
|
||||||
|
local prefix="$1"
|
||||||
|
|
||||||
|
# Get the keys of the history items that match
|
||||||
|
local -a histkeys
|
||||||
|
histkeys=(${(k)history[(r)$prefix*]})
|
||||||
|
|
||||||
|
# Echo the value of the first key
|
||||||
|
echo -E "${history[$histkeys[1]]}"
|
||||||
|
}
|
||||||
49
src/strategies/match_prev_cmd.zsh
Normal file
49
src/strategies/match_prev_cmd.zsh
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Match Previous Command Suggestion Strategy #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Suggests the most recent history item that matches the given
|
||||||
|
# prefix and whose preceding history item also matches the most
|
||||||
|
# recently executed command.
|
||||||
|
#
|
||||||
|
# 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'.
|
||||||
|
#
|
||||||
|
|
||||||
|
_zsh_autosuggest_strategy_match_prev_cmd() {
|
||||||
|
local prefix="$1"
|
||||||
|
|
||||||
|
# Get all history event numbers that correspond to history
|
||||||
|
# entries that match pattern $prefix*
|
||||||
|
local history_match_keys
|
||||||
|
history_match_keys=(${(k)history[(R)$prefix*]})
|
||||||
|
|
||||||
|
# By default we use the first history number (most recent history entry)
|
||||||
|
local histkey="${history_match_keys[1]}"
|
||||||
|
|
||||||
|
# Get the previously executed command
|
||||||
|
local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")"
|
||||||
|
|
||||||
|
# Iterate up to the first 200 history event numbers that match $prefix
|
||||||
|
for key in "${(@)history_match_keys[1,200]}"; do
|
||||||
|
# Stop if we ran out of history
|
||||||
|
[[ $key -gt 1 ]] || break
|
||||||
|
|
||||||
|
# See if the history entry preceding the suggestion matches the
|
||||||
|
# previous command, and use it if it does
|
||||||
|
if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then
|
||||||
|
histkey="$key"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Echo the matched history entry
|
||||||
|
echo -E "$history[$histkey]"
|
||||||
|
}
|
||||||
21
src/suggestion.zsh
Normal file
21
src/suggestion.zsh
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Suggestion #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Delegate to the selected strategy to determine a suggestion
|
||||||
|
_zsh_autosuggest_suggestion() {
|
||||||
|
local escaped_prefix="$(_zsh_autosuggest_escape_command "$1")"
|
||||||
|
local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY"
|
||||||
|
|
||||||
|
if [ -n "$functions[$strategy_function]" ]; then
|
||||||
|
echo -E "$($strategy_function "$escaped_prefix")"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_escape_command() {
|
||||||
|
setopt localoptions EXTENDED_GLOB
|
||||||
|
|
||||||
|
# Escape special chars in the string (requires EXTENDED_GLOB)
|
||||||
|
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
||||||
|
}
|
||||||
125
src/widgets.zsh
Normal file
125
src/widgets.zsh
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Autosuggest Widget Implementations #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Clear the suggestion
|
||||||
|
_zsh_autosuggest_clear() {
|
||||||
|
# Remove the suggestion
|
||||||
|
unset POSTDISPLAY
|
||||||
|
|
||||||
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
}
|
||||||
|
|
||||||
|
# Modify the buffer and get a new suggestion
|
||||||
|
_zsh_autosuggest_modify() {
|
||||||
|
local -i retval
|
||||||
|
|
||||||
|
# Clear suggestion while original widget runs
|
||||||
|
unset POSTDISPLAY
|
||||||
|
|
||||||
|
# Original widget modifies the buffer
|
||||||
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
retval=$?
|
||||||
|
|
||||||
|
# Get a new suggestion if the buffer is not empty after modification
|
||||||
|
local suggestion
|
||||||
|
if [ $#BUFFER -gt 0 ]; then
|
||||||
|
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Add the suggestion to the POSTDISPLAY
|
||||||
|
if [ -n "$suggestion" ]; then
|
||||||
|
POSTDISPLAY="${suggestion#$BUFFER}"
|
||||||
|
else
|
||||||
|
unset POSTDISPLAY
|
||||||
|
fi
|
||||||
|
|
||||||
|
return $retval
|
||||||
|
}
|
||||||
|
|
||||||
|
# Accept the entire suggestion
|
||||||
|
_zsh_autosuggest_accept() {
|
||||||
|
local -i max_cursor_pos=$#BUFFER
|
||||||
|
|
||||||
|
# When vicmd keymap is active, the cursor can't move all the way
|
||||||
|
# to the end of the buffer
|
||||||
|
if [ "$KEYMAP" = "vicmd" ]; then
|
||||||
|
max_cursor_pos=$((max_cursor_pos - 1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Only accept if the cursor is at the end of the buffer
|
||||||
|
if [ $CURSOR -eq $max_cursor_pos ]; then
|
||||||
|
# Add the suggestion to the buffer
|
||||||
|
BUFFER="$BUFFER$POSTDISPLAY"
|
||||||
|
|
||||||
|
# Remove the suggestion
|
||||||
|
unset POSTDISPLAY
|
||||||
|
|
||||||
|
# Move the cursor to the end of the buffer
|
||||||
|
CURSOR=${#BUFFER}
|
||||||
|
fi
|
||||||
|
|
||||||
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
}
|
||||||
|
|
||||||
|
# Accept the entire suggestion and execute it
|
||||||
|
_zsh_autosuggest_execute() {
|
||||||
|
# Add the suggestion to the buffer
|
||||||
|
BUFFER="$BUFFER$POSTDISPLAY"
|
||||||
|
|
||||||
|
# Remove the suggestion
|
||||||
|
unset POSTDISPLAY
|
||||||
|
|
||||||
|
# Call the original `accept-line` to handle syntax highlighting or
|
||||||
|
# other potential custom behavior
|
||||||
|
_zsh_autosuggest_invoke_original_widget "accept-line"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Partially accept the suggestion
|
||||||
|
_zsh_autosuggest_partial_accept() {
|
||||||
|
local -i retval
|
||||||
|
|
||||||
|
# Save the contents of the buffer so we can restore later if needed
|
||||||
|
local original_buffer="$BUFFER"
|
||||||
|
|
||||||
|
# Temporarily accept the suggestion.
|
||||||
|
BUFFER="$BUFFER$POSTDISPLAY"
|
||||||
|
|
||||||
|
# Original widget moves the cursor
|
||||||
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
retval=$?
|
||||||
|
|
||||||
|
# If we've moved past the end of the original buffer
|
||||||
|
if [ $CURSOR -gt $#original_buffer ]; then
|
||||||
|
# Set POSTDISPLAY to text right of the cursor
|
||||||
|
POSTDISPLAY="$RBUFFER"
|
||||||
|
|
||||||
|
# Clip the buffer at the cursor
|
||||||
|
BUFFER="$LBUFFER"
|
||||||
|
else
|
||||||
|
# Restore the original buffer
|
||||||
|
BUFFER="$original_buffer"
|
||||||
|
fi
|
||||||
|
|
||||||
|
return $retval
|
||||||
|
}
|
||||||
|
|
||||||
|
for action in clear modify accept partial_accept execute; do
|
||||||
|
eval "_zsh_autosuggest_widget_$action() {
|
||||||
|
local -i retval
|
||||||
|
|
||||||
|
_zsh_autosuggest_highlight_reset
|
||||||
|
|
||||||
|
_zsh_autosuggest_$action \$@
|
||||||
|
retval=\$?
|
||||||
|
|
||||||
|
_zsh_autosuggest_highlight_apply
|
||||||
|
|
||||||
|
return \$retval
|
||||||
|
}"
|
||||||
|
done
|
||||||
|
|
||||||
|
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
||||||
|
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||||
|
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
||||||
45
test/bind_test.zsh
Normal file
45
test/bind_test.zsh
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
testInvokeOriginalWidgetDefined() {
|
||||||
|
stub_and_eval \
|
||||||
|
zle \
|
||||||
|
'return 1'
|
||||||
|
|
||||||
|
_zsh_autosuggest_invoke_original_widget 'self-insert'
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'1' \
|
||||||
|
"$?"
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
'zle was not invoked' \
|
||||||
|
'stub_called zle'
|
||||||
|
|
||||||
|
restore zle
|
||||||
|
}
|
||||||
|
|
||||||
|
testInvokeOriginalWidgetUndefined() {
|
||||||
|
stub_and_eval \
|
||||||
|
zle \
|
||||||
|
'return 1'
|
||||||
|
|
||||||
|
_zsh_autosuggest_invoke_original_widget 'some-undefined-widget'
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'0' \
|
||||||
|
"$?"
|
||||||
|
|
||||||
|
assertFalse \
|
||||||
|
'zle was invoked' \
|
||||||
|
'stub_called zle'
|
||||||
|
|
||||||
|
restore zle
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
73
test/highlight_test.zsh
Normal file
73
test/highlight_test.zsh
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
testHighlightDefaultStyle() {
|
||||||
|
assertEquals \
|
||||||
|
'fg=8' \
|
||||||
|
"$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
|
||||||
|
}
|
||||||
|
|
||||||
|
testHighlightApplyWithSuggestion() {
|
||||||
|
local 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"
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
56
test/strategies/default_test.zsh
Executable file
56
test/strategies/default_test.zsh
Executable file
@@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/../test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
testNoMatch() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
ls foo
|
||||||
|
ls bar
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'foo' \
|
||||||
|
''
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls q' \
|
||||||
|
''
|
||||||
|
}
|
||||||
|
|
||||||
|
testBasicMatches() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
ls foo
|
||||||
|
ls bar
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls f' \
|
||||||
|
'ls foo'
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls b' \
|
||||||
|
'ls bar'
|
||||||
|
}
|
||||||
|
|
||||||
|
testMostRecentMatch() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
ls foo
|
||||||
|
cd bar
|
||||||
|
ls baz
|
||||||
|
cd quux
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls' \
|
||||||
|
'ls baz'
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'cd' \
|
||||||
|
'cd quux'
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
74
test/strategies/match_prev_cmd_test.zsh
Executable file
74
test/strategies/match_prev_cmd_test.zsh
Executable file
@@ -0,0 +1,74 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/../test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
|
||||||
|
ZSH_AUTOSUGGEST_STRATEGY=match_prev_cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
testNoMatch() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
ls foo
|
||||||
|
ls bar
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'foo' \
|
||||||
|
''
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls q' \
|
||||||
|
''
|
||||||
|
}
|
||||||
|
|
||||||
|
testBasicMatches() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
ls foo
|
||||||
|
ls bar
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls f' \
|
||||||
|
'ls foo'
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls b' \
|
||||||
|
'ls bar'
|
||||||
|
}
|
||||||
|
|
||||||
|
testMostRecentMatch() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
ls foo
|
||||||
|
cd bar
|
||||||
|
ls baz
|
||||||
|
cd quux
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls' \
|
||||||
|
'ls baz'
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'cd' \
|
||||||
|
'cd quux'
|
||||||
|
}
|
||||||
|
|
||||||
|
testMatchMostRecentAfterPreviousCmd() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
echo what
|
||||||
|
ls foo
|
||||||
|
ls bar
|
||||||
|
echo what
|
||||||
|
ls baz
|
||||||
|
ls quux
|
||||||
|
echo what
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'ls' \
|
||||||
|
'ls baz'
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
95
test/strategies_test.zsh
Normal file
95
test/strategies_test.zsh
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
assertBackslashSuggestion() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
echo "hello\nworld"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'echo "hello\' \
|
||||||
|
'echo "hello\nworld"'
|
||||||
|
}
|
||||||
|
|
||||||
|
assertDoubleBackslashSuggestion() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
echo "\\"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'echo "\\' \
|
||||||
|
'echo "\\"'
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTildeSuggestion() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
cd ~/something
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'cd' \
|
||||||
|
'cd ~/something'
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'cd ~' \
|
||||||
|
'cd ~/something'
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'cd ~/s' \
|
||||||
|
'cd ~/something'
|
||||||
|
}
|
||||||
|
|
||||||
|
assertParenthesesSuggestion() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
echo "$(ls foo)"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'echo "$(' \
|
||||||
|
'echo "$(ls foo)"'
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSquareBracketsSuggestion() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
echo "$history[123]"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'echo "$history[' \
|
||||||
|
'echo "$history[123]"'
|
||||||
|
}
|
||||||
|
|
||||||
|
assertHashSuggestion() {
|
||||||
|
set_history <<-'EOF'
|
||||||
|
echo "#yolo"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertSuggestion \
|
||||||
|
'echo "#' \
|
||||||
|
'echo "#yolo"'
|
||||||
|
}
|
||||||
|
|
||||||
|
testSpecialCharsForAllStrategies() {
|
||||||
|
local strategies
|
||||||
|
strategies=(
|
||||||
|
"default"
|
||||||
|
"match_prev_cmd"
|
||||||
|
)
|
||||||
|
|
||||||
|
for s in $strategies; do
|
||||||
|
ZSH_AUTOSUGGEST_STRATEGY="$s"
|
||||||
|
|
||||||
|
assertBackslashSuggestion
|
||||||
|
assertDoubleBackslashSuggestion
|
||||||
|
assertTildeSuggestion
|
||||||
|
assertParenthesesSuggestion
|
||||||
|
assertSquareBracketsSuggestion
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
46
test/suggestion_test.zsh
Normal file
46
test/suggestion_test.zsh
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
testEscapeCommand() {
|
||||||
|
assertEquals \
|
||||||
|
'Did not escape single backslash' \
|
||||||
|
'\\' \
|
||||||
|
"$(_zsh_autosuggest_escape_command '\')"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not escape two backslashes' \
|
||||||
|
'\\\\' \
|
||||||
|
"$(_zsh_autosuggest_escape_command '\\')"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not escape parentheses' \
|
||||||
|
'\(\)' \
|
||||||
|
"$(_zsh_autosuggest_escape_command '()')"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not escape square brackets' \
|
||||||
|
'\[\]' \
|
||||||
|
"$(_zsh_autosuggest_escape_command '[]')"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not escape pipe' \
|
||||||
|
'\|' \
|
||||||
|
"$(_zsh_autosuggest_escape_command '|')"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not escape star' \
|
||||||
|
'\*' \
|
||||||
|
"$(_zsh_autosuggest_escape_command '*')"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not escape question mark' \
|
||||||
|
'\?' \
|
||||||
|
"$(_zsh_autosuggest_escape_command '?')"
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
60
test/test_helper.zsh
Normal file
60
test/test_helper.zsh
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
DIR="${0:a:h}"
|
||||||
|
ROOT_DIR="$DIR/.."
|
||||||
|
VENDOR_DIR="$ROOT_DIR/vendor"
|
||||||
|
|
||||||
|
# Use stub.sh for stubbing/mocking
|
||||||
|
source "$VENDOR_DIR/stub.sh/stub.sh"
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Helper Functions #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Source the autosuggestions plugin file
|
||||||
|
source_autosuggestions() {
|
||||||
|
source "$ROOT_DIR/zsh-autosuggestions.zsh"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Set history list from stdin
|
||||||
|
set_history() {
|
||||||
|
# Make a tmp file in shunit's tmp dir
|
||||||
|
local tmp=$(mktemp "$SHUNIT_TMPDIR/hist.XXX")
|
||||||
|
|
||||||
|
# Write from stdin to the tmp file
|
||||||
|
> "$tmp"
|
||||||
|
|
||||||
|
# Write an extra line to simulate history active mode
|
||||||
|
# See https://github.com/zsh-users/zsh/blob/ca3bc0d95d7deab4f5381f12b15047de748c0814/Src/hist.c#L69-L82
|
||||||
|
echo >> "$tmp"
|
||||||
|
|
||||||
|
# Clear history and re-read from the tmp file
|
||||||
|
fc -P; fc -p; fc -R "$tmp"
|
||||||
|
|
||||||
|
rm "$tmp"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Should be called at the bottom of every test suite file
|
||||||
|
# Pass in the name of the test script ($0) for shunit
|
||||||
|
run_tests() {
|
||||||
|
local test_script="$1"
|
||||||
|
shift
|
||||||
|
|
||||||
|
# Required for shunit to work with zsh
|
||||||
|
setopt localoptions shwordsplit
|
||||||
|
SHUNIT_PARENT="$test_script"
|
||||||
|
|
||||||
|
source "$VENDOR_DIR/shunit2/2.1.6/src/shunit2"
|
||||||
|
}
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Custom Assertions #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
assertSuggestion() {
|
||||||
|
local prefix="$1"
|
||||||
|
local expected_suggestion="$2"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"Did not get correct suggestion for prefix:<$prefix> using strategy <$ZSH_AUTOSUGGEST_STRATEGY>" \
|
||||||
|
"$expected_suggestion" \
|
||||||
|
"$(_zsh_autosuggest_suggestion "$prefix")"
|
||||||
|
}
|
||||||
161
test/widgets/accept_test.zsh
Normal file
161
test/widgets/accept_test.zsh
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/../test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
setUp() {
|
||||||
|
BUFFER=''
|
||||||
|
POSTDISPLAY=''
|
||||||
|
CURSOR=0
|
||||||
|
KEYMAP='main'
|
||||||
|
}
|
||||||
|
|
||||||
|
tearDown() {
|
||||||
|
restore _zsh_autosuggest_invoke_original_widget
|
||||||
|
}
|
||||||
|
|
||||||
|
testCursorAtEnd() {
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
testCursorNotAtEnd() {
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
testViCursorAtEnd() {
|
||||||
|
BUFFER='echo'
|
||||||
|
POSTDISPLAY=' hello'
|
||||||
|
CURSOR=3
|
||||||
|
KEYMAP='vicmd'
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
testViCursorNotAtEnd() {
|
||||||
|
BUFFER='echo'
|
||||||
|
POSTDISPLAY=' hello'
|
||||||
|
CURSOR=2
|
||||||
|
KEYMAP='vicmd'
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
testRetval() {
|
||||||
|
stub_and_eval \
|
||||||
|
_zsh_autosuggest_invoke_original_widget \
|
||||||
|
'return 1'
|
||||||
|
|
||||||
|
_zsh_autosuggest_widget_accept 'original-widget'
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not return correct value from original widget' \
|
||||||
|
'1' \
|
||||||
|
"$?"
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidget() {
|
||||||
|
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'
|
||||||
|
|
||||||
|
restore _zsh_autosuggest_highlight_reset
|
||||||
|
restore _zsh_autosuggest_accept
|
||||||
|
restore _zsh_autosuggest_highlight_apply
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
77
test/widgets/clear_test.zsh
Normal file
77
test/widgets/clear_test.zsh
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/../test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
setUp() {
|
||||||
|
BUFFER=''
|
||||||
|
POSTDISPLAY=''
|
||||||
|
}
|
||||||
|
|
||||||
|
tearDown() {
|
||||||
|
restore _zsh_autosuggest_invoke_original_widget
|
||||||
|
}
|
||||||
|
|
||||||
|
testClear() {
|
||||||
|
BUFFER='ec'
|
||||||
|
POSTDISPLAY='ho hello'
|
||||||
|
|
||||||
|
_zsh_autosuggest_clear 'original-widget'
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'BUFFER was modified' \
|
||||||
|
'ec' \
|
||||||
|
"$BUFFER"
|
||||||
|
|
||||||
|
assertNull \
|
||||||
|
'POSTDISPLAY was not cleared' \
|
||||||
|
"$POSTDISPLAY"
|
||||||
|
}
|
||||||
|
|
||||||
|
testRetval() {
|
||||||
|
stub_and_eval \
|
||||||
|
_zsh_autosuggest_invoke_original_widget \
|
||||||
|
'return 1'
|
||||||
|
|
||||||
|
_zsh_autosuggest_widget_clear 'original-widget'
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not return correct value from original widget' \
|
||||||
|
'1' \
|
||||||
|
"$?"
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidget() {
|
||||||
|
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'
|
||||||
|
|
||||||
|
restore _zsh_autosuggest_highlight_reset
|
||||||
|
restore _zsh_autosuggest_clear
|
||||||
|
restore _zsh_autosuggest_highlight_apply
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
26
test/widgets/execute_test.zsh
Normal file
26
test/widgets/execute_test.zsh
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/../test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
tearDown() {
|
||||||
|
restore _zsh_autosuggest_invoke_original_widget
|
||||||
|
}
|
||||||
|
|
||||||
|
testRetval() {
|
||||||
|
stub_and_eval \
|
||||||
|
_zsh_autosuggest_invoke_original_widget \
|
||||||
|
'return 1'
|
||||||
|
|
||||||
|
_zsh_autosuggest_widget_execute 'original-widget'
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not return correct value from original widget' \
|
||||||
|
'1' \
|
||||||
|
"$?"
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
58
test/widgets/modify_test.zsh
Normal file
58
test/widgets/modify_test.zsh
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/../test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
setUp() {
|
||||||
|
BUFFER=''
|
||||||
|
POSTDISPLAY=''
|
||||||
|
}
|
||||||
|
|
||||||
|
tearDown() {
|
||||||
|
restore _zsh_autosuggest_invoke_original_widget
|
||||||
|
restore _zsh_autosuggest_suggestion
|
||||||
|
}
|
||||||
|
|
||||||
|
testModify() {
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
testRetval() {
|
||||||
|
stub_and_eval \
|
||||||
|
_zsh_autosuggest_invoke_original_widget \
|
||||||
|
'return 1'
|
||||||
|
|
||||||
|
_zsh_autosuggest_widget_modify 'original-widget'
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not return correct value from original widget' \
|
||||||
|
'1' \
|
||||||
|
"$?"
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
84
test/widgets/partial_accept_test.zsh
Normal file
84
test/widgets/partial_accept_test.zsh
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
source "${0:a:h}/../test_helper.zsh"
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
source_autosuggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
setUp() {
|
||||||
|
BUFFER=''
|
||||||
|
POSTDISPLAY=''
|
||||||
|
CURSOR=0
|
||||||
|
}
|
||||||
|
|
||||||
|
tearDown() {
|
||||||
|
restore _zsh_autosuggest_invoke_original_widget
|
||||||
|
}
|
||||||
|
|
||||||
|
testCursorMovesOutOfBuffer() {
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
testCursorStaysInBuffer() {
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
testRetval() {
|
||||||
|
stub_and_eval \
|
||||||
|
_zsh_autosuggest_invoke_original_widget \
|
||||||
|
'return 1'
|
||||||
|
|
||||||
|
_zsh_autosuggest_widget_partial_accept 'original-widget'
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
'Did not return correct value from original widget' \
|
||||||
|
'1' \
|
||||||
|
"$?"
|
||||||
|
}
|
||||||
|
|
||||||
|
run_tests "$0"
|
||||||
1
vendor/shunit2
vendored
Submodule
1
vendor/shunit2
vendored
Submodule
Submodule vendor/shunit2 added at 46973db9df
1
vendor/stub.sh
vendored
Submodule
1
vendor/stub.sh
vendored
Submodule
Submodule vendor/stub.sh added at bd6f3c4666
@@ -1 +0,0 @@
|
|||||||
autosuggestions.zsh
|
|
||||||
1
zsh-autosuggestions.plugin.zsh
Normal file
1
zsh-autosuggestions.plugin.zsh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
source ${0:A:h}/zsh-autosuggestions.zsh
|
||||||
444
zsh-autosuggestions.zsh
Normal file
444
zsh-autosuggestions.zsh
Normal file
@@ -0,0 +1,444 @@
|
|||||||
|
# Fish-like fast/unobtrusive autosuggestions for zsh.
|
||||||
|
# https://github.com/zsh-users/zsh-autosuggestions
|
||||||
|
# v0.3.2
|
||||||
|
# Copyright (c) 2013 Thiago de Arruda
|
||||||
|
# Copyright (c) 2016 Eric Freese
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person
|
||||||
|
# obtaining a copy of this software and associated documentation
|
||||||
|
# files (the "Software"), to deal in the Software without
|
||||||
|
# restriction, including without limitation the rights to use,
|
||||||
|
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the
|
||||||
|
# Software is furnished to do so, subject to the following
|
||||||
|
# conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
# OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Global Configuration Variables #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Color to use when highlighting suggestion
|
||||||
|
# Uses format of `region_highlight`
|
||||||
|
# More info: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets
|
||||||
|
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
|
||||||
|
|
||||||
|
# Prefix to use when saving original versions of bound widgets
|
||||||
|
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
||||||
|
|
||||||
|
ZSH_AUTOSUGGEST_STRATEGY=default
|
||||||
|
|
||||||
|
# Widgets that clear the suggestion
|
||||||
|
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-history
|
||||||
|
down-line-or-history
|
||||||
|
accept-line
|
||||||
|
)
|
||||||
|
|
||||||
|
# Widgets that accept the entire suggestion
|
||||||
|
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
||||||
|
forward-char
|
||||||
|
end-of-line
|
||||||
|
vi-forward-char
|
||||||
|
vi-end-of-line
|
||||||
|
vi-add-eol
|
||||||
|
)
|
||||||
|
|
||||||
|
# Widgets that accept the entire suggestion and execute it
|
||||||
|
ZSH_AUTOSUGGEST_EXECUTE_WIDGETS=(
|
||||||
|
)
|
||||||
|
|
||||||
|
# Widgets that accept the suggestion as far as the cursor moves
|
||||||
|
ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
|
||||||
|
forward-word
|
||||||
|
vi-forward-word
|
||||||
|
vi-forward-word-end
|
||||||
|
vi-forward-blank-word
|
||||||
|
vi-forward-blank-word-end
|
||||||
|
)
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Handle Deprecated Variables/Widgets #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
_zsh_autosuggest_deprecated_warning() {
|
||||||
|
>&2 echo "zsh-autosuggestions: $@"
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_check_deprecated_config() {
|
||||||
|
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then
|
||||||
|
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_HIGHLIGHT_COLOR is deprecated. Use ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE instead."
|
||||||
|
[ -z "$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" ] && ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=$AUTOSUGGESTION_HIGHLIGHT_STYLE
|
||||||
|
unset AUTOSUGGESTION_HIGHLIGHT_STYLE
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_CURSOR" ]; then
|
||||||
|
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_HIGHLIGHT_CURSOR is deprecated."
|
||||||
|
unset AUTOSUGGESTION_HIGHLIGHT_CURSOR
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$AUTOSUGGESTION_ACCEPT_RIGHT_ARROW" ]; then
|
||||||
|
_zsh_autosuggest_deprecated_warning "AUTOSUGGESTION_ACCEPT_RIGHT_ARROW is deprecated. The right arrow now accepts the suggestion by default."
|
||||||
|
unset AUTOSUGGESTION_ACCEPT_RIGHT_ARROW
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_deprecated_start_widget() {
|
||||||
|
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. For more info, see the README at https://github.com/zsh-users/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 #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
|
||||||
|
_zsh_autosuggest_bind_widget() {
|
||||||
|
local widget=$1
|
||||||
|
local autosuggest_action=$2
|
||||||
|
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
|
||||||
|
|
||||||
|
# Save a reference to the original widget
|
||||||
|
case $widgets[$widget] in
|
||||||
|
# Already bound
|
||||||
|
user:_zsh_autosuggest_(bound|orig)_*);;
|
||||||
|
|
||||||
|
# User-defined widget
|
||||||
|
user:*)
|
||||||
|
zle -N $prefix$widget ${widgets[$widget]#*:}
|
||||||
|
;;
|
||||||
|
|
||||||
|
# Built-in widget
|
||||||
|
builtin)
|
||||||
|
eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
|
||||||
|
zle -N $prefix$widget _zsh_autosuggest_orig_$widget
|
||||||
|
;;
|
||||||
|
|
||||||
|
# Completion widget
|
||||||
|
completion:*)
|
||||||
|
eval "zle -C $prefix${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# 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_${(q)widget}() {
|
||||||
|
_zsh_autosuggest_widget_$autosuggest_action $prefix${(q)widget} \$@
|
||||||
|
}"
|
||||||
|
|
||||||
|
# Create the bound widget
|
||||||
|
zle -N $widget _zsh_autosuggest_bound_$widget
|
||||||
|
}
|
||||||
|
|
||||||
|
# Map all configured widgets to the right autosuggest widgets
|
||||||
|
_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*|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 accept
|
||||||
|
elif [ ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]; then
|
||||||
|
_zsh_autosuggest_bind_widget $widget execute
|
||||||
|
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
|
||||||
|
_zsh_autosuggest_bind_widget $widget partial_accept
|
||||||
|
else
|
||||||
|
# Assume any unspecified widget might modify the buffer
|
||||||
|
_zsh_autosuggest_bind_widget $widget modify
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# Given the name of an original widget and args, invoke it, if it exists
|
||||||
|
_zsh_autosuggest_invoke_original_widget() {
|
||||||
|
# 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 -- $@
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Highlighting #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# If there was a highlight, remove it
|
||||||
|
_zsh_autosuggest_highlight_reset() {
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
||||||
|
|
||||||
|
if [ -n "$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT" ]; then
|
||||||
|
region_highlight=("${(@)region_highlight:#$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT}")
|
||||||
|
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# If there's a suggestion, highlight it
|
||||||
|
_zsh_autosuggest_highlight_apply() {
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
||||||
|
|
||||||
|
if [ $#POSTDISPLAY -gt 0 ]; then
|
||||||
|
_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT="$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
|
||||||
|
region_highlight+=("$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT")
|
||||||
|
else
|
||||||
|
unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Autosuggest Widget Implementations #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Clear the suggestion
|
||||||
|
_zsh_autosuggest_clear() {
|
||||||
|
# Remove the suggestion
|
||||||
|
unset POSTDISPLAY
|
||||||
|
|
||||||
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
}
|
||||||
|
|
||||||
|
# Modify the buffer and get a new suggestion
|
||||||
|
_zsh_autosuggest_modify() {
|
||||||
|
local -i retval
|
||||||
|
|
||||||
|
# Clear suggestion while original widget runs
|
||||||
|
unset POSTDISPLAY
|
||||||
|
|
||||||
|
# Original widget modifies the buffer
|
||||||
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
retval=$?
|
||||||
|
|
||||||
|
# Get a new suggestion if the buffer is not empty after modification
|
||||||
|
local suggestion
|
||||||
|
if [ $#BUFFER -gt 0 ]; then
|
||||||
|
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Add the suggestion to the POSTDISPLAY
|
||||||
|
if [ -n "$suggestion" ]; then
|
||||||
|
POSTDISPLAY="${suggestion#$BUFFER}"
|
||||||
|
else
|
||||||
|
unset POSTDISPLAY
|
||||||
|
fi
|
||||||
|
|
||||||
|
return $retval
|
||||||
|
}
|
||||||
|
|
||||||
|
# Accept the entire suggestion
|
||||||
|
_zsh_autosuggest_accept() {
|
||||||
|
local -i max_cursor_pos=$#BUFFER
|
||||||
|
|
||||||
|
# When vicmd keymap is active, the cursor can't move all the way
|
||||||
|
# to the end of the buffer
|
||||||
|
if [ "$KEYMAP" = "vicmd" ]; then
|
||||||
|
max_cursor_pos=$((max_cursor_pos - 1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Only accept if the cursor is at the end of the buffer
|
||||||
|
if [ $CURSOR -eq $max_cursor_pos ]; then
|
||||||
|
# Add the suggestion to the buffer
|
||||||
|
BUFFER="$BUFFER$POSTDISPLAY"
|
||||||
|
|
||||||
|
# Remove the suggestion
|
||||||
|
unset POSTDISPLAY
|
||||||
|
|
||||||
|
# Move the cursor to the end of the buffer
|
||||||
|
CURSOR=${#BUFFER}
|
||||||
|
fi
|
||||||
|
|
||||||
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
}
|
||||||
|
|
||||||
|
# Accept the entire suggestion and execute it
|
||||||
|
_zsh_autosuggest_execute() {
|
||||||
|
# Add the suggestion to the buffer
|
||||||
|
BUFFER="$BUFFER$POSTDISPLAY"
|
||||||
|
|
||||||
|
# Remove the suggestion
|
||||||
|
unset POSTDISPLAY
|
||||||
|
|
||||||
|
# Call the original `accept-line` to handle syntax highlighting or
|
||||||
|
# other potential custom behavior
|
||||||
|
_zsh_autosuggest_invoke_original_widget "accept-line"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Partially accept the suggestion
|
||||||
|
_zsh_autosuggest_partial_accept() {
|
||||||
|
local -i retval
|
||||||
|
|
||||||
|
# Save the contents of the buffer so we can restore later if needed
|
||||||
|
local original_buffer="$BUFFER"
|
||||||
|
|
||||||
|
# Temporarily accept the suggestion.
|
||||||
|
BUFFER="$BUFFER$POSTDISPLAY"
|
||||||
|
|
||||||
|
# Original widget moves the cursor
|
||||||
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
retval=$?
|
||||||
|
|
||||||
|
# If we've moved past the end of the original buffer
|
||||||
|
if [ $CURSOR -gt $#original_buffer ]; then
|
||||||
|
# Set POSTDISPLAY to text right of the cursor
|
||||||
|
POSTDISPLAY="$RBUFFER"
|
||||||
|
|
||||||
|
# Clip the buffer at the cursor
|
||||||
|
BUFFER="$LBUFFER"
|
||||||
|
else
|
||||||
|
# Restore the original buffer
|
||||||
|
BUFFER="$original_buffer"
|
||||||
|
fi
|
||||||
|
|
||||||
|
return $retval
|
||||||
|
}
|
||||||
|
|
||||||
|
for action in clear modify accept partial_accept execute; do
|
||||||
|
eval "_zsh_autosuggest_widget_$action() {
|
||||||
|
local -i retval
|
||||||
|
|
||||||
|
_zsh_autosuggest_highlight_reset
|
||||||
|
|
||||||
|
_zsh_autosuggest_$action \$@
|
||||||
|
retval=\$?
|
||||||
|
|
||||||
|
_zsh_autosuggest_highlight_apply
|
||||||
|
|
||||||
|
return \$retval
|
||||||
|
}"
|
||||||
|
done
|
||||||
|
|
||||||
|
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
||||||
|
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||||
|
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Suggestion #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Delegate to the selected strategy to determine a suggestion
|
||||||
|
_zsh_autosuggest_suggestion() {
|
||||||
|
local escaped_prefix="$(_zsh_autosuggest_escape_command "$1")"
|
||||||
|
local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY"
|
||||||
|
|
||||||
|
if [ -n "$functions[$strategy_function]" ]; then
|
||||||
|
echo -E "$($strategy_function "$escaped_prefix")"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_escape_command() {
|
||||||
|
setopt localoptions EXTENDED_GLOB
|
||||||
|
|
||||||
|
# Escape special chars in the string (requires EXTENDED_GLOB)
|
||||||
|
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
||||||
|
}
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Default Suggestion Strategy #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Suggests the most recent history item that matches the given
|
||||||
|
# prefix.
|
||||||
|
#
|
||||||
|
|
||||||
|
_zsh_autosuggest_strategy_default() {
|
||||||
|
local prefix="$1"
|
||||||
|
|
||||||
|
# Get the keys of the history items that match
|
||||||
|
local -a histkeys
|
||||||
|
histkeys=(${(k)history[(r)$prefix*]})
|
||||||
|
|
||||||
|
# Echo the value of the first key
|
||||||
|
echo -E "${history[$histkeys[1]]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Match Previous Command Suggestion Strategy #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Suggests the most recent history item that matches the given
|
||||||
|
# prefix and whose preceding history item also matches the most
|
||||||
|
# recently executed command.
|
||||||
|
#
|
||||||
|
# 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'.
|
||||||
|
#
|
||||||
|
|
||||||
|
_zsh_autosuggest_strategy_match_prev_cmd() {
|
||||||
|
local prefix="$1"
|
||||||
|
|
||||||
|
# Get all history event numbers that correspond to history
|
||||||
|
# entries that match pattern $prefix*
|
||||||
|
local history_match_keys
|
||||||
|
history_match_keys=(${(k)history[(R)$prefix*]})
|
||||||
|
|
||||||
|
# By default we use the first history number (most recent history entry)
|
||||||
|
local histkey="${history_match_keys[1]}"
|
||||||
|
|
||||||
|
# Get the previously executed command
|
||||||
|
local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")"
|
||||||
|
|
||||||
|
# Iterate up to the first 200 history event numbers that match $prefix
|
||||||
|
for key in "${(@)history_match_keys[1,200]}"; do
|
||||||
|
# Stop if we ran out of history
|
||||||
|
[[ $key -gt 1 ]] || break
|
||||||
|
|
||||||
|
# See if the history entry preceding the suggestion matches the
|
||||||
|
# previous command, and use it if it does
|
||||||
|
if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then
|
||||||
|
histkey="$key"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Echo the matched history entry
|
||||||
|
echo -E "$history[$histkey]"
|
||||||
|
}
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Start #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Start the autosuggestion widgets
|
||||||
|
_zsh_autosuggest_start() {
|
||||||
|
_zsh_autosuggest_check_deprecated_config
|
||||||
|
_zsh_autosuggest_bind_widgets
|
||||||
|
}
|
||||||
|
|
||||||
|
autoload -Uz add-zsh-hook
|
||||||
|
add-zsh-hook precmd _zsh_autosuggest_start
|
||||||
Reference in New Issue
Block a user