comparison .zfun/zsh-autosuggestions/src/bind.zsh @ 467:e1ce8897030d

zsh: import df6f6f9ff41 of zsh-autosuggestions
author Augie Fackler <raf@durin42.com>
date Mon, 03 Dec 2018 22:37:29 -0500
parents
children
comparison
equal deleted inserted replaced
466:f248cf012d9a 467:e1ce8897030d
1
2 #--------------------------------------------------------------------#
3 # Widget Helpers #
4 #--------------------------------------------------------------------#
5
6 _zsh_autosuggest_incr_bind_count() {
7 if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then
8 ((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]++))
9 else
10 _ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=1
11 fi
12
13 typeset -gi bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]
14 }
15
16 _zsh_autosuggest_get_bind_count() {
17 if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then
18 typeset -gi bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]
19 else
20 typeset -gi bind_count=0
21 fi
22 }
23
24 # Bind a single widget to an autosuggest widget, saving a reference to the original widget
25 _zsh_autosuggest_bind_widget() {
26 typeset -gA _ZSH_AUTOSUGGEST_BIND_COUNTS
27
28 local widget=$1
29 local autosuggest_action=$2
30 local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
31
32 local -i bind_count
33
34 # Save a reference to the original widget
35 case $widgets[$widget] in
36 # Already bound
37 user:_zsh_autosuggest_(bound|orig)_*);;
38
39 # User-defined widget
40 user:*)
41 _zsh_autosuggest_incr_bind_count $widget
42 zle -N $prefix${bind_count}-$widget ${widgets[$widget]#*:}
43 ;;
44
45 # Built-in widget
46 builtin)
47 _zsh_autosuggest_incr_bind_count $widget
48 eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
49 zle -N $prefix${bind_count}-$widget _zsh_autosuggest_orig_$widget
50 ;;
51
52 # Completion widget
53 completion:*)
54 _zsh_autosuggest_incr_bind_count $widget
55 eval "zle -C $prefix${bind_count}-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
56 ;;
57 esac
58
59 _zsh_autosuggest_get_bind_count $widget
60
61 # Pass the original widget's name explicitly into the autosuggest
62 # function. Use this passed in widget name to call the original
63 # widget instead of relying on the $WIDGET variable being set
64 # correctly. $WIDGET cannot be trusted because other plugins call
65 # zle without the `-w` flag (e.g. `zle self-insert` instead of
66 # `zle self-insert -w`).
67 eval "_zsh_autosuggest_bound_${bind_count}_${(q)widget}() {
68 _zsh_autosuggest_widget_$autosuggest_action $prefix$bind_count-${(q)widget} \$@
69 }"
70
71 # Create the bound widget
72 zle -N -- $widget _zsh_autosuggest_bound_${bind_count}_$widget
73 }
74
75 # Map all configured widgets to the right autosuggest widgets
76 _zsh_autosuggest_bind_widgets() {
77 emulate -L zsh
78
79 local widget
80 local ignore_widgets
81
82 ignore_widgets=(
83 .\*
84 _\*
85 zle-\*
86 autosuggest-\*
87 $ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
88 $ZSH_AUTOSUGGEST_IGNORE_WIDGETS
89 )
90
91 # Find every widget we might want to bind and bind it appropriately
92 for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
93 if [[ -n ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]]; then
94 _zsh_autosuggest_bind_widget $widget clear
95 elif [[ -n ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]]; then
96 _zsh_autosuggest_bind_widget $widget accept
97 elif [[ -n ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]]; then
98 _zsh_autosuggest_bind_widget $widget execute
99 elif [[ -n ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]]; then
100 _zsh_autosuggest_bind_widget $widget partial_accept
101 else
102 # Assume any unspecified widget might modify the buffer
103 _zsh_autosuggest_bind_widget $widget modify
104 fi
105 done
106 }
107
108 # Given the name of an original widget and args, invoke it, if it exists
109 _zsh_autosuggest_invoke_original_widget() {
110 # Do nothing unless called with at least one arg
111 (( $# )) || return 0
112
113 local original_widget_name="$1"
114
115 shift
116
117 if (( ${+widgets[$original_widget_name]} )); then
118 zle $original_widget_name -- $@
119 fi
120 }