💥 Rewrite line numbers system for Emacs 25

This removes the various doom-line-number* variables and replaces it
with the Emacs 26 display-line-numbers API, which I've ported to Emacs
25.x (however, it uses nlinum under the hood, and not all of
display-line-numbers options are supported).
This commit is contained in:
Henrik Lissner 2018-08-21 02:54:28 +02:00
parent 24cddafc8a
commit a7da9a4738
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
5 changed files with 90 additions and 42 deletions

View file

@ -0,0 +1,82 @@
;;; core/autoload/line-numbers.el -*- lexical-binding: t; -*-
;;;###if (not (boundp 'display-line-numbers))
;; This was lifted out of the display-line-numbers library in Emacs 26.1 and
;; modified to use nlinum for Emacs 25.x users. It should be removed should
;; Emacs 25 support be removed.
;;;###autoload
(defvar display-line-numbers t
"Non-nil means display line numbers.
If the value is t, display the absolute number of each line of a buffer
shown in a window. Absolute line numbers count from the beginning of
the current narrowing, or from buffer beginning. If the value is
relative, display for each line not containing the window's point its
relative number instead, i.e. the number of the line relative to the
line showing the window's point.
In either case, line numbers are displayed at the beginning of each
non-continuation line that displays buffer text, i.e. after each newline
character that comes from the buffer. The value visual is like
relative but counts screen lines instead of buffer lines. In practice
this means that continuation lines count as well when calculating the
relative number of a line.
Lisp programs can disable display of a line number of a particular
buffer line by putting the display-line-numbers-disable text property
or overlay property on the first visible character of that line.")
;;;###autoload
(defcustom display-line-numbers-type t
"The default type of line numbers to use in `display-line-numbers-mode'.
See `display-line-numbers' for value options."
:type '(choice (const :tag "Relative line numbers" relative)
(const :tag "Relative visual line numbers" visual)
(other :tag "Absolute line numbers" t)))
;;;###autoload
(defcustom display-line-numbers-grow-only nil
"If non-nil, do not shrink line number width."
:type 'boolean)
;;;###autoload
(defcustom display-line-numbers-width-start nil
"If non-nil, count number of lines to use for line number width.
When `display-line-numbers-mode' is turned on,
`display-line-numbers-width' is set to the minimum width necessary
to display all line numbers in the buffer."
:type 'boolean)
(defun display-line-numbers-update-width ()
"Prevent the line number width from shrinking."
(let ((width (line-number-display-width)))
(when (> width (or display-line-numbers-width 1))
(setq display-line-numbers-width width))))
;;;###autoload
(define-minor-mode display-line-numbers-mode
"Toggle display of line numbers in the buffer.
This uses `display-line-numbers' internally.
To change the type of line numbers displayed by default,
customize `display-line-numbers-type'. To change the type while
the mode is on, set `display-line-numbers' directly."
:lighter nil
(cond ((eq display-line-numbers-type 'relative)
(if display-line-numbers-mode
(nlinum-relative-off)
(nlinum-relative-on)))
((not (null display-line-numbers-type))
(nlinum-mode (if display-line-numbers-mode +1 -1)))))
(defun display-line-numbers--turn-on ()
"Turn on `display-line-numbers-mode'."
(unless (or (minibufferp)
;; taken from linum.el
(and (daemonp) (null (frame-parameter nil 'client))))
(display-line-numbers-mode)))
;;;###autoload
(define-globalized-minor-mode global-display-line-numbers-mode
display-line-numbers-mode display-line-numbers--turn-on)

View file

@ -296,43 +296,11 @@ from the default."
;; Line numbers ;; Line numbers
;; ;;
(defvar doom-line-numbers-style t ;; Emacs 26+ has native line number support, and will ignore nlinum. This is for
"The default styles to use for the line number display. Accepts one of the ;; Emacs 25 users:
following: (defun doom|enable-line-numbers () (display-line-numbers-mode +1))
(defun doom|disable-line-numbers () (display-line-numbers-mode -1))
nil No line numbers
t Ordinary line numbers
'relative Relative line numbers
Use `doom/toggle-line-numbers' to cycle between these line number styles.")
(when (boundp 'display-line-numbers)
(defvar doom-line-numbers-visual-style nil
"If non-nil, relative line numbers will be countered by screen line, rather
than buffer line. Setting this to non-nil is the equivalent of using 'visual in
`display-line-numbers'.
It has no effect on nlinum."))
(defun doom|enable-line-numbers (&optional arg)
"Enables the display of line numbers, using `display-line-numbers' (in Emacs
26+) or `nlinum-mode'.
See `doom-line-numbers-style' to control the style of line numbers to display."
(cond ((boundp 'display-line-numbers)
(setq display-line-numbers (unless (eq arg -1) doom-line-numbers-style)))
((eq doom-line-numbers-style 'relative)
(if (eq arg -1)
(nlinum-relative-off)
(nlinum-relative-on)))
((not (null doom-line-numbers-style))
(nlinum-mode (or arg +1)))))
(defun doom|disable-line-numbers ()
"Disable the display of line numbers."
(doom|enable-line-numbers -1))
;; Emacs 26+ has native line number support.
;; Line number column. A faster (or equivalent, in the worst case) line number ;; Line number column. A faster (or equivalent, in the worst case) line number
;; plugin than `linum-mode'. ;; plugin than `linum-mode'.
(def-package! nlinum (def-package! nlinum
@ -484,7 +452,7 @@ frame's window-system, the theme will be reloaded.")
;; a good indicator that Emacs isn't frozen ;; a good indicator that Emacs isn't frozen
(add-hook 'doom-init-ui-hook #'blink-cursor-mode) (add-hook 'doom-init-ui-hook #'blink-cursor-mode)
;; line numbers in most modes ;; line numbers in most modes
(add-hook! (prog-mode text-mode conf-mode) #'doom|enable-line-numbers) (add-hook! (prog-mode text-mode conf-mode) #'display-line-numbers-mode)
;; More sensibile replacements for default commands ;; More sensibile replacements for default commands
(define-key! 'global (define-key! 'global

View file

@ -7,9 +7,7 @@
"What to set `line-spacing' in `+write-mode'.") "What to set `line-spacing' in `+write-mode'.")
(defun +write|init-line-numbers () (defun +write|init-line-numbers ()
(if +write-mode (display-line-numbers-mode (if +write-mode +1 -1)))
(doom|enable-line-numbers)
(doom|disable-line-numbers)))
(defun +write|init-mixed-pitch () (defun +write|init-mixed-pitch ()
(mixed-pitch-mode (if +write-mode +1 -1))) (mixed-pitch-mode (if +write-mode +1 -1)))

View file

@ -34,7 +34,7 @@
(unless (featurep! :lang julia) (unless (featurep! :lang julia)
(add-to-list 'auto-mode-alist '("\\.jl\\'" . ess-julia-mode))) (add-to-list 'auto-mode-alist '("\\.jl\\'" . ess-julia-mode)))
:config :config
(add-hook 'ess-mode-hook #'doom|enable-line-numbers) (add-hook 'ess-mode-hook #'display-line-numbers-mode)
(setq ess-offset-continued 'straight (setq ess-offset-continued 'straight
ess-expression-offset 2 ess-expression-offset 2
ess-nuke-trailing-whitespace-p t ess-nuke-trailing-whitespace-p t

View file

@ -6,7 +6,7 @@
(set-popup-rule! "^\\*HTTP Response" :size 0.4 :quit 'other) (set-popup-rule! "^\\*HTTP Response" :size 0.4 :quit 'other)
;; line numbers aren't enabled by default in fundamental-mode-derived modes ;; line numbers aren't enabled by default in fundamental-mode-derived modes
(add-hook 'restclient-mode-hook #'doom|enable-line-numbers) (add-hook 'restclient-mode-hook #'display-line-numbers-mode)
;; Forces underlying SSL verification to prompt for self-signed or invalid ;; Forces underlying SSL verification to prompt for self-signed or invalid
;; certs, rather than silently reject them. ;; certs, rather than silently reject them.