openlang

Mode line columns drift as soon as a buffer name has Hangul in it

Padded the buffer name in my mode line to a fixed width so the bits after it line up, and it worked for a long while (well, in every buffer I happened to open).

(defun my/pad-name (name width)
  (concat name (make-string (max 0 (- width (length name))) ?\s)))

Then I opened 회의록.org and everything to the right of it slid over by three columns. length counts characters, and three Hangul syllables are three characters, but each one takes two columns on screen. (string-width "회의록") says 6, (length "회의록") says 3. (M-: both and you'll see it straight away.)

The fix is a function that counts columns to begin with.

(defun my/pad-name (name width)
  (truncate-string-to-width name width nil ?\s))

It pads short names and cuts long ones to the same width. (The manual is upfront that string-width is an approximation, composed characters and fonts can still fool it, but Hangul syllables come out right.) Rough rule from here on, anything headed for the screen gets measured with string-width and never with length.

The conversation continues 1

Follow the reasoning
  1. Permalink

    Tried your version with a width of 9 and 회의록정리.org. The fifth syllable would land on columns 9 and 10, so it gets dropped, and the ?\s adds one space to fill the gap. Without the padding argument that line comes out a column short and the drift is back, just smaller. ((truncate-string-to-width name width nil ?\s t) with the ellipsis argument cut long names with a little ellipsis on the end, which I liked better.)

Other sides of the same question

  1. Is an NFC key a sensible default for name lookup?

    Okay, a user pastes their name and it ends in e followed by U+0301. Later they type the same name and the keyboard gives U+00…

  2. `isdigit()` passed a superscript two that `int()` refused

    A quantity field in a small form checks s.isdigit() and then calls int(s). Someone pasted ² and got a ValueError from the ver…

    Replies 4python
  3. NFC lookup keys and JCS signatures

    Signing complicates kim_dh's earlier question about NFC lookup keys, which separates the submitted name from an NFC lookup ke…