Skip to content

Fix cursor position for chars where char-width disagrees with terminal#90

Merged
dakra merged 1 commit intomainfrom
fix/cursor-position-char-width
Apr 12, 2026
Merged

Fix cursor position for chars where char-width disagrees with terminal#90
dakra merged 1 commit intomainfrom
fix/cursor-position-char-width

Conversation

@dakra
Copy link
Copy Markdown
Owner

@dakra dakra commented Apr 12, 2026

Summary

  • Fix cursor landing on the wrong character on lines containing box-drawing glyphs (e.g. ) or other characters where Emacs' char-width disagrees with the terminal's column width
  • Replace move-to-column (which relies on char-width) with a new positionCursorByCell helper that iterates the cursor row's terminal cells to compute the exact Emacs character offset, then uses goto-char
  • The new approach is also faster since it avoids Elisp display-width scanning (~40% faster in benchmarks with non-ASCII content)

Fixes #86

Test plan

  • zig build check compiles cleanly
  • All 56 Elisp tests pass
  • Benchmarked before/after: no regression, actually faster across all scenarios
  • Needs verification on Linux pgtk with CJK locale (the reporter's environment)

…erminal

Emacs' move-to-column uses char-width for column counting, which can
disagree with libghostty's wcwidth-based columns for certain characters
(e.g. box-drawing glyphs like ┃ on CJK/pgtk systems where char-width
returns 2 but the terminal treats them as single-width).  This caused
the cursor to land on the wrong character.

Replace move-to-column with a new positionCursorByCell helper that
iterates the cursor row's terminal cells to compute the exact Emacs
character offset for the target column, then uses goto-char.  This is
both correct (independent of char-width) and faster (avoids Elisp
display-width scanning).

Fixes #86
@jixiuf
Copy link
Copy Markdown

jixiuf commented Apr 12, 2026

I tried this, and it is fixed by this PR.

@dakra
Copy link
Copy Markdown
Owner Author

dakra commented Apr 12, 2026

Great. Thanks for testing and the wide-character hint :)

@dakra dakra merged commit fcb8d3b into main Apr 12, 2026
16 checks passed
@dakra dakra deleted the fix/cursor-position-char-width branch April 14, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cursor position in opencode is wrong

2 participants