mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-09-11 13:01:28 +02:00
This avoids the send-break which is both visually unappealing and might break some use cases where the user wishes to wrap edit-command-line in another widget.
56 lines
1.9 KiB
Text
56 lines
1.9 KiB
Text
# Edit the command line using your usual editor.
|
|
# Binding this to '!' in the vi command mode map,
|
|
# autoload -Uz edit-command-line
|
|
# zle -N edit-command-line
|
|
# bindkey -M vicmd '!' edit-command-line
|
|
# will give ksh-like behaviour for that key,
|
|
# except that it will handle multi-line buffers properly.
|
|
|
|
emulate -L zsh
|
|
local prebuffer
|
|
# see below comment for why this is needed
|
|
if (( ! ZLE_RECURSIVE )); then
|
|
prebuffer=$PREBUFFER
|
|
fi
|
|
|
|
() {
|
|
exec </dev/tty
|
|
|
|
# Compute the cursor's position in bytes, not characters.
|
|
setopt localoptions nomultibyte noksharrays
|
|
|
|
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[2]
|
|
|
|
# Open the editor, placing the cursor at the right place if we know how.
|
|
local editor=( "${(@Q)${(z)${VISUAL:-${EDITOR:-vi}}}}" )
|
|
case $editor in
|
|
(*vim*)
|
|
integer byteoffset=$(( $#PREBUFFER + $#LBUFFER + 1 ))
|
|
"${(@)editor}" -c "normal! ${byteoffset}go" -- $1;;
|
|
(*emacs*)
|
|
local lines=( "${(@f):-"$PREBUFFER$LBUFFER"}" )
|
|
"${(@)editor}" +${#lines}:$((${#lines[-1]} + 1)) $1;;
|
|
(*) "${(@)editor}" $1;;
|
|
esac
|
|
|
|
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[1]
|
|
|
|
# Replace the buffer with the editor output.
|
|
# avoid drawing a new prompt when we can:
|
|
# - in recursive-edit, the send-break will just cancel the recursive-edit
|
|
# rather than reload the line from print -z so in that case we want to
|
|
# just set $BUFFER (unfortunately, recursive-edit doesn't reset CONTEXT
|
|
# or PREBUFFER so we have to explicitly handle this case, which overrides
|
|
# the following point)
|
|
# - when we are at PS2 (CONTEXT == cont && ! ZLE_RECURSIVE) we do want the
|
|
# break or otherwise the text from PREBUFFER will be inserted twice
|
|
# - in all other cases (that I can think of) we also just want to set
|
|
# $BUFFER directly.
|
|
if [[ $CONTEXT != cont ]] || (( ZLE_RECURSIVE )); then
|
|
BUFFER="$(<$1)"
|
|
else
|
|
print -Rz - "$(<$1)"
|
|
zle send-break
|
|
fi
|
|
|
|
} =(<<<"$prebuffer$BUFFER")
|