* PATCH: edit-command-line: when possible, set $BUFFER directly
@ 2020-08-08 9:51 Mikael Magnusson
2020-08-08 10:17 ` PATCH: edit-command-line: add editor style Mikael Magnusson
2020-08-08 10:18 ` PATCH: edit-command-line: restrict editing to region if it is active Mikael Magnusson
0 siblings, 2 replies; 3+ messages in thread
From: Mikael Magnusson @ 2020-08-08 9:51 UTC (permalink / raw)
To: zsh-workers
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.
---
See also: 23588 and 47295
Functions/Zle/edit-command-line | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line
index 991775ea50..1103ca556c 100644
--- a/Functions/Zle/edit-command-line
+++ b/Functions/Zle/edit-command-line
@@ -7,6 +7,11 @@
# 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
@@ -31,7 +36,21 @@ 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")
-
-zle send-break # Force reload from the buffer stack
+ # 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")
--
2.15.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* PATCH: edit-command-line: add editor style
2020-08-08 9:51 PATCH: edit-command-line: when possible, set $BUFFER directly Mikael Magnusson
@ 2020-08-08 10:17 ` Mikael Magnusson
2020-08-08 10:18 ` PATCH: edit-command-line: restrict editing to region if it is active Mikael Magnusson
1 sibling, 0 replies; 3+ messages in thread
From: Mikael Magnusson @ 2020-08-08 10:17 UTC (permalink / raw)
To: zsh-workers
---
Completion/Zsh/Command/_zstyle | 1 +
Doc/Zsh/contrib.yo | 6 ++++++
Functions/Zle/edit-command-line | 8 ++++++--
3 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/Completion/Zsh/Command/_zstyle b/Completion/Zsh/Command/_zstyle
index e9a5d800cd..bb871762ed 100644
--- a/Completion/Zsh/Command/_zstyle
+++ b/Completion/Zsh/Command/_zstyle
@@ -143,6 +143,7 @@ styles=(
cursor e:
edit-buffer e:bool
edit-previous e:bool
+ editor e:
insert-kept e:
leave-cursor e:bool
match e:
diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 66e6bdc1e4..bab1e30231 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -2472,6 +2472,12 @@ item(tt(edit-command-line))(
Edit the command line using your visual editor, as in tt(ksh).
example(bindkey -M vicmd v edit-command-line)
+
+The editor to be used can also be specified using the tt(editor) style in
+the context of the widget. It is specified as an array of command and
+arguments:
+
+example(zstyle :zle:edit-command-line editor gvim -f)
)
tindex(expand-absolute-path)
item(tt(expand-absolute-path))(
diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line
index 1103ca556c..8aaeb738e5 100644
--- a/Functions/Zle/edit-command-line
+++ b/Functions/Zle/edit-command-line
@@ -22,8 +22,12 @@ fi
(( $+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 ))
"${(@)editor}" -c "normal! ${byteoffset}go" -- $1;;
--
2.15.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* PATCH: edit-command-line: restrict editing to region if it is active
2020-08-08 9:51 PATCH: edit-command-line: when possible, set $BUFFER directly Mikael Magnusson
2020-08-08 10:17 ` PATCH: edit-command-line: add editor style Mikael Magnusson
@ 2020-08-08 10:18 ` Mikael Magnusson
1 sibling, 0 replies; 3+ messages in thread
From: Mikael Magnusson @ 2020-08-08 10:18 UTC (permalink / raw)
To: zsh-workers
---
Functions/Zle/edit-command-line | 36 +++++++++++++++++++++++++++++-------
1 file changed, 29 insertions(+), 7 deletions(-)
diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line
index 8aaeb738e5..3781244b27 100644
--- a/Functions/Zle/edit-command-line
+++ b/Functions/Zle/edit-command-line
@@ -7,9 +7,20 @@
# 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
+local left right prebuffer buffer=$BUFFER lbuffer=$LBUFFER
+# 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
@@ -29,10 +40,10 @@ fi
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
@@ -48,13 +59,24 @@ fi
# 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 [[ $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)"
else
print -Rz - "$(<$1)"
zle send-break
fi
-} =(<<<"$prebuffer$BUFFER")
+} =(<<<"$prebuffer$buffer")
--
2.15.1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-08-08 10:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-08 9:51 PATCH: edit-command-line: when possible, set $BUFFER directly Mikael Magnusson
2020-08-08 10:17 ` PATCH: edit-command-line: add editor style Mikael Magnusson
2020-08-08 10:18 ` PATCH: edit-command-line: restrict editing to region if it is active Mikael Magnusson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).