From d80c388f27fc8168b4f5283e34733b4e4a398da8 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 30 Apr 2016 23:13:39 -0400 Subject: [PATCH] Clean up and refactor nlinum defuns --- core/core-ui.el | 42 +++--------------------------------- core/defuns/defuns-nlinum.el | 42 +++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/core/core-ui.el b/core/core-ui.el index 44b60f1cc..660397101 100644 --- a/core/core-ui.el +++ b/core/core-ui.el @@ -192,48 +192,12 @@ (defvar nlinum-format "%4d ") (setq linum-format "%3d ") :init - (defun narf|nlinum-enable () - (nlinum-mode +1) - (add-hook 'post-command-hook 'narf|nlinum-hl-line)) - (add-hook! (markdown-mode prog-mode scss-mode web-mode conf-mode) - 'narf|nlinum-enable) + 'nlinum-mode) + (add-hook! 'nlinum-mode-hook + (add-hook 'post-command-hook 'narf|nlinum-hl-line nil t)) :config - (defun narf|nlinum-unhl-line () - "Unhighlight line number" - (when narf--hl-nlinum-overlay - (let* ((disp (get-text-property - 0 'display (overlay-get narf--hl-nlinum-overlay 'before-string))) - (str (nth 1 disp))) - (put-text-property 0 (length str) 'face 'linum str) - (setq narf--hl-nlinum-overlay nil - narf--hl-nlinum-line nil) - disp))) - - (defun narf|nlinum-hl-line (&optional line) - "Highlight line number" - (let ((line-no (or line (string-to-number (format-mode-line "%l"))))) - (when (and nlinum-mode (not (eq line-no narf--hl-nlinum-line))) - (let* ((pbol (if line - (save-excursion (goto-char (point-min)) - (forward-line line-no) - (line-beginning-position)) - (line-beginning-position))) - (peol (1+ pbol))) - ;; Handle EOF case - (let ((max (point-max))) - (when (>= peol max) - (setq peol max))) - (jit-lock-fontify-now pbol peol) - (let ((ov (-first (lambda (item) (overlay-get item 'nlinum)) (overlays-in pbol peol)))) - (when ov - (narf|nlinum-unhl-line) - (let ((str (nth 1 (get-text-property 0 'display (overlay-get ov 'before-string))))) - (put-text-property 0 (length str) 'face 'linum-highlight-face str) - (setq narf--hl-nlinum-overlay ov - narf--hl-nlinum-line line-no)))))))) - (add-hook! nlinum-mode (setq nlinum--width (length (save-excursion (goto-char (point-max)) diff --git a/core/defuns/defuns-nlinum.el b/core/defuns/defuns-nlinum.el index 405f6edd1..279bbd4c0 100644 --- a/core/defuns/defuns-nlinum.el +++ b/core/defuns/defuns-nlinum.el @@ -9,9 +9,45 @@ ;;;###autoload (defun narf|nlinum-disable () - (nlinum-mode -1) - (remove-hook 'post-command-hook 'narf|nlinum-hl-line) - (narf|nlinum-unhl-line)) + (nlinum-mode -1) + (remove-hook 'post-command-hook 'narf|nlinum-hl-line t) + (narf|nlinum-unhl-line)) + +;;;###autoload +(defun narf|nlinum-unhl-line () + "Unhighlight line number" + (when narf--hl-nlinum-overlay + (let* ((disp (get-text-property + 0 'display (overlay-get narf--hl-nlinum-overlay 'before-string))) + (str (nth 1 disp))) + (put-text-property 0 (length str) 'face 'linum str) + (setq narf--hl-nlinum-overlay nil + narf--hl-nlinum-line nil) + disp))) + +;;;###autoload +(defun narf|nlinum-hl-line (&optional line) + "Highlight line number" + (let ((line-no (or line (string-to-number (format-mode-line "%l"))))) + (if (and nlinum-mode (not (eq line-no narf--hl-nlinum-line))) + (let* ((pbol (if line + (save-excursion (goto-char 1) + (forward-line line-no) + (line-beginning-position)) + (line-beginning-position))) + (peol (1+ pbol)) + (max (point-max))) + ;; Handle EOF case + (when (>= peol max) + (setq peol max)) + (jit-lock-fontify-now pbol peol) + (let ((ov (-first (lambda (item) (overlay-get item 'nlinum)) (overlays-in pbol peol)))) + (when ov + (narf|nlinum-unhl-line) + (let ((str (nth 1 (get-text-property 0 'display (overlay-get ov 'before-string))))) + (put-text-property 0 (length str) 'face 'linum-highlight-face str) + (setq narf--hl-nlinum-overlay ov + narf--hl-nlinum-line line-no)))))))) (provide 'defuns-nlinum) ;;; defuns-nlinum.el ends here