[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/nlinum 0ef1df7 10/20: * nlinum.el: Add highlighting of
From: |
Stefan Monnier |
Subject: |
[elpa] externals/nlinum 0ef1df7 10/20: * nlinum.el: Add highlighting of the current line |
Date: |
Sat, 28 Nov 2020 18:42:24 -0500 (EST) |
branch: externals/nlinum
commit 0ef1df7d816b4a5df55be28cec2beaf2f368ea5d
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
* nlinum.el: Add highlighting of the current line
(nlinum): New group.
(nlinum-highlight-current-line): New defcustom.
(nlinum-current-line): New face.
(nlinum--current-line): New var.
(nlinum-format-function): Use them.
(nlinum--current-line-update): New function.
(nlinum-mode): Use it.
---
nlinum.el | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 78 insertions(+), 12 deletions(-)
diff --git a/nlinum.el b/nlinum.el
index 98c9cbc..358cad4 100644
--- a/nlinum.el
+++ b/nlinum.el
@@ -1,6 +1,6 @@
;;; nlinum.el --- Show line numbers in the margin -*- lexical-binding: t -*-
-;; Copyright (C) 2012, 2014, 2015 Free Software Foundation, Inc.
+;; Copyright (C) 2012, 2014, 2015, 2016 Free Software Foundation, Inc.
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
;; Keywords: convenience
@@ -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,33 @@
;;; 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
+ :group 'linum
+ :prefix "nlinum")
+
+(defcustom nlinum-highlight-current-line nil
+ ;; FIXME: It would be good to enable it by default, but only once we make it
+ ;; work right with multiple-windows.
+ "Whether the current line number should be highlighted.
+When non-nil, the current line number is highlighted in `nlinum-current-line'
+face."
+ :type 'boolean)
+
+(defface nlinum-current-line
+ '((t :inherit linum :weight bold))
+ "Face for displaying current line.")
(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 +80,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 +92,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 'text-scale-mode-hook #'nlinum--setup-window nil :local)
(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 '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 +162,34 @@ 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
+ (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 (natnump line-diff)
+ ;; Point is moving upward.
+ (progn
+ (setq beg (line-beginning-position))
+ (setq end (line-end-position (1+ line-diff))))
+ ;; Point is moving downward.
+ (setq beg (line-beginning-position (1+ line-diff)))
+ (setq end (line-end-position)))
+
+ ;; (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
+ ;; Handle the case of blank lines too.
+ (min (point-max) (1+ end))
+ '(fontified)))))))
+
;; (defun nlinum--ol-count ()
;; (let ((i 0))
;; (dolist (ol (overlays-in (point-min) (point-max)))
@@ -192,8 +251,9 @@ Linum mode is a buffer-local minor mode."
(setq nlinum--line-number-cache nil))
(defun nlinum--line-number-at-pos ()
- "Like `line-number-at-pos' but sped up with a cache."
- ;; (assert (bolp))
+ "Like `line-number-at-pos' but sped up with a cache.
+Only works right if point is at BOL."
+ ;; (cl-assert (bolp))
(let ((pos
(if (and nlinum--line-number-cache
(> (- (point) (point-min))
@@ -215,11 +275,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
- [elpa] branch externals/nlinum created (now 60d6af0), Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 37cc4b2 01/20: Add nlinum.el, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum c64e489 05/20: * packages/nlinum/nlinum.el (nlinum-mode): Don't leave overlays around when, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 5081418 02/20: * nlinum.el: Speed up by caching last line-number., Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum d7dbf4a 03/20: * nlinum.el: Add global-nlinum-mode and nlinum-format-function., Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 9a52252 07/20: Fixes: debbugs:17906, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 02d3ccf 08/20: * nlinum.el: Use face-width if available. Hook into text-scale-mode, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum fbe07f1 09/20: * nlinum.el (nlinum--setup-window): Better preserve margin settings, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 0ef1df7 10/20: * nlinum.el: Add highlighting of the current line,
Stefan Monnier <=
- [elpa] externals/nlinum a4ae169 12/20: nlinum.el: Make it possible to use the right margin, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum f959610 13/20: * nlinum/nlinum.el: Don't assume nlinum-use-right-margin is fixed, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 132ef19 04/20: * nlinum.el (nlinum-format): New custom variable., Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum ba66199 06/20: * packages/nlinum/nlinum.el (nlinum--face-height): New function., Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 16d30cf 19/20: * packages/nlinum/nlinum.el (nlinum--flush): Widen to really flush all., Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 99d0fef 16/20: * packages/nlinum/nlinum.el (nlinum-widen): New custom var, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 05d0274 17/20: * packages/nlinum/nlinum.el (nlinum--check-narrowing): Flush the line-number-cache as well., Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 5cda97a 11/20: * nlinum.el: Bump version to 1.7, Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 8fd46ee 14/20: Fix nlinum face height function (Bug#26552), Stefan Monnier, 2020/11/28
- [elpa] externals/nlinum 7b18687 15/20: * packages/nlinum/nlinum.el: Bump version to 1.8.1., Stefan Monnier, 2020/11/28