On Tue, Jul 19, 2016 at 8:28 AM Stefan Monnier <
address@hidden> wrote:
> I need the line-diff to get the line begin/end positions for the last-line.
Exactly: if you have a marker showing you where is that "last-line", you
don't need the line-diff. If you really want the beg/end of that line,
you `goto-char' to that marker and then use line-beginning-position and
line-end-position from there with no argument.
Got it. I will work on that optimization as soon as I get chance.
Oh, the gloabl-nlinum-mode was missing a ":group 'linum" argument!
OK, I added ":group 'nlinum" in the latest patch now.
> ===== patch follows
Please install it, and thank you very much.
The final (for now) patch is below. Thanks for all your help. Can you please commit it? I do not have commit rights, but my copyright assignment is on file.
=====
From c9c5e4a5a158cde292353ba8e0669e5877365a5a Mon Sep 17 00:00:00 2001
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 | 90 ++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 78 insertions(+), 12 deletions(-)
diff --git a/packages/nlinum/nlinum.el b/packages/nlinum/nlinum.el
index 98c9cbc..0e7cf06 100644
--- a/packages/nlinum/nlinum.el
+++ b/packages/nlinum/nlinum.el
@@ -4,7 +4,7 @@
;; Keywords: convenience
-;; Version: 1.6
+;; Version: 1.7
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@@ -26,6 +26,11 @@
;;; News:
+;; v1.7:
+;; - Add ability to highlight current line number.
+;; - New custom variable `nlinum-highlight-current-line' and
+;; face `nlinum-current-line'.
+
;; v1.3:
;; - New custom variable `nlinum-format'.
;; - Change in calling convention of `nlinum-format-function'.
@@ -36,11 +41,32 @@
;;; Code:
-(require 'linum) ;For its face.
+(require 'linum) ; for its face
+
+(defgroup nlinum nil
+ "Show line numbers in the margin, (hopefully) more efficiently."
+ :group 'convenience
+ :prefix "nlinum")
+
+(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)
+
+(defface nlinum-current-line
+ '((t :inherit linum :weight bold))
+ "Face for displaying current line."
+ :group 'nlinum)
(defvar nlinum--width 2)
(make-variable-buffer-local 'nlinum--width)
+(defvar nlinum--current-line 0
+ "Store current line number.")
+(make-variable-buffer-local 'nlinum--current-line)
+
;; (defvar nlinum--desc "")
;;;###autoload
@@ -53,9 +79,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 +91,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 +161,35 @@ 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)
+ ;; Remove the text properties only if the current line has changed
+ (when (not (zerop line-diff))
+ ;; If point is moving upwards
+ (if (natnump line-diff)
+ (progn
+ (setq beg (line-beginning-position)) ; beginning of the current line
+ (setq end (line-end-position (1+ line-diff))) ; end of the last line
+ ;; Handle the case of blank lines too.
+ (setq end (min (point-max) (1+ end))))
+ ;; If point is moving downwards
+ (setq beg (line-beginning-position (1+ line-diff))) ; beginning of the last line
+ (setq end (line-end-position)) ; end of the current line
+ ;; Handle the case of blank lines too.
+ (setq end (min (point-max) (1+ end))))
+ ;; (message "curr-line:%d [beg/end:%d/%d] -- last-line:%d"
+ ;; nlinum--current-line beg end last-line)
+ (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 +274,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
@@ -260,7 +325,8 @@ it may cause the margin to be resized and line numbers to be recomputed.")
;;;###autoload
(define-globalized-minor-mode global-nlinum-mode nlinum-mode
- (lambda () (unless (minibufferp) (nlinum-mode))))
+ (lambda () (unless (minibufferp) (nlinum-mode)))
+ :group 'nlinum)
(provide 'nlinum)
;;; nlinum.el ends here
--
2.6.0.rc0.24.gec371ff