display-line-numbers.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ;;; display-line-numbers.el --- interface for display-line-numbers -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2017 Free Software Foundation, Inc.
  3. ;; Maintainer: emacs-devel@gnu.org
  4. ;; Keywords: convenience
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Provides a minor mode interface for `display-line-numbers'.
  18. ;;
  19. ;; Toggle display of line numbers with M-x display-line-numbers-mode.
  20. ;; To enable line numbering in all buffers, use M-x
  21. ;; global-display-line-numbers-mode. To change the default type of
  22. ;; line numbers displayed, customize display-line-numbers-type.
  23. ;; NOTE: Customization variables for `display-line-numbers' itself are
  24. ;; defined in cus-start.el.
  25. ;;; Code:
  26. (defgroup display-line-numbers nil
  27. "Display line numbers in the buffer."
  28. :group 'display)
  29. (defcustom display-line-numbers-type t
  30. "The default type of line numbers to use in `display-line-numbers-mode'.
  31. See `display-line-numbers' for value options."
  32. :group 'display-line-numbers
  33. :type '(choice (const :tag "Relative line numbers" relative)
  34. (const :tag "Relative visual line numbers" visual)
  35. (other :tag "Absolute line numbers" t))
  36. :version "26.1")
  37. (defcustom display-line-numbers-grow-only nil
  38. "If non-nil, do not shrink line number width."
  39. :group 'display-line-numbers
  40. :type 'boolean
  41. :version "26.1")
  42. (defcustom display-line-numbers-width-start nil
  43. "If non-nil, count number of lines to use for line number width.
  44. When `display-line-numbers-mode' is turned on,
  45. `display-line-numbers-width' is set to the minimum width necessary
  46. to display all line numbers in the buffer."
  47. :group 'display-line-numbers
  48. :type 'boolean
  49. :version "26.1")
  50. (defun display-line-numbers-update-width ()
  51. "Prevent the line number width from shrinking."
  52. (let ((width (line-number-display-width)))
  53. (when (> width (or display-line-numbers-width 1))
  54. (setq display-line-numbers-width width))))
  55. ;;;###autoload
  56. (define-minor-mode display-line-numbers-mode
  57. "Toggle display of line numbers in the buffer.
  58. This uses `display-line-numbers' internally.
  59. To change the type of line numbers displayed by default,
  60. customize `display-line-numbers-type'. To change the type while
  61. the mode is on, set `display-line-numbers' directly."
  62. :lighter nil
  63. (if display-line-numbers-mode
  64. (progn
  65. (when display-line-numbers-width-start
  66. (setq display-line-numbers-width
  67. (length (number-to-string
  68. (count-lines (point-min) (point-max))))))
  69. (when display-line-numbers-grow-only
  70. (add-hook 'pre-command-hook #'display-line-numbers-update-width nil t))
  71. (setq display-line-numbers display-line-numbers-type))
  72. (remove-hook 'pre-command-hook #'display-line-numbers-update-width t)
  73. (setq display-line-numbers nil)))
  74. (defun display-line-numbers--turn-on ()
  75. "Turn on `display-line-numbers-mode'."
  76. (unless (or (minibufferp)
  77. ;; taken from linum.el
  78. (and (daemonp) (null (frame-parameter nil 'client))))
  79. (display-line-numbers-mode)))
  80. ;;;###autoload
  81. (define-globalized-minor-mode global-display-line-numbers-mode
  82. display-line-numbers-mode display-line-numbers--turn-on)
  83. (provide 'display-line-numbers)
  84. ;;; display-line-numbers.el ends here