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