How to record the line number pixel width for each window.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to record the line number pixel width for each window.

Keith David Bershatsky
I am working on feature requests 22873 (multiple fake cursors) and 17684 (crosshairs and visible fill column, both of which able to vertically intersect characters at any screen X coordinate).  The latest patch is:

    VERSION: 022.005 [11/17/2019]

    https://debbugs.gnu.org/cgi/bugreport.cgi?bug=22873#176

Features 17684/22873 have been purposefully designed to obviate the need to call start_display; i.e., there is no need to move IT in order to obtain any values used in the implementation of 17684/22873.

I do not want to use the built-in function line_number_display_width, which fires up start_display and moves IT to figure out the line number pixel width (among other values).  Instead, I would like to devise a reliable method whereby the display engine records the value (line number pixel width) for each window (wherever features 17684/22873 are active), such that those values are reliably accessible when Emacs calls update_window (located in dispnew.c).

I have been using the snippet below to record the value of the line number pixel width for each window.  This value, however, is unreliable when dealing with a window containing folded/hidden text, such as an org-mode buffer.  When dealing with folded/hidden text (e.g., org-mode), the recorded value sporadically changes while moving the cursor (e.g. pressing the arrow keys) even though the contents of the window do not change.

How can I perfect the recorded value of the line number pixel width, and also avoid expressly firing up a new instance of start_display and moving IT for the sole purpose of determining the value for each window during update_window (in dispnew.c)?

Thanks,

Keith

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


diff --git a/src/xdisp.c b/src/xdisp.c
index 2467b33..dd901da 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -22720,6 +22720,28 @@ maybe_produce_line_number (struct it *it)
  }
     }
 
+
+/* *************************************************************************** */
+/* MULTIPLE-CURSORS */
+
+  struct buffer *b = XBUFFER (it->w->contents);
+  struct buffer *old_buffer = NULL;
+  /* Needed so that buffer-local values can be determined; e.g., when switching
+  to the minibuffer. */
+  if (b != current_buffer)
+    {
+      old_buffer = current_buffer;
+      set_buffer_internal (b);
+    }
+  it->w->mc.lnum_pixel_width = (!NILP (Vdisplay_line_numbers))
+                               ? tem_it.current_x
+                               : 0;
+  if (old_buffer)
+    set_buffer_internal (old_buffer);
+
+/* *************************************************************************** */
+
+
   /* Record the width in pixels we need for the line number display.  */
   it->lnum_pixel_width = tem_it.current_x;
   /* Copy the produced glyphs into IT's glyph_row.  */

Reply | Threaded
Open this post in threaded view
|

Re: How to record the line number pixel width for each window.

Eli Zaretskii
> Date: Thu, 05 Dec 2019 10:19:07 -0800
> From: Keith David Bershatsky <[hidden email]>
>
> How can I perfect the recorded value of the line number pixel width, and also avoid expressly firing up a new instance of start_display and moving IT for the sole purpose of determining the value for each window during update_window (in dispnew.c)?

I guess you will have to write code that stores the last value
computed during redisplay in the window object.

Not that I think this is a good idea, because the display engine has
its own rules for which parts of the window it redraws, so you might
bump into situations where the value is not accurate.  Nor do I
understand why you don't like what line_number_display_width does,
because that's the right solution for this kind of problems.

Reply | Threaded
Open this post in threaded view
|

Re: How to record the line number pixel width for each window.

Keith David Bershatsky
Thank you, Eli, for reading and responding to this thread.  Once the redisplay cycle gets to the place where update_window is called, all simulations performed by moving IT have already concluded, and update_window is the launch-point for features 17684/22873.  I would be comfortable using an existing instance of screen simulation and perform the essence of line_number_display_width without launching a new session with start_display.  The goal is to be as efficient as possible when storing the values so that they are reliably accessible when update_window gets called.  My general feeling is that update_window is not the place to do screen simulations (moving IT) and running a _new_ instance of start_display for each window is not efficient.

Is there an existing instance of a running simulation for each window somewhere in xdisp.c where I could add the guts of line_number_display_width without launching a new start_display; and, if so, some pointers on where that might be located would be greatly appreciated?

On Dec 6, 2019, at 12:10 AM, Eli Zaretskii <[hidden email]> wrote:

>> Date: Thu, 05 Dec 2019 10:19:07 -0800
>> From: Keith David Bershatsky <[hidden email]>
>>
>> How can I perfect the recorded value of the line number pixel width, and also avoid expressly firing up a new instance of start_display and moving IT for the sole purpose of determining the value for each window during update_window (in dispnew.c)?
>
> I guess you will have to write code that stores the last value
> computed during redisplay in the window object.
>
> Not that I think this is a good idea, because the display engine has
> its own rules for which parts of the window it redraws, so you might
> bump into situations where the value is not accurate.  Nor do I
> understand why you don't like what line_number_display_width does,
> because that's the right solution for this kind of problems.

Reply | Threaded
Open this post in threaded view
|

Re: How to record the line number pixel width for each window.

Eli Zaretskii
> Cc: "[hidden email]" <[hidden email]>
> From: Keith David Bershatsky <[hidden email]>
> Date: Fri, 6 Dec 2019 09:07:10 -0800
>
> Thank you, Eli, for reading and responding to this thread.  Once the redisplay cycle gets to the place where update_window is called, all simulations performed by moving IT have already concluded, and update_window is the launch-point for features 17684/22873.  I would be comfortable using an existing instance of screen simulation and perform the essence of line_number_display_width without launching a new session with start_display.  The goal is to be as efficient as possible when storing the values so that they are reliably accessible when update_window gets called.  My general feeling is that update_window is not the place to do screen simulations (moving IT) and running a _new_ instance of start_display for each window is not efficient.
>
> Is there an existing instance of a running simulation for each window somewhere in xdisp.c where I could add the guts of line_number_display_width without launching a new start_display; and, if so, some pointers on where that might be located would be greatly appreciated?

