Improve stability of nlinum flushing

This commit is contained in:
Henrik Lissner 2017-05-28 15:33:05 +02:00
parent 1cc199497d
commit e3162c453a

View file

@ -208,37 +208,43 @@ file."
(nlinum-mode +1))) (nlinum-mode +1)))
:config :config
(defun doom-nlinum-flush-window (&optional window)
(let ((window (or window (selected-window)))
(orig-win (selected-window)))
(with-selected-window window
(when nlinum-mode
(if (not (eq window orig-win))
(nlinum--flush)
;; done in two steps to leave current line number highlighting alone
(nlinum--region (point-min) (max 1 (1- (line-beginning-position))))
(nlinum--region (min (point-max) (1+ (line-end-position))) (point-max)))))))
;; nlinum has a tendency to lose line numbers over time; a known issue. These
;; hooks/advisors attempt to stave off these glitches.
(defun doom*nlinum-flush-all-windows (&rest _) (defun doom*nlinum-flush-all-windows (&rest _)
"Fix nlinum margins after major UI changes (like a change of font)." "Fix nlinum margins after major UI changes (like a change of font)."
(dolist (buffer (doom-visible-buffers)) (mapc #'doom-nlinum-flush-window (doom-visible-windows))
(with-current-buffer buffer nil)
(when nlinum-mode (nlinum--flush)))))
(advice-add #'set-frame-font :after #'doom*nlinum-flush-all-windows) (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) (defun doom*nlinum-flush (&optional _ norecord)
;; norecord check is necessary to prevent infinite recursion in ;; norecord check is necessary to prevent infinite recursion in
;; `select-window' ;; `select-window'
(when (and nlinum-mode (not norecord)) (when (not norecord) (doom-nlinum-flush-window)))
(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 :before #'doom*nlinum-flush)
(advice-add #'select-window :after #'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-all-windows t)
(add-hook '+evil-esc-hook #'doom*nlinum-flush) (add-hook 'focus-in-hook #'doom*nlinum-flush-all-windows)
(add-hook 'focus-out-hook #'doom*nlinum-flush-all-windows)
;; ;;
(after! web-mode (after! web-mode
(advice-add #'web-mode-fold-or-unfold :after #'nlinum--flush)) (advice-add #'web-mode-fold-or-unfold :after #'doom*nlinum-flush))
;; Optimization: calculate line number column width beforehand ;; Optimization: calculate line number column width beforehand
(add-hook! nlinum-mode (add-hook! nlinum-mode
(setq nlinum--width (length (save-excursion (goto-char (point-max)) (setq nlinum--width
(length (save-excursion (goto-char (point-max))
(format-mode-line "%l")))))) (format-mode-line "%l"))))))
;; Helps us distinguish stacked delimiter pairs. Especially in parentheses-drunk ;; Helps us distinguish stacked delimiter pairs. Especially in parentheses-drunk