1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| | # Lets you implement widgets that can execute arbitrary commands without losing
# the current command line, in a fashion similar to the 'run-help' and
# 'which-command' widgets. See the manual for more details.
zmodload -F zsh/zutil b:zparseopts
local -A opts
zparseopts -D -A opts - e s v:
local -a err
zle ||
err+=( "${0}: can only be called from zle widgets" )
(( # )) ||
err+=( "${0}: not enough arguments" )
if [[ -n $err ]]; then
print -lu2 -- $err[@] \
$'Usage: execute-commands [ <options> ] [--] <command> ...
Execute commands from zle widget, without mangling prompt or buffer.
Options:
-e echo commands before executing
-s save commands to history
-v <name> store last command\'s exit status in param <name>'
return 1
fi
case $CONTEXT in
( cont | start )
print -rz -- "$PREBUFFER$BUFFER" # Push all lines to buffer stack.
[[ -v opts[-e] ]] &&
BUFFER="${(F)@}" # Echo commands to buffer.
[[ -v opts[-s] ]] &&
print -rS -- "${(F)@}" # Save commands to history.
eval "${(F)@}" # Execute commands.
local -i ret=$?
[[ -v opts[-v] ]] &&
eval "$opts[-v]=$ret" # Store exit status.
;;
( * )
return 75 # EX_TEMPFAIL; see `man 3 sysexits`.
;;
esac
zle .send-break
|