]> code.delx.au - gnu-emacs-elpa/blob - packages/nlinum/nlinum.el
Merge branch 'master' of github.com:leoliu/ggtags
[gnu-emacs-elpa] / packages / nlinum / nlinum.el
1 ;;; nlinum.el --- Show line numbers in the margin -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: convenience
7 ;; Version: 1.1
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 ;;; Code:
28
29 (require 'linum) ;For its face.
30
31 (defvar nlinum--width 2)
32 (make-variable-buffer-local 'nlinum--width)
33
34 ;; (defvar nlinum--desc "")
35
36 ;;;###autoload
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.
42
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-hook 'after-change-functions #'nlinum--after-change)
48 (kill-local-variable 'nlinum--line-number-cache)
49 (remove-overlays (point-min) (point-max) 'nlinum t)
50 ;; (kill-local-variable 'nlinum--ol-counter)
51 (kill-local-variable 'nlinum--width)
52 (when nlinum-mode
53 (add-hook 'window-configuration-change-hook #'nlinum--setup-window nil t)
54 (add-hook 'after-change-functions #'nlinum--after-change nil t)
55 (jit-lock-register #'nlinum--region t))
56 (nlinum--setup-windows))
57
58 (defun nlinum--setup-window ()
59 (set-window-margins nil (if nlinum-mode nlinum--width)
60 (cdr (window-margins))))
61
62 (defun nlinum--setup-windows ()
63 (dolist (win (get-buffer-window-list nil nil t))
64 (with-selected-window win (nlinum--setup-window))))
65
66 (defun nlinum--new-width ()
67 (nlinum--setup-windows)
68 ;; (kill-local-variable 'nlinum--ol-counter)
69 (remove-overlays (point-min) (point-max) 'nlinum t)
70 (run-with-timer 0 nil
71 (lambda (buf)
72 (with-current-buffer buf
73 (with-silent-modifications
74 (remove-text-properties
75 (point-min) (point-max) '(fontified)))))
76 (current-buffer)))
77
78 ;; (defun nlinum--ol-count ()
79 ;; (let ((i 0))
80 ;; (dolist (ol (overlays-in (point-min) (point-max)))
81 ;; (when (overlay-get ol 'nlinum) (incf i)))
82 ;; i))
83
84 ;; (defvar nlinum--ol-counter 100)
85 ;; (make-variable-buffer-local 'nlinum--ol-counter)
86
87 ;; (defun nlinum--flush-overlays (buffer)
88 ;; (with-current-buffer buffer
89 ;; (kill-local-variable 'nlinum--ol-counter)
90 ;; ;; We've created many overlays in this buffer, which can slow
91 ;; ;; down operations significantly. Let's flush them.
92 ;; ;; An easy way to flush them is
93 ;; ;; (remove-overlays min max 'nlinum t)
94 ;; ;; (put-text-property min max 'fontified nil)
95 ;; ;; but if the visible part of the buffer requires more than
96 ;; ;; nlinum-overlay-threshold overlays, then we'll inf-loop.
97 ;; ;; So let's be more careful about removing overlays.
98 ;; (let ((windows (get-buffer-window-list nil nil t))
99 ;; (start (point-min))
100 ;; (debug-count (nlinum--ol-count)))
101 ;; (with-silent-modifications
102 ;; (while (< start (point-max))
103 ;; (let ((end (point-max)))
104 ;; (dolist (window windows)
105 ;; (cond
106 ;; ((< start (1- (window-start window)))
107 ;; (setq end (min (1- (window-start window)) end)))
108 ;; ((< start (1+ (window-end window)))
109 ;; (setq start (1+ (window-end window))))))
110 ;; (when (< start end)
111 ;; (remove-overlays start end 'nlinum t)
112 ;; ;; Warn jit-lock that this part of the buffer is not done any
113 ;; ;; more. This has the downside that font-lock will be re-applied
114 ;; ;; as well. But jit-lock doesn't know how to (and doesn't want
115 ;; ;; to) keep track of the status of its various
116 ;; ;; clients independently.
117 ;; (put-text-property start end 'fontified nil)
118 ;; (setq start (+ end 1))))))
119 ;; (let ((debug-new-count (nlinum--ol-count)))
120 ;; (message "Flushed %d overlays, %d remaining"
121 ;; (- debug-count debug-new-count) debug-new-count)))))
122
123
124 (defvar nlinum--line-number-cache nil)
125 (make-variable-buffer-local 'nlinum--line-number-cache)
126
127 (defun nlinum--after-change (&rest _args)
128 (setq nlinum--line-number-cache nil))
129
130 (defun nlinum--line-number-at-pos ()
131 "Like `line-number-at-pos' but sped up with a cache."
132 ;; (assert (bolp))
133 (let ((pos
134 (if (and nlinum--line-number-cache
135 (> (- (point) (point-min))
136 (abs (- (point) (car nlinum--line-number-cache)))))
137 (funcall (if (> (point) (car nlinum--line-number-cache))
138 #'+ #'-)
139 (cdr nlinum--line-number-cache)
140 (count-lines (point) (car nlinum--line-number-cache)))
141 (line-number-at-pos))))
142 ;;(assert (= pos (line-number-at-pos)))
143 (setq nlinum--line-number-cache (cons (point) pos))
144 pos))
145
146 (defun nlinum--region (start limit)
147 (save-excursion
148 ;; Text may contain those nasty intangible properties, but
149 ;; that shouldn't prevent us from counting those lines.
150 (let ((inhibit-point-motion-hooks t))
151 (goto-char start)
152 (unless (bolp) (forward-line 1))
153 (remove-overlays (point) limit 'nlinum t)
154 (let ((line (nlinum--line-number-at-pos))
155 (fmt (format "%%%dd" nlinum--width)))
156 (while
157 (and (not (eobp)) (< (point) limit)
158 (let* ((ol (make-overlay (point) (1+ (point))))
159 (str (format fmt line))
160 (width (string-width str)))
161 (when (< nlinum--width width)
162 (setq nlinum--width width)
163 (nlinum--new-width))
164 (overlay-put ol 'nlinum t)
165 (overlay-put ol 'evaporate t)
166 (overlay-put ol 'before-string
167 (propertize " " 'display
168 `((margin left-margin)
169 ,(propertize str
170 'face 'linum))))
171 ;; (setq nlinum--ol-counter (1- nlinum--ol-counter))
172 ;; (when (= nlinum--ol-counter 0)
173 ;; (run-with-idle-timer 0.5 nil #'nlinum--flush-overlays
174 ;; (current-buffer)))
175 (setq line (1+ line))
176 (zerop (forward-line 1))))))))
177 ;; (setq nlinum--desc (format "-%d" (nlinum--ol-count)))
178 nil)
179
180
181 (provide 'nlinum)
182 ;;; nlinum.el ends here