elisp: Emulating Mode Line
22.4.8 Emulating Mode Line Formatting
-------------------------------------
You can use the function ‘format-mode-line’ to compute the text that
would appear in a mode line or header line based on a certain mode line
construct.
-- Function: format-mode-line format &optional face window buffer
This function formats a line of text according to FORMAT as if it
were generating the mode line for WINDOW, but it also returns the
text as a string. The argument WINDOW defaults to the selected
window. If BUFFER is non-‘nil’, all the information used is taken
from BUFFER; by default, it comes from WINDOW’s buffer.
The value string normally has text properties that correspond to
the faces, keymaps, etc., that the mode line would have. Any
character for which no ‘face’ property is specified by FORMAT gets
a default value determined by FACE. If FACE is ‘t’, that stands
for either ‘mode-line’ if WINDOW is selected, otherwise
‘mode-line-inactive’. If FACE is ‘nil’ or omitted, that stands for
the default face. If FACE is an integer, the value returned by
this function will have no text properties.
You can also specify other valid faces as the value of FACE. If
specified, that face provides the ‘face’ property for characters
whose face is not specified by FORMAT.
Note that using ‘mode-line’, ‘mode-line-inactive’, or ‘header-line’
as FACE will actually redisplay the mode line or the header line,
respectively, using the current definitions of the corresponding
face, in addition to returning the formatted string. (Other faces
do not cause redisplay.)
For example, ‘(format-mode-line header-line-format)’ returns the
text that would appear in the selected window’s header line (‘""’
if it has no header line). ‘(format-mode-line header-line-format
'header-line)’ returns the same text, with each character carrying
the face that it will have in the header line itself, and also
redraws the header line.