From 4ec160f63168fc385e9021ba376162de4f050234 Mon Sep 17 00:00:00 2001 From: bptato Date: Wed, 11 Jan 2023 16:27:39 +0100 Subject: buffer/container: fix cursor overwriting double-width chars In some terminals, placing the cursor on the second cell of a double-width character deletes half of said character, so let's not do that. --- src/buffer/container.nim | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/buffer/container.nim b/src/buffer/container.nim index 3315b79c..38b58011 100644 --- a/src/buffer/container.nim +++ b/src/buffer/container.nim @@ -183,8 +183,29 @@ func cursorLastX(container: Container): int = w += r.twidth(w) return max(w - 1, 0) +# Last cell for tab, first cell for everything else (e.g. double width.) +# This is needed because moving the cursor to the 2nd cell of a double +# width character clears it on some terminals. +func cursorDispX(container: Container): int = + if container.numLines == 0: return 0 + let line = container.currentLine + if line.len == 0: return 0 + var w = 0 + var pw = 0 + var i = 0 + var r: Rune + let cc = container.cursorx + while i < line.len and w <= cc: + fastRuneAt(line, i, r) + pw = w + w += r.twidth(w) + if r == Rune('\t'): + return max(w - 1, 0) + else: + return pw + func acursorx*(container: Container): int = - max(0, container.cursorLastX() - container.fromx) + max(0, container.cursorDispX() - container.fromx) func acursory*(container: Container): int = container.cursory - container.fromy -- cgit 1.4.1-2-gfad0