Mercurial > emacs
annotate lisp/linum.el @ 97650:8b1a761cfe61
[!subprocesses]: Define QCname.
(syms_of_process): Intern and staticpro it.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Sat, 23 Aug 2008 16:48:55 +0000 |
parents | ee5932bf781d |
children | 77e42c247dba |
rev | line source |
---|---|
88073 | 1 ;;; linum.el --- display line numbers in the left margin |
2 | |
3 ;; Copyright (C) 2008 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Markus Triska <markus.triska@gmx.at> | |
6 ;; Maintainer: FSF | |
7 ;; Keywords: convenience | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
92141
diff
changeset
|
11 ;; GNU Emacs is free software: you can redistribute it and/or modify |
88073 | 12 ;; it under the terms of the GNU General Public License as published by |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
92141
diff
changeset
|
13 ;; the Free Software Foundation, either version 3 of the License, or |
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
92141
diff
changeset
|
14 ;; (at your option) any later version. |
88073 | 15 |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
92141
diff
changeset
|
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
88073 | 23 |
24 ;;; Commentary: | |
25 | |
26 ;; Display line numbers for the current buffer. | |
27 ;; | |
88075 | 28 ;; Toggle display of line numbers with M-x linum-mode. To enable |
88073 | 29 ;; line numbering in all buffers, use M-x global-linum-mode. |
30 | |
31 ;;; Code: | |
32 | |
33 (defconst linum-version "0.9wx") | |
34 | |
35 (defvar linum-overlays nil "Overlays used in this buffer.") | |
36 (defvar linum-available nil "Overlays available for reuse.") | |
37 (defvar linum-before-numbering-hook nil | |
38 "Functions run in each buffer before line numbering starts.") | |
39 | |
40 (mapc #'make-variable-buffer-local '(linum-overlays linum-available)) | |
41 | |
42 (defgroup linum nil | |
43 "Show line numbers in the left margin." | |
44 :group 'convenience) | |
45 | |
46 ;;;###autoload | |
47 (defcustom linum-format 'dynamic | |
48 "Format used to display line numbers. | |
49 Either a format string like \"%7d\", `dynamic' to adapt the width | |
50 as needed, or a function that is called with a line number as its | |
51 argument and should evaluate to a string to be shown on that line. | |
52 See also `linum-before-numbering-hook'." | |
53 :group 'linum | |
54 :type 'sexp) | |
55 | |
56 (defface linum | |
57 '((t :inherit shadow)) | |
58 "Face for displaying line numbers in the display margin." | |
59 :group 'linum) | |
60 | |
61 (defcustom linum-eager t | |
62 "Whether line numbers should be updated after each command. | |
63 The conservative setting `nil' might miss some buffer changes, | |
64 and you have to scroll or press \\[recenter-top-bottom] to update the numbers." | |
65 :group 'linum | |
66 :type 'boolean) | |
67 | |
68 (defcustom linum-delay t | |
69 "Delay updates to give Emacs a chance for other changes." | |
70 :group 'linum | |
71 :type 'boolean) | |
72 | |
73 ;;;###autoload | |
74 (define-minor-mode linum-mode | |
75 "Toggle display of line numbers in the left margin." | |
76 :lighter "" ; for desktop.el | |
77 (if linum-mode | |
78 (progn | |
79 (if linum-eager | |
80 (add-hook 'post-command-hook (if linum-delay | |
81 'linum-schedule | |
82 'linum-update-current) nil t) | |
83 (add-hook 'after-change-functions 'linum-after-change nil t)) | |
84 (add-hook 'window-scroll-functions 'linum-after-scroll nil t) | |
85 ;; mistake in Emacs: window-size-change-functions cannot be local | |
86 (add-hook 'window-size-change-functions 'linum-after-size) | |
87 (add-hook 'change-major-mode-hook 'linum-delete-overlays nil t) | |
88 (add-hook 'window-configuration-change-hook | |
89 'linum-after-config nil t) | |
90 (linum-update-current)) | |
91 (remove-hook 'post-command-hook 'linum-update-current t) | |
92 (remove-hook 'post-command-hook 'linum-schedule t) | |
93 (remove-hook 'window-size-change-functions 'linum-after-size) | |
94 (remove-hook 'window-scroll-functions 'linum-after-scroll t) | |
95 (remove-hook 'after-change-functions 'linum-after-change t) | |
96 (remove-hook 'window-configuration-change-hook 'linum-after-config t) | |
97 (remove-hook 'change-major-mode-hook 'linum-delete-overlays t) | |
98 (linum-delete-overlays))) | |
99 | |
100 ;;;###autoload | |
101 (define-globalized-minor-mode global-linum-mode linum-mode linum-on) | |
102 | |
103 (defun linum-on () | |
104 (unless (minibufferp) | |
105 (linum-mode 1))) | |
106 | |
107 (defun linum-delete-overlays () | |
108 "Delete all overlays displaying line numbers for this buffer." | |
109 (mapc #'delete-overlay linum-overlays) | |
110 (setq linum-overlays nil) | |
111 (dolist (w (get-buffer-window-list (current-buffer) nil t)) | |
112 (set-window-margins w 0))) | |
113 | |
114 (defun linum-update-current () | |
115 "Update line numbers for the current buffer." | |
116 (linum-update (current-buffer))) | |
117 | |
118 (defun linum-update (buffer) | |
119 "Update line numbers for all windows displaying BUFFER." | |
120 (with-current-buffer buffer | |
121 (when linum-mode | |
122 (setq linum-available linum-overlays) | |
123 (setq linum-overlays nil) | |
124 (save-excursion | |
125 (mapc #'linum-update-window | |
126 (get-buffer-window-list buffer nil 'visible))) | |
127 (mapc #'delete-overlay linum-available) | |
128 (setq linum-available nil)))) | |
129 | |
130 (defun linum-update-window (win) | |
131 "Update line numbers for the portion visible in window WIN." | |
132 (goto-char (window-start win)) | |
133 (let ((line (line-number-at-pos)) | |
134 (limit (1+ (window-end win t))) | |
135 (fmt (cond ((stringp linum-format) linum-format) | |
136 ((eq linum-format 'dynamic) | |
137 (let ((w (length (number-to-string | |
138 (count-lines (point-min) (point-max)))))) | |
139 (concat "%" (number-to-string w) "d"))))) | |
140 (width 0) | |
141 visited | |
142 ov) | |
143 (run-hooks 'linum-before-numbering-hook) | |
144 ;; Create an overlay (or reuse an existing one) for each | |
145 ;; line visible in this window, if necessary. | |
146 (while (and (not (eobp)) (< (point) limit)) | |
147 (setq visited nil) | |
148 (dolist (o (overlays-in (point) (point))) | |
149 (when (eq (overlay-get o 'linum-line) line) | |
150 (unless (memq o linum-overlays) | |
151 (push o linum-overlays)) | |
152 (setq linum-available (delete o linum-available)) | |
153 (setq visited t))) | |
154 (let ((str (if fmt | |
155 (propertize (format fmt line) 'face 'linum) | |
156 (funcall linum-format line)))) | |
157 (setq width (max width (length str))) | |
158 (unless visited | |
159 (if (null linum-available) | |
160 (setq ov (make-overlay (point) (point))) | |
161 (setq ov (pop linum-available)) | |
162 (move-overlay ov (point) (point))) | |
163 (push ov linum-overlays) | |
164 (setq str (propertize " " 'display `((margin left-margin) ,str))) | |
165 (overlay-put ov 'before-string str) | |
166 (overlay-put ov 'linum-line line))) | |
167 (forward-line) | |
168 (setq line (1+ line))) | |
169 (set-window-margins win width))) | |
170 | |
171 (defun linum-after-change (beg end len) | |
172 ;; update overlays on deletions, and after newlines are inserted | |
173 (when (or (= beg end) | |
174 (= end (point-max)) | |
175 (string-match-p "\n" (buffer-substring-no-properties beg end))) | |
176 (linum-update-current))) | |
177 | |
178 (defun linum-after-scroll (win start) | |
179 (linum-update (window-buffer win))) | |
180 | |
181 (defun linum-after-size (frame) | |
182 (linum-after-config)) | |
183 | |
184 (defun linum-schedule () | |
185 ;; schedule an update; the delay gives Emacs a chance for display changes | |
186 (run-with-idle-timer 0 nil #'linum-update-current)) | |
187 | |
188 (defun linum-after-config () | |
92141
87d23b09ce4d
(linum-after-config): Update all visible windows.
Juanma Barranquero <lekktu@gmail.com>
parents:
88090
diff
changeset
|
189 (walk-windows (lambda (w) (linum-update (window-buffer w))) nil 'visible)) |
88073 | 190 |
88090
b796e645bb4e
(linum-unload-function): New function.
Juanma Barranquero <lekktu@gmail.com>
parents:
88076
diff
changeset
|
191 (defun linum-unload-function () |
b796e645bb4e
(linum-unload-function): New function.
Juanma Barranquero <lekktu@gmail.com>
parents:
88076
diff
changeset
|
192 "Unload the Linum library." |
b796e645bb4e
(linum-unload-function): New function.
Juanma Barranquero <lekktu@gmail.com>
parents:
88076
diff
changeset
|
193 (global-linum-mode -1) |
b796e645bb4e
(linum-unload-function): New function.
Juanma Barranquero <lekktu@gmail.com>
parents:
88076
diff
changeset
|
194 ;; continue standard unloading |
b796e645bb4e
(linum-unload-function): New function.
Juanma Barranquero <lekktu@gmail.com>
parents:
88076
diff
changeset
|
195 nil) |
b796e645bb4e
(linum-unload-function): New function.
Juanma Barranquero <lekktu@gmail.com>
parents:
88076
diff
changeset
|
196 |
88073 | 197 (provide 'linum) |
198 | |
88076 | 199 ;; arch-tag: dea45631-ed3c-4867-8b49-1c41c80aec6a |
88073 | 200 ;;; linum.el ends here |