aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Gramiak2017-07-22 12:16:08 +0300
committerEli Zaretskii2017-07-22 12:16:08 +0300
commitebb78a7bfa3e6a87cfb53f1f2b17fc2f61add595 (patch)
tree4a5e32ce7751b92194e349c5fa76489fd1ce0b6a
parent6d7e34b692edd61ac2f0872db521bcec321453cf (diff)
downloademacs-ebb78a7bfa3e6a87cfb53f1f2b17fc2f61add595.tar.gz
emacs-ebb78a7bfa3e6a87cfb53f1f2b17fc2f61add595.zip
Add a minor mode interface for display-line-numbers
* lisp/cus-start.el: Use the new display-line-numbers group. * lisp/display-line-numbers.el: New file. * doc/emacs/custom.texi (Init Rebinding): Re-add entry that used to belong to linum-mode. * doc/emacs/modes.texi (Minor Modes): Summarize the mode. * etc/NEWS: Document display-line-numbers-mode and its customization variables, and mention that display-line-numbers-width is buffer-local. * src/xdisp.c (syms_of_xdisp) <display-line-numbers-width>: Fix a typo.
-rw-r--r--doc/emacs/custom.texi1
-rw-r--r--doc/emacs/modes.texi7
-rw-r--r--etc/NEWS44
-rw-r--r--lisp/cus-start.el8
-rw-r--r--lisp/display-line-numbers.el103
-rw-r--r--src/xdisp.c2
6 files changed, 145 insertions, 20 deletions
diff --git a/doc/emacs/custom.texi b/doc/emacs/custom.texi
index a756a89e3f8..1c9c14a962a 100644
--- a/doc/emacs/custom.texi
+++ b/doc/emacs/custom.texi
@@ -1701,6 +1701,7 @@ and mouse events:
1701(global-set-key (kbd "C-c y") 'clipboard-yank) 1701(global-set-key (kbd "C-c y") 'clipboard-yank)
1702(global-set-key (kbd "C-M-q") 'query-replace) 1702(global-set-key (kbd "C-M-q") 'query-replace)
1703(global-set-key (kbd "<f5>") 'flyspell-mode) 1703(global-set-key (kbd "<f5>") 'flyspell-mode)
1704(global-set-key (kbd "C-<f5>") 'display-line-numbers-mode)
1704(global-set-key (kbd "C-<right>") 'forward-sentence) 1705(global-set-key (kbd "C-<right>") 'forward-sentence)
1705(global-set-key (kbd "<mouse-2>") 'mouse-save-then-kill) 1706(global-set-key (kbd "<mouse-2>") 'mouse-save-then-kill)
1706@end example 1707@end example
diff --git a/doc/emacs/modes.texi b/doc/emacs/modes.texi
index eb0c88b2901..876431aa9e9 100644
--- a/doc/emacs/modes.texi
+++ b/doc/emacs/modes.texi
@@ -225,6 +225,13 @@ Font-Lock mode automatically highlights certain textual units found in
225programs. It is enabled globally by default, but you can disable it 225programs. It is enabled globally by default, but you can disable it
226in individual buffers. @xref{Faces}. 226in individual buffers. @xref{Faces}.
227 227
228@findex display-line-numbers-mode
229@cindex display-line-numbers-mode
230@item
231Display Line Numbers mode is a convenience wrapper around
232@code{display-line-numbers}, setting it using the value of
233@code{display-line-numbers-type}. @xref{Display Custom}.
234
228@item 235@item
229Outline minor mode provides similar facilities to the major mode 236Outline minor mode provides similar facilities to the major mode
230called Outline mode. @xref{Outline Mode}. 237called Outline mode. @xref{Outline Mode}.
diff --git a/etc/NEWS b/etc/NEWS
index 04971544d36..50945f0cfa3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -438,13 +438,18 @@ range of indentation.
438This is similar to what linum-mode provides, but much faster and 438This is similar to what linum-mode provides, but much faster and
439doesn't usurp the display margin for the line numbers. Customize the 439doesn't usurp the display margin for the line numbers. Customize the
440buffer-local variable 'display-line-numbers' to activate this optional 440buffer-local variable 'display-line-numbers' to activate this optional
441display. If set to t, Emacs will display the number of each line 441display. Alternatively, you can use the `display-line-numbers-mode'
442before the line. If set to 'relative', Emacs will display the line 442minor mode or the global `global-display-line-numbers-mode'. When
443number relative to the line showing point, with that line's number 443using these modes, customize `display-line-numbers-type' with the same
444displayed as absolute. If set to 'visual', Emacs will display a 444value as you would use with `display-line-numbers'.
445relative number for every screen line, i.e. it will count screen lines 445
446rather than buffer lines. The default is nil, which doesn't display 446If `display-line-numbers' is set to t, Emacs will display the number
447the line numbers. 447of each line before the line. If set to 'relative', Emacs will
448display the line number relative to the line showing point, with that
449line's number displayed as absolute. If set to 'visual', Emacs will
450display a relative number for every screen line, i.e. it will count
451screen lines rather than buffer lines. The default is nil, which
452doesn't display the line numbers.
448 453
449In 'relative' and 'visual' modes, the variable 454In 'relative' and 'visual' modes, the variable
450'display-line-numbers-current-absolute' controls what number is 455'display-line-numbers-current-absolute' controls what number is
@@ -462,14 +467,23 @@ new face 'line-number-current-line' can be customized to display the
462current line's number differently from all the other line numbers; by 467current line's number differently from all the other line numbers; by
463default these two faces are identical. 468default these two faces are identical.
464 469
465You can also customize the new variable 'display-line-numbers-width' to 470You can also customize the new buffer-local variable
466specify a fixed minimal with of the area allocated to line-number 471'display-line-numbers-width' to specify a fixed minimal with of the
467display. The default is nil, meaning that Emacs will dynamically 472area allocated to line-number display. The default is nil, meaning
468calculate the area width, enlarging or shrinking it as needed. 473that Emacs will dynamically calculate the area width, enlarging or
469Setting it to a non-negative integer specifies that as the minimal 474shrinking it as needed. Setting it to a non-negative integer
470width; selecting a value that is large enough to display all line 475specifies that as the minimal width; selecting a value that is large
471numbers in a buffer will then keep the line-number display area of 476enough to display all line numbers in a buffer will then keep the
472constant width at all times, if that is desired. 477line-number display area of constant width at all times, if that is
478desired.
479
480When using `display-line-numbers-mode', you can customize the variable
481`display-line-numbers-grow-only' to a non-nil value; this means that
482Emacs may grow the above area width dynamically, but never shrink it.
483Under this mode, customizing the variable
484`display-line-numbers-width-start' to a non-nil value will cause Emacs
485to set `display-line-numbers-width' to the minimum width necessary to
486display all line numbers in the current buffer when first visiting it.
473 487
474Lisp programs can disable line-number display for a particular screen 488Lisp programs can disable line-number display for a particular screen
475line by putting the 'display-line-numbers-disable' text property or 489line by putting the 'display-line-numbers-disable' text property or
diff --git a/lisp/cus-start.el b/lisp/cus-start.el
index ed913e32688..c28b8a147fc 100644
--- a/lisp/cus-start.el
+++ b/lisp/cus-start.el
@@ -584,7 +584,7 @@ since it could result in memory overflow and make Emacs crash."
584 (const :tag "Grow only" :value grow-only)) 584 (const :tag "Grow only" :value grow-only))
585 "25.1") 585 "25.1")
586 (display-raw-bytes-as-hex display boolean "26.1") 586 (display-raw-bytes-as-hex display boolean "26.1")
587 (display-line-numbers display 587 (display-line-numbers display-line-numbers
588 (choice 588 (choice
589 (const :tag "Off (nil)" :value nil) 589 (const :tag "Off (nil)" :value nil)
590 (const :tag "Absolute line numbers" 590 (const :tag "Absolute line numbers"
@@ -594,7 +594,7 @@ since it could result in memory overflow and make Emacs crash."
594 (const :tag "Visually relative line numbers" 594 (const :tag "Visually relative line numbers"
595 :value visual)) 595 :value visual))
596 "26.1") 596 "26.1")
597 (display-line-numbers-width display 597 (display-line-numbers-width display-line-numbers
598 (choice 598 (choice
599 (const :tag "Dynamically computed" 599 (const :tag "Dynamically computed"
600 :value nil) 600 :value nil)
@@ -602,14 +602,14 @@ since it could result in memory overflow and make Emacs crash."
602 :value 2 602 :value 2
603 :format "%v")) 603 :format "%v"))
604 "26.1") 604 "26.1")
605 (display-line-numbers-current-absolute display 605 (display-line-numbers-current-absolute display-line-numbers
606 (choice 606 (choice
607 (const :tag "Display actual number of current line" 607 (const :tag "Display actual number of current line"
608 :value t) 608 :value t)
609 (const :tag "Display zero as number of current line" 609 (const :tag "Display zero as number of current line"
610 :value nil)) 610 :value nil))
611 "26.1") 611 "26.1")
612 (display-line-numbers-widen display 612 (display-line-numbers-widen display-line-numbers
613 (choice 613 (choice
614 (const :tag "Disregard narrowing when calculating line numbers" 614 (const :tag "Disregard narrowing when calculating line numbers"
615 :value t) 615 :value t)
diff --git a/lisp/display-line-numbers.el b/lisp/display-line-numbers.el
new file mode 100644
index 00000000000..95237250f93
--- /dev/null
+++ b/lisp/display-line-numbers.el
@@ -0,0 +1,103 @@
1;;; display-line-numbers.el --- interface for display-line-numbers -*- lexical-binding: t -*-
2
3;; Copyright (C) 2017 Free Software Foundation, Inc.
4
5;; Maintainer: emacs-devel@gnu.org
6;; Keywords: convenience
7
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23;;; Commentary:
24
25;; Provides a minor mode interface for `display-line-numbers'.
26;;
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.
31
32;; NOTE: Customization variables for `display-line-numbers' itself are
33;; defined in cus-start.el.
34
35;;; Code:
36
37(defgroup display-line-numbers nil
38 "Display line numbers in the buffer."
39 :group 'display)
40
41(defcustom display-line-numbers-type t
42 "The default type of line numbers to use in `display-line-numbers-mode'.
43See `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")
49
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")
55
56(defcustom display-line-numbers-width-start nil
57 "If non-nil, count number of lines to use for line number width.
58When `display-line-numbers-mode' is turned on,
59`display-line-numbers-width' is set to the minimum width necessary
60to display all line numbers in the buffer."
61 :group 'display-line-numbers
62 :type 'boolean
63 :version "26.1")
64
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))))
70
71;;;###autoload
72(define-minor-mode display-line-numbers-mode
73 "Toggle display of line numbers in the buffer.
74This uses `display-line-numbers' internally.
75
76To change the type of line numbers displayed by default,
77customize `display-line-numbers-type'. To change the type while
78the 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)))
91
92;;;###autoload
93(define-globalized-minor-mode global-display-line-numbers-mode
94 display-line-numbers-mode
95 (lambda ()
96 (unless (or (minibufferp)
97 ;; taken from linum.el
98 (and (daemonp) (null (frame-parameter nil 'client))))
99 (display-line-numbers-mode))))
100
101(provide 'display-line-numbers)
102
103;;; display-line-numbers.el ends here
diff --git a/src/xdisp.c b/src/xdisp.c
index 3e5657ffe6f..422912e57a6 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -32745,7 +32745,7 @@ even if the actual number needs less space.
32745The default value of nil means compute the space dynamically. 32745The default value of nil means compute the space dynamically.
32746Any other value is treated as nil. */); 32746Any other value is treated as nil. */);
32747 Vdisplay_line_numbers_width = Qnil; 32747 Vdisplay_line_numbers_width = Qnil;
32748 DEFSYM (Qdisplay_line_numbers_width, "display-line-number-width"); 32748 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32749 Fmake_variable_buffer_local (Qdisplay_line_numbers_width); 32749 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32750 32750
32751 DEFVAR_LISP ("display-line-numbers-current-absolute", 32751 DEFVAR_LISP ("display-line-numbers-current-absolute",