summaryrefslogtreecommitdiff
path: root/Functions/Zle
diff options
context:
space:
mode:
Diffstat (limited to 'Functions/Zle')
-rw-r--r--Functions/Zle/edit-command-line60
-rw-r--r--Functions/Zle/zed-set-file-name27
2 files changed, 75 insertions, 12 deletions
diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line
index 991775ea5..5f7ea321f 100644
--- a/Functions/Zle/edit-command-line
+++ b/Functions/Zle/edit-command-line
@@ -7,6 +7,23 @@
# except that it will handle multi-line buffers properly.
emulate -L zsh
+local left right prebuffer buffer=$BUFFER lbuffer=$LBUFFER
+local TMPSUFFIX=.zsh
+# set up parameters depending on which context we are called from,
+# 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
+fi
() {
exec </dev/tty
@@ -17,13 +34,17 @@ emulate -L zsh
(( $+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
+ local -a editor
+ zstyle -a :zle:$WIDGET editor editor
+ if (( ! $#editor )); then
+ editor=( "${(@Q)${(z)${VISUAL:-${EDITOR:-vi}}}}" )
+ fi
+ case $editor in
(*vim*)
- integer byteoffset=$(( $#PREBUFFER + $#LBUFFER + 1 ))
+ integer byteoffset=$(( $#prebuffer + $#lbuffer + 1 ))
"${(@)editor}" -c "normal! ${byteoffset}go" -- $1;;
(*emacs*)
- local lines=( "${(@f):-"$PREBUFFER$LBUFFER"}" )
+ local lines=( "${(@f):-"$prebuffer$lbuffer"}" )
"${(@)editor}" +${#lines}:$((${#lines[-1]} + 1)) $1;;
(*) "${(@)editor}" $1;;
esac
@@ -31,7 +52,32 @@ emulate -L zsh
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[1]
# Replace the buffer with the editor output.
- print -Rz - "$(<$1)"
-} =(<<<"$PREBUFFER$BUFFER")
+ # 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
+ # - 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
+ # $BUFFER directly.
+ 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)"
+ else
+ print -Rz - "$(<$1)"
+ zle send-break
+ fi
-zle send-break # Force reload from the buffer stack
+} =(<<<"$prebuffer$buffer")
diff --git a/Functions/Zle/zed-set-file-name b/Functions/Zle/zed-set-file-name
index da3421e71..745610660 100644
--- a/Functions/Zle/zed-set-file-name
+++ b/Functions/Zle/zed-set-file-name
@@ -2,8 +2,25 @@ emulate -L zsh
autoload -Uz read-from-minibuffer
-zle -K zed-normal-keymap
-
-local REPLY
-read-from-minibuffer "File name: "
-zed_file_name=$REPLY
+case $curcontext in
+ (zed:::)
+ local curcontext=zed-set-file-name:::
+ # The call to vared from zed does the equivalent of
+ # bindkey -A zed main
+ # which confuses read-from-minibuffer. Fix it.
+ bindkey -A zed-normal-keymap main;;
+ (zed-set-file-name:::)
+ zle -M "zed-set-file-name: may not be called recursively"
+ return 1;;
+ (*)
+ zle -M "zed-set-file-name: not called from within zed"
+ return 1;;
+esac
+{
+ local REPLY
+ read-from-minibuffer "File name: "
+ zed_file_name=$REPLY
+} always {
+ # Re-install the zed keymap in the way vared should have all along
+ zle -K zed
+}