Mercurial > emacs
annotate lisp/linum.el @ 112450:16ddab338c43
Remove HAVE_RAW_DECL_CHOWN etc. from config.h
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sun, 23 Jan 2011 20:53:39 -0800 |
parents | ef719132ddfa |
children |
rev | line source |
---|---|
97899 | 1 ;;; linum.el --- display line numbers in the left margin |
2 | |
112218
376148b31b5e
Add 2011 to FSF/AIST copyright years.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
3 ;; Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
97899 | 4 |
5 ;; Author: Markus Triska <markus.triska@gmx.at> | |
6 ;; Maintainer: FSF | |
7 ;; Keywords: convenience | |
110016
a3e1f7134e6e
Add "Version:" and "Package:" Lisp file headers.
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
8 ;; Version: 0.9x |
97899 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software: you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation, either version 3 of the License, or | |
15 ;; (at your option) any later version. | |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; Display line numbers for the current buffer. | |
28 ;; | |
29 ;; Toggle display of line numbers with M-x linum-mode. To enable | |
30 ;; line numbering in all buffers, use M-x global-linum-mode. | |
31 | |
32 ;;; Code: | |
33 | |
34 (defconst linum-version "0.9x") | |
35 | |
36 (defvar linum-overlays nil "Overlays used in this buffer.") | |
37 (defvar linum-available nil "Overlays available for reuse.") | |
38 (defvar linum-before-numbering-hook nil | |
39 "Functions run in each buffer before line numbering starts.") | |
40 | |
41 (mapc #'make-variable-buffer-local '(linum-overlays linum-available)) | |
42 | |
43 (defgroup linum nil | |
44 "Show line numbers in the left margin." | |
45 :group 'convenience) | |
46 | |
47 ;;;###autoload | |
48 (defcustom linum-format 'dynamic | |
49 "Format used to display line numbers. | |
50 Either a format string like \"%7d\", `dynamic' to adapt the width | |
51 as needed, or a function that is called with a line number as its | |
52 argument and should evaluate to a string to be shown on that line. | |
53 See also `linum-before-numbering-hook'." | |
54 :group 'linum | |
55 :type 'sexp) | |
56 | |
57 (defface linum | |
58 '((t :inherit (shadow default))) | |
59 "Face for displaying line numbers in the display margin." | |
60 :group 'linum) | |
61 | |
62 (defcustom linum-eager t | |
63 "Whether line numbers should be updated after each command. | |
64 The conservative setting `nil' might miss some buffer changes, | |
65 and you have to scroll or press \\[recenter-top-bottom] to update the numbers." | |
66 :group 'linum | |
67 :type 'boolean) | |
68 | |
69 (defcustom linum-delay nil | |
70 "Delay updates to give Emacs a chance for other changes." | |
71 :group 'linum | |
72 :type 'boolean) | |
73 | |
74 ;;;###autoload | |
75 (define-minor-mode linum-mode | |
76 "Toggle display of line numbers in the left margin." | |
77 :lighter "" ; for desktop.el | |
78 (if linum-mode | |
79 (progn | |
80 (if linum-eager | |
81 (add-hook 'post-command-hook (if linum-delay | |
82 'linum-schedule | |
83 'linum-update-current) nil t) | |
84 (add-hook 'after-change-functions 'linum-after-change nil t)) | |
85 (add-hook 'window-scroll-functions 'linum-after-scroll nil t) | |
104365
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
86 ;; Using both window-size-change-functions and |
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
87 ;; window-configuration-change-hook seems redundant. --Stef |
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
88 ;; (add-hook 'window-size-change-functions 'linum-after-size nil t) |
97899 | 89 (add-hook 'change-major-mode-hook 'linum-delete-overlays nil t) |
90 (add-hook 'window-configuration-change-hook | |
104365
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
91 ;; FIXME: If the buffer is shown in N windows, this |
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
92 ;; will be called N times rather than once. We should use |
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
93 ;; something like linum-update-window instead. |
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
94 'linum-update-current nil t) |
97899 | 95 (linum-update-current)) |
96 (remove-hook 'post-command-hook 'linum-update-current t) | |
97 (remove-hook 'post-command-hook 'linum-schedule t) | |
104365
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
98 ;; (remove-hook 'window-size-change-functions 'linum-after-size t) |
97899 | 99 (remove-hook 'window-scroll-functions 'linum-after-scroll t) |
100 (remove-hook 'after-change-functions 'linum-after-change t) | |
104365
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
101 (remove-hook 'window-configuration-change-hook 'linum-update-current t) |
97899 | 102 (remove-hook 'change-major-mode-hook 'linum-delete-overlays t) |
103 (linum-delete-overlays))) | |
104 | |
105 ;;;###autoload | |
106 (define-globalized-minor-mode global-linum-mode linum-mode linum-on) | |
107 | |
108 (defun linum-on () | |
109 (unless (minibufferp) | |
110 (linum-mode 1))) | |
111 | |
112 (defun linum-delete-overlays () | |
113 "Delete all overlays displaying line numbers for this buffer." | |
114 (mapc #'delete-overlay linum-overlays) | |
115 (setq linum-overlays nil) | |
116 (dolist (w (get-buffer-window-list (current-buffer) nil t)) | |
105135
708c5f10a0be
* linum.el (linum-delete-overlays, linum-update-window):
Juanma Barranquero <lekktu@gmail.com>
parents:
104365
diff
changeset
|
117 (set-window-margins w 0 (cdr (window-margins w))))) |
97899 | 118 |
119 (defun linum-update-current () | |
120 "Update line numbers for the current buffer." | |
121 (linum-update (current-buffer))) | |
122 | |
123 (defun linum-update (buffer) | |
124 "Update line numbers for all windows displaying BUFFER." | |
125 (with-current-buffer buffer | |
126 (when linum-mode | |
127 (setq linum-available linum-overlays) | |
128 (setq linum-overlays nil) | |
129 (save-excursion | |
130 (mapc #'linum-update-window | |
131 (get-buffer-window-list buffer nil 'visible))) | |
132 (mapc #'delete-overlay linum-available) | |
133 (setq linum-available nil)))) | |
134 | |
135 (defun linum-update-window (win) | |
136 "Update line numbers for the portion visible in window WIN." | |
137 (goto-char (window-start win)) | |
138 (let ((line (line-number-at-pos)) | |
139 (limit (window-end win t)) | |
140 (fmt (cond ((stringp linum-format) linum-format) | |
141 ((eq linum-format 'dynamic) | |
142 (let ((w (length (number-to-string | |
143 (count-lines (point-min) (point-max)))))) | |
144 (concat "%" (number-to-string w) "d"))))) | |
145 (width 0)) | |
146 (run-hooks 'linum-before-numbering-hook) | |
147 ;; Create an overlay (or reuse an existing one) for each | |
148 ;; line visible in this window, if necessary. | |
149 (while (and (not (eobp)) (<= (point) limit)) | |
150 (let* ((str (if fmt | |
151 (propertize (format fmt line) 'face 'linum) | |
152 (funcall linum-format line))) | |
153 (visited (catch 'visited | |
154 (dolist (o (overlays-in (point) (point))) | |
101274
4bcf5c96d5ee
* linum.el (linum-update-window): Use `delq' instead of `delete';
Juanma Barranquero <lekktu@gmail.com>
parents:
100908
diff
changeset
|
155 (when (equal-including-properties |
4bcf5c96d5ee
* linum.el (linum-update-window): Use `delq' instead of `delete';
Juanma Barranquero <lekktu@gmail.com>
parents:
100908
diff
changeset
|
156 (overlay-get o 'linum-str) str) |
97899 | 157 (unless (memq o linum-overlays) |
158 (push o linum-overlays)) | |
101274
4bcf5c96d5ee
* linum.el (linum-update-window): Use `delq' instead of `delete';
Juanma Barranquero <lekktu@gmail.com>
parents:
100908
diff
changeset
|
159 (setq linum-available (delq o linum-available)) |
97899 | 160 (throw 'visited t)))))) |
161 (setq width (max width (length str))) | |
162 (unless visited | |
163 (let ((ov (if (null linum-available) | |
164 (make-overlay (point) (point)) | |
165 (move-overlay (pop linum-available) (point) (point))))) | |
166 (push ov linum-overlays) | |
167 (overlay-put ov 'before-string | |
168 (propertize " " 'display `((margin left-margin) ,str))) | |
169 (overlay-put ov 'linum-str str)))) | |
106225
5418676a97db
(linum-update-window): Ignore intangible (bug#4996).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105135
diff
changeset
|
170 ;; Text may contain those nasty intangible properties, but that |
5418676a97db
(linum-update-window): Ignore intangible (bug#4996).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105135
diff
changeset
|
171 ;; shouldn't prevent us from counting those lines. |
5418676a97db
(linum-update-window): Ignore intangible (bug#4996).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105135
diff
changeset
|
172 (let ((inhibit-point-motion-hooks t)) |
5418676a97db
(linum-update-window): Ignore intangible (bug#4996).
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105135
diff
changeset
|
173 (forward-line)) |
97899 | 174 (setq line (1+ line))) |
105135
708c5f10a0be
* linum.el (linum-delete-overlays, linum-update-window):
Juanma Barranquero <lekktu@gmail.com>
parents:
104365
diff
changeset
|
175 (set-window-margins win width (cdr (window-margins win))))) |
97899 | 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 | |
104365
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
187 ;; (defun linum-after-size (frame) |
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
188 ;; (linum-after-config)) |
97899 | 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 | |
104365
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
194 ;; (defun linum-after-config () |
fe22bf79af7f
(linum-mode): window-size-change-functions is redundant.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
101274
diff
changeset
|
195 ;; (walk-windows (lambda (w) (linum-update (window-buffer w))) nil 'visible)) |
97899 | 196 |
197 (defun linum-unload-function () | |
198 "Unload the Linum library." | |
199 (global-linum-mode -1) | |
200 ;; continue standard unloading | |
201 nil) | |
202 | |
203 (provide 'linum) | |
204 | |
205 ;;; linum.el ends here |