Skip to content

Commit

Permalink
Add enable/disable/toggle widgets to disable suggestion functionality
Browse files Browse the repository at this point in the history
[GitHub #219]

Intended to be helpful for folks using bracketed-paste-magic and other
widgets that use `zle -U`.
  • Loading branch information
ericfreese committed Mar 3, 2017
1 parent 582dfcc commit 4dba939
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ As of `v0.4.0`, suggestions are fetched asynchronously using the `zsh/zpty` modu
### 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.
2. `autosuggest-execute`: Accepts and executes 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.
Expand Down
19 changes: 19 additions & 0 deletions spec/widgets/disable_spec.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions spec/widgets/enable_spec.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions spec/widgets/fetch_spec.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions spec/widgets/toggle_spec.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 30 additions & 1 deletion src/widgets.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
# 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
_zsh_autosuggest_clear() {
# Remove the suggestion
Expand Down Expand Up @@ -51,6 +72,11 @@ _zsh_autosuggest_modify() {
return $retval
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
if [ $#BUFFER -gt 0 ]; then
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -le "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
Expand Down Expand Up @@ -150,7 +176,7 @@ _zsh_autosuggest_partial_accept() {
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() {
local -i retval
Expand All @@ -172,3 +198,6 @@ zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
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
31 changes: 30 additions & 1 deletion zsh-autosuggestions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,27 @@ _zsh_autosuggest_highlight_apply() {
# 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
_zsh_autosuggest_clear() {
# Remove the suggestion
Expand Down Expand Up @@ -329,6 +350,11 @@ _zsh_autosuggest_modify() {
return $retval
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
if [ $#BUFFER -gt 0 ]; then
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -le "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
Expand Down Expand Up @@ -428,7 +454,7 @@ _zsh_autosuggest_partial_accept() {
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() {
local -i retval
Expand All @@ -450,6 +476,9 @@ zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
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 #
Expand Down

0 comments on commit 4dba939

Please sign in to comment.