Merge branch 'master' of git.savannah.gnu.org:/srv/git/emacs
[emacs.git] / lisp / display-line-numbers.el
blobc9dd28a40fa5940d8b373e894a9f36397dd84de6
1 ;;; display-line-numbers.el --- interface for display-line-numbers -*- lexical-binding: t -*-
3 ;; Copyright (C) 2017 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 <http://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 'display)
41 (defcustom display-line-numbers-type t
42 "The default type of line numbers to use in `display-line-numbers-mode'.
43 See `display-line-numbers' for value options."
44 :group 'display-line-numbers
45 :type '(choice (const :tag "Relative line numbers" relative)
46 (const :tag "Relative visual line numbers" visual)
47 (other :tag "Absolute line numbers" t))
48 :version "26.1")
50 (defcustom display-line-numbers-grow-only nil
51 "If non-nil, do not shrink line number width."
52 :group 'display-line-numbers
53 :type 'boolean
54 :version "26.1")
56 (defcustom display-line-numbers-width-start nil
57 "If non-nil, count number of lines to use for line number width.
58 When `display-line-numbers-mode' is turned on,
59 `display-line-numbers-width' is set to the minimum width necessary
60 to display all line numbers in the buffer."
61 :group 'display-line-numbers
62 :type 'boolean
63 :version "26.1")
65 (defun display-line-numbers-update-width ()
66 "Prevent the line number width from shrinking."
67 (let ((width (line-number-display-width)))
68 (when (> width (or display-line-numbers-width 1))
69 (setq display-line-numbers-width width))))
71 ;;;###autoload
72 (define-minor-mode display-line-numbers-mode
73 "Toggle display of line numbers in the buffer.
74 This uses `display-line-numbers' internally.
76 To change the type of line numbers displayed by default,
77 customize `display-line-numbers-type'. To change the type while
78 the mode is on, set `display-line-numbers' directly."
79 :lighter nil
80 (if display-line-numbers-mode
81 (progn
82 (when display-line-numbers-width-start
83 (setq display-line-numbers-width
84 (length (number-to-string
85 (count-lines (point-min) (point-max))))))
86 (when display-line-numbers-grow-only
87 (add-hook 'pre-command-hook #'display-line-numbers-update-width nil t))
88 (setq display-line-numbers display-line-numbers-type))
89 (remove-hook 'pre-command-hook #'display-line-numbers-update-width t)
90 (setq display-line-numbers nil)))
92 (defun display-line-numbers--turn-on ()
93 "Turn on `display-line-numbers-mode'."
94 (unless (or (minibufferp)
95 ;; taken from linum.el
96 (and (daemonp) (null (frame-parameter nil 'client))))
97 (display-line-numbers-mode)))
99 ;;;###autoload
100 (define-globalized-minor-mode global-display-line-numbers-mode
101 display-line-numbers-mode display-line-numbers--turn-on)
103 (provide 'display-line-numbers)
105 ;;; display-line-numbers.el ends here