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