From 76a9f03ca629d3e5a596c3aa7f62a4649ac2ae8a Mon Sep 17 00:00:00 2001 From: Juanma Barranquero Date: Wed, 9 Oct 2019 12:49:39 +0200 Subject: [PATCH] Implement offsets for absolute line numbers * src/xdisp.c (syms_of_xdisp) : New variable to add an offset to absolute line numbers. (syms_of_xdisp) : 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 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 406feb8c127..cb37ef448e8 100644 --- a/doc/emacs/display.texi +++ b/doc/emacs/display.texi @@ -1855,6 +1855,13 @@ the variable @code{display-line-numbers-widen} to a non-@code{nil} 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 2ca681ff9b9..49aa7f6007b 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 51b3b621ff4..018c2f578e4 100644 --- a/lisp/frame.el +++ b/lisp/frame.el @@ -2726,6 +2726,7 @@ See also `toggle-frame-maximized'." 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 52275a11b86..893ce9269c1 100644 --- a/src/xdisp.c +++ b/src/xdisp.c @@ -22512,10 +22512,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 @@ -22530,7 +22542,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) { @@ -22620,6 +22632,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. */ @@ -34711,12 +34725,18 @@ To add a prefix to continuation lines, use `wrap-prefix'. */); 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 @@ -34757,6 +34777,15 @@ either `relative' or `visual'. */); 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.39.5