emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Patch to highlight current line number [nlinum.el]


From: Kaushal Modi
Subject: Re: Patch to highlight current line number [nlinum.el]
Date: Mon, 18 Jul 2016 22:55:44 +0000

On Mon, Jul 18, 2016 at 6:05 PM Kaushal Modi <address@hidden> wrote:
My comments are inline below, with a formatted patch at the end of this email.

Here is a slightly optimized version of the same patch that results in lesser save-excursion and remove-text-properties calls in nlinum--current-line-update function:

I would like to optimize this even more.. I feel lag when I keep C-n/C-p pressed with nlinum-highlight-current-line set to t vs nil. Any pointers? You mentioned the use of markers instead of line numbers. How to use those if that could improve the performance?

From 7eb48b322578cc199eaa45de6bfe778e2b5f63de Mon Sep 17 00:00:00 2001
From: Kaushal Modi <address@hidden>
Date: Mon, 18 Jul 2016 17:42:45 -0400
Subject: [PATCH] Add ability to highlight current line number

* nlinum.el (nlinum-highlight-current-line): New defcustom to enable
highlighting current line number when non-nil (default is nil).
        (nlinum-current-line): New face for highlighting the current
line number.
---
 packages/nlinum/nlinum.el | 88 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 78 insertions(+), 10 deletions(-)

diff --git a/packages/nlinum/nlinum.el b/packages/nlinum/nlinum.el
index 98c9cbc..093d217 100644
--- a/packages/nlinum/nlinum.el
+++ b/packages/nlinum/nlinum.el
@@ -36,11 +36,27 @@
 
 ;;; Code:
 
-(require 'linum)                        ;For its face.
+(require 'linum) ; For its face.
+
+(defcustom nlinum-highlight-current-line nil
+  "Whether the current line number should be highlighted.
+When non-nil, the current line number is highlighted in `nlinum-current-line'
+face. "
+  :group 'nlinum
+  :type 'boolean)
 
 (defvar nlinum--width 2)
 (make-variable-buffer-local 'nlinum--width)
 
+(defface nlinum-current-line
+  '((t :inherit linum :weight bold))
+  "Face for displaying current line."
+  :group 'nlinum)
+
+(defvar nlinum--current-line 0
+  "Store current line number.")
+(make-variable-buffer-local 'nlinum--current-line)
+
 ;; (defvar nlinum--desc "")
 
 ;;;###autoload
@@ -53,9 +69,10 @@ if ARG is omitted or nil.
 Linum mode is a buffer-local minor mode."
   :lighter nil ;; (" NLinum" nlinum--desc)
   (jit-lock-unregister #'nlinum--region)
-  (remove-hook 'window-configuration-change-hook #'nlinum--setup-window t)
-  (remove-hook 'text-scale-mode-hook #'nlinum--setup-window t)
-  (remove-hook 'after-change-functions #'nlinum--after-change t)
+  (remove-hook 'window-configuration-change-hook #'nlinum--setup-window :local)
+  (remove-hook 'text-scale-mode-hook #'nlinum--setup-window :local)
+  (remove-hook 'after-change-functions #'nlinum--after-change :local)
+  (remove-hook 'post-command-hook #'nlinum--current-line-update :local)
   (kill-local-variable 'nlinum--line-number-cache)
   (remove-overlays (point-min) (point-max) 'nlinum t)
   ;; (kill-local-variable 'nlinum--ol-counter)
@@ -64,10 +81,13 @@ Linum mode is a buffer-local minor mode."
     ;; FIXME: Another approach would be to make the mode permanent-local,
     ;; which might indeed be preferable.
     (add-hook 'change-major-mode-hook (lambda () (nlinum-mode -1)))
-    (add-hook 'text-scale-mode-hook #'nlinum--setup-window nil t)
-    (add-hook 'window-configuration-change-hook #'nlinum--setup-window nil t)
-    (add-hook 'after-change-functions #'nlinum--after-change nil t)
-    (jit-lock-register #'nlinum--region t))
+    (add-hook 'text-scale-mode-hook #'nlinum--setup-window nil :local)
+    (add-hook 'window-configuration-change-hook #'nlinum--setup-window nil :local)
+    (add-hook 'after-change-functions #'nlinum--after-change nil :local)
+    (if nlinum-highlight-current-line
+        (add-hook 'post-command-hook #'nlinum--current-line-update nil :local)
+      (remove-hook 'post-command-hook #'nlinum--current-line-update :local))
+    (jit-lock-register #'nlinum--region :contextual))
   (nlinum--setup-windows))
 
 (defun nlinum--face-height (face)
@@ -131,6 +151,48 @@ Linum mode is a buffer-local minor mode."
                          (point-min) (point-max) '(fontified)))))
                   (current-buffer)))
 
+(defun nlinum--current-line-update ()
+  "Update current line number."
+  (let ((last-line nlinum--current-line))
+    (setq nlinum--current-line (save-excursion
+                                 (unless (bolp)
+                                   (forward-line 0))
+                                 (nlinum--line-number-at-pos)))
+
+    (let ((line-diff (- last-line nlinum--current-line))
+          beg end rm-text-prop)
+      (cond
+       ;; If point is moving upwards, set BEG to be the line-beginning of
+       ;; the current line, and END to be the line-beginning of the next
+       ;; line after the last line.
+       ((> line-diff 0)
+        (setq beg (line-beginning-position)) ; beginning of the current line
+        ;; Handle the case of blank lines too.
+        ;; Using the beginning of the next line ensures that the
+        ;; BEG and END are never equal.
+        (setq end (save-excursion ; beginning of the line after the last line
+                    (forward-line (1+ line-diff))
+                    (line-beginning-position)))
+        (setq rm-text-prop 1))
+
+       ;; If point is moving downwards, set BEG to be the line-beginning of
+       ;; the last line, and END to be the line-beginning of the next
+       ;; line after the current line.
+       ((< line-diff 0)
+        (setq beg (save-excursion ; beginning of the last line
+                    (forward-line line-diff)
+                    (line-beginning-position)))
+        (setq end (save-excursion ; beginning of the line after the current line
+                    (forward-line 1)
+                    (line-beginning-position)))
+        (setq rm-text-prop 1)))
+
+      ;; Remove the text properties only if the current line has changed,
+      ;; i.e. line-diff != 0
+      (when rm-text-prop
+        (with-silent-modifications
+          (remove-text-properties beg end '(fontified)))))))
+
 ;; (defun nlinum--ol-count ()
 ;;   (let ((i 0))
 ;;     (dolist (ol (overlays-in (point-min) (point-max)))
@@ -215,11 +277,17 @@ Used by the default `nlinum-format-function'."
 
 (defvar nlinum-format-function
   (lambda (line width)
-    (let ((str (format nlinum-format line)))
+    (let* ((is-current-line (= line nlinum--current-line))
+           (str (format nlinum-format line)))
       (when (< (length str) width)
         ;; Left pad to try and right-align the line-numbers.
         (setq str (concat (make-string (- width (length str)) ?\ ) str)))
-      (put-text-property 0 width 'face 'linum str)
+      (put-text-property 0 width 'face
+                         (if (and nlinum-highlight-current-line
+                                  is-current-line)
+                             'nlinum-current-line
+                           'linum)
+                         str)
       str))
   "Function to build the string representing the line number.
 Takes 2 arguments LINE and WIDTH, both of them numbers, and should return
-- 
2.6.0.rc0.24.gec371ff

 
--

Kaushal Modi


reply via email to

[Prev in Thread] Current Thread [Next in Thread]