1 ;;; nlinum.el --- Show line numbers in the margin -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012, 2014, 2015 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)
30 ;; - New custom variable `nlinum-format'.
31 ;; - Change in calling convention of `nlinum-format-function'.
34 ;; - New global mode `global-nlinum-mode'.
35 ;; - New config var `nlinum-format-function'.
39 (require 'linum) ;For its face.
41 (defvar nlinum--width 2)
42 (make-variable-buffer-local 'nlinum--width)
44 ;; (defvar nlinum--desc "")
47 (define-minor-mode nlinum-mode
48 "Toggle display of line numbers in the left margin (Linum mode).
49 With a prefix argument ARG, enable Linum mode if ARG is positive,
50 and disable it otherwise. If called from Lisp, enable the mode
51 if ARG is omitted or nil.
53 Linum mode is a buffer-local minor mode."
54 :lighter nil ;; (" NLinum" nlinum--desc)
55 (jit-lock-unregister #'nlinum--region)
56 (remove-hook 'window-configuration-change-hook #'nlinum--setup-window t)
57 (remove-hook 'text-scale-mode-hook #'nlinum--setup-window t)
58 (remove-hook 'after-change-functions #'nlinum--after-change t)
59 (kill-local-variable 'nlinum--line-number-cache)
60 (remove-overlays (point-min) (point-max) 'nlinum t)
61 ;; (kill-local-variable 'nlinum--ol-counter)
62 (kill-local-variable 'nlinum--width)
64 ;; FIXME: Another approach would be to make the mode permanent-local,
65 ;; which might indeed be preferable.
66 (add-hook 'change-major-mode-hook (lambda () (nlinum-mode -1)))
67 (add-hook 'text-scale-mode-hook #'nlinum--setup-window nil t)
68 (add-hook 'window-configuration-change-hook #'nlinum--setup-window nil t)
69 (add-hook 'after-change-functions #'nlinum--after-change nil t)
70 (jit-lock-register #'nlinum--region t))
71 (nlinum--setup-windows))
73 (defun nlinum--face-height (face)
74 (aref (font-info (face-font face)) 2))
76 (defun nlinum--face-width (face) ;New info only in Emacs>=25.
77 (let ((fi (font-info (face-font face))))
78 (when (> (length fi) 11)
79 (let ((width (aref fi 11)))
84 (defun nlinum--setup-window ()
85 (let ((width (if (display-graphic-p)
87 (let ((width (nlinum--face-width 'linum)))
89 (/ (* nlinum--width 1.0 width)
91 (/ (* nlinum--width 1.0
92 (nlinum--face-height 'linum))
93 (frame-char-height)))))
95 (set-window-margins nil (if nlinum-mode width)
96 (cdr (window-margins)))))
98 (defun nlinum--setup-windows ()
99 (dolist (win (get-buffer-window-list nil nil t))
100 (with-selected-window win (nlinum--setup-window))))
102 (defun nlinum--flush ()
103 (nlinum--setup-windows)
104 ;; (kill-local-variable 'nlinum--ol-counter)
105 (remove-overlays (point-min) (point-max) 'nlinum t)
106 (run-with-timer 0 nil
108 (with-current-buffer buf
109 (with-silent-modifications
110 ;; FIXME: only remove `fontified' on those parts of the
111 ;; buffer that had an nlinum overlay!
112 (remove-text-properties
113 (point-min) (point-max) '(fontified)))))
116 ;; (defun nlinum--ol-count ()
118 ;; (dolist (ol (overlays-in (point-min) (point-max)))
119 ;; (when (overlay-get ol 'nlinum) (incf i)))
122 ;; (defvar nlinum--ol-counter 100)
123 ;; (make-variable-buffer-local 'nlinum--ol-counter)
125 ;; (defun nlinum--flush-overlays (buffer)
126 ;; (with-current-buffer buffer
127 ;; (kill-local-variable 'nlinum--ol-counter)
128 ;; ;; We've created many overlays in this buffer, which can slow
129 ;; ;; down operations significantly. Let's flush them.
130 ;; ;; An easy way to flush them is
131 ;; ;; (remove-overlays min max 'nlinum t)
132 ;; ;; (put-text-property min max 'fontified nil)
133 ;; ;; but if the visible part of the buffer requires more than
134 ;; ;; nlinum-overlay-threshold overlays, then we'll inf-loop.
135 ;; ;; So let's be more careful about removing overlays.
136 ;; (let ((windows (get-buffer-window-list nil nil t))
137 ;; (start (point-min))
138 ;; (debug-count (nlinum--ol-count)))
139 ;; (with-silent-modifications
140 ;; (while (< start (point-max))
141 ;; (let ((end (point-max)))
142 ;; (dolist (window windows)
144 ;; ((< start (1- (window-start window)))
145 ;; (setq end (min (1- (window-start window)) end)))
146 ;; ((< start (1+ (window-end window)))
147 ;; (setq start (1+ (window-end window))))))
148 ;; (when (< start end)
149 ;; (remove-overlays start end 'nlinum t)
150 ;; ;; Warn jit-lock that this part of the buffer is not done any
151 ;; ;; more. This has the downside that font-lock will be re-applied
152 ;; ;; as well. But jit-lock doesn't know how to (and doesn't want
153 ;; ;; to) keep track of the status of its various
154 ;; ;; clients independently.
155 ;; (put-text-property start end 'fontified nil)
156 ;; (setq start (+ end 1))))))
157 ;; (let ((debug-new-count (nlinum--ol-count)))
158 ;; (message "Flushed %d overlays, %d remaining"
159 ;; (- debug-count debug-new-count) debug-new-count)))))
162 (defvar nlinum--line-number-cache nil)
163 (make-variable-buffer-local 'nlinum--line-number-cache)
165 ;; We could try and avoid flushing the cache at every change, e.g. with:
166 ;; (defun nlinum--before-change (start _end)
167 ;; (if (and nlinum--line-number-cache
168 ;; (< start (car nlinum--line-number-cache)))
169 ;; (save-excursion (goto-char start) (nlinum--line-number-at-pos))))
170 ;; But it's far from clear that it's worth the trouble. The current simplistic
171 ;; approach seems to be good enough in practice.
173 (defun nlinum--after-change (&rest _args)
174 (setq nlinum--line-number-cache nil))
176 (defun nlinum--line-number-at-pos ()
177 "Like `line-number-at-pos' but sped up with a cache."
180 (if (and nlinum--line-number-cache
181 (> (- (point) (point-min))
182 (abs (- (point) (car nlinum--line-number-cache)))))
183 (funcall (if (> (point) (car nlinum--line-number-cache))
185 (cdr nlinum--line-number-cache)
186 (count-lines (point) (car nlinum--line-number-cache)))
187 (line-number-at-pos))))
188 ;;(assert (= pos (line-number-at-pos)))
189 (setq nlinum--line-number-cache (cons (point) pos))
192 (defcustom nlinum-format "%d"
193 "Format of the line numbers.
194 Used by the default `nlinum-format-function'."
198 (defvar nlinum-format-function
200 (let ((str (format nlinum-format line)))
201 (when (< (length str) width)
202 ;; Left pad to try and right-align the line-numbers.
203 (setq str (concat (make-string (- width (length str)) ?\ ) str)))
204 (put-text-property 0 width 'face 'linum str)
206 "Function to build the string representing the line number.
207 Takes 2 arguments LINE and WIDTH, both of them numbers, and should return
208 a string. WIDTH is the ideal width of the result. If the result is larger,
209 it may cause the margin to be resized and line numbers to be recomputed.")
211 (defun nlinum--region (start limit)
213 ;; Text may contain those nasty intangible properties, but
214 ;; that shouldn't prevent us from counting those lines.
215 (let ((inhibit-point-motion-hooks t))
217 (unless (bolp) (forward-line 1))
218 (remove-overlays (point) limit 'nlinum t)
219 (let ((line (nlinum--line-number-at-pos)))
221 (and (not (eobp)) (< (point) limit)
222 (let* ((ol (make-overlay (point) (1+ (point))))
223 (str (funcall nlinum-format-function
225 (width (string-width str)))
226 (when (< nlinum--width width)
227 (setq nlinum--width width)
229 (overlay-put ol 'nlinum t)
230 (overlay-put ol 'evaporate t)
231 (overlay-put ol 'before-string
232 (propertize " " 'display
233 `((margin left-margin) ,str)))
234 ;; (setq nlinum--ol-counter (1- nlinum--ol-counter))
235 ;; (when (= nlinum--ol-counter 0)
236 ;; (run-with-idle-timer 0.5 nil #'nlinum--flush-overlays
237 ;; (current-buffer)))
238 (setq line (1+ line))
239 (zerop (forward-line 1))))))))
240 ;; (setq nlinum--desc (format "-%d" (nlinum--ol-count)))
244 (define-globalized-minor-mode global-nlinum-mode nlinum-mode
245 (lambda () (unless (minibufferp) (nlinum-mode))))
248 ;;; nlinum.el ends here