Skip to content

Commit 17d47d9

Browse files
committed
fix: skip pixel model-line alignment on Emacs <29
`string-pixel-width' and `buffer-text-pixel-size' are both Emacs 29+. Remove dead `buffer-text-pixel-size' branch and skip pixel right-alignment on 28.x, where `string-width' fallback would push right segments past the window edge. Guard pixel-specific tests.
1 parent b12e409 commit 17d47d9

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

eca-chat.el

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,15 +2595,10 @@ are in progress."
25952595
"Return the rendered pixel width of STRING for mode-line alignment.
25962596
Honors `display' specs (e.g. the context bar's pixel-width spaces) and
25972597
wide glyphs, unlike `length'. Uses `string-pixel-width' when
2598-
available, otherwise `buffer-text-pixel-size', falling back to
2599-
`string-width' on Emacsen without either."
2598+
available, falling back to `string-width' on Emacsen without it."
26002599
(cond
26012600
((string-empty-p string) 0)
26022601
((fboundp 'string-pixel-width) (string-pixel-width string))
2603-
((fboundp 'buffer-text-pixel-size)
2604-
(with-temp-buffer
2605-
(insert string)
2606-
(car (buffer-text-pixel-size nil nil t))))
26072602
(t (string-width string))))
26082603

26092604
(defun eca-chat--mode-line-string (session)
@@ -2629,7 +2624,8 @@ available, otherwise `buffer-text-pixel-size', falling back to
26292624
(eca-chat--mode-line-module session m))
26302625
right-modules))
26312626
"")))
2632-
(fill (if (string-empty-p right)
2627+
(fill (if (or (string-empty-p right)
2628+
(not (fboundp 'string-pixel-width)))
26332629
""
26342630
(let ((px (eca-chat--string-pixel-width right)))
26352631
(propertize

test/eca-chat-test.el

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,8 @@ does not treat the first line as metadata. Returns FN's value."
988988
(expect (eca-chat--string-pixel-width "") :to-equal 0))
989989

990990
(it "honors pixel-width display specs instead of counting chars"
991+
(unless (fboundp 'string-pixel-width)
992+
(buttercup-skip "string-pixel-width not available"))
991993
;; A single space carrying a 40px display width must measure ~40, not
992994
;; 1 (its `length'). Counting it as 1 char is what pushed the :usage
993995
;; and :trust mode-line segments off the right edge once the context
@@ -997,6 +999,8 @@ does not treat the first line as metadata. Returns FN's value."
997999
(expect (eca-chat--string-pixel-width s) :to-equal 40)))
9981000

9991001
(it "measures a pixel context-bar wider than its char length"
1002+
(unless (fboundp 'string-pixel-width)
1003+
(buttercup-skip "string-pixel-width not available"))
10001004
(let ((bar (eca-chat--context-bar-pixels
10011005
(list (list :name "System prompt" :tokens 100 :color "#ff0000"))
10021006
(list :freeColor "#222222")
@@ -1005,6 +1009,8 @@ does not treat the first line as metadata. Returns FN's value."
10051009

10061010
(describe "eca-chat context-bar compaction marker"
10071011
(it "keeps the pixel-bar total width when the marker is overlaid"
1012+
(unless (fboundp 'string-pixel-width)
1013+
(buttercup-skip "string-pixel-width not available"))
10081014
(let* ((cats (list (list :name "System prompt" :tokens 100 :color "#ff0000")))
10091015
(bd (list :freeColor "#222222"))
10101016
(plain (eca-chat--context-bar-pixels cats bd 16 0.5))

0 commit comments

Comments
 (0)