unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Kaushal Modi <kaushal.modi@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: code.falling@gmail.com, Emacs developers <emacs-devel@gnu.org>
Subject: Re: Patch to highlight current line number [nlinum.el]
Date: Mon, 18 Jul 2016 22:55:44 +0000	[thread overview]
Message-ID: <CAFyQvY3JKzqk-Ke26KSd9bBPqOKRR7Vfv8sOLLZiyks9hNuWzg@mail.gmail.com> (raw)
In-Reply-To: <CAFyQvY0GeKh145SH1ep-9wbps4FsHEO3Sipw4Nhknd3wzs980A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 7081 bytes --]

On Mon, Jul 18, 2016 at 6:05 PM Kaushal Modi <kaushal.modi@gmail.com> 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 <kaushal.modi@gmail.com>
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

[-- Attachment #2: Type: text/html, Size: 9957 bytes --]

  reply	other threads:[~2016-07-18 22:55 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-15 17:10 Patch to highlight current line number [nlinum.el] Kaushal Modi
2016-07-16  2:15 ` Stefan Monnier
2016-07-18  4:31   ` Kaushal Modi
2016-07-18 13:40     ` Stefan Monnier
2016-07-18 15:28       ` Kaushal Modi
2016-07-18 17:09         ` Stefan Monnier
2016-07-18 22:05           ` Kaushal Modi
2016-07-18 22:55             ` Kaushal Modi [this message]
2016-07-19  0:32             ` Stefan Monnier
2016-07-19  5:00               ` Kaushal Modi
2016-07-19 12:28                 ` Stefan Monnier
2016-07-19 12:40                   ` Kaushal Modi
2016-07-20 19:30                     ` Stefan Monnier
2016-07-20 19:38                       ` Kaushal Modi
2016-07-20 20:28                         ` Stefan Monnier
2016-07-20 20:31                           ` Kaushal Modi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFyQvY3JKzqk-Ke26KSd9bBPqOKRR7Vfv8sOLLZiyks9hNuWzg@mail.gmail.com \
    --to=kaushal.modi@gmail.com \
    --cc=code.falling@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).