Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / linum.el
blob6e673e58b09789e83b0b1edcdee9f64323f7b284
1 ;;; linum.el --- display line numbers in the left margin -*- lexical-binding: t -*-
3 ;; Copyright (C) 2008-2018 Free Software Foundation, Inc.
5 ;; Author: Markus Triska <markus.triska@gmx.at>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: convenience
8 ;; Version: 0.9x
10 ;; This file is part of GNU Emacs.
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.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Display line numbers for the current buffer.
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.
32 ;;; Code:
34 (defconst linum-version "0.9x")
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.")
41 (mapc #'make-variable-buffer-local '(linum-overlays linum-available))
43 (defgroup linum nil
44 "Show line numbers in the left margin."
45 :group 'convenience)
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 '(choice (string :tag "Format string")
55 (const :tag "Dynamic width" dynamic)
56 (function :tag "Function")))
58 (defface linum
59 '((t :inherit (shadow default)))
60 "Face for displaying line numbers in the display margin."
61 :group 'linum)
63 (defcustom linum-eager t
64 "Whether line numbers should be updated after each command.
65 The conservative setting nil might miss some buffer changes,
66 and you have to scroll or press \\[recenter-top-bottom] to update the numbers."
67 :group 'linum
68 :type 'boolean)
70 (defcustom linum-delay nil
71 "Delay updates to give Emacs a chance for other changes."
72 :group 'linum
73 :type 'boolean)
75 ;;;###autoload
76 (define-minor-mode linum-mode
77 "Toggle display of line numbers in the left margin (Linum mode).
79 Linum mode is a buffer-local minor mode."
80 :lighter "" ; for desktop.el
81 :append-arg-docstring t
82 (if linum-mode
83 (progn
84 (if linum-eager
85 (add-hook 'post-command-hook (if linum-delay
86 'linum-schedule
87 'linum-update-current) nil t)
88 (add-hook 'after-change-functions 'linum-after-change nil t))
89 (add-hook 'window-scroll-functions 'linum-after-scroll nil t)
90 ;; Using both window-size-change-functions and
91 ;; window-configuration-change-hook seems redundant. --Stef
92 ;; (add-hook 'window-size-change-functions 'linum-after-size nil t)
93 (add-hook 'change-major-mode-hook 'linum-delete-overlays nil t)
94 (add-hook 'window-configuration-change-hook
95 ;; FIXME: If the buffer is shown in N windows, this
96 ;; will be called N times rather than once. We should use
97 ;; something like linum-update-window instead.
98 'linum-update-current nil t)
99 (linum-update-current))
100 (remove-hook 'post-command-hook 'linum-update-current t)
101 (remove-hook 'post-command-hook 'linum-schedule t)
102 ;; (remove-hook 'window-size-change-functions 'linum-after-size t)
103 (remove-hook 'window-scroll-functions 'linum-after-scroll t)
104 (remove-hook 'after-change-functions 'linum-after-change t)
105 (remove-hook 'window-configuration-change-hook 'linum-update-current t)
106 (remove-hook 'change-major-mode-hook 'linum-delete-overlays t)
107 (linum-delete-overlays)))
109 ;;;###autoload
110 (define-globalized-minor-mode global-linum-mode linum-mode linum-on)
112 (defun linum-on ()
113 (unless (or (minibufferp)
114 ;; Turning linum-mode in the daemon's initial frame
115 ;; could significantly slow down startup, if the buffer
116 ;; in which this is done is large, because Emacs thinks
117 ;; the "window" spans the entire buffer then. This
118 ;; could happen when restoring session via desktop.el,
119 ;; if some large buffer was under linum-mode when
120 ;; desktop was saved. So we disable linum-mode for
121 ;; non-client frames in a daemon session.
122 (and (daemonp) (null (frame-parameter nil 'client))))
123 (linum-mode 1)))
125 (defun linum-delete-overlays ()
126 "Delete all overlays displaying line numbers for this buffer."
127 (mapc #'delete-overlay linum-overlays)
128 (setq linum-overlays nil)
129 (dolist (w (get-buffer-window-list (current-buffer) nil t))
130 ;; restore margins if needed FIXME: This still fails if the
131 ;; "other" mode has incidentally set margins to exactly what linum
132 ;; had: see bug#20674 for a similar workaround in nlinum.el
133 (let ((set-margins (window-parameter w 'linum--set-margins))
134 (current-margins (window-margins w)))
135 (when (and set-margins
136 (equal set-margins current-margins))
137 (set-window-margins w 0 (cdr current-margins))
138 (set-window-parameter w 'linum--set-margins nil)))))
140 (defun linum-update-current ()
141 "Update line numbers for the current buffer."
142 (linum-update (current-buffer)))
144 (defun linum-update (buffer)
145 "Update line numbers for all windows displaying BUFFER."
146 (with-current-buffer buffer
147 (when linum-mode
148 (setq linum-available linum-overlays)
149 (setq linum-overlays nil)
150 (save-excursion
151 (mapc #'linum-update-window
152 (get-buffer-window-list buffer nil 'visible)))
153 (mapc #'delete-overlay linum-available)
154 (setq linum-available nil))))
156 ;; Behind display-graphic-p test.
157 (declare-function font-info "font.c" (name &optional frame))
159 (defun linum--face-width (face)
160 (let ((info (font-info (face-font face)))
161 width)
162 (setq width (aref info 11))
163 (if (<= width 0)
164 (setq width (aref info 10)))
165 width))
167 (defun linum-update-window (win)
168 "Update line numbers for the portion visible in window WIN."
169 (goto-char (window-start win))
170 (let ((line (line-number-at-pos))
171 (limit (window-end win t))
172 (fmt (cond ((stringp linum-format) linum-format)
173 ((eq linum-format 'dynamic)
174 (let ((w (length (number-to-string
175 (count-lines (point-min) (point-max))))))
176 (concat "%" (number-to-string w) "d")))))
177 (width 0))
178 (run-hooks 'linum-before-numbering-hook)
179 ;; Create an overlay (or reuse an existing one) for each
180 ;; line visible in this window, if necessary.
181 (while (and (not (eobp)) (< (point) limit))
182 (let* ((str (if fmt
183 (propertize (format fmt line) 'face 'linum)
184 (funcall linum-format line)))
185 (visited (catch 'visited
186 (dolist (o (overlays-in (point) (point)))
187 (when (equal-including-properties
188 (overlay-get o 'linum-str) str)
189 (unless (memq o linum-overlays)
190 (push o linum-overlays))
191 (setq linum-available (delq o linum-available))
192 (throw 'visited t))))))
193 (setq width (max width (length str)))
194 (unless visited
195 (let ((ov (if (null linum-available)
196 (make-overlay (point) (point))
197 (move-overlay (pop linum-available) (point) (point)))))
198 (push ov linum-overlays)
199 (overlay-put ov 'before-string
200 (propertize " " 'display `((margin left-margin) ,str)))
201 (overlay-put ov 'linum-str str))))
202 ;; Text may contain those nasty intangible properties, but that
203 ;; shouldn't prevent us from counting those lines.
204 (let ((inhibit-point-motion-hooks t))
205 (forward-line))
206 (setq line (1+ line)))
207 (when (display-graphic-p)
208 (setq width (ceiling
209 (/ (* width 1.0 (linum--face-width 'linum))
210 (frame-char-width)))))
211 ;; open up space in the left margin, if needed, and record that
212 ;; fact as the window-parameter `linum--set-margins'
213 (let ((existing-margins (window-margins win)))
214 (when (> width (or (car existing-margins) 0))
215 (set-window-margins win width (cdr existing-margins))
216 (set-window-parameter win 'linum--set-margins (window-margins win))))))
218 (defun linum-after-change (beg end _len)
219 ;; update overlays on deletions, and after newlines are inserted
220 (when (or (= beg end)
221 (= end (point-max))
222 (string-match-p "\n" (buffer-substring-no-properties beg end)))
223 (linum-update-current)))
225 (defun linum-after-scroll (win _start)
226 (linum-update (window-buffer win)))
228 ;; (defun linum-after-size (frame)
229 ;; (linum-after-config))
231 (defun linum-schedule ()
232 ;; schedule an update; the delay gives Emacs a chance for display changes
233 (run-with-idle-timer 0 nil #'linum-update-current))
235 ;; (defun linum-after-config ()
236 ;; (walk-windows (lambda (w) (linum-update (window-buffer w))) nil 'visible))
238 (defun linum-unload-function ()
239 "Unload the Linum library."
240 (global-linum-mode -1)
241 ;; continue standard unloading
242 nil)
244 (provide 'linum)
246 ;;; linum.el ends here