Index | Thread | Search

From:
Walter Alejandro Iglesias <wai@roquesor.com>
Subject:
Re: vi(1) count paste bug. New diff.
To:
tech@openbsd.org
Date:
Mon, 11 Aug 2025 09:36:45 +0200

Download raw body.

Thread
I'd like to add.

In a discussion I had in private with Zhihao Yuan, nvi2 github
maintainer, he didn't understand why, when my original report was about
a bug in count+paste vi(1) command, I insisted on analyzing the cursor
movement behavior at the same time.

This made me realize I didn't explain myself well from the start.  So,
let me elaborate a bit more.

The counter prefix is a later hack (v_put() in vi/v_put.c), implemented
separately from the original put() (common/put.c) function, Loading the
string to the buffer and moving the cursor happen in put() again and
again on each iteration called from the count loop in v_put().

The following is the line that moves the cursor (only in the cursor-mode
pasting scenario):

  rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0);

  (common/put.c, l. 160)

This line decides on the fly, on each iteration of the loop, depending
on whether the string must be inserted or appended, where to move the
cursor after pasting (again, each occurrence).  Put all graphically,
suppose we want to paste "Hello" two times times with '2p'.  Currently,
this takes to the code these steps (roughly):

  1. Load "Hello" to the buffer.
  2. Decide where to move the cursor.
  3. Paste "Hello" and move the cursor.
  4. Load second occurrence of "Hello" to the buffer.
  2. Decide where to move the cursor.
  6. Paste the second occurrence of "Hello" and move the cursor.

In my last patch, as I explained, the loop with the counter is in the
put() function itself, that means that all occurrences of "Hello" are
loaded into the buffer before the cursor movement is decided.  The '2p'
(or '2P') command is reduced to:

  1. Load "HelloHello" to the buffer.
  2. Decide where to move the cursor.
  3. Paste "HelloHello" and move the cursor.

This comes with an added benefit.  From then on, the "where to move the
cursor after pasting" behaviour can be modified independently (in case
we're willing so).  For this it will be enough to tweak the line that
moves the cursor (the one I quoted above).


-- 
Walter