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