summaryrefslogtreecommitdiff
path: root/lisp/display-line-numbers.el
diff options
context:
space:
mode:
authorLars Ingebrigtsen <larsi@gnus.org>2022-05-13 21:24:12 +0200
committerLars Ingebrigtsen <larsi@gnus.org>2022-05-13 21:24:12 +0200
commitdea41d4c24d0d33e359cbfe34054d9048761adfd (patch)
treea9e1f4738e631d62e9f33ef68e4e4699dba3ff10 /lisp/display-line-numbers.el
parent72b5b4194106d38432cd9892bc01e95d71bf75db (diff)
downloademacs-dea41d4c24d0d33e359cbfe34054d9048761adfd.tar.gz
Add new minor mode `header-line-indent-mode'
This is mostly factored out from tabulated-list.el (with bugs fixed). * doc/lispref/modes.texi (Header Lines): Document it. * lisp/display-line-numbers.el (header-line-indent): New variable. (header-line-indent--line-number-width) (header-line-indent--watch-line-number-width) (header-line-indent--window-scroll-function): New helper functions. (header-line-indent-mode): New minor mode. * lisp/display-line-numbers.el (header-line-indent-width): New variable. * lisp/emacs-lisp/tabulated-list.el (tabulated-list-line-number-width) (tabulated-list-watch-line-number-width) (tabulated-list-window-scroll-function): Make into obsolete aliases. (tabulated-list-mode): Use 'header-line-indent-mode'. * lisp/emacs-lisp/tabulated-list.el (tabulated-list-init-header): Adjust the header line format and computation. * src/buffer.c (syms_of_buffer): Mention header-line-indent-mode.
Diffstat (limited to 'lisp/display-line-numbers.el')
-rw-r--r--lisp/display-line-numbers.el78
1 files changed, 78 insertions, 0 deletions
diff --git a/lisp/display-line-numbers.el b/lisp/display-line-numbers.el
index 860aa758bce..897a88398fd 100644
--- a/lisp/display-line-numbers.el
+++ b/lisp/display-line-numbers.el
@@ -108,6 +108,84 @@ the mode is on, set `display-line-numbers' directly."
(define-globalized-minor-mode global-display-line-numbers-mode
display-line-numbers-mode display-line-numbers--turn-on)
+
+
+;;;###autoload
+(defvar header-line-indent ""
+ "String to indent at the start if the header line.
+This is used in `header-line-indent-mode', and buffers that have
+this switched on should have a `header-line-format' that look like:
+
+ (\"\" header-line-indent THE-REST...)
+
+Also see `header-line-indent-width'.")
+
+;;;###autoload
+(defvar header-line-indent-width 0
+ "The width of the current line numbers displayed.
+This is updated when `header-line-indent-mode' is switched on.
+
+Also see `header-line-indent'.")
+
+(defun header-line-indent--line-number-width ()
+ "Return the width taken by `display-line-numbers' in the current buffer."
+ ;; line-number-display-width returns the value for the selected
+ ;; window, which might not be the window in which the current buffer
+ ;; is displayed.
+ (if (not display-line-numbers)
+ 0
+ (let ((cbuf-window (get-buffer-window (current-buffer) t)))
+ (if (window-live-p cbuf-window)
+ (with-selected-window cbuf-window
+ (truncate (line-number-display-width 'columns)))
+ 4))))
+
+(defun header-line-indent--watch-line-number-width (_window)
+ (let ((width (header-line-indent--line-number-width)))
+ (setq header-line-indent-width width)
+ (unless (= (length header-line-indent) width)
+ (setq header-line-indent (make-string width ?\s)))))
+
+(defun header-line-indent--window-scroll-function (window _start)
+ (let ((width (with-selected-window window
+ (truncate (line-number-display-width 'columns)))))
+ (setq header-line-indent-width width)
+ (unless (= (length header-line-indent) width)
+ (setq header-line-indent (make-string width ?\s)))))
+
+;;;###autoload
+(define-minor-mode header-line-indent-mode
+ "Mode to indent the header line in `display-line-numbers-mode' buffers.
+This means that the header line will be kept indented so that it
+has blank space that's as wide as the displayed line numbers in
+the buffer.
+
+Buffers that have this switched on should have a
+`header-line-format' that look like:
+
+ (\"\" header-line-indent THE-REST...)
+
+The `header-line-indent-width' variable is also kept updated, and
+has the width of `header-line-format'. This can be used, for
+instance, in `:align-to' specs, like:
+
+ (space :align-to (+ header-line-indent-width 10))"
+ :lighter nil
+ (if header-line-indent-mode
+ (progn
+ (setq-local header-line-indent ""
+ header-line-indent-width 0)
+ (add-hook 'pre-redisplay-functions
+ #'header-line-indent--watch-line-number-width nil t)
+ (add-hook 'window-scroll-functions
+ #'header-line-indent--window-scroll-function nil t))
+ (setq-local header-line-indent ""
+ header-line-indent-width 0)
+ (remove-hook 'pre-redisplay-functions
+ #'header-line-indent--watch-line-number-width t)
+ (remove-hook 'window-scroll-functions
+ #'header-line-indent--window-scroll-function t)))
+
(provide 'display-line-numbers)
;;; display-line-numbers.el ends here