|
1 ### a generic accept-line wrapper |
|
2 ### distributed under the same terms as zsh itself. |
|
3 # |
|
4 # (Mikael: copied from Frank Terbeck repository |
|
5 # at https://dev.0x50.de/projects/ftzsh/repository) |
|
6 |
|
7 function Accept-Line() { |
|
8 setopt localoptions noksharrays |
|
9 local -a subs |
|
10 local -xi aldone |
|
11 local sub |
|
12 local alcontext=${1:-$alcontext} |
|
13 |
|
14 zstyle -a ":acceptline:${alcontext}" actions subs |
|
15 |
|
16 (( ${#subs} < 1 )) && return 0 |
|
17 |
|
18 (( aldone = 0 )) |
|
19 for sub in ${subs} ; do |
|
20 [[ ${sub} == 'accept-line' ]] && sub='.accept-line' |
|
21 zle ${sub} |
|
22 |
|
23 (( aldone > 0 )) && break |
|
24 done |
|
25 } |
|
26 |
|
27 function Accept-Line-getdefault() { |
|
28 local default_action |
|
29 |
|
30 zstyle -s ":acceptline:${alcontext}" default_action default_action |
|
31 case ${default_action} in |
|
32 ((accept-line|)) |
|
33 REPLY=".accept-line" |
|
34 ;; |
|
35 (*) |
|
36 REPLY=${default_action} |
|
37 ;; |
|
38 esac |
|
39 } |
|
40 |
|
41 function Accept-Line-HandleContext() { |
|
42 local REPLY |
|
43 |
|
44 zle Accept-Line |
|
45 Accept-Line-getdefault |
|
46 default_action="${REPLY}" |
|
47 zstyle -T ":acceptline:${alcontext}" call_default \ |
|
48 && zle ${default_action} |
|
49 } |
|
50 |
|
51 function accept-line() { |
|
52 setopt localoptions noksharrays |
|
53 local -ax cmdline |
|
54 local -x alcontext |
|
55 local buf com fname format msg default_action |
|
56 |
|
57 alcontext='default' |
|
58 buf="${BUFFER}" |
|
59 cmdline=(${(z)BUFFER}) |
|
60 com="${cmdline[1]}" |
|
61 fname="_${com}" |
|
62 |
|
63 Accept-Line 'preprocess' |
|
64 |
|
65 zstyle -t ":acceptline:${alcontext}" rehash \ |
|
66 && [[ -z ${commands[$com]} ]] \ |
|
67 && rehash |
|
68 |
|
69 if [[ -n ${com} ]] \ |
|
70 && [[ -n ${reswords[(r)$com]} ]] \ |
|
71 || [[ -n ${aliases[$com]} ]] \ |
|
72 || [[ -n ${functions[$com]} ]] \ |
|
73 || [[ -n ${builtins[$com]} ]] \ |
|
74 || [[ -n ${commands[$com]} ]] ; then |
|
75 |
|
76 # there is something sensible to execute, just do it. |
|
77 alcontext='normal' |
|
78 Accept-Line-HandleContext |
|
79 |
|
80 return |
|
81 fi |
|
82 |
|
83 if [[ -o correct ]] \ |
|
84 || [[ -o correctall ]] \ |
|
85 && [[ -n ${functions[$fname]} ]] ; then |
|
86 |
|
87 # nothing there to execute but there is a function called |
|
88 # _command_name; a completion widget. Makes no sense to |
|
89 # call it on the commandline, but the correct{,all} options |
|
90 # will ask for it nevertheless, so warn the user. |
|
91 if [[ ${LASTWIDGET} == 'accept-line' ]] ; then |
|
92 # Okay, we warned the user before, he called us again, |
|
93 # so have it his way. |
|
94 alcontext='force' |
|
95 Accept-Line-HandleContext |
|
96 |
|
97 return |
|
98 fi |
|
99 |
|
100 if zstyle -t ":acceptline:${alcontext}" nocompwarn ; then |
|
101 alcontext='normal' |
|
102 Accept-Line-HandleContext |
|
103 else |
|
104 # prepare warning message for the user, configurable via zstyle. |
|
105 zstyle -s ":acceptline:${alcontext}" compwarnfmt msg |
|
106 |
|
107 if [[ -z ${msg} ]] ; then |
|
108 msg="%c will not execute and completion %f exists." |
|
109 fi |
|
110 |
|
111 zformat -f msg "${msg}" "c:${com}" "f:${fname}" |
|
112 |
|
113 zle -M -- "${msg}" |
|
114 fi |
|
115 return |
|
116 elif [[ -n ${buf//[$' \t\n']##/} ]] ; then |
|
117 # If we are here, the commandline contains something that is not |
|
118 # executable, which is neither subject to _command_name correction |
|
119 # and is not empty. might be a variable assignment |
|
120 alcontext='misc' |
|
121 Accept-Line-HandleContext |
|
122 |
|
123 return |
|
124 fi |
|
125 |
|
126 # If we got this far, the commandline only contains whitespace, or is empty. |
|
127 alcontext='empty' |
|
128 Accept-Line-HandleContext |
|
129 } |
|
130 |
|
131 zle -N accept-line |
|
132 zle -N Accept-Line |
|
133 zle -N Accept-Line-HandleContext |