1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-01-01 17:24:50 +01:00

47307: edit-command-line: restrict editing to region if it is active

This commit is contained in:
Mikael Magnusson 2020-08-05 17:14:13 +02:00
parent 841188439b
commit 0bc559d091

View file

@ -7,9 +7,20 @@
# except that it will handle multi-line buffers properly. # except that it will handle multi-line buffers properly.
emulate -L zsh emulate -L zsh
local prebuffer local left right prebuffer buffer=$BUFFER lbuffer=$LBUFFER
# see below comment for why this is needed # set up parameters depending on which context we are called from,
if (( ! ZLE_RECURSIVE )); then # see below comment for more details
if (( REGION_ACTIVE )); then
if (( CURSOR < MARK )); then
left=$CURSOR right=$MARK
lbuffer=
else
left=$MARK right=$CURSOR
lbuffer[right-left,-1]=
fi
(( left++ ))
buffer=$BUFFER[left,right]
elif (( ! ZLE_RECURSIVE )); then
prebuffer=$PREBUFFER prebuffer=$PREBUFFER
fi fi
@ -29,10 +40,10 @@ fi
fi fi
case $editor in case $editor in
(*vim*) (*vim*)
integer byteoffset=$(( $#PREBUFFER + $#LBUFFER + 1 )) integer byteoffset=$(( $#prebuffer + $#lbuffer + 1 ))
"${(@)editor}" -c "normal! ${byteoffset}go" -- $1;; "${(@)editor}" -c "normal! ${byteoffset}go" -- $1;;
(*emacs*) (*emacs*)
local lines=( "${(@f):-"$PREBUFFER$LBUFFER"}" ) local lines=( "${(@f):-"$prebuffer$lbuffer"}" )
"${(@)editor}" +${#lines}:$((${#lines[-1]} + 1)) $1;; "${(@)editor}" +${#lines}:$((${#lines[-1]} + 1)) $1;;
(*) "${(@)editor}" $1;; (*) "${(@)editor}" $1;;
esac esac
@ -48,13 +59,24 @@ fi
# the following point) # the following point)
# - when we are at PS2 (CONTEXT == cont && ! ZLE_RECURSIVE) we do want the # - when we are at PS2 (CONTEXT == cont && ! ZLE_RECURSIVE) we do want the
# break or otherwise the text from PREBUFFER will be inserted twice # break or otherwise the text from PREBUFFER will be inserted twice
# - when the region is active, we only want to change the parts of BUFFER
# covered by the region, and any PREBUFFER stays as PREBUFFER
# - in all other cases (that I can think of) we also just want to set # - in all other cases (that I can think of) we also just want to set
# $BUFFER directly. # $BUFFER directly.
if [[ $CONTEXT != cont ]] || (( ZLE_RECURSIVE )); then if (( REGION_ACTIVE )); then
# adjust the length of the region to the length of the edited text
local prelen=$#BUFFER
BUFFER[left,right]="$(<$1)"
if (( MARK > CURSOR )); then
(( MARK += $#BUFFER - prelen ))
else
(( CURSOR += $#BUFFER - prelen ))
fi
elif [[ $CONTEXT != cont ]] || (( ZLE_RECURSIVE )); then
BUFFER="$(<$1)" BUFFER="$(<$1)"
else else
print -Rz - "$(<$1)" print -Rz - "$(<$1)"
zle send-break zle send-break
fi fi
} =(<<<"$prebuffer$BUFFER") } =(<<<"$prebuffer$buffer")