Refactor nlinum hl/unhl; sacrifice a little performance for stability

This commit is contained in:
Henrik Lissner 2016-06-09 00:27:56 -04:00
parent 2c55f53cbd
commit 906e5006ae

View file

@ -11,36 +11,27 @@
(defun doom|nlinum-enable (&rest _) (defun doom|nlinum-enable (&rest _)
(nlinum-mode +1) (nlinum-mode +1)
(add-hook 'post-command-hook 'doom|nlinum-hl-line nil t) (add-hook 'post-command-hook 'doom|nlinum-hl-line nil t)
(doom|nlinum-unhl-line)) (doom--nlinum-unhl-line))
;;;###autoload ;;;###autoload
(defun doom|nlinum-disable (&rest _) (defun doom|nlinum-disable (&rest _)
(nlinum-mode -1) (nlinum-mode -1)
(remove-hook 'post-command-hook 'doom|nlinum-hl-line t) (remove-hook 'post-command-hook 'doom|nlinum-hl-line t)
(doom|nlinum-unhl-line)) (doom--nlinum-unhl-line))
;;;###autoload (defun doom--nlinum-unhl-line ()
(defun doom|nlinum-unhl-line ()
"Unhighlight line number" "Unhighlight line number"
(when doom--hl-nlinum-overlay (when doom--hl-nlinum-overlay
(let* ((disp (get-text-property (let* ((disp (get-text-property
0 'display (overlay-get doom--hl-nlinum-overlay 'before-string))) 0 'display (overlay-get doom--hl-nlinum-overlay 'before-string)))
(str (nth 1 disp))) (str (nth 1 disp)))
(put-text-property 0 (length str) 'face 'linum str) (put-text-property 0 (length str) 'face 'linum str)
(setq doom--hl-nlinum-overlay nil (setq doom--hl-nlinum-overlay nil)
doom--hl-nlinum-line nil)
disp))) disp)))
;;;###autoload ;;;###autoload
(defun doom|nlinum-hl-line (&optional line) (defun doom|nlinum-hl-line ()
"Highlight line number" "Highlight line number"
(let ((line-no (or line (string-to-number (format-mode-line "%l")))))
(unless doom--hl-nlinum-line
(setq doom--hl-nlinum-line line-no))
(if (and nlinum-mode (or (not (= line-no doom--hl-nlinum-line))
(memq this-command '(next-line previous-line
evil-window-bottom evil-window-top
evil-goto-line evil-goto-first-line))))
(let* ((pbol (line-beginning-position)) (let* ((pbol (line-beginning-position))
(peol (1+ pbol)) (peol (1+ pbol))
(max (point-max))) (max (point-max)))
@ -49,12 +40,11 @@
(setq peol max)) (setq peol max))
(jit-lock-fontify-now pbol peol) (jit-lock-fontify-now pbol peol)
(let ((ov (--first (overlay-get it 'nlinum) (overlays-in pbol peol)))) (let ((ov (--first (overlay-get it 'nlinum) (overlays-in pbol peol))))
(doom--nlinum-unhl-line)
(when ov (when ov
(doom|nlinum-unhl-line)
(let ((str (nth 1 (get-text-property 0 'display (overlay-get ov 'before-string))))) (let ((str (nth 1 (get-text-property 0 'display (overlay-get ov 'before-string)))))
(put-text-property 0 (length str) 'face 'doom-nlinum-highlight str) (put-text-property 0 (length str) 'face 'doom-nlinum-highlight str)
(setq doom--hl-nlinum-line line-no (setq doom--hl-nlinum-overlay ov))))))
doom--hl-nlinum-overlay ov))))))))
(provide 'defuns-nlinum) (provide 'defuns-nlinum)
;;; defuns-nlinum.el ends here ;;; defuns-nlinum.el ends here