mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-12-29 07:34:11 +08:00
Add enable/disable/toggle widgets to disable suggestion functionality
[GitHub #219] Intended to be helpful for folks using bracketed-paste-magic and other widgets that use `zle -U`.
This commit is contained in:
parent
e1959d0f61
commit
7d4a1d9a4a
|
@ -101,11 +101,15 @@ As of `v0.4.0`, suggestions are fetched asynchronously using the `zsh/zpty` modu
|
||||||
|
|
||||||
### Key Bindings
|
### Key Bindings
|
||||||
|
|
||||||
This plugin provides three widgets that you can use with `bindkey`:
|
This plugin provides a few widgets that you can use with `bindkey`:
|
||||||
|
|
||||||
1. `autosuggest-accept`: Accepts the current suggestion.
|
1. `autosuggest-accept`: Accepts the current suggestion.
|
||||||
2. `autosuggest-execute`: Accepts and executes the current suggestion.
|
2. `autosuggest-execute`: Accepts and executes the current suggestion.
|
||||||
3. `autosuggest-clear`: Clears the current suggestion.
|
3. `autosuggest-clear`: Clears the current suggestion.
|
||||||
|
4. `autosuggest-fetch`: Fetches a suggestion (works even when suggestions are disabled).
|
||||||
|
5. `autosuggest-disable`: Disables suggestions.
|
||||||
|
6. `autosuggest-enable`: Re-enables suggestions.
|
||||||
|
7. `autosuggest-toggle`: Toggles between enabled/disabled suggestions.
|
||||||
|
|
||||||
For example, this would bind <kbd>ctrl</kbd> + <kbd>space</kbd> to accept the current suggestion.
|
For example, this would bind <kbd>ctrl</kbd> + <kbd>space</kbd> to accept the current suggestion.
|
||||||
|
|
||||||
|
|
19
spec/widgets/disable_spec.rb
Normal file
19
spec/widgets/disable_spec.rb
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
describe 'the `autosuggest-disable` widget' do
|
||||||
|
before do
|
||||||
|
session.run_command('bindkey ^B autosuggest-disable')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'disables suggestions and clears the suggestion' do
|
||||||
|
with_history('echo hello') do
|
||||||
|
session.send_string('echo')
|
||||||
|
wait_for { session.content }.to eq('echo hello')
|
||||||
|
|
||||||
|
session.send_keys('C-b')
|
||||||
|
wait_for { session.content }.to eq('echo')
|
||||||
|
|
||||||
|
session.send_string(' h')
|
||||||
|
sleep 1
|
||||||
|
expect(session.content).to eq('echo h')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
21
spec/widgets/enable_spec.rb
Normal file
21
spec/widgets/enable_spec.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
describe 'the `autosuggest-enable` widget' do
|
||||||
|
before do
|
||||||
|
session.
|
||||||
|
run_command('typeset -g _ZSH_AUTOSUGGEST_DISABLED').
|
||||||
|
run_command('bindkey ^B autosuggest-enable')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'enables suggestions and fetches a suggestion' do
|
||||||
|
with_history('echo world', 'echo hello') do
|
||||||
|
session.send_string('echo')
|
||||||
|
sleep 1
|
||||||
|
expect(session.content).to eq('echo')
|
||||||
|
|
||||||
|
session.send_keys('C-b')
|
||||||
|
wait_for { session.content }.to eq('echo hello')
|
||||||
|
|
||||||
|
session.send_string(' w')
|
||||||
|
wait_for { session.content }.to eq('echo world')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
24
spec/widgets/fetch_spec.rb
Normal file
24
spec/widgets/fetch_spec.rb
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
describe 'the `autosuggest-fetch` widget' do
|
||||||
|
context 'when suggestions are disabled' do
|
||||||
|
before do
|
||||||
|
session.
|
||||||
|
run_command('bindkey ^B autosuggest-disable').
|
||||||
|
run_command('bindkey ^F autosuggest-fetch').
|
||||||
|
send_keys('C-b')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will fetch and display a suggestion' do
|
||||||
|
with_history('echo hello') do
|
||||||
|
session.send_string('echo h')
|
||||||
|
sleep 1
|
||||||
|
expect(session.content).to eq('echo h')
|
||||||
|
|
||||||
|
session.send_keys('C-f')
|
||||||
|
wait_for { session.content }.to eq('echo hello')
|
||||||
|
|
||||||
|
session.send_string('e')
|
||||||
|
wait_for { session.content }.to eq('echo hello')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
26
spec/widgets/toggle_spec.rb
Normal file
26
spec/widgets/toggle_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
describe 'the `autosuggest-toggle` widget' do
|
||||||
|
before do
|
||||||
|
session.run_command('bindkey ^B autosuggest-toggle')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'toggles suggestions' do
|
||||||
|
with_history('echo world', 'echo hello') do
|
||||||
|
session.send_string('echo')
|
||||||
|
wait_for { session.content }.to eq('echo hello')
|
||||||
|
|
||||||
|
session.send_keys('C-b')
|
||||||
|
wait_for { session.content }.to eq('echo')
|
||||||
|
|
||||||
|
session.send_string(' h')
|
||||||
|
sleep 1
|
||||||
|
expect(session.content).to eq('echo h')
|
||||||
|
|
||||||
|
session.send_keys('C-b')
|
||||||
|
wait_for { session.content }.to eq('echo hello')
|
||||||
|
|
||||||
|
session.send_keys('C-h')
|
||||||
|
session.send_string('w')
|
||||||
|
wait_for { session.content }.to eq('echo world')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,6 +3,27 @@
|
||||||
# Autosuggest Widget Implementations #
|
# Autosuggest Widget Implementations #
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Disable suggestions
|
||||||
|
_zsh_autosuggest_disable() {
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_DISABLED
|
||||||
|
_zsh_autosuggest_clear
|
||||||
|
}
|
||||||
|
|
||||||
|
# Enable suggestions
|
||||||
|
_zsh_autosuggest_enable() {
|
||||||
|
unset _ZSH_AUTOSUGGEST_DISABLED
|
||||||
|
_zsh_autosuggest_fetch
|
||||||
|
}
|
||||||
|
|
||||||
|
# Toggle suggestions (enable/disable)
|
||||||
|
_zsh_autosuggest_toggle() {
|
||||||
|
if [ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]; then
|
||||||
|
_zsh_autosuggest_enable
|
||||||
|
else
|
||||||
|
_zsh_autosuggest_disable
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Clear the suggestion
|
# Clear the suggestion
|
||||||
_zsh_autosuggest_clear() {
|
_zsh_autosuggest_clear() {
|
||||||
# Remove the suggestion
|
# Remove the suggestion
|
||||||
|
@ -51,6 +72,11 @@ _zsh_autosuggest_modify() {
|
||||||
return $retval
|
return $retval
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Bail out if suggestions are disabled
|
||||||
|
if [ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]; then
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
|
||||||
# Get a new suggestion if the buffer is not empty after modification
|
# Get a new suggestion if the buffer is not empty after modification
|
||||||
if [ $#BUFFER -gt 0 ]; then
|
if [ $#BUFFER -gt 0 ]; then
|
||||||
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -le "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
|
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -le "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
|
||||||
|
@ -150,7 +176,7 @@ _zsh_autosuggest_partial_accept() {
|
||||||
return $retval
|
return $retval
|
||||||
}
|
}
|
||||||
|
|
||||||
for action in clear modify fetch suggest accept partial_accept execute; do
|
for action in clear modify fetch suggest accept partial_accept execute enable disable toggle; do
|
||||||
eval "_zsh_autosuggest_widget_$action() {
|
eval "_zsh_autosuggest_widget_$action() {
|
||||||
local -i retval
|
local -i retval
|
||||||
|
|
||||||
|
@ -172,3 +198,6 @@ zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest
|
||||||
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
||||||
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||||
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
||||||
|
zle -N autosuggest-enable _zsh_autosuggest_widget_enable
|
||||||
|
zle -N autosuggest-disable _zsh_autosuggest_widget_disable
|
||||||
|
zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle
|
||||||
|
|
|
@ -281,6 +281,27 @@ _zsh_autosuggest_highlight_apply() {
|
||||||
# Autosuggest Widget Implementations #
|
# Autosuggest Widget Implementations #
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Disable suggestions
|
||||||
|
_zsh_autosuggest_disable() {
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_DISABLED
|
||||||
|
_zsh_autosuggest_clear
|
||||||
|
}
|
||||||
|
|
||||||
|
# Enable suggestions
|
||||||
|
_zsh_autosuggest_enable() {
|
||||||
|
unset _ZSH_AUTOSUGGEST_DISABLED
|
||||||
|
_zsh_autosuggest_fetch
|
||||||
|
}
|
||||||
|
|
||||||
|
# Toggle suggestions (enable/disable)
|
||||||
|
_zsh_autosuggest_toggle() {
|
||||||
|
if [ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]; then
|
||||||
|
_zsh_autosuggest_enable
|
||||||
|
else
|
||||||
|
_zsh_autosuggest_disable
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Clear the suggestion
|
# Clear the suggestion
|
||||||
_zsh_autosuggest_clear() {
|
_zsh_autosuggest_clear() {
|
||||||
# Remove the suggestion
|
# Remove the suggestion
|
||||||
|
@ -329,6 +350,11 @@ _zsh_autosuggest_modify() {
|
||||||
return $retval
|
return $retval
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Bail out if suggestions are disabled
|
||||||
|
if [ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]; then
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
|
||||||
# Get a new suggestion if the buffer is not empty after modification
|
# Get a new suggestion if the buffer is not empty after modification
|
||||||
if [ $#BUFFER -gt 0 ]; then
|
if [ $#BUFFER -gt 0 ]; then
|
||||||
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -le "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
|
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -le "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
|
||||||
|
@ -428,7 +454,7 @@ _zsh_autosuggest_partial_accept() {
|
||||||
return $retval
|
return $retval
|
||||||
}
|
}
|
||||||
|
|
||||||
for action in clear modify fetch suggest accept partial_accept execute; do
|
for action in clear modify fetch suggest accept partial_accept execute enable disable toggle; do
|
||||||
eval "_zsh_autosuggest_widget_$action() {
|
eval "_zsh_autosuggest_widget_$action() {
|
||||||
local -i retval
|
local -i retval
|
||||||
|
|
||||||
|
@ -450,6 +476,9 @@ zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest
|
||||||
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
||||||
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||||
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
||||||
|
zle -N autosuggest-enable _zsh_autosuggest_widget_enable
|
||||||
|
zle -N autosuggest-disable _zsh_autosuggest_widget_disable
|
||||||
|
zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle
|
||||||
|
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
# Default Suggestion Strategy #
|
# Default Suggestion Strategy #
|
||||||
|
|
Loading…
Reference in New Issue
Block a user