emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/nlinum 99d0fef 16/20: * packages/nlinum/nlinum.el (nlin


From: Stefan Monnier
Subject: [elpa] externals/nlinum 99d0fef 16/20: * packages/nlinum/nlinum.el (nlinum-widen): New custom var
Date: Sat, 28 Nov 2020 18:42:25 -0500 (EST)

branch: externals/nlinum
commit 99d0fef381e9f44a3fdcf66f28c28109a7cdaf45
Author: Stefan Monnier <monnier@iro.umontreal.ca>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>

    * packages/nlinum/nlinum.el (nlinum-widen): New custom var
    
    (nlinum--line-number-at-pos): Use it.
    (nlinum--last-point-min): New var.
    (nlinum--check-narrowing): New function.
    (nlinum-mode): Use it.
---
 nlinum.el | 63 ++++++++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 48 insertions(+), 15 deletions(-)

diff --git a/nlinum.el b/nlinum.el
index 97efee8..99d32e7 100644
--- a/nlinum.el
+++ b/nlinum.el
@@ -1,10 +1,10 @@
 ;;; nlinum.el --- Show line numbers in the margin  -*- lexical-binding: t -*-
 
-;; Copyright (C) 2012, 2014-2017  Free Software Foundation, Inc.
+;; Copyright (C) 2012, 2014-2019  Free Software Foundation, Inc.
 
 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
 ;; Keywords: convenience
-;; Version: 1.8.1
+;; Version: 1.9
 
 ;; 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
@@ -24,7 +24,10 @@
 ;; This is like linum-mode, but uses jit-lock to be (hopefully)
 ;; more efficient.
 
-;;; News:
+;;;; News:
+
+;; v1.9:
+;; - Add `nlinu-widen'.
 
 ;; v1.8:
 ;; - Add `nlinum-use-right-margin'.
@@ -43,6 +46,12 @@
 ;; - New global mode `global-nlinum-mode'.
 ;; - New config var `nlinum-format-function'.
 
+;;;; Known bugs:
+;; - When narrowed, there can be a "bogus" line number that appears at the
+;;   very end of the buffer.
+;; - After widening, the current line's number may stay stale until the
+;;   next command.
+
 ;;; Code:
 
 (require 'linum)                        ;For its face
@@ -61,6 +70,10 @@ When non-nil, the current line number is highlighted in 
`nlinum-current-line'
 face."
   :type 'boolean)
 
+(defcustom nlinum-widen nil
+  "If nil, count lines within the current narrowing only."
+  :type 'boolean)
+
 (defface nlinum-current-line
   '((t :inherit linum :weight bold))
   "Face for displaying current line.")
@@ -95,6 +108,7 @@ Linum mode is a buffer-local minor mode."
   (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)
+  (remove-hook 'pre-redisplay-functions #'nlinum--check-narrowing :local)
   (kill-local-variable 'nlinum--line-number-cache)
   (remove-overlays (point-min) (point-max) 'nlinum t)
   ;; (kill-local-variable 'nlinum--ol-counter)
@@ -112,6 +126,7 @@ Linum mode is a buffer-local minor mode."
     (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 :local)
+    (add-hook 'pre-redisplay-functions #'nlinum--check-narrowing 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))
@@ -271,22 +286,40 @@ Linum mode is a buffer-local minor mode."
 (defun nlinum--after-change (&rest _args)
   (setq nlinum--line-number-cache nil))
 
+(defvar nlinum--last-point-min nil)
+(make-variable-buffer-local 'nlinum--last-point-min)
+
+(defun nlinum--check-narrowing (&optional _win)
+  ;; FIXME: We should also flush if nlinum-widen was changed.
+  ;; Note: if nlinum-widen is t the flush is still needed when
+  ;; point-min is/was in the middle of a line.
+  (unless (eql nlinum--last-point-min (point-min))
+    (setq nlinum--last-point-min (point-min))
+    (nlinum--flush)))
+
 (defun nlinum--line-number-at-pos ()
   "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))
-                     (abs (- (point) (car nlinum--line-number-cache)))))
-             (funcall (if (> (point) (car nlinum--line-number-cache))
-                          #'+ #'-)
-                      (cdr nlinum--line-number-cache)
-                      (count-lines (point) (car nlinum--line-number-cache)))
-           (line-number-at-pos))))
-    ;;(assert (= pos (line-number-at-pos)))
-    (setq nlinum--line-number-cache (cons (point) pos))
-    pos))
+  (if nlinum-widen
+      (save-excursion
+        (save-restriction
+          (widen)
+          (forward-line 0)              ;In case (point-min) was not at BOL.
+          (let ((nlinum-widen nil))
+            (nlinum--line-number-at-pos))))
+    (let ((pos
+           (if (and nlinum--line-number-cache
+                    (> (- (point) (point-min))
+                       (abs (- (point) (car nlinum--line-number-cache)))))
+               (funcall (if (> (point) (car nlinum--line-number-cache))
+                            #'+ #'-)
+                        (cdr nlinum--line-number-cache)
+                        (count-lines (point) (car nlinum--line-number-cache)))
+             (line-number-at-pos))))
+      ;;(assert (= pos (line-number-at-pos)))
+      (setq nlinum--line-number-cache (cons (point) pos))
+      pos)))
 
 (defcustom nlinum-format "%d"
   "Format of the line numbers.



reply via email to

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