]> code.delx.au - gnu-emacs-elpa/blob - packages/nlinum/nlinum.el
Merge commit '1b1896fcd7885280f390b37bf3572b74fbfcc3cf'
[gnu-emacs-elpa] / packages / nlinum / nlinum.el
1 ;;; nlinum.el --- Show line numbers in the margin -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2012, 2014, 2015 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: convenience
7 ;; Version: 1.6
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
24 ;; This is like linum-mode, but uses jit-lock to be (hopefully)
25 ;; more efficient.
26
27 ;;; News:
28
29 ;; v1.3:
30 ;; - New custom variable `nlinum-format'.
31 ;; - Change in calling convention of `nlinum-format-function'.
32
33 ;; v1.2:
34 ;; - New global mode `global-nlinum-mode'.
35 ;; - New config var `nlinum-format-function'.
36
37 ;;; Code:
38
39 (require 'linum) ;For its face.
40
41 (defvar nlinum--width 2)
42 (make-variable-buffer-local 'nlinum--width)
43
44 ;; (defvar nlinum--desc "")
45
46 ;;;###autoload
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.
52
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)
63 (when nlinum-mode
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))
72
73 (defun nlinum--face-height (face)
74 (aref (font-info (face-font face)) 2))
75
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)))
80 (if (<= width 0)
81 (aref fi 10)
82 width)))))
83
84 (defun nlinum--setup-window ()
85 (let ((width (if (display-graphic-p)
86 (ceiling
87 (let ((width (nlinum--face-width 'linum)))
88 (if width
89 (/ (* nlinum--width 1.0 width)
90 (frame-char-width))
91 (/ (* nlinum--width 1.0
92 (nlinum--face-height 'linum))
93 (frame-char-height)))))
94 nlinum--width)))
95 (set-window-margins nil (if nlinum-mode width)
96 (cdr (window-margins)))))
97
98 (defun nlinum--setup-windows ()
99 (dolist (win (get-buffer-window-list nil nil t))
100 (with-selected-window win (nlinum--setup-window))))
101
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
107 (lambda (buf)
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)))))
114 (current-buffer)))
115
116 ;; (defun nlinum--ol-count ()
117 ;; (let ((i 0))
118 ;; (dolist (ol (overlays-in (point-min) (point-max)))
119 ;; (when (overlay-get ol 'nlinum) (incf i)))
120 ;; i))
121
122 ;; (defvar nlinum--ol-counter 100)
123 ;; (make-variable-buffer-local 'nlinum--ol-counter)
124
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)
143 ;; (cond
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)))))
160
161
162 (defvar nlinum--line-number-cache nil)
163 (make-variable-buffer-local 'nlinum--line-number-cache)
164
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.
172
173 (defun nlinum--after-change (&rest _args)
174 (setq nlinum--line-number-cache nil))
175
176 (defun nlinum--line-number-at-pos ()
177 "Like `line-number-at-pos' but sped up with a cache."
178 ;; (assert (bolp))
179 (let ((pos
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))
184 #'+ #'-)
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))
190 pos))
191
192 (defcustom nlinum-format "%d"
193 "Format of the line numbers.
194 Used by the default `nlinum-format-function'."
195 :type 'string
196 :group 'linum)
197
198 (defvar nlinum-format-function
199 (lambda (line width)
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)
205 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.")
210
211 (defun nlinum--region (start limit)
212 (save-excursion
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))
216 (goto-char start)
217 (unless (bolp) (forward-line 1))
218 (remove-overlays (point) limit 'nlinum t)
219 (let ((line (nlinum--line-number-at-pos)))
220 (while
221 (and (not (eobp)) (< (point) limit)
222 (let* ((ol (make-overlay (point) (1+ (point))))
223 (str (funcall nlinum-format-function
224 line nlinum--width))
225 (width (string-width str)))
226 (when (< nlinum--width width)
227 (setq nlinum--width width)
228 (nlinum--flush))
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)))
241 nil)
242
243 ;;;###autoload
244 (define-globalized-minor-mode global-nlinum-mode nlinum-mode
245 (lambda () (unless (minibufferp) (nlinum-mode))))
246
247 (provide 'nlinum)
248 ;;; nlinum.el ends here