CVSROOT:        /cvs
Module name:    src
Changes by:     schwa...@cvs.openbsd.org        2025/07/20 15:24:07

Modified files:
        bin/ksh        : vi.c 

Log message:
Completely rewrite rewindow(), the function that chooses a new starting
byte for the editing window when the cursor leaves the editing window
to the right or to the left side.

The main reason for the rewrite is that UTF-8 handling was broken and would
often start the editing window on a UTF-8 continuation byte, sending UTF-8
syntax errors to the terminal.  Besides, the function was unnecessarily
complicated (iterating twice) and inefficient (iterating the whole buffer
from the beginning even when the cursor was far out to the right) without
even being exact (picking a bit position that brings the cursor near the
middle of the window, but nor always as close to the middle as possible).
The old algorithm was rougly O(L + 2*W), where W is half of the window
width, and L is the amount of text left of the window.

Instead, pick an algorithm that is simpler, more robust, and faster
than before, but avoid complication by still being content with an
approximate solution that can be implemented with a simple O(W)
backwards linear search.  Do not go overboard with an O(W log W)
binary search or an O(W^2) linear search, either of which would
be required to find the optimal position.

OK millert@

Reply via email to