I'm sorry, I don't understand the question.  What do you call
"simulation" in this context?

Reply | Threaded
Open this post in threaded view
|

Re: How to record the line number pixel width for each window.

Keith David Bershatsky
By "simulate", I had in mind actively using the move_it family of functions -- where Emacs inhibits updating the glass and we can navigate the text to ascertain and/ or set certain values.  For example, the sample snippet/diff in the opening post of this thread occurs when Emacs is using IT to set the value of the line number pixel width, and this is happening in the context of what I think of as a redisplay simulation.  The solution to my issue may be as simple as devising a condition limiting just when to set the window object with the line number pixel width; e.g., when IT is on the first visible line of text in the window (following the header line, if any).  Right now, the snippet sets the value when IT is on other lines besides the first line of text in the window, perhaps even a hidden / invisible line due to folded text.  Perhaps that folded / invisible line would have a different line number pixel width, but it is not used because it is hidden and the value is erroneous.



On Dec 6, 2019, at 10:42 AM, Eli Zaretskii <[hidden email]> wrote:

>> Cc: "[hidden email]" <[hidden email]>
>> From: Keith David Bershatsky <[hidden email]>
>> Date: Fri, 6 Dec 2019 09:07:10 -0800
>>
>> Thank you, Eli, for reading and responding to this thread.  Once the redisplay cycle gets to the place where update_window is called, all simulations performed by moving IT have already concluded, and update_window is the launch-point for features 17684/22873.  I would be comfortable using an existing instance of screen simulation and perform the essence of line_number_display_width without launching a new session with start_display.  The goal is to be as efficient as possible when storing the values so that they are reliably accessible when update_window gets called.  My general feeling is that update_window is not the place to do screen simulations (moving IT) and running a _new_ instance of start_display for each window is not efficient.
>>
>> Is there an existing instance of a running simulation for each window somewhere in xdisp.c where I could add the guts of line_number_display_width without launching a new start_display; and, if so, some pointers on where that might be located would be greatly appreciated?
>
> I'm sorry, I don't understand the question.  What do you call
> "simulation" in this context?

Reply | Threaded
Open this post in threaded view
|

Re: How to record the line number pixel width for each window.

Eli Zaretskii
> Cc: "[hidden email]" <[hidden email]>
> From: Keith David Bershatsky <[hidden email]>
> Date: Fri, 6 Dec 2019 11:29:04 -0800
>
> By "simulate", I had in mind actively using the move_it family of functions -- where Emacs inhibits updating the glass and we can navigate the text to ascertain and/ or set certain values.  For example, the sample snippet/diff in the opening post of this thread occurs when Emacs is using IT to set the value of the line number pixel width, and this is happening in the context of what I think of as a redisplay simulation.  The solution to my issue may be as simple as devising a condition limiting just when to set the window object with the line number pixel width; e.g., when IT is on the first visible line of text in the window (following the header line, if any).  Right now, the snippet sets the value when IT is on other lines besides the first line of text in the window, perhaps even a hidden / invisible line due to folded text.  Perhaps that folded / invisible line would have a different line number pixel width, but it is not used because it is hidden and the value is erroneous.

I don't have any specific advice for you.  In general, the required
width of the line-number display is calculated when a window's
redisplay starts, the first time display_line is called for that
window's text area.  In some rare cases, displaying subsequent lines
comes up with a larger value (as can be seen in Org mode with many
folded line).  So maybe looking at the last value in a window will do
what you want.

Again, I don't recommend this approach, as it is fundamentally wrong
IMO.

Reply | Threaded
Open this post in threaded view
|

Re: How to record the line number pixel width for each window.

Keith David Bershatsky
In reply to this post by Keith David Bershatsky
Thank you, Eli, for your insight -- greatly appreciated!  Based on your helpful comments and also based upon examining the code for line_number_display_width, I came up with the following conditions to use when setting the window object with the line number pixel width (which is set from within maybe_produce_line_number):

  if (it->area == TEXT_AREA
      && IT_CHARPOS (*it) == wstart.charpos)
    {
      [Set the window object with the line number pixel width.]
    }

The entire snippet is listed below.  I did some limited testing this evening in an org-mode buffer and so far so good.  I will continue to test out the revised code in the coming days to see if any problems arise.


diff --git a/src/xdisp.c b/src/xdisp.c
index 2467b33..dfc9a61 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -22720,6 +22720,34 @@ maybe_produce_line_number (struct it *it)
  }
     }
 
+
+/* *************************************************************************** */
+/* MULTIPLE-CURSORS */
+
+  struct text_pos wstart;
+  SET_TEXT_POS_FROM_MARKER (wstart, it->w->start);
+  if (it->area == TEXT_AREA
+      && IT_CHARPOS (*it) == wstart.charpos)
+    {
+      struct buffer *b = XBUFFER (it->w->contents);
+      struct buffer *old_buffer = NULL;
+      /* Needed so that buffer-local values can be determined; e.g., when switching
+      to the minibuffer. */
+      if (b != current_buffer)
+        {
+          old_buffer = current_buffer;
+          set_buffer_internal (b);
+        }
+      it->w->mc.lnum_pixel_width = (!NILP (Vdisplay_line_numbers))
+                                   ? tem_it.current_x
+                                   : 0;
+      if (old_buffer)
+        set_buffer_internal (old_buffer);
+    }
+
+/* *************************************************************************** */
+
+
   /* Record the width in pixels we need for the line number display.  */
   it->lnum_pixel_width = tem_it.current_x;
   /* Copy the produced glyphs into IT's glyph_row.  */