Download
(require 'timer)
(defconst linum-version "0.991")
(defvar linum-overlays nil "Overlays used in this buffer.")
(defvar linum-available nil "Overlays available for reuse.")
(defvar linum-before-numbering-hook nil
"Functions run in each buffer before line numbering starts.")
(mapc #'make-variable-buffer-local '(linum-overlays linum-available))
(defgroup linum nil
"Show line numbers to the left of buffers"
:group 'convenience)
(defcustom linum-format 'dynamic
"Format used to display line numbers. Either a format string
like \"%7d\", 'dynamic to adapt the width as needed, or a
function that is called with a line number as its argument and
should evaluate to a string to be shown on that line. See also
`linum-before-numbering-hook'."
:group 'linum
:type 'sexp)
(defface linum
'((t :inherit (shadow default)))
"Face for displaying line numbers in the display margin."
:group 'linum)
(defcustom linum-eager t
"Whether line numbers should be updated after each command.
The conservative setting `nil' might miss some buffer changes,
and you have to scroll or press C-l to update the numbers."
:group 'linum
:type 'boolean)
(defcustom linum-delay nil
"Delay updates to give Emacs a chance for other changes."
:group 'linum
:type 'boolean)
(defvar linum--delay-time 0.1
"Delay time. See also `linum-delay'")
(defvar linum--last-scroll nil
"Time of last scroll event. See also `linum-delay'")
(defvar linum--last-cmd nil
"Time of last command. See also `linum-delay'")
(defvar linum--win nil
"Window of the last scroll event. See also `linum-delay'")
(define-minor-mode linum-mode
"Toggle display of line numbers in the left marginal area."
:lighter ""
(if linum-mode
(progn
(if linum-eager
(add-hook 'post-command-hook 'linum-post-command)
(add-hook 'after-change-functions 'linum-after-change nil t))
(add-hook 'window-scroll-functions 'linum-after-scroll nil t)
(add-hook 'window-size-change-functions 'linum-after-size)
(add-hook 'change-major-mode-hook 'linum-delete-overlays nil t)
(add-hook 'window-configuration-change-hook
'linum-after-config nil t)
(set (make-local-variable 'linum--win) nil)
(set (make-local-variable 'linum--last-scroll) nil)
(set (make-local-variable 'linum--last-cmd) nil)
(linum-update-current))
(remove-hook 'post-command-hook 'linum-post-command t)
(remove-hook 'window-size-change-functions 'linum-after-size)
(remove-hook 'window-scroll-functions 'linum-after-scroll t)
(remove-hook 'after-change-functions 'linum-after-change t)
(remove-hook 'window-configuration-change-hook 'linum-after-config t)
(remove-hook 'change-major-mode-hook 'linum-delete-overlays t)
(linum-delete-overlays)))
(define-globalized-minor-mode global-linum-mode linum-mode linum-on)
(defun linum-on ()
(unless (minibufferp)
(linum-mode 1)))
(defun linum-delete-overlays ()
"Delete all overlays displaying line numbers for this buffer."
(mapc #'delete-overlay linum-overlays)
(setq linum-overlays nil)
(dolist (w (get-buffer-window-list (current-buffer) nil t))
(set-window-margins w 0)))
(defun linum-update-current ()
"Update line numbers for the current buffer."
(linum-update (current-buffer)))
(defun linum-update (buffer)
"Update line numbers for all windows displaying BUFFER."
(with-current-buffer buffer
(when linum-mode
(setq linum-available linum-overlays)
(setq linum-overlays nil)
(save-excursion
(mapc #'linum-update-window
(get-buffer-window-list buffer nil 'visible)))
(mapc #'delete-overlay linum-available)
(setq linum-available nil
linum--last-cmd nil
linum--last-scroll nil))))
(defun linum-update-window (win)
"Update line numbers for the portion visible in window WIN."
(goto-char (window-start win))
(let ((line (line-number-at-pos))
(limit (window-end win t))
(fmt (cond ((stringp linum-format) linum-format)
((eq linum-format 'dynamic)
(let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
(concat "%" (number-to-string w) "d")))))
(width 0))
(run-hooks 'linum-before-numbering-hook)
(while (and (not (eobp)) (<= (point) limit))
(let* ((str (if fmt
(propertize (format fmt line) 'face 'linum)
(funcall linum-format line)))
(visited (catch 'visited
(dolist (o (overlays-in (point) (point)))
(when (string= (overlay-get o 'linum-str) str)
(unless (memq o linum-overlays)
(push o linum-overlays))
(setq linum-available (delete o linum-available))
(throw 'visited t))))))
(setq width (max width (length str)))
(unless visited
(let ((ov (if (null linum-available)
(make-overlay (point) (point))
(move-overlay (pop linum-available) (point) (point)))))
(push ov linum-overlays)
(overlay-put ov 'before-string
(propertize " " 'display `((margin left-margin) ,str)))
(overlay-put ov 'linum-str str))))
(forward-line)
(setq line (1+ line)))
(set-window-margins win width)))
(defun linum-after-change (beg end len)
(when (or (= beg end)
(= end (point-max))
(string-match "\n" (buffer-substring-no-properties beg end)))
(linum-update-current)))
(defun linum--after-scroll-fired ()
(if linum--last-scroll
(let ((now (current-time))
(one-moment-after-scroll (timer-relative-time linum--last-scroll linum--delay-time)))
(if (time-less-p one-moment-after-scroll now)
(linum-update linum--win)))))
(defun linum-after-scroll (win start)
(if linum-delay
(progn
(setq linum--win (window-buffer win))
(setq linum--last-scroll (current-time))
(run-with-idle-timer linum--delay-time nil 'linum--after-scroll-fired))
(linum-update (window-buffer win))))
(defun linum--post-command-fired ()
(if linum--last-cmd
(let ((now (current-time))
(one-moment-after-cmd (timer-relative-time linum--last-cmd linum--delay-time)))
(if (time-less-p one-moment-after-cmd now)
(linum-update-current)))))
(defun linum-post-command ()
(if linum-delay
(progn
(setq linum--win (window-buffer win))
(setq linum--last-cmd (current-time))
(run-with-idle-timer linum--delay-time nil 'linum--post-command-fired))
(linum-update-current)))
(defun linum-after-size (frame)
(linum-after-config))
(defun linum-after-config ()
(walk-windows (lambda (w) (linum-update (window-buffer w))) nil 'visible))
(provide 'linum-ex)