From 2461a98857c137cbc3c93d0fa93e56854007af89 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 15 Feb 2016 08:31:00 -0700 Subject: [PATCH] Fix segfaults once and for all? --- README.md | 23 +---------------------- script/test.zsh | 16 ++++++++-------- src/bind.zsh | 20 ++++++++++++++++---- zsh-autosuggestions.zsh | 20 ++++++++++++++++---- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 4364419..84e52aa 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,6 @@ It suggests commands as you type, based on command history. 3. Start a new terminal session. -**Note:** There is an open issue ([#102](https://github.com/tarruda/zsh-autosuggestions/issues/102)) when using this plugin with `bracketed-paste-magic`, which is enabled by default by Oh My Zsh. See the comments in that issue for a workaround. - ## Usage @@ -95,32 +93,13 @@ bindkey '^ ' autosuggest-accept ### [`zsh-history-substring-search`](https://github.com/zsh-users/zsh-history-substring-search) -When the buffer is empty and one of the `history-substring-search-up/down` widgets is invoked, it will call the `up/down-line-or-history` widget. If the `up/down-line-or-history` widgets are in `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` (the list of widgets that clear the suggestion), this can create an infinite recursion, crashing the shell session. - -For best results, you'll want to remove `up-line-or-history` and `down-line-or-history` from `ZSH_AUTOSUGGEST_CLEAR_WIDGETS`: - -``` -# Remove *-line-or-history widgets from list of widgets that clear the autosuggestion to avoid conflict with history-substring-search-* widgets -ZSH_AUTOSUGGEST_CLEAR_WIDGETS=("${(@)ZSH_AUTOSUGGEST_CLEAR_WIDGETS:#(up|down)-line-or-history}") -``` - -Additionally, the `history-substring-search-up` and `history-substring-search-down` widgets are not bound by default. You'll probably want to add them to `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` so that the suggestion will be cleared when you start searching through history: +The `history-substring-search-up` and `history-substring-search-down` widgets are not bound by default. You'll probably want to add them to `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` so that the suggestion will be cleared when you start searching through history: ```sh # Add history-substring-search-* widgets to list of widgets that clear the autosuggestion ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down) ``` -For example: - -```sh -source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh -source ~/Code/zsh-history-substring-search/zsh-history-substring-search.zsh - -ZSH_AUTOSUGGEST_CLEAR_WIDGETS=("${(@)ZSH_AUTOSUGGEST_CLEAR_WIDGETS:#(up|down)-line-or-history}") -ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down) -``` - ## Troubleshooting diff --git a/script/test.zsh b/script/test.zsh index 23f2793..67c6882 100755 --- a/script/test.zsh +++ b/script/test.zsh @@ -85,7 +85,7 @@ testWidgetFunctionClear() { BUFFER="ec" POSTDISPLAY="ho hello" - _zsh_autosuggest_clear + _zsh_autosuggest_clear "original-widget" assertEquals \ "BUFFER was modified" \ @@ -109,7 +109,7 @@ testWidgetFunctionModify() { _zsh_autosuggest_suggestion \ "echo hello" - _zsh_autosuggest_modify + _zsh_autosuggest_modify "original-widget" assertTrue \ "original widget not invoked" \ @@ -136,7 +136,7 @@ testWidgetFunctionAcceptCursorAtEnd() { stub _zsh_autosuggest_invoke_original_widget - _zsh_autosuggest_accept + _zsh_autosuggest_accept "original-widget" assertTrue \ "original widget not invoked" \ @@ -160,7 +160,7 @@ testWidgetFunctionAcceptCursorNotAtEnd() { stub _zsh_autosuggest_invoke_original_widget - _zsh_autosuggest_accept + _zsh_autosuggest_accept "original-widget" assertTrue \ "original widget not invoked" \ @@ -186,7 +186,7 @@ testWidgetFunctionPartialAcceptCursorMovesOutOfBuffer() { _zsh_autosuggest_invoke_original_widget \ 'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"' - _zsh_autosuggest_partial_accept + _zsh_autosuggest_partial_accept "original-widget" assertTrue \ "original widget not invoked" \ @@ -212,7 +212,7 @@ testWidgetFunctionPartialAcceptCursorStaysInBuffer() { _zsh_autosuggest_invoke_original_widget \ 'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"' - _zsh_autosuggest_partial_accept + _zsh_autosuggest_partial_accept "original-widget" assertTrue \ "original widget not invoked" \ @@ -236,7 +236,7 @@ testWidgetAccept() { # Call the function pointed to by the widget since we can't call # the widget itself when zle is not active - ${widgets[autosuggest-accept]#*:} + ${widgets[autosuggest-accept]#*:} "original-widget" assertTrue \ "autosuggest-accept widget does not exist" \ @@ -262,7 +262,7 @@ testWidgetClear() { # Call the function pointed to by the widget since we can't call # the widget itself when zle is not active - ${widgets[autosuggest-clear]#*:} + ${widgets[autosuggest-clear]#*:} "original-widget" assertTrue \ "autosuggest-clear widget does not exist" \ diff --git a/src/bind.zsh b/src/bind.zsh index 01a211a..6c6d8f9 100644 --- a/src/bind.zsh +++ b/src/bind.zsh @@ -12,7 +12,7 @@ _zsh_autosuggest_bind_widget() { # Save a reference to the original widget case $widgets[$widget] in # Already bound - user:_zsh_autosuggest_(widget|orig)_*);; + user:_zsh_autosuggest_(bound|orig)_*);; # User-defined widget user:*) @@ -31,8 +31,18 @@ _zsh_autosuggest_bind_widget() { ;; 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_$widget() { + _zsh_autosuggest_widget_$autosuggest_action $prefix$widget $@ + }" + # Create the bound widget - zle -N $widget _zsh_autosuggest_widget_$autosuggest_action + zle -N $widget _zsh_autosuggest_bound_$widget } # Map all configured widgets to the right autosuggest widgets @@ -54,9 +64,11 @@ _zsh_autosuggest_bind_widgets() { done } -# Given the name of a widget, invoke the original we saved, if it exists +# Given the name of an original widget and args, invoke it, if it exists _zsh_autosuggest_invoke_original_widget() { - local original_widget_name="$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX$WIDGET" + local original_widget_name=$1 + + shift if [ $widgets[$original_widget_name] ]; then zle $original_widget_name -- $@ diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index f5b0f6f..fc84ff8 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -114,7 +114,7 @@ _zsh_autosuggest_bind_widget() { # Save a reference to the original widget case $widgets[$widget] in # Already bound - user:_zsh_autosuggest_(widget|orig)_*);; + user:_zsh_autosuggest_(bound|orig)_*);; # User-defined widget user:*) @@ -133,8 +133,18 @@ _zsh_autosuggest_bind_widget() { ;; 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_$widget() { + _zsh_autosuggest_widget_$autosuggest_action $prefix$widget $@ + }" + # Create the bound widget - zle -N $widget _zsh_autosuggest_widget_$autosuggest_action + zle -N $widget _zsh_autosuggest_bound_$widget } # Map all configured widgets to the right autosuggest widgets @@ -156,9 +166,11 @@ _zsh_autosuggest_bind_widgets() { done } -# Given the name of a widget, invoke the original we saved, if it exists +# Given the name of an original widget and args, invoke it, if it exists _zsh_autosuggest_invoke_original_widget() { - local original_widget_name="$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX$WIDGET" + local original_widget_name=$1 + + shift if [ $widgets[$original_widget_name] ]; then zle $original_widget_name -- $@