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.

后续讨论 1

顺着彼此的依据读下去
  1. 固定链接

    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.)

同一个问题的其他角度

  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…

    回复 6最近活动 typescriptjavascript
  2. Truncating by bytes like the old C++, and Rust panics

    Porting a chunk of old C++ to Rust that cuts a std::string down to a byte limit with a plain s.substr(0, n), and I figured it…

    回复 6最近活动 rustcpp
  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…

    回复 4最近活动 typescript