From 8ca6e2630b08cfb054d063d7e3ed2183840ea4ac Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 27 May 2017 14:33:59 +0200 Subject: [PATCH] Bring back nlinum linum-mode *really* slows down buffers when they're displayed in more than one window. This lag isn't present in nlinum. nlinum isn't perfect either but... lesser of two evils. This includes advisors and an ESC hook to mitigate the issue of disappearing nlinum line numbers. --- core/autoload/ui.el | 8 ++++- core/core-ui.el | 47 ++++++++++++++++++++++++----- core/packages.el | 2 +- modules/app/regex/autoload/regex.el | 4 +-- modules/ui/doom/config.el | 4 +++ 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/core/autoload/ui.el b/core/autoload/ui.el index 20a0b690a..29e28b226 100644 --- a/core/autoload/ui.el +++ b/core/autoload/ui.el @@ -13,7 +13,13 @@ (defun doom/toggle-line-numbers (&optional arg) "Toggle `linum-mode'." (interactive "P") - (linum-mode (or arg (if linum-mode -1 +1)))) + (let ((arg (or arg (if linum-mode -1 +1)))) + (cond ((featurep 'nlinum) + (nlinum-mode arg)) + ((featurep 'linum-mode) + (linum-mode arg)) + (t + (error "No line number plugin detected"))))) ;;;###autoload (defun doom-resize-window (new-size &optional horizontal) diff --git a/core/core-ui.el b/core/core-ui.el index a7f36c251..0f393e94b 100644 --- a/core/core-ui.el +++ b/core/core-ui.el @@ -195,18 +195,51 @@ file." (add-hook 'evil-visual-state-entry-hook #'doom|hl-line-off nil t) (add-hook 'evil-visual-state-exit-hook #'hl-line-mode nil t))))) -;; Line numbers -(def-package! linum - :commands linum-mode - :preface (defvar linum-format "%4d ") +;; Line number column. A faster (or equivalent, in the worst case) line number +;; plugin than the built-in `linum'. +(def-package! nlinum + :commands nlinum-mode + :preface + (defvar linum-format "%3d ") + (defvar nlinum-format "%4d ") :init (add-hook! (prog-mode text-mode) (unless (eq major-mode 'org-mode) - (linum-mode +1))) + (nlinum-mode +1))) :config - (require 'hlinum) ; highlight current line number - (hlinum-activate)) + (defun doom*nlinum-flush-all-windows (&rest _) + "Fix nlinum margins after major UI changes (like a change of font)." + (dolist (buffer (doom-visible-buffers)) + (with-current-buffer buffer + (when nlinum-mode (nlinum--flush))))) + (advice-add #'set-frame-font :after #'doom*nlinum-flush-all-windows) + + ;; A known issue with nlinum is that line numbers disappear over time. These + ;; hooks/advisors will attempt to stave off these glitches. + (defun doom*nlinum-flush (&optional _ norecord) + ;; norecord check is necessary to prevent infinite recursion in + ;; `select-window' + (when (and nlinum-mode (not norecord)) + (if _ + (nlinum--flush) + ;; done in two steps to leave current line number highlighting alone + (nlinum--region (point-min) (line-beginning-position)) + (nlinum--region (line-end-position) (point-max))))) + ;; refresh when switching windows + (advice-add #'select-window :before #'doom*nlinum-flush) + (advice-add #'select-window :after #'doom*nlinum-flush) + ;; and when pressing ESC in normal mode + (add-hook '+evil-esc-hook #'doom*nlinum-flush) + + ;; + (after! web-mode + (advice-add #'web-mode-fold-or-unfold :after #'nlinum--flush)) + + ;; Optimization: calculate line number column width beforehand + (add-hook! nlinum-mode + (setq nlinum--width (length (save-excursion (goto-char (point-max)) + (format-mode-line "%l")))))) ;; Helps us distinguish stacked delimiter pairs. Especially in parentheses-drunk ;; languages like Lisp. diff --git a/core/packages.el b/core/packages.el index 5fdf714e1..bb06a4ab3 100644 --- a/core/packages.el +++ b/core/packages.el @@ -14,7 +14,7 @@ ;; core-ui.el (package! highlight-indentation) (package! highlight-numbers) -(package! hlinum) +(package! nlinum) (package! rainbow-delimiters) (package! vi-tilde-fringe) (package! visual-fill-column) diff --git a/modules/app/regex/autoload/regex.el b/modules/app/regex/autoload/regex.el index 0a994c26a..f9499a5da 100644 --- a/modules/app/regex/autoload/regex.el +++ b/modules/app/regex/autoload/regex.el @@ -71,7 +71,7 @@ (with-current-buffer +regex--expr-buffer (conf-mode) (rainbow-delimiters-mode +1) - (linum-mode -1) + (doom/toggle-line-numbers +1) (setq-local require-final-newline nil) (+regex-mode +1) (text-scale-set 3))) @@ -98,7 +98,7 @@ (switch-to-buffer (get-buffer-create "*doom-regex-repl*")) (conf-mode) (rainbow-delimiters-mode +1) - (linum-mode -1) + (doom/toggle-line-numbers -1) (setq-local require-final-newline nil) (setq mode-line-format nil +regex--expr-replace-buffer (current-buffer)) diff --git a/modules/ui/doom/config.el b/modules/ui/doom/config.el index 510da6684..2a7e9af1e 100644 --- a/modules/ui/doom/config.el +++ b/modules/ui/doom/config.el @@ -43,6 +43,10 @@ :config (load-theme +doom-theme t) + ;; nlinum line highlighting + (when (featurep 'nlinum) + (doom-themes-nlinum-config)) + ;; Add file icons to doom-neotree (doom-themes-neotree-config) (setq doom-neotree-enable-variable-pitch t