linum.el 8.0 KB

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