aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2017-07-06 20:22:16 +0300
committerEli Zaretskii2017-07-06 20:22:16 +0300
commit25bc3911615d1160d47287c023545c6e0587739f (patch)
treec32eaa3034c93867ae3c820731057505f8fc3f52
parentd5f8a3d03f6c0c98f3280d55a2d88ddb40aa1f3e (diff)
downloademacs-25bc3911615d1160d47287c023545c6e0587739f.tar.gz
emacs-25bc3911615d1160d47287c023545c6e0587739f.zip
Implement line numbers that disregard narrowing
* src/xdisp.c (display_count_lines_logically): New function, counts line numbers disregarding narrowing. Suggested by Andy Moreton <andrewjmoreton@gmail.com>. (maybe_produce_line_number): Call display_count_lines_logically instead of display_count_lines. Adapt BEGV, ZV, etc. to display-line-numbers-widen. (syms_of_xdisp) <display-line-numbers-widen>: New buffer-local variable. * lisp/cus-start.el (standard): Provide a customization form for display-line-numbers-widen. * lisp/frame.el: Add display-line-numbers-widen, display-line-numbers-current-absolute, and display-line-number-width to the list of variables that should trigger redisplay of the current buffer. * doc/emacs/display.texi (Display Custom): Document display-line-numbers-widen.
-rw-r--r--doc/emacs/display.texi8
-rw-r--r--lisp/cus-start.el9
-rw-r--r--lisp/frame.el3
-rw-r--r--src/xdisp.c49
4 files changed, 57 insertions, 12 deletions
diff --git a/doc/emacs/display.texi b/doc/emacs/display.texi
index 61ca7e24f84..083fcdf97a6 100644
--- a/doc/emacs/display.texi
+++ b/doc/emacs/display.texi
@@ -1756,6 +1756,14 @@ zero. This is handy if you don't care about the number of the current
1756line, and want to leave more horizontal space for text in large 1756line, and want to leave more horizontal space for text in large
1757buffers. 1757buffers.
1758 1758
1759@vindex display-line-numbers-widen
1760In a narrowed buffer (@pxref{Narrowing}) lines are normally numbered
1761starting at the beginning of the narrowing. However, if you customize
1762the variable @code{display-line-numbers-widen} to a non-@code{nil}
1763value, line numbers will disregard any narrowing and will start at the
1764first character of the buffer.
1765
1766@cindex line-number face
1759The line numbers are displayed in a special face @code{line-number}. 1767The line numbers are displayed in a special face @code{line-number}.
1760The current line number is displayed in a different face, 1768The current line number is displayed in a different face,
1761@code{line-number-current-line}, so you can make the current line's 1769@code{line-number-current-line}, so you can make the current line's
diff --git a/lisp/cus-start.el b/lisp/cus-start.el
index a89d5dff0cd..017e7f9fa55 100644
--- a/lisp/cus-start.el
+++ b/lisp/cus-start.el
@@ -608,7 +608,14 @@ since it could result in memory overflow and make Emacs crash."
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
613 (choice
614 (const :tag "Disregard narrowing when calculating line numbers"
615 :value t)
616 (const :tag "Count lines from beinning of narrowed region"
617 :value nil))
618 "26.1")
612 ;; xfaces.c 619 ;; xfaces.c
613 (scalable-fonts-allowed display boolean "22.1") 620 (scalable-fonts-allowed display boolean "22.1")
614 ;; xfns.c 621 ;; xfns.c
diff --git a/lisp/frame.el b/lisp/frame.el
index 8f51afa2a9a..f3e59edb2f5 100644
--- a/lisp/frame.el
+++ b/lisp/frame.el
@@ -2435,6 +2435,9 @@ See also `toggle-frame-maximized'."
2435 wrap-prefix 2435 wrap-prefix
2436 truncate-lines 2436 truncate-lines
2437 display-line-numbers 2437 display-line-numbers
2438 display-line-number-width
2439 display-line-numbers-current-absolute
2440 display-line-numbers-widen
2438 bidi-paragraph-direction 2441 bidi-paragraph-direction
2439 bidi-display-reordering)) 2442 bidi-display-reordering))
2440 2443
diff --git a/src/xdisp.c b/src/xdisp.c
index 312ee10f280..92ce1451867 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -20749,6 +20749,24 @@ find_row_edges (struct it *it, struct glyph_row *row,
20749 row->maxpos = it->current.pos; 20749 row->maxpos = it->current.pos;
20750} 20750}
20751 20751
20752/* Like display_count_lines, but capable of counting outside of the
20753 current narrowed region. */
20754static ptrdiff_t
20755display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20756 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20757{
20758 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20759 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20760
20761 ptrdiff_t val;
20762 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20763 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20764 Fwiden ();
20765 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20766 unbind_to (pdl_count, Qnil);
20767 return val;
20768}
20769
20752/* Count the number of screen lines in window IT->w between character 20770/* Count the number of screen lines in window IT->w between character
20753 position IT_CHARPOS(*IT) and the line showing that window's point. */ 20771 position IT_CHARPOS(*IT) and the line showing that window's point. */
20754static ptrdiff_t 20772static ptrdiff_t
@@ -20806,6 +20824,9 @@ maybe_produce_line_number (struct it *it)
20806 ptrdiff_t start_from, bytepos; 20824 ptrdiff_t start_from, bytepos;
20807 ptrdiff_t this_line; 20825 ptrdiff_t this_line;
20808 bool first_time = false; 20826 bool first_time = false;
20827 ptrdiff_t beg = display_line_numbers_widen ? BEG : BEGV;
20828 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20829 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20809 void *itdata = bidi_shelve_cache (); 20830 void *itdata = bidi_shelve_cache ();
20810 20831
20811 if (EQ (Vdisplay_line_numbers, Qvisual)) 20832 if (EQ (Vdisplay_line_numbers, Qvisual))
@@ -20815,7 +20836,7 @@ maybe_produce_line_number (struct it *it)
20815 if (!last_line) 20836 if (!last_line)
20816 { 20837 {
20817 /* FIXME: Maybe reuse the data in it->w->base_line_number. */ 20838 /* FIXME: Maybe reuse the data in it->w->base_line_number. */
20818 start_from = BEGV; 20839 start_from = beg;
20819 if (!it->lnum_bytepos) 20840 if (!it->lnum_bytepos)
20820 first_time = true; 20841 first_time = true;
20821 } 20842 }
@@ -20825,17 +20846,17 @@ maybe_produce_line_number (struct it *it)
20825 /* Paranoia: what if someone changes the narrowing since the 20846 /* Paranoia: what if someone changes the narrowing since the
20826 last time display_line was called? Shouldn't really happen, 20847 last time display_line was called? Shouldn't really happen,
20827 but who knows what some crazy Lisp invoked by :eval could do? */ 20848 but who knows what some crazy Lisp invoked by :eval could do? */
20828 if (!(BEGV_BYTE <= start_from && start_from < ZV_BYTE)) 20849 if (!(beg_byte <= start_from && start_from < z_byte))
20829 { 20850 {
20830 last_line = 0; 20851 last_line = 0;
20831 start_from = BEGV_BYTE; 20852 start_from = beg_byte;
20832 } 20853 }
20833 20854
20834 this_line = 20855 this_line =
20835 last_line + display_count_lines (start_from, 20856 last_line + display_count_lines_logically (start_from,
20836 IT_BYTEPOS (*it), IT_CHARPOS (*it), 20857 IT_BYTEPOS (*it),
20837 &bytepos); 20858 IT_CHARPOS (*it), &bytepos);
20838 eassert (this_line > 0 || (this_line == 0 && start_from == BEGV_BYTE)); 20859 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20839 eassert (bytepos == IT_BYTEPOS (*it)); 20860 eassert (bytepos == IT_BYTEPOS (*it));
20840 } 20861 }
20841 20862
@@ -20863,11 +20884,11 @@ maybe_produce_line_number (struct it *it)
20863 ptrdiff_t ignored; 20884 ptrdiff_t ignored;
20864 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual)) 20885 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
20865 it->pt_lnum = 20886 it->pt_lnum =
20866 this_line + display_count_lines (it->lnum_bytepos, PT_BYTE, PT, 20887 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
20867 &ignored); 20888 PT, &ignored);
20868 else 20889 else
20869 it->pt_lnum = display_count_lines (BEGV_BYTE, PT_BYTE, PT, 20890 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
20870 &ignored); 20891 &ignored);
20871 } 20892 }
20872 /* Compute the required width if needed. */ 20893 /* Compute the required width if needed. */
20873 if (!it->lnum_width) 20894 if (!it->lnum_width)
@@ -32604,6 +32625,12 @@ This variable has effect only when `display-line-numbers' is
32604either `relative' or `visual'. */); 32625either `relative' or `visual'. */);
32605 Vdisplay_line_numbers_current_absolute = Qt; 32626 Vdisplay_line_numbers_current_absolute = Qt;
32606 32627
32628 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32629 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32630 display_line_numbers_widen = false;
32631 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32632 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32633
32607 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay, 32634 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32608 doc: /* Non-nil means don't eval Lisp during redisplay. */); 32635 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32609 inhibit_eval_during_redisplay = false; 32636 inhibit_eval_during_redisplay = false;