zsh-workers
 help / color / mirror / code / Atom feed
From: Christoffer Lundell <christofferlundell@protonmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: [PATCH] Enable linewise edit-command when in visual-line
Date: Wed, 23 Aug 2023 09:06:00 +0000	[thread overview]
Message-ID: <XyFbtVQZPqS2ohsFK-alrnKKiX2lVU7t_wvfzgTHnSlUABXLKvdp7Bi4_97ZeDH0aUx7v_nd5MZwnTwKW6WwpimMnxRxsQDghQmZGRtjKfU=@protonmail.com> (raw)
In-Reply-To: <CAH+w=7aCqvj0p=9JBXwRiZNDeJdW0yrbvErciUqCATs0bMfUqA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3163 bytes --]

> > + left=${(SB)${BUFFER[1,mark_left]}%$'\n'}
> > + (( left != 1 )) && (( left++ ))
>
> This I follow. If the (SB) is replaced with ...
>
> local nl=$'\n'
> left=${${BUFFER[1,mark_left]}[(I)$nl]}

Thank you, this is much better.

>
> > + offset_right=${(SB)${BUFFER[mark_right+1,-1]}#$'\n'}
>
>
> This I'm not sure of -- why mark_right+1 ? In this direction that
> seems to mean that if the mark is exactly on the newline, you're
> extending to the following newline. Is that how it's meant to work?

This is because CURSOR and MARK are 0-indexed, according to zshzle(1). And so a
cursor at the first position in a line will have the same index as a newline of
the previous line inside an array subscript. Unless I am missing something I
would insist on the following.

right=${${BUFFER[MARK+1,-1]}[(i)$nl]}
(( right += MARK ))

which also means we later on no longer need to immediately predecrement $right,
since the matched index will be 1 lower.


There is also the issue about supporting cursor position in emacs, since now
$lbuffer is used to split the selected lines and figure out the cursor line and
column. But in both the original code and in your patch improvement, $lbuffer
will contain text from the beginning of $BUFFER regardless of where the visual
selection actually starts. This works fine in vim since they only use byte
offsets, but I would recommend changing it to the following in _both_ cases.

lbuffer=$lbuffer[++left,-1]

This leaves me with the attached suggestion (with the caveat that multiple
empty trailing lines will be trimmed to one newline)

Thank you a bunch for taking the time to help me, I understand this is not the
most exciting feature :)
Christoffer

---
 Functions/Zle/edit-command-line | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line
index 5f7ea321f..d4b405eaf 100644
--- a/Functions/Zle/edit-command-line
+++ b/Functions/Zle/edit-command-line
@@ -11,15 +11,30 @@ 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 (( REGION_ACTIVE == 1 )); then
   if (( CURSOR < MARK )); then
     left=$CURSOR right=$MARK
-    lbuffer=
   else
     left=$MARK right=$CURSOR
-    lbuffer[right-left,-1]=
   fi
-  (( left++ ))
+  lbuffer=$lbuffer[++left,-1]
+  buffer=$BUFFER[left,++right]
+elif (( REGION_ACTIVE == 2 )); then
+  local nl=$'\n'
+  if (( CURSOR < MARK )); then
+    left=${${BUFFER[1,CURSOR]}[(I)$nl]}
+    right=${${BUFFER[MARK+1,-1]}[(i)$nl]}
+    (( right += MARK ))
+  else
+    left=${${BUFFER[1,MARK]}[(I)$nl]}
+    right=${${BUFFER[CURSOR+1,-1]}[(i)$nl]}
+    (( right += CURSOR ))
+  fi
+  lbuffer=$lbuffer[++left,-1]
+  if [[ $BUFFER[right] = $nl ]]; then
+    # Keep the newline because "$(<$1)" below trims it
+    (( --right ))
+  fi
   buffer=$BUFFER[left,right]
 elif (( ! ZLE_RECURSIVE )); then
   prebuffer=$PREBUFFER
-- 
2.41.0

[-- Attachment #2: edcomln_v3.txt --]
[-- Type: text/plain, Size: 1260 bytes --]

diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line
index 5f7ea321f..d4b405eaf 100644
--- a/Functions/Zle/edit-command-line
+++ b/Functions/Zle/edit-command-line
@@ -11,15 +11,30 @@ 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 (( REGION_ACTIVE == 1 )); then
   if (( CURSOR < MARK )); then
     left=$CURSOR right=$MARK
-    lbuffer=
   else
     left=$MARK right=$CURSOR
-    lbuffer[right-left,-1]=
   fi
-  (( left++ ))
+  lbuffer=$lbuffer[++left,-1]
+  buffer=$BUFFER[left,++right]
+elif (( REGION_ACTIVE == 2 )); then
+  local nl=$'\n'
+  if (( CURSOR < MARK )); then
+    left=${${BUFFER[1,CURSOR]}[(I)$nl]}
+    right=${${BUFFER[MARK+1,-1]}[(i)$nl]}
+    (( right += MARK ))
+  else
+    left=${${BUFFER[1,MARK]}[(I)$nl]}
+    right=${${BUFFER[CURSOR+1,-1]}[(i)$nl]}
+    (( right += CURSOR ))
+  fi
+  lbuffer=$lbuffer[++left,-1]
+  if [[ $BUFFER[right] = $nl ]]; then
+    # Keep the newline because "$(<$1)" below trims it
+    (( --right ))
+  fi
   buffer=$BUFFER[left,right]
 elif (( ! ZLE_RECURSIVE )); then
   prebuffer=$PREBUFFER
-- 
2.41.0

  reply	other threads:[~2023-08-23  9:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-21 14:13 Christoffer Lundell
2023-08-23  3:27 ` Bart Schaefer
2023-08-23  9:06   ` Christoffer Lundell [this message]
2023-08-23 17:28     ` Bart Schaefer
2023-08-27 11:38       ` Christoffer Lundell
2023-08-27 20:24         ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='XyFbtVQZPqS2ohsFK-alrnKKiX2lVU7t_wvfzgTHnSlUABXLKvdp7Bi4_97ZeDH0aUx7v_nd5MZwnTwKW6WwpimMnxRxsQDghQmZGRtjKfU=@protonmail.com' \
    --to=christofferlundell@protonmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).