Improve documentation of 'header-line-indent-mode'

* doc/lispref/modes.texi (Header Lines): Rewrite the documentation
of 'header-line-indent-mode' and its two variables.  Fix the
example.
* doc/lispref/display.texi (Pixel Specification): More accurate
description of what happens with :align-to in header-lines.
Improve indexing.  (Bug#61239)

* src/buffer.c (syms_of_buffer) <header-line-format>:
* lisp/display-line-numbers.el (header-line-indent)
(header-line-indent-width, header-line-indent-mode): Doc fixes.

* etc/NEWS: Enhance the announcement of 'header-line-indent-mode'.
This commit is contained in:
Eli Zaretskii 2023-02-03 16:45:56 +02:00
parent c3f58a6651
commit 35e238cae8
5 changed files with 136 additions and 38 deletions

View file

@ -5285,9 +5285,10 @@ space taken by the line-number display.
The @code{left}, @code{center}, and @code{right} positions can be The @code{left}, @code{center}, and @code{right} positions can be
used with @code{:align-to} to specify a position relative to the left used with @code{:align-to} to specify a position relative to the left
edge, center, or right edge of the text area. When the window edge, center, or right edge of the text area. When the window
displays line numbers, the @code{left} and the @code{center} positions displays line numbers, and @code{:align-to} is used in display
are offset to account for the screen space taken by the line-number properties of buffer text (as opposed to header line, see below), the
display. @code{left} and the @code{center} positions are offset to account for
the screen space taken by the line-number display.
Any of the above window elements (except @code{text}) can also be Any of the above window elements (except @code{text}) can also be
used with @code{:align-to} to specify that the position is relative to used with @code{:align-to} to specify that the position is relative to
@ -5301,11 +5302,11 @@ the left-margin, use
:align-to (+ left-margin (0.5 . left-margin)) :align-to (+ left-margin (0.5 . left-margin))
@end example @end example
If no specific base offset is set for alignment, it is always relative If no specific base offset is set for alignment, it is always
to the left edge of the text area. For example, @samp{:align-to 0} in a relative to the left edge of the text area. For example,
header-line aligns with the first text column in the text area. When @samp{:align-to 0} aligns with the first text column in the text area.
the window displays line numbers, the text is considered to start where When the window displays line numbers, the text is considered to start
the space used for line-number display ends. where the space used for line-number display ends.
A value of the form @code{(@var{num} . @var{expr})} stands for the A value of the form @code{(@var{num} . @var{expr})} stands for the
product of the values of @var{num} and @var{expr}. For example, product of the values of @var{num} and @var{expr}. For example,
@ -5317,6 +5318,35 @@ product of the values of @var{num} and @var{expr}. For example,
expressions. The form @code{(- @var{expr} ...)} negates or subtracts expressions. The form @code{(- @var{expr} ...)} negates or subtracts
the value of the expressions. the value of the expressions.
@vindex header-line-format@r{, and } :align-to
@cindex aligning header line, when line numbers are displayed
@cindex header line alignment when line numbers are displayed
Text shown in the header line that uses @code{:align-to} display
specifications is not automatically realigned when
@code{display-line-numbers-mode} is turned on and off, or when the
width of line numbers on display changes. To arrange for the
header-line text alignment to be updated, thus keeping the header-line
text aligned with the buffer text, turn on the
@code{header-line-indent-mode} in the buffer and use its two
variables, @code{header-line-indent} and
@code{header-line-indent-width}, in the display specification.
@xref{Header Lines}. Here's a simple example:
@lisp
(setq header-line-format
(concat (propertize " "
'display
'(space :align-to
(+ header-line-indent-width 10)))
"Column"))
@end lisp
@noindent
This will keep the text @samp{Column} on the header line aligned with
column 10 of buffer text, regardless of whether
@code{display-line-numbers-mode} is on or off, and also when
line-number display changes its width.
@node Other Display Specs @node Other Display Specs
@subsection Other Display Specifications @subsection Other Display Specifications

View file

