]> git.eshelyaron.com Git - emacs.git/commitdiff
Add a minor mode interface for display-line-numbers
authorAlexander Gramiak <agrambot@gmail.com>
Sat, 22 Jul 2017 09:16:08 +0000 (12:16 +0300)
committerEli Zaretskii <eliz@gnu.org>
Sat, 22 Jul 2017 09:16:08 +0000 (12:16 +0300)
* 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.

doc/emacs/custom.texi
doc/emacs/modes.texi
etc/NEWS
lisp/cus-start.el
lisp/display-line-numbers.el [new file with mode: 0644]
src/xdisp.c

index a756a89e3f845075137a6fb36af59bf2d83040dc..1c9c14a962a2165003dbaea5df74c8fd40a25a28 100644 (file)
@@ -1701,6 +1701,7 @@ and mouse events:
 (global-set-key (kbd "C-c y") 'clipboard-yank)
 (global-set-key (kbd "C-M-q") 'query-replace)
 (global-set-key (kbd "<f5>") 'flyspell-mode)
+(global-set-key (kbd "C-<f5>") 'display-line-numbers-mode)
 (global-set-key (kbd "C-<right>") 'forward-sentence)
 (global-set-key (kbd "<mouse-2>") 'mouse-save-then-kill)
 @end example
index eb0c88b29017fda76b4a440dcb3911acce17a004..876431aa9e99d63f2b274dfb25ef20db38be516f 100644 (file)
@@ -225,6 +225,13 @@ Font-Lock mode automatically highlights certain textual units found in
 programs.  It is enabled globally by default, but you can disable it
 in individual buffers.  @xref{Faces}.
 
+@findex display-line-numbers-mode
+@cindex display-line-numbers-mode
+@item
+Display Line Numbers mode is a convenience wrapper around
+@code{display-line-numbers}, setting it using the value of
+@code{display-line-numbers-type}.  @xref{Display Custom}.
+
 @item
 Outline minor mode provides similar facilities to the major mode
 called Outline mode.  @xref{Outline Mode}.
index 04971544d369d9e5a25a893a4d816285c0ca62d6..50945f0cfa3b114833bdf5a38b5107eec2fdf742 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -438,13 +438,18 @@ range of indentation.
 This is similar to what linum-mode provides, but much faster and
 doesn't usurp the display margin for the line numbers.  Customize the
 buffer-local variable 'display-line-numbers' to activate this optional
-display.  If set to t, Emacs will display the number of each line
-before the line.  If set to 'relative', Emacs will display the line
-number relative to the line showing point, with that line's number
-displayed as absolute.  If set to 'visual', Emacs will display a
-relative number for every screen line, i.e. it will count screen lines
-rather than buffer lines.  The default is nil, which doesn't display
-the line numbers.
+display.  Alternatively, you can use the `display-line-numbers-mode'
+minor mode or the global `global-display-line-numbers-mode'.  When
+using these modes, customize `display-line-numbers-type' with the same
+value as you would use with `display-line-numbers'.
+
+If `display-line-numbers' is set to t, Emacs will display the number
+of each line before the line.  If set to 'relative', Emacs will
+display the line number relative to the line showing point, with that
+line's number displayed as absolute.  If set to 'visual', Emacs will
+display a relative number for every screen line, i.e. it will count
+screen lines rather than buffer lines.  The default is nil, which
+doesn't display the line numbers.
 
 In 'relative' and 'visual' modes, the variable
 '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
 current line's number differently from all the other line numbers; by
 default these two faces are identical.
 
-You can also customize the new variable 'display-line-numbers-width' to
-specify a fixed minimal with of the area allocated to line-number
-display.  The default is nil, meaning that Emacs will dynamically
-calculate the area width, enlarging or shrinking it as needed.
-Setting it to a non-negative integer specifies that as the minimal
-width; selecting a value  that is large enough to display all line
-numbers in a buffer will then keep the line-number display area of
-constant width at all times, if that is desired.
+You can also customize the new buffer-local variable
+'display-line-numbers-width' to specify a fixed minimal with of the
+area allocated to line-number display.  The default is nil, meaning
+that Emacs will dynamically calculate the area width, enlarging or
+shrinking it as needed.  Setting it to a non-negative integer
+specifies that as the minimal width; selecting a value that is large
+enough to display all line numbers in a buffer will then keep the
+line-number display area of constant width at all times, if that is
+desired.
+
+When using `display-line-numbers-mode', you can customize the variable
+`display-line-numbers-grow-only' to a non-nil value; this means that
+Emacs may grow the above area width dynamically, but never shrink it.
+Under this mode, customizing the variable
+`display-line-numbers-width-start' to a non-nil value will cause Emacs
+to set `display-line-numbers-width' to the minimum width necessary to
+display all line numbers in the current buffer when first visiting it.
 
 Lisp programs can disable line-number display for a particular screen
 line by putting the 'display-line-numbers-disable' text property or
index ed913e32688d96c91ef3e70d62fa9ce767be4491..c28b8a147fc7feab5158696bfb296d57cab3ae49 100644 (file)
@@ -584,7 +584,7 @@ since it could result in memory overflow and make Emacs crash."
                       (const :tag "Grow only" :value grow-only))
              "25.1")
             (display-raw-bytes-as-hex display boolean "26.1")
-             (display-line-numbers display
+             (display-line-numbers display-line-numbers
                                    (choice
                                     (const :tag "Off (nil)" :value nil)
                                     (const :tag "Absolute line numbers"
@@ -594,7 +594,7 @@ since it could result in memory overflow and make Emacs crash."
                                     (const :tag "Visually relative line numbers"
                                            :value visual))
                                    "26.1")
-             (display-line-numbers-width display
+             (display-line-numbers-width display-line-numbers
                                  (choice
                                   (const :tag "Dynamically computed"
                                          :value nil)
@@ -602,14 +602,14 @@ since it could result in memory overflow and make Emacs crash."
                                            :value 2
                                            :format "%v"))
                                  "26.1")
-             (display-line-numbers-current-absolute display
+             (display-line-numbers-current-absolute display-line-numbers
                                  (choice
                                   (const :tag "Display actual number of current line"
                                          :value t)
                                   (const :tag "Display zero as number of current line"
                                          :value nil))
                                  "26.1")
-             (display-line-numbers-widen display
+             (display-line-numbers-widen display-line-numbers
                                  (choice
                                   (const :tag "Disregard narrowing when calculating line numbers"
                                          :value t)
diff --git a/lisp/display-line-numbers.el b/lisp/display-line-numbers.el
new file mode 100644 (file)
index 0000000..9523725
--- /dev/null
@@ -0,0 +1,103 @@
+;;; display-line-numbers.el --- interface for display-line-numbers -*- lexical-binding: t -*-
+
+;; Copyright (C) 2017 Free Software Foundation, Inc.
+
+;; Maintainer: emacs-devel@gnu.org
+;; Keywords: convenience
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Provides a minor mode interface for `display-line-numbers'.
+;;
+;; Toggle display of line numbers with M-x display-line-numbers-mode.
+;; To enable line numbering in all buffers, use M-x
+;; global-display-line-numbers-mode.  To change the default type of
+;; line numbers displayed, customize display-line-numbers-type.
+
+;; NOTE: Customization variables for `display-line-numbers' itself are
+;; defined in cus-start.el.
+
+;;; Code:
+
+(defgroup display-line-numbers nil
+  "Display line numbers in the buffer."
+  :group 'display)
+
+(defcustom display-line-numbers-type t
+  "The default type of line numbers to use in `display-line-numbers-mode'.
+See `display-line-numbers' for value options."
+  :group 'display-line-numbers
+  :type '(choice (const :tag "Relative line numbers" relative)
+                 (const :tag "Relative visual line numbers" visual)
+                 (other :tag "Absolute line numbers" t))
+  :version "26.1")
+
+(defcustom display-line-numbers-grow-only nil
+  "If non-nil, do not shrink line number width."
+  :group 'display-line-numbers
+  :type 'boolean
+  :version "26.1")
+
+(defcustom display-line-numbers-width-start nil
+  "If non-nil, count number of lines to use for line number width.
+When `display-line-numbers-mode' is turned on,
+`display-line-numbers-width' is set to the minimum width necessary
+to display all line numbers in the buffer."
+  :group 'display-line-numbers
+  :type 'boolean
+  :version "26.1")
+
+(defun display-line-numbers-update-width ()
+  "Prevent the line number width from shrinking."
+  (let ((width (line-number-display-width)))
+    (when (> width (or display-line-numbers-width 1))
+      (setq display-line-numbers-width width))))
+
+;;;###autoload
+(define-minor-mode display-line-numbers-mode
+  "Toggle display of line numbers in the buffer.
+This uses `display-line-numbers' internally.
+
+To change the type of line numbers displayed by default,
+customize `display-line-numbers-type'.  To change the type while
+the mode is on, set `display-line-numbers' directly."
+  :lighter nil
+  (if display-line-numbers-mode
+      (progn
+        (when display-line-numbers-width-start
+          (setq display-line-numbers-width
+                (length (number-to-string
+                         (count-lines (point-min) (point-max))))))
+        (when display-line-numbers-grow-only
+          (add-hook 'pre-command-hook #'display-line-numbers-update-width nil t))
+        (setq display-line-numbers display-line-numbers-type))
+    (remove-hook 'pre-command-hook #'display-line-numbers-update-width t)
+    (setq display-line-numbers nil)))
+
+;;;###autoload
+(define-globalized-minor-mode global-display-line-numbers-mode
+  display-line-numbers-mode
+  (lambda ()
+    (unless (or (minibufferp)
+                ;; taken from linum.el
+                (and (daemonp) (null (frame-parameter nil 'client))))
+      (display-line-numbers-mode))))
+
+(provide 'display-line-numbers)
+
+;;; display-line-numbers.el ends here
index 3e5657ffe6f0867250abd6296b2c7b832ff0b2b2..422912e57a6d89b2faca77a9fa330b3a680aca21 100644 (file)
@@ -32745,7 +32745,7 @@ even if the actual number needs less space.
 The default value of nil means compute the space dynamically.
 Any other value is treated as nil.  */);
   Vdisplay_line_numbers_width = Qnil;
-  DEFSYM (Qdisplay_line_numbers_width, "display-line-number-width");
+  DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
   Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
 
   DEFVAR_LISP ("display-line-numbers-current-absolute",