(defun rose-line-left-pixel-width (elem)
"Calculate pixel width of components on the left of ELEM."
(string-pixel-width
(format-mode-line
(cl-subseq mode-line-format
0 (cl-position elem mode-line-format :test 'equal)))))
(defun rose-line-right-pixel-width (elem)
"Calculate pixel width of components on the right of ELEM."
(string-pixel-width
(format-mode-line
(cl-subseq mode-line-format
(1+ (cl-position elem mode-line-format :test 'equal))))))
(defvar rose-line-spaces
`(:eval
(let ((space-pixel-width (string-pixel-width " "))
(s-pixel-width
(- (window-pixel-width)
(rose-line-left-pixel-width 'rose-line-spaces)
(rose-line-right-pixel-width 'rose-line-spaces))))
(if (> s-pixel-width space-pixel-width)
(make-string (floor (/ s-pixel-width space-pixel-width))
(string-to-char " "))
"")))
"Spaces to fill in the middle of mode line, calculated in pixels.
Do NOT have more than one of it in mode-line-format.")
(put 'rose-line-spaces 'risky-local-variable t)
the implementation of mode-line-format-right-align, copied verbatim from bindings.el
(defun mode--line-format-right-align ()
"Right-align all following mode-line constructs.
When the symbol `mode-line-format-right-align' appears in
`mode-line-format', return a string of one space, with a display
property to make it appear long enough to align anything after
that symbol to the right of the rendered mode line. Exactly how
far to the right is controlled by `mode-line-right-align-edge'.
It is important that the symbol `mode-line-format-right-align' be
included in `mode-line-format' (and not another similar construct
such as `(:eval (mode-line-format-right-align)'). This is because
the symbol `mode-line-format-right-align' is processed by
`format-mode-line' as a variable."
(let* ((rest (cdr (memq 'mode-line-format-right-align
mode-line-format)))
(rest-str (format-mode-line `("" ,@rest)))
(rest-width (progn
(add-face-text-property
0 (length rest-str) 'mode-line t rest-str)
(string-pixel-width rest-str))))
(propertize " " 'display
;; The `right' spec doesn't work on TTY frames
;; when windows are split horizontally (bug#59620)
(if (and (display-graphic-p)
(not (eq mode-line-right-align-edge 'window)))
`(space :align-to (- ,mode-line-right-align-edge
(,rest-width)))
`(space :align-to (,(- (window-pixel-width)
(window-scroll-bar-width)
(window-right-divider-width)
(* (or (car (window-margins)) 0)
(frame-char-width))
;; Manually account for value of
;; `mode-line-right-align-edge' even
;; when display is non-graphical
(pcase mode-line-right-align-edge
('right-margin
(or (cdr (window-margins)) 0))
('right-fringe
;; what here?
(or (cadr (window-fringes)) 0))
(_ 0))
rest-width)))))))
(defvar mode-line-format-right-align '(:eval (mode--line-format-right-align))
"Mode line construct to right align all following constructs.")
excerpt from 42.16.2 Specified Spaces of the GNU Emacs Lisp Reference Manual corresponding to Emacs version 30.1
:align-to hpos
Specifies that the space should be wide enough to reach the column
hpos. If hpos is a number, it is a column number, and is
measured in units of the canonical character width (see Frame Font). hpos can also be a pixel width specification
(see Pixel Specification for Spaces). When the current line is wider than
the window, and is either displayed by one or more continuation lines,
or is truncated and possibly scrolled horizontally (see Horizontal Scrolling), hpos is measured from the beginning of the logical
line, not from the visual beginning of the screen line. This way,
alignment produced by :align-to is consistent with functions
that count columns, such as current-column and
move-to-column (see Counting Columns). (There’s a single exception
from this rule: when :align-to is used to specify whitespace of
the wrap-prefix variable or text property, see Truncation.)