@ -2578,21 +2578,70 @@ header line, for windows displaying the buffer. The format of the value
is the same as for @code{mode-line-format} (@pxref{Mode Line Data}). is the same as for @code{mode-line-format} (@pxref{Mode Line Data}).
It is normally @code{nil}, so that ordinary buffers have no header It is normally @code{nil}, so that ordinary buffers have no header
line. line.
@end defvar
@findex header-line-indent-mode If @code{display-line-numbers-mode} is turned on in a buffer
If @code{display-line-numbers-mode} is used, and you want the header (@pxref{Display Custom, display-line-numbers-mode,, emacs, The GNU
line to be indented by the same amount as the buffer contents, you can Emacs Manual}), the buffer text is indented on display by the amount
use the @code{header-line-indent-mode} minor mode. This minor mode of screen space needed to show the line numbers. By contrast, text of
keeps the @code{header-line-indent} variable updated, so that you can the header line is not automatically indented, because a header line
say something like: never displays a line number, and because the text of the header line
is not necessarily directly related to buffer text below it. If a
Lisp program needs the header-line text to be aligned with buffer text
(for example, if the buffer displays columnar data, like
@code{tabulated-list-mode} does, @pxref{Tabulated List Mode}), it
should turn on the minor mode @code{header-line-indent-mode}.
@deffn Command header-line-indent-mode
This buffer-local minor mode tracks the changes of the width of the
line-number display on screen (which may vary depending on the range
of line numbers shown in the window), and allows Lisp programs to
arrange that header-line text is always aligned with buffer text when
the line-number width changes. Such Lisp programs should turn on this
mode in the buffer, and use the variables @code{header-line-indent}
and @code{header-line-indent-width} in the @code{header-line-format}
to ensure it is adjusted to the text indentation at all times.
@end deffn
@defvar header-line-indent
This variable's value is a whitespace string whose width is kept equal
to the current width of line-numbers on display, provided that
@code{header-line-indent-mode} is turned on in the buffer shown in the
window. The number of spaces is calculated under the assumption that
the face of the header-line text uses the same font, including size,
as the frame's default font; if that assumption is false, use
@code{header-line-indent-width}, described below, instead. This
variable is intended to be used in simple situations where the
header-line text needs to be indented as a whole to be realigned with
buffer text, by prepending this variable's value to the actual
header-line text. For example, the following definition of
@code{header-line-format}:
@lisp @lisp
(setq header-line-format (setq header-line-format
`("" header-line-format ,my-header-line)) `("" header-line-indent ,my-header-line))
@end lisp @end lisp
This can be useful if you're displaying columnar data, and the header @noindent
line should align with that data in the buffer. where @code{my-header-line} is the format string that produces the
actual text of the header line, will make sure the header-line text
is always indented like the buffer text below it.
@end defvar
@defvar header-line-indent-width
This variable's value is kept updated to provide the current width, in
units of the frame's canonical character width, used for displaying
the line numbers, provided that @code{header-line-indent-mode} is
turned on in the buffer shown in the window. It can be used for
aligning the header-line text with the buffer text when
@code{header-line-indent} is not flexible enough. For example, if the
header line uses a font whose metrics is different from the default
face's font, your Lisp program can calculate the width of line-number
display in pixels, by multiplying the value of this variable by the
value returned by @code{frame-char-width} (@pxref{Frame Font}), and
then use the result to align header-line text using the
@code{:align-to} display property spec (@pxref{Specified Space}) in
pixels on the relevant parts of @code{header-line-frormat}.
@end defvar @end defvar
@defun window-header-line-height &optional window @defun window-header-line-height &optional window

View file

@ -3946,9 +3946,11 @@ This argument can be used to override values of print-related settings.
+++ +++
** New minor mode 'header-line-indent-mode'. ** New minor mode 'header-line-indent-mode'.
This is meant to be used in modes that have a header line that should This is meant to be used by Lisp programs that show a header line
be kept aligned with the buffer contents when the user switches which should be kept aligned with the buffer contents when the user
'display-line-numbers-mode' on or off. switches 'display-line-numbers-mode' on or off, and when the width of
line-number display changes. See the "(elisp) Header Lines" node in
the Emacs Lisp Reference manual for more information.
+++ +++
** New global minor mode 'lost-selection-mode'. ** New global minor mode 'lost-selection-mode'.

View file

@ -112,19 +112,27 @@ the mode is on, set `display-line-numbers' directly."
;;;###autoload ;;;###autoload
(defvar header-line-indent "" (defvar header-line-indent ""
"String to indent at the start if the header line. "String of spaces to indent the beginning of header-line due to line numbers.
This is used in `header-line-indent-mode', and buffers that have This is intended to be used in `header-line-format', and requires
this switched on should have a `header-line-format' that look like: the `header-line-indent-mode' to be turned on, in order for the width
of this string to be kept updated when the line-number width changes
on display. An example of a `header-line-format' that uses this
variable might look like this:
(\"\" header-line-indent THE-REST...) (\"\" header-line-indent THE-REST...)
where THE-REST is the format string which produces the actual text
of the header-line.
Also see `header-line-indent-width'.") Also see `header-line-indent-width'.")
;;;###autoload ;;;###autoload
(defvar header-line-indent-width 0 (defvar header-line-indent-width 0
"The width of the current line numbers displayed. "The width of the current line number display in the window.
This is updated when `header-line-indent-mode' is switched on. This is measured in units of the frame's canonical columns.
This is updated when `header-line-indent-mode' is switched on,
and is intended for use in `:align-to' display specifications
that are part of `header-line-format', when portions of header-line
text should be aligned to respective parts of buffer text.
Also see `header-line-indent'.") Also see `header-line-indent'.")
(defun header-line-indent--line-number-width () (defun header-line-indent--line-number-width ()
@ -155,21 +163,30 @@ Also see `header-line-indent'.")
;;;###autoload ;;;###autoload
(define-minor-mode header-line-indent-mode (define-minor-mode header-line-indent-mode
"Mode to indent the header line in `display-line-numbers-mode' buffers. "Minor mode to help with alignment of header line when line numbers are shown.
This means that the header line will be kept indented so that it This minor mode should be turned on in buffers which dispay header-line
has blank space that's as wide as the displayed line numbers in that needs to be aligned with buffer text when `display-line-numbers-mode'
the buffer. is turned on in the buffer.
Buffers that have this switched on should have a Buffers that have this switched on should have a `header-line-format'
`header-line-format' that look like: that uses the `header-line-indent' or the `header-line-indent-width'
variables, which this mode will keep up-to-date with the current
display of line numbers. For example, a `header-line-format' that
looks like this:
(\"\" header-line-indent THE-REST...) (\"\" header-line-indent THE-REST...)
The `header-line-indent-width' variable is also kept updated, and will make sure the text produced by THE-REST (which should be
has the width of `header-line-format'. This can be used, for a header-line format string) is always indented to be aligned on
instance, in `:align-to' specs, like: display with the first column of buffer text.
(space :align-to (+ header-line-indent-width 10))" The `header-line-indent-width' variable is also kept updated,
and can be used, for instance, in `:align-to' specs as part
of `header-line-format', like this:
(space :align-to (+ header-line-indent-width 10))
See also `line-number-display-width'."
:lighter nil :lighter nil
(if header-line-indent-mode (if header-line-indent-mode
(progn (progn

View file

@ -5081,8 +5081,8 @@ the mode line appears at the bottom. */);
The header line appears, optionally, at the top of a window; the mode The header line appears, optionally, at the top of a window; the mode
line appears at the bottom. line appears at the bottom.
Also see `header-line-indent-mode' if `display-line-number-mode' is Also see `header-line-indent-mode' if `display-line-numbers-mode' is
used. */); turned on and header-line text should be aligned with buffer text. */);
DEFVAR_PER_BUFFER ("mode-line-format", &BVAR (current_buffer, mode_line_format), DEFVAR_PER_BUFFER ("mode-line-format", &BVAR (current_buffer, mode_line_format),
Qnil, Qnil,