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