all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Juanma Barranquero <lekktu@gmail.com>
To: "Yuri D'Elia" <wavexx@thregr.org>
Cc: spacibba@aol.com, rpluim@gmail.com, stefan@marxist.se,
	Emacs developers <emacs-devel@gnu.org>,
	Lars Magne Ingebrigtsen <larsi@gnus.org>,
	Eli Zaretskii <eliz@gnu.org>
Subject: Re: Native line numbers landed on master
Date: Mon, 7 Oct 2019 02:14:05 +0200	[thread overview]
Message-ID: <CAAeL0SRXUcnH-A7u3rC8RXHOhcVq4oUzxD72zth887pT728Hyw@mail.gmail.com> (raw)
In-Reply-To: <CAAeL0SStFgE6sQx4ujzS316VwwXBzTZmN=2p4GPmAZ_Jn3wgpg@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 1273 bytes --]

This implements display-line-numbers-offset, and adds info, NEWS, etc.
Better wording welcome, as always. The variable is automatically
buffer-local because I don't imagine that there's a good default value
(other than zero) and each buffer that uses it will want its own.

I've done some testing, but more eyes would be great.

As an aside, this allows some dirty tricks, like numbering from 0 (by
setting it to -1), or this funny monstrosity, which allows to reverse-count
the buffer:

  (let ((old nil)
        (buf (current-buffer)))
    (add-hook 'before-change-functions
              (lambda (beg end)
                (when (eq buf (current-buffer))
                  (setq old (count-lines beg end)))))
    (add-hook 'after-change-functions
              (lambda (beg end _len)
                (when (and (eq buf (current-buffer))
                           old
                           (/= old (count-lines beg end)))
                  (setq display-line-numbers-offset (- -1
                                                     (count-lines
(point-min)

(point-max)))
                        old nil)))))

Of course, if the buffer is immutable, this is just

  (setq display-line-numbers-offset (- -1 (count-lines (point-min)
(point-max))))

in some mode hook.

[-- Attachment #1.2: Type: text/html, Size: 1685 bytes --]

[-- Attachment #2: 0001-Implement-offsets-for-absolute-line-numbers.patch --]
[-- Type: application/octet-stream, Size: 6719 bytes --]

From 223ebc2a9a2a7cecbfae5e884e0e3bdc8995e000 Mon Sep 17 00:00:00 2001
From: Juanma Barranquero <lekktu@gmail.com>
Date: Mon, 7 Oct 2019 01:22:14 +0200
Subject: [PATCH] Implement offsets for absolute line numbers

* src/xdisp.c (syms_of_xdisp) <display-line-numbers-offset>:
New variable to add an offset to absolute line numbers.
(syms_of_xdisp) <display-line-numbers>: Mention it in docstring.
(maybe_produce_line_number): Use it.

* doc/emacs/display.texi (Display Custom): Document it.

* etc/NEWS (value): Announce it.

* lisp/frame.el: Add `display-line-numbers-offset' to list of
variables to which should trigger redisplay of the current buffer.
---
 doc/emacs/display.texi |  7 +++++++
 etc/NEWS               |  4 ++++
 lisp/frame.el          |  1 +
 src/xdisp.c            | 43 +++++++++++++++++++++++++++++++++++-------
 4 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/doc/emacs/display.texi b/doc/emacs/display.texi
index 406feb8c12..cb37ef448e 100644
--- a/doc/emacs/display.texi
+++ b/doc/emacs/display.texi
@@ -1855,6 +1855,13 @@ Display Custom
 value, line numbers will disregard any narrowing and will start at the
 first character of the buffer.
 
+@vindex display-line-numbers-offset
+If the value of @code{display-line-numbers-offset} is non-zero, it is
+added to each absolute line number, and lines are counted from the
+beginning of the buffer, as if @code{display-line-numbers-widen} were
+non-@code{nil}.  It has no effect when set to zero, or when line
+numbers are not absolute.
+
 @vindex display-line-numbers-width-start
 @vindex display-line-numbers-grow-only
 @vindex display-line-numbers-width
diff --git a/etc/NEWS b/etc/NEWS
index 4a32300268..a40eb06b85 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -560,11 +560,15 @@ now prompts the user for the directory containing the desktop file.
 
 +++
 ** display-line-numbers-mode
+
 *** New faces 'line-number-major-tick' and 'line-number-minor-tick',
 and customizable variables 'display-line-numbers-major-tick' and
 'display-line-numbers-minor-tick' can be used to highlight the line
 numbers of lines multiple of certain numbers.
 
+*** New variable `display-line-numbers-offset', when non-zero, adds
+an offset to absolute line numbers.
+
 +++
 ** winner
 *** A new variable, 'winner-boring-buffers-regexp', has been added.
diff --git a/lisp/frame.el b/lisp/frame.el
index 51b3b621ff..018c2f578e 100644
--- a/lisp/frame.el
+++ b/lisp/frame.el
@@ -2726,6 +2726,7 @@ 'automatic-hscrolling
         display-line-numbers-widen
         display-line-numbers-major-tick
         display-line-numbers-minor-tick
+        display-line-numbers-offset
         display-fill-column-indicator
         display-fill-column-indicator-column
         display-fill-column-indicator-character
diff --git a/src/xdisp.c b/src/xdisp.c
index 1f3a8136f8..563cf473cf 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -22506,10 +22506,22 @@ maybe_produce_line_number (struct it *it)
   ptrdiff_t start_from, bytepos;
   ptrdiff_t this_line;
   bool first_time = false;
-  ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
-  ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
+  ptrdiff_t beg_byte;
+  ptrdiff_t z_byte;
+  bool line_numbers_wide;
   void *itdata = bidi_shelve_cache ();
 
+  if (display_line_numbers_offset
+      && !display_line_numbers_widen
+      && !EQ (Vdisplay_line_numbers, Qvisual)
+      && !EQ (Vdisplay_line_numbers, Qrelative))
+    line_numbers_wide = true;
+  else
+    line_numbers_wide = display_line_numbers_widen;
+
+  beg_byte = line_numbers_wide ? BEG_BYTE : BEGV_BYTE;
+  z_byte = line_numbers_wide ? Z_BYTE : ZV_BYTE;
+
   if (EQ (Vdisplay_line_numbers, Qvisual))
     this_line = display_count_lines_visually (it);
   else
@@ -22524,7 +22536,7 @@ maybe_produce_line_number (struct it *it)
 		 numbers, so we cannot use its data if the user wants
 		 line numbers that disregard narrowing, or if the
 		 buffer's narrowing has just changed.  */
-	      && !(display_line_numbers_widen
+	      && !(line_numbers_wide
 		   && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
 	      && !current_buffer->clip_changed)
 	    {
@@ -22614,6 +22626,8 @@ maybe_produce_line_number (struct it *it)
     lnum_offset = it->pt_lnum;
   else if (EQ (Vdisplay_line_numbers, Qvisual))
     lnum_offset = 0;
+  else if (display_line_numbers_offset)
+    lnum_offset -= display_line_numbers_offset;
 
   /* Under 'relative', display the absolute line number for the
      current line, unless the user requests otherwise.  */
@@ -34701,12 +34715,18 @@ syms_of_xdisp (void)
 
   DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
     doc: /* Non-nil means display line numbers.
+
 If the value is t, display the absolute number of each line of a buffer
 shown in a window.  Absolute line numbers count from the beginning of
-the current narrowing, or from buffer beginning.  If the value is
-`relative', display for each line not containing the window's point its
-relative number instead, i.e. the number of the line relative to the
-line showing the window's point.
+the current narrowing, or from buffer beginning.  The variable
+`display-line-numbers-offset', if non-zero, is a signed offset added
+to each absolute line number; it also forces line numbers to be counted
+from the beginning of the buffer, as if `display-line-numbers-wide'
+were non-nil.  It has no effect when line numbers are not absolute.
+
+If the value is `relative', display for each line not containing the
+window's point its relative number instead, i.e. the number of the line
+relative to the line showing the window's point.
 
 In either case, line numbers are displayed at the beginning of each
 non-continuation line that displays buffer text, i.e. after each newline
@@ -34747,6 +34767,15 @@ syms_of_xdisp (void)
   DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
   Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
 
+  DEFVAR_INT ("display-line-numbers-offset", display_line_numbers_offset,
+    doc: /* A signed integer added to each absolute line number.
+When this variable is non-zero, line numbers are always counted from
+the beginning of the buffer even if `display-line-numbers-widen' is nil.
+It has no effect when set to 0, or when line numbers are not absolute.  */);
+  display_line_numbers_offset = 0;
+  DEFSYM (Qdisplay_line_numbers_offset, "display-line-numbers-offset");
+  Fmake_variable_buffer_local (Qdisplay_line_numbers_offset);
+
   DEFVAR_BOOL ("display-fill-column-indicator", Vdisplay_fill_column_indicator,
     doc: /* Non-nil means display the fill column indicator.  */);
   Vdisplay_fill_column_indicator = false;
-- 
2.23.0.windows.1


  reply	other threads:[~2019-10-07  0:14 UTC|newest]

Thread overview: 129+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-08  7:58 Native line numbers landed on master Eli Zaretskii
2017-07-08  8:41 ` martin rudalics
2017-07-08 10:23   ` Eli Zaretskii
2017-07-08 22:38 ` Alex
2017-07-09 14:22   ` Eli Zaretskii
2017-07-09 22:56     ` Alex
2017-07-10 17:50       ` Eli Zaretskii
2017-07-10 20:31         ` Alex
2017-07-11 15:12           ` Eli Zaretskii
2017-07-11 20:44             ` Alex
2017-07-12 14:40               ` Eli Zaretskii
2017-07-16  7:30                 ` Alex
2017-07-16 14:10                   ` Eli Zaretskii
2017-07-16 19:31                     ` Alex
2017-07-17 15:00                       ` Eli Zaretskii
2017-07-17 20:34                         ` Alex
2017-07-22  9:18                           ` Eli Zaretskii
2017-07-10 22:19     ` John Wiegley
2017-07-11  2:29       ` Eli Zaretskii
2017-07-11 15:27     ` Stefan Monnier
2017-07-11 16:04       ` Eli Zaretskii
2017-07-11 16:16         ` Stefan Monnier
2017-07-11 17:23           ` Eli Zaretskii
2017-07-11 17:48             ` Stefan Monnier
2017-07-11 18:04               ` Eli Zaretskii
2017-07-11 18:19                 ` Stefan Monnier
2017-07-11 18:18               ` Sharp-quoting function symbols (Was: Native line numbers landed on master) Kaushal Modi
2017-09-30 18:36                 ` Philipp Stephani
2017-12-30 21:09                   ` Philipp Stephani
2017-07-10 16:51 ` Native line numbers landed on master Filipe Silva
2017-07-11 13:00 ` Robert Pluim
2017-07-11 13:37   ` Jean-Christophe Helary
2017-07-11 13:47     ` Robert Pluim
2017-07-11 14:19       ` Jean-Christophe Helary
2017-07-11 15:24   ` Eli Zaretskii
2017-07-11 15:29     ` Robert Pluim
2017-07-11 16:07       ` Eli Zaretskii
2017-07-11 16:12         ` Robert Pluim
2017-07-11 17:33           ` Eli Zaretskii
2017-07-12  3:23 ` Kaushal Modi
2017-07-12  7:11   ` martin rudalics
2017-07-12 14:27     ` Eli Zaretskii
2017-07-12 15:49       ` martin rudalics
2017-07-15 22:02 ` Yuri D'Elia
2017-07-16  2:34   ` Eli Zaretskii
2017-07-16 14:25     ` Eli Zaretskii
2017-07-17  9:44       ` Yuri D'Elia
2017-07-17 14:16         ` Eli Zaretskii
2019-06-10  2:46   ` Juanma Barranquero
2019-06-10  8:32     ` Yuri D'Elia
2019-06-10 12:38       ` Juanma Barranquero
2019-06-10 15:22     ` Eli Zaretskii
2019-06-10 15:32       ` Juanma Barranquero
2019-06-10 15:33       ` Yuri D'Elia
2019-06-10 15:54         ` Eli Zaretskii
2019-06-10 16:23           ` Yuri D'Elia
2019-06-10 17:41             ` Eli Zaretskii
2019-09-30 10:01               ` Juanma Barranquero
2019-09-30 10:21                 ` Eli Zaretskii
2019-10-01  5:44                   ` Juanma Barranquero
2019-10-01  7:05                     ` Eli Zaretskii
2019-10-01  8:49                       ` Juanma Barranquero
2019-10-01  8:55                         ` Juanma Barranquero
2019-10-01  9:26                           ` Eli Zaretskii
2019-10-01  9:25                         ` Eli Zaretskii
2019-10-01  9:09                     ` Yuri D'Elia
2019-10-01  9:21                       ` Juanma Barranquero
2019-10-01  9:51                         ` Yuri D'Elia
2019-10-01 10:23                           ` Juanma Barranquero
2019-10-01 10:40                             ` Yuri D'Elia
2019-10-01 10:39                           ` Eli Zaretskii
2019-10-01 10:47                         ` Lars Ingebrigtsen
2019-10-01 11:07                           ` Eli Zaretskii
2019-10-01 11:11                           ` Juanma Barranquero
2019-10-01 22:52                             ` Ergus
2019-10-01 23:51                               ` Juanma Barranquero
2019-10-02  3:41                                 ` Ergus
2019-10-02  9:40                                   ` Juanma Barranquero
2019-10-02 13:56                                     ` Ergus
2019-10-02 15:06                                 ` Eli Zaretskii
2019-10-03  4:11                                   ` Juanma Barranquero
2019-10-03  8:16                                     ` martin rudalics
2019-10-03 14:43                                       ` Juanma Barranquero
2019-10-03  9:10                                     ` Robert Pluim
2019-10-03 14:47                                       ` Juanma Barranquero
2019-10-03 15:18                                         ` Robert Pluim
2019-10-03 20:37                                         ` Stefan Kangas
2019-10-03 21:48                                           ` Juanma Barranquero
2019-10-03 22:37                                             ` Yuri D'Elia
2019-10-04  1:51                                               ` Juanma Barranquero
2019-10-04  7:45                                                 ` Eli Zaretskii
2019-10-04  9:52                                                   ` Yuri D'Elia
2019-10-04 10:24                                                     ` Juanma Barranquero
2019-10-05  6:26                                                     ` Juanma Barranquero
2019-10-07  0:14                                                       ` Juanma Barranquero [this message]
2019-10-07  6:54                                                         ` Robert Pluim
2019-10-07  7:39                                                           ` Juanma Barranquero
2019-10-07  8:09                                                             ` Robert Pluim
2019-10-07  8:39                                                               ` Juanma Barranquero
2019-10-07 18:52                                                                 ` Juri Linkov
2019-10-08  0:57                                                                   ` Juanma Barranquero
2019-10-19 20:38                                                                     ` Juri Linkov
2019-10-07 16:30                                                               ` Eli Zaretskii
2019-10-08 11:15                                                                 ` Robert Pluim
2019-10-08 12:23                                                                   ` Eli Zaretskii
2019-10-09  7:19                                                                     ` Robert Pluim
2019-10-09  8:16                                                                       ` Eli Zaretskii
2019-10-09 12:14                                                                         ` Robert Pluim
2019-10-09 12:23                                                                           ` Eli Zaretskii
2019-10-09 13:19                                                                             ` Robert Pluim
2019-10-09 10:51                                                         ` Juanma Barranquero
2019-10-04 10:22                                                   ` Ergus
2019-10-04 10:26                                                     ` Juanma Barranquero
2019-10-03 12:28                                     ` Yuri Khan
2019-10-03 14:48                                       ` Juanma Barranquero
2019-10-03 17:56                                         ` Yuri D'Elia
2019-10-03 18:40                                           ` Eli Zaretskii
2019-10-03 19:01                                             ` Yuri D'Elia
2019-10-04  2:01                                             ` Juanma Barranquero
2019-10-04  5:01                                               ` Juanma Barranquero
2019-10-04 15:57                                                 ` Johan Bockgård
2019-10-04 17:28                                                   ` Juanma Barranquero
2019-10-04 19:24                                                     ` Stefan Monnier
2019-10-04 20:12                                                       ` Yuri D'Elia
2019-10-04 22:45                                                         ` Juanma Barranquero
2019-10-06 14:04                                                           ` Juanma Barranquero
2019-10-06 14:45                                                             ` Juanma Barranquero
2019-10-06 18:02                                                             ` Eli Zaretskii
2019-10-01  9:24                       ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAAeL0SRXUcnH-A7u3rC8RXHOhcVq4oUzxD72zth887pT728Hyw@mail.gmail.com \
    --to=lekktu@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.org \
    --cc=rpluim@gmail.com \
    --cc=spacibba@aol.com \
    --cc=stefan@marxist.se \
    --cc=wavexx@thregr.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.