v0.8.0: add cursor position recovery — if text exists but pos is 0, place cursor at end
position-cursor now also clamps cursor-line to valid range and recovers from cursor-pos resets by moving to end of text.
This commit is contained in:
@@ -343,11 +343,7 @@ and current sidebar mode (:auto/:visible/:hidden)."
|
|||||||
(setf (st :dirty) (list nil nil nil))))
|
(setf (st :dirty) (list nil nil nil))))
|
||||||
|
|
||||||
(defun position-cursor (fb w h)
|
(defun position-cursor (fb w h)
|
||||||
"Draw cursor at the input insertion point using reverse video (Emacs-style).
|
"Draw cursor at the input insertion point using reverse video (Emacs-style)."
|
||||||
|
|
||||||
The character under the cursor is redrawn with foreground and background
|
|
||||||
swapped. If the cursor is past the end of the input string, a reversed
|
|
||||||
space is drawn."
|
|
||||||
(let* ((sw (if (sidebar-visible-p w) (or (st :sidebar-width) 42) 0))
|
(let* ((sw (if (sidebar-visible-p w) (or (st :sidebar-width) 42) 0))
|
||||||
(cw (- w sw))
|
(cw (- w sw))
|
||||||
(hpad 2)
|
(hpad 2)
|
||||||
@@ -358,12 +354,18 @@ and current sidebar mode (:auto/:visible/:hidden)."
|
|||||||
(lines (cl-tty.box:word-wrap text prompt-w))
|
(lines (cl-tty.box:word-wrap text prompt-w))
|
||||||
(n-lines (max 1 (length lines)))
|
(n-lines (max 1 (length lines)))
|
||||||
(cl 0) (cc 0) (accum 0))
|
(cl 0) (cc 0) (accum 0))
|
||||||
;; Find which wrapped line the cursor is on
|
;; Find which wrapped line the cursor falls on
|
||||||
(dotimes (i n-lines)
|
(dotimes (i n-lines)
|
||||||
(let ((len (length (nth i lines))))
|
(let ((len (length (nth i lines))))
|
||||||
(when (and (>= pos accum) (<= pos (+ accum len)))
|
(when (and (>= pos accum) (or (<= pos (+ accum len))
|
||||||
|
(= i (1- n-lines))))
|
||||||
(setf cl i cc (- pos accum)))
|
(setf cl i cc (- pos accum)))
|
||||||
(incf accum (1+ len))))
|
(incf accum (1+ len))))
|
||||||
|
;; If text exists but pos is 0, move cursor to end (recovery for pos reset)
|
||||||
|
(when (and (plusp text-len) (zerop pos))
|
||||||
|
(setf pos text-len
|
||||||
|
cl (1- n-lines)
|
||||||
|
cc (length (car (last lines)))))
|
||||||
(let* ((panel-rows (max 4 (+ n-lines 2)))
|
(let* ((panel-rows (max 4 (+ n-lines 2)))
|
||||||
(panel-top (- h 4 panel-rows -1))
|
(panel-top (- h 4 panel-rows -1))
|
||||||
(cx (+ hpad 2 cc))
|
(cx (+ hpad 2 cc))
|
||||||
|
|||||||
Reference in New Issue
Block a user