Subject: move_it_in_display_line / horizontal scrolling / tab stretch
From: Keith David Bershatsky <[email-masked]>
To: Emacs Bug Reports <[email-masked]>
Date: Mon, 04 Jun 2018 17:38:17 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Step 1.  Build a new Emacs (master branch) with the function bug-christmas-ghost (below).  [E.g., place the new function somewhere appropriate inside of xdisp.c.]

Step 2.  Launch the newly built Emacs from the terminal so that we can see the STDERR output.

Step 3.  Paste the following Lisp code into a scratch buffer and evaluate the code.

    (setq display-line-numbers t)
    (setq buffer-display-table (make-display-table))
    (aset buffer-display-table
          ?\t
          (vector (make-glyph-code ?\u00BB 'font-lock-warning-face)
                  (make-glyph-code ?\t 'highlight)))
    (setq tab-width 8)

Step 4.  On a new line in the scratch buffer type:  C-q TAB Hello

Step 5.  Place the cursor somewhere on the word "Hello".

Step 6.  Evaluate:  (scroll-left 2)

Step 7.  Evaluate:  (bug-christmas-ghost)

OBSERVATIONS:

A.  When w->hscroll is >= 2 and IT is within the tab STRETCH, move_it_in_display_line_to reports an erroneous it.hpos subsequent to the line numbers.  In this example, it.hpos is reported as being either 0 or 5, even though the tab STRETCH visibly begins at an it.hpos of 4 when line numbers are 2 digits wide.

B.  When w->hscroll is >= 2 and IT is within the tab STRETCH, the latter portion contains one or more it.c ghosts of Christmas future.  I.e., the letter "H" gets returned at several locations along the tab STRETCH.

EXPECTATIONS / DESIRED BEHAVIOR.

i.  Loops 7 to 12 should report an it.hpos of 4.

ii.  Loops 7 to 12 should report an it.c of 9 (aka a tab character).

Here is an excerpt from the STDERR output after calling (scroll-left 2) and moving IT by X -- incrementing each loop by font->space_width.

[ * * * BEGIN excerpt of STDERR output]

[IT.HPOS IS 4, NOT 0.]
7.  TAB STRETCH
    it.c (9)
    char (	)
    it.first_visible_x (14)
    w->hscroll (2)
    it.hpos (0)
    it.first_visible_x (14)
    it.current_x (7)
    relative_x (28)
    new_x (42)
    font->space_width (7)
    it.pixel_width (49)
    it.lnum_pixel_width (28)
    rc (MOVE_X_REACHED)

[IT.HPOS IS 4, NOT 0.]
8.  TAB STRETCH
    it.c (9)
    char (	)
    it.first_visible_x (14)
    w->hscroll (2)
    it.hpos (0)
    it.first_visible_x (14)
    it.current_x (7)
    relative_x (35)
    new_x (49)
    font->space_width (7)
    it.pixel_width (49)
    it.lnum_pixel_width (28)
    rc (MOVE_X_REACHED)

[IT.C IS 9, NOT 72; AND, IT.HPOS IS 4, NOT 5.]
9.  TEXT
    it.c (72)
    char (H)
    it.first_visible_x (14)
    w->hscroll (2)
    it.hpos (5)
    it.first_visible_x (14)
    it.current_x (84)
    relative_x (42)
    new_x (56)
    font->space_width (7)
    it.pixel_width (7)
    it.lnum_pixel_width (28)
    rc (MOVE_X_REACHED)

[IT.C IS 9, NOT 72; AND, IT.HPOS IS 4, NOT 5.]
10.  TEXT
    it.c (72)
    char (H)
    it.first_visible_x (14)
    w->hscroll (2)
    it.hpos (5)
    it.first_visible_x (14)
    it.current_x (84)
    relative_x (49)
    new_x (63)
    font->space_width (7)
    it.pixel_width (7)
    it.lnum_pixel_width (28)
    rc (MOVE_X_REACHED)

[IT.C IS 9, NOT 72; AND, IT.HPOS IS 4, NOT 5.]
11.  TEXT
    it.c (72)
    char (H)
    it.first_visible_x (14)
    w->hscroll (2)
    it.hpos (5)
    it.first_visible_x (14)
    it.current_x (84)
    relative_x (56)
    new_x (70)
    font->space_width (7)
    it.pixel_width (7)
    it.lnum_pixel_width (28)
    rc (MOVE_X_REACHED)

[IT.C IS 9, NOT 72; AND, IT.HPOS IS 4, NOT 5.]
12.  TEXT
    it.c (72)
    char (H)
    it.first_visible_x (14)
    w->hscroll (2)
    it.hpos (5)
    it.first_visible_x (14)
    it.current_x (84)
    relative_x (63)
    new_x (77)
    font->space_width (7)
    it.pixel_width (7)
    it.lnum_pixel_width (28)
    rc (MOVE_X_REACHED)

[THIS IS THE REAL LETTER "H"]
13.  TEXT
    it.c (72)
    char (H)
    it.first_visible_x (14)
    w->hscroll (2)
    it.hpos (5)
    it.first_visible_x (14)
    it.current_x (84)
    relative_x (70)
    new_x (84)
    font->space_width (7)
    it.pixel_width (7)
    it.lnum_pixel_width (28)
    rc (MOVE_X_REACHED)

[ * * * END excerpt of STDERR output]

DEFUN ("bug-christmas-ghost", Fbug_christmas_ghost, Sbug_christmas_ghost, 0, 0, 0,
       doc: /* Debug the Ghost of Christmas Future. */)
  (void)
{
/* ******************************************************************************
                      PRELIMINARY STUFF
****************************************************************************** */
  struct window *w = decode_live_window (selected_window);
  struct frame *f = XFRAME (w->frame);
  struct it it;
  void *itdata = bidi_shelve_cache ();
  enum move_it_result rc = MOVE_X_REACHED;
  struct text_pos start_text_position;
  int count = 0;
  int new_x = 0;
  int relative_x = 0;
/* ******************************************************************************
                      START DISPLAY -- w->start
****************************************************************************** */
  /* Begin the journey at w->start. */
  SET_TEXT_POS_FROM_MARKER (start_text_position, w->start);
  start_display (&it, w, start_text_position);
  struct face *face = FACE_FROM_ID (it.f, it.face_id);
  struct font *font = face->font;
/* ******************************************************************************
                GO TO THE BEGINNING OF THE CURRENT LINE.
****************************************************************************** */
  /* Place the IT on the current line containing PT. */
  int voffset = (WINDOW_HEADER_LINE_HEIGHT (w) > 0
                 && w->output_cursor.vpos > 0)
                  ? w->output_cursor.vpos - 1
                  : w->output_cursor.vpos;
  if (voffset > 0)
    move_it_by_lines (&it, voffset);
  struct glyph_row *glyph_row = MATRIX_ROW (w->current_matrix, it.vpos);
/* ******************************************************************************
             MOVE IT OVER EACH CHARACTER ON THE CURRENT LINE.
****************************************************************************** */
  while (true)
    {
      count += 1;
      if (new_x > 0)
        relative_x = new_x - it.first_visible_x;
      if (ITERATOR_AT_END_OF_LINE_P (&it)
          || FETCH_BYTE (IT_BYTEPOS (it)) == '\n'
          || rc == MOVE_POS_MATCH_OR_ZV)
        break;
/* ******************************************************************************
                       DUMP RELEVANT GLYPH INFORMATION
****************************************************************************** */
      if (w->hscroll > 0)
        {
          int w_hscroll = w->hscroll;
      fprintf (stderr, "\n%d.  %s\n\
    it.c (%d)\n\
    char (%s)\n\
    it.first_visible_x (%d)\n\
    w->hscroll (%d)\n\
    it.hpos (%d)\n\
    it.first_visible_x (%d)\n\
    it.current_x (%d)\n\
    relative_x (%d)\n\
    new_x (%d)\n\
    font->space_width (%d)\n\
    it.pixel_width (%d)\n\
    it.lnum_pixel_width (%d)\n\
    rc (%s)\n",
            count,
            (it.c == 0
              ? "NOTHING"
              : it.c == 187
                ? "TAB CHARACTER"
              : it.c == '\t'
                ? "TAB STRETCH"
              : "TEXT"),
            it.c,
            SSDATA (Fchar_to_string (make_number (it.c))),
            it.first_visible_x,
            w_hscroll,
            it.hpos,
            it.first_visible_x,
            it.current_x,
            relative_x,
            new_x,
            font->space_width,
            it.pixel_width,
            it.lnum_pixel_width,
            (rc == MOVE_UNDEFINED
              ? "MOVE_UNDEFINED"
              : rc == MOVE_POS_MATCH_OR_ZV
                ? "MOVE_POS_MATCH_OR_ZV"
              : rc == MOVE_X_REACHED
                ? "MOVE_X_REACHED"
              : rc == MOVE_LINE_CONTINUED
                ? "MOVE_LINE_CONTINUED"
              : rc == MOVE_LINE_TRUNCATED
                ? "MOVE_LINE_TRUNCATED"
              : rc == MOVE_NEWLINE_OR_CR
                ? "MOVE_NEWLINE_OR_CR"
              : "OOPS"));
        }
/* ******************************************************************************
                       MOVE IT -- INCREMENT == FONT->SPACE_WIDTH
****************************************************************************** */
      new_x += font->space_width;
      rc = move_it_in_display_line_to (&it, ZV, new_x, MOVE_TO_POS | MOVE_TO_X);
      if (rc == MOVE_LINE_CONTINUED)
        break;
      if (it.current_x - it.first_visible_x + font->space_width >=
          window_box_width (w, TEXT_AREA))
        break;
    }
/* ******************************************************************************
                         REDISPLAY AND DUMP_GLPYH_ROW
****************************************************************************** */
  redisplay_internal ();
  fprintf (stderr, "\n");
  dump_glyph_row (glyph_row, it.vpos, 2);
  bidi_unshelve_cache (itdata, false);
  return Qnil;
}