1 ;;; nlinum.el --- Show line numbers in the margin -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: convenience
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; This is like linum-mode, but uses jit-lock to be (hopefully)
29 (require 'linum) ;For its face.
31 (defvar nlinum--width 2)
32 (make-variable-buffer-local 'nlinum--width)
34 ;; (defvar nlinum--desc "")
37 (define-minor-mode nlinum-mode
38 "Toggle display of line numbers in the left margin (Linum mode).
39 With a prefix argument ARG, enable Linum mode if ARG is positive,
40 and disable it otherwise. If called from Lisp, enable the mode
41 if ARG is omitted or nil.
43 Linum mode is a buffer-local minor mode."
44 :lighter nil ;; (" NLinum" nlinum--desc)
45 (jit-lock-unregister #'nlinum--region)
46 (remove-hook 'window-configuration-change-hook #'nlinum--setup-window t)
47 (remove-overlays (point-min) (point-max) 'nlinum t)
48 ;; (kill-local-variable 'nlinum--ol-counter)
49 (kill-local-variable 'nlinum--width)
51 (add-hook 'window-configuration-change-hook #'nlinum--setup-window nil t)
52 (jit-lock-register #'nlinum--region t))
53 (nlinum--setup-windows))
55 (defun nlinum--setup-window ()
56 (set-window-margins nil (if nlinum-mode nlinum--width)
57 (cdr (window-margins))))
59 (defun nlinum--setup-windows ()
60 (dolist (win (get-buffer-window-list nil nil t))
61 (with-selected-window win (nlinum--setup-window))))
63 (defun nlinum--new-width ()
64 (nlinum--setup-windows)
65 ;; (kill-local-variable 'nlinum--ol-counter)
66 (remove-overlays (point-min) (point-max) 'nlinum t)
69 (with-current-buffer buf
70 (with-silent-modifications
71 (remove-text-properties
72 (point-min) (point-max) '(fontified)))))
75 ;; (defun nlinum--ol-count ()
77 ;; (dolist (ol (overlays-in (point-min) (point-max)))
78 ;; (when (overlay-get ol 'nlinum) (incf i)))
81 ;; (defvar nlinum--ol-counter 100)
82 ;; (make-variable-buffer-local 'nlinum--ol-counter)
84 ;; (defun nlinum--flush-overlays (buffer)
85 ;; (with-current-buffer buffer
86 ;; (kill-local-variable 'nlinum--ol-counter)
87 ;; ;; We've created many overlays in this buffer, which can slow
88 ;; ;; down operations significantly. Let's flush them.
89 ;; ;; An easy way to flush them is
90 ;; ;; (remove-overlays min max 'nlinum t)
91 ;; ;; (put-text-property min max 'fontified nil)
92 ;; ;; but if the visible part of the buffer requires more than
93 ;; ;; nlinum-overlay-threshold overlays, then we'll inf-loop.
94 ;; ;; So let's be more careful about removing overlays.
95 ;; (let ((windows (get-buffer-window-list nil nil t))
96 ;; (start (point-min))
97 ;; (debug-count (nlinum--ol-count)))
98 ;; (with-silent-modifications
99 ;; (while (< start (point-max))
100 ;; (let ((end (point-max)))
101 ;; (dolist (window windows)
103 ;; ((< start (1- (window-start window)))
104 ;; (setq end (min (1- (window-start window)) end)))
105 ;; ((< start (1+ (window-end window)))
106 ;; (setq start (1+ (window-end window))))))
107 ;; (when (< start end)
108 ;; (remove-overlays start end 'nlinum t)
109 ;; ;; Warn jit-lock that this part of the buffer is not done any
110 ;; ;; more. This has the downside that font-lock will be re-applied
111 ;; ;; as well. But jit-lock doesn't know how to (and doesn't want
112 ;; ;; to) keep track of the status of its various
113 ;; ;; clients independently.
114 ;; (put-text-property start end 'fontified nil)
115 ;; (setq start (+ end 1))))))
116 ;; (let ((debug-new-count (nlinum--ol-count)))
117 ;; (message "Flushed %d overlays, %d remaining"
118 ;; (- debug-count debug-new-count) debug-new-count)))))
121 (defun nlinum--region (start limit)
123 ;; Text may contain those nasty intangible properties, but
124 ;; that shouldn't prevent us from counting those lines.
125 (let ((inhibit-point-motion-hooks t))
127 (unless (bolp) (forward-line 1))
128 (remove-overlays (point) limit 'nlinum t)
129 (let ((line (line-number-at-pos))
130 (fmt (format "%%%dd" nlinum--width)))
132 (and (not (eobp)) (< (point) limit)
133 (let* ((ol (make-overlay (point) (1+ (point))))
134 (str (format fmt line))
135 (width (string-width str)))
136 (when (< nlinum--width width)
137 (setq nlinum--width width)
139 (overlay-put ol 'nlinum t)
140 (overlay-put ol 'evaporate t)
141 (overlay-put ol 'before-string
142 (propertize " " 'display
143 `((margin left-margin)
146 ;; (setq nlinum--ol-counter (1- nlinum--ol-counter))
147 ;; (when (= nlinum--ol-counter 0)
148 ;; (run-with-idle-timer 0.5 nil #'nlinum--flush-overlays
149 ;; (current-buffer)))
150 (setq line (1+ line))
151 (zerop (forward-line 1))))))))
152 ;; (setq nlinum--desc (format "-%d" (nlinum--ol-count)))
157 ;;; nlinum.el ends here