1
0
mirror of git://git.code.sf.net/p/zsh/code synced 2024-11-18 21:14:11 +01:00
zsh/Functions/Misc/zed

100 lines
2.1 KiB
Plaintext
Raw Normal View History

2001-04-02 15:04:04 +02:00
#
# zed
#
# No other shell could do this.
# Edit small files with the command line editor.
# Use ^X^W to save, ^C to abort.
# Option -f: edit shell functions. (Also if called as fned.)
2004-06-23 18:14:24 +02:00
local var opt zed_file_name
2001-04-02 15:04:04 +02:00
# We do not want timeout while we are editing a file
2004-06-23 18:14:24 +02:00
integer TMOUT=0 okargs=1 fun bind
while getopts "fb" opt; do
case $opt in
(f)
fun=1
;;
(b)
bind=1
;;
esac
done
shift $(( OPTIND - 1 ))
2001-04-02 15:04:04 +02:00
2004-06-23 18:14:24 +02:00
[[ $0 = fned ]] && fun=1
(( bind )) && okargs=0
2001-04-02 15:04:04 +02:00
2004-06-23 18:14:24 +02:00
if (( $# != okargs )); then
echo 'Usage:
zed filename
zed -f function
zed -b'
return 1
fi
2001-04-02 15:04:04 +02:00
local curcontext=zed:::
zstyle -m ":completion:zed:*" insert-tab '*' ||
zstyle ":completion:zed:*" insert-tab yes
2004-06-23 18:14:24 +02:00
if (( bind )) || ! bindkey -M zed >&/dev/null; then
# Make the zed keymap a copy of the current main.
bindkey -N zed main
2004-06-23 18:14:24 +02:00
# Save the current main. In zle widgets called from
# zed we may want to set this temporally.
bindkey -A main zed-normal-keymap
# Assign some default keys.
# Depending on your stty's, you may be able to use ^J as accept-line, else:
# The following isn't useful if we are copying viins, but that's
# a nicety.
bindkey -M zed '^x^w' accept-line
bindkey -M zed '^M' self-insert-unmeta
2004-06-23 18:14:24 +02:00
# Make zed-set-file-name available.
# Assume it's in fpath; there's no error at this point if it isn't
autoload -U zed-set-file-name
zle -N zed-set-file-name
fi
2004-06-23 18:14:24 +02:00
if (( bind )) || ! bindkey -M zed-vicmd >&/dev/null; then
bindkey -N zed-vicmd vicmd
bindkey -M zed-vicmd "ZZ" accept-line
fi
2001-04-02 15:04:04 +02:00
2004-06-23 18:14:24 +02:00
(( bind )) && return 0
2001-04-02 15:04:04 +02:00
# don't mangle !'s
setopt localoptions nobanghist
if ((fun)) then
var="$(functions $1)"
# If function is undefined but autoloadable, load it
if [[ $var = *\#\ undefined* ]] then
var="$(autoload +X $1; functions $1)"
2001-04-02 15:04:04 +02:00
elif [[ -z $var ]] then
var="$1() {
}"
fi
vared -M zed -m zed-vicmd var && eval function "$var"
2001-04-02 15:04:04 +02:00
else
2004-06-23 18:14:24 +02:00
zed_file_name=$1
2001-04-02 15:04:04 +02:00
[[ -f $1 ]] && var="$(<$1)"
while vared -M zed -m zed-vicmd var
2001-04-02 15:04:04 +02:00
do
2004-06-23 18:14:24 +02:00
{
print -r -- "$var" >| $zed_file_name
} always {
(( TRY_BLOCK_ERROR = 0 ))
} && break
2001-04-02 15:04:04 +02:00
echo -n -e '\a'
done
fi
return 0
# End of zed