root/src/xdisp.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. it_char_has_category
  2. char_can_wrap_before
  3. char_can_wrap_after
  4. fill_column_indicator_column
  5. redisplay_other_windows
  6. wset_redisplay
  7. fset_redisplay
  8. bset_redisplay
  9. bset_update_mode_line
  10. wset_update_mode_line
  11. ATTRIBUTE_FORMAT_PRINTF
  12. ATTRIBUTE_FORMAT_PRINTF
  13. window_text_bottom_y
  14. window_box_width
  15. window_box_height
  16. window_box_left_offset
  17. window_box_right_offset
  18. window_box_left
  19. window_box_right
  20. window_box
  21. window_box_edges
  22. line_bottom_y
  23. DEFUN
  24. default_line_pixel_height
  25. string_from_display_spec
  26. window_hscroll_limited
  27. reset_box_start_end_flags
  28. pos_visible_p
  29. check_char_and_length
  30. string_pos_nchars_ahead
  31. string_pos
  32. c_string_pos
  33. number_of_chars
  34. compute_string_pos
  35. estimate_mode_line_height
  36. pixel_to_glyph_coords
  37. x_y_to_hpos_vpos
  38. frame_to_window_pixel_xy
  39. get_glyph_string_clip_rects
  40. get_glyph_string_clip_rect
  41. get_phys_cursor_geometry
  42. remember_mouse_glyph
  43. adjust_window_ends
  44. hscrolling_current_line_p
  45. safe_eval_handler
  46. safe__call
  47. safe_call
  48. safe_call1
  49. safe__call1
  50. safe_eval
  51. safe__eval
  52. safe_call2
  53. CHECK_IT
  54. CHECK_WINDOW_END
  55. init_iterator
  56. get_narrowed_width
  57. get_narrowed_len
  58. get_medium_narrowing_begv
  59. get_medium_narrowing_zv
  60. get_nearby_bol_pos
  61. get_small_narrowing_begv
  62. get_large_narrowing_begv
  63. get_large_narrowing_zv
  64. unwind_narrowed_begv
  65. start_display
  66. in_ellipses_for_invisible_text_p
  67. init_from_display_pos
  68. init_to_row_start
  69. init_to_row_end
  70. handle_stop
  71. compute_stop_pos
  72. compute_display_string_pos
  73. compute_display_string_end
  74. handle_fontified_prop
  75. face_at_pos
  76. handle_face_prop
  77. underlying_face_id
  78. face_before_or_after_it_pos
  79. handle_invisible_prop
  80. setup_for_ellipsis
  81. find_display_property
  82. get_display_property
  83. display_min_width
  84. handle_display_prop
  85. handle_display_spec
  86. display_prop_end
  87. handle_single_display_spec
  88. display_prop_intangible_p
  89. single_display_spec_string_p
  90. display_prop_string_p
  91. string_buffer_position_lim
  92. string_buffer_position
  93. handle_composition_prop
  94. handle_overlay_change
  95. next_overlay_string
  96. compare_overlay_entries
  97. load_overlay_strings
  98. get_overlay_strings_1
  99. get_overlay_strings
  100. push_it
  101. iterate_out_of_display_property
  102. restore_face_box_flags
  103. pop_it
  104. back_to_previous_line_start
  105. strings_with_newlines
  106. forward_to_next_line_start
  107. back_to_previous_visible_line_start
  108. reseat_at_previous_visible_line_start
  109. reseat_at_next_visible_line_start
  110. reseat
  111. reseat_1
  112. reseat_to_string
  113. lookup_glyphless_char_display
  114. merge_escape_glyph_face
  115. merge_glyphless_glyph_face
  116. forget_escape_and_glyphless_faces
  117. get_next_display_element
  118. set_iterator_to_next
  119. next_element_from_display_vector
  120. get_visually_first_element
  121. next_element_from_string
  122. next_element_from_c_string
  123. next_element_from_ellipsis
  124. next_element_from_image
  125. next_element_from_xwidget
  126. next_element_from_stretch
  127. compute_stop_pos_backwards
  128. handle_stop_backwards
  129. next_element_from_buffer
  130. next_element_from_composition
  131. move_it_in_display_line_to
  132. move_it_in_display_line
  133. move_it_to
  134. move_it_vertically_backward
  135. move_it_vertically
  136. move_it_past_eol
  137. move_it_by_lines
  138. partial_line_height
  139. fast_move_it_horizontally
  140. in_display_vector_p
  141. window_text_pixel_size
  142. DEFUN
  143. format_nargs
  144. add_to_log
  145. vadd_to_log
  146. message_log_maybe_newline
  147. message_dolog
  148. message_log_check_duplicate
  149. message3
  150. message_to_stderr
  151. message3_nolog
  152. message1
  153. message1_nolog
  154. message_with_string
  155. ATTRIBUTE_FORMAT_PRINTF
  156. message
  157. update_echo_area
  158. ensure_echo_area_buffers
  159. with_echo_area_buffer
  160. with_echo_area_buffer_unwind_data
  161. unwind_with_echo_area_buffer
  162. setup_echo_area_for_printing
  163. display_echo_area
  164. display_echo_area_1
  165. resize_echo_area_exactly
  166. resize_mini_window_1
  167. resize_mini_window
  168. current_message
  169. current_message_1
  170. push_message
  171. restore_message
  172. pop_message_unwind
  173. check_message_stack
  174. clear_message_stack
  175. truncate_echo_area
  176. truncate_message_1
  177. set_message
  178. set_message_1
  179. clear_message
  180. clear_garbaged_frames
  181. echo_area_display
  182. window_buffer_changed
  183. mode_line_update_needed
  184. window_frozen_p
  185. format_mode_line_unwind_data
  186. unwind_format_mode_line
  187. store_mode_line_noprop_char
  188. store_mode_line_noprop
  189. gui_consider_frame_title
  190. needs_no_redisplay
  191. prepare_menu_bars
  192. update_menu_bar
  193. restore_selected_window
  194. restore_frame_selected_window
  195. update_tab_bar
  196. display_tab_bar
  197. build_desired_tab_bar_string
  198. display_tab_bar_line
  199. tab_bar_height
  200. redisplay_tab_bar
  201. tab_bar_item_info
  202. get_tab_bar_item
  203. handle_tab_bar_click
  204. note_tab_bar_highlight
  205. tty_get_tab_bar_item
  206. tty_handle_tab_bar_click
  207. update_tool_bar
  208. build_desired_tool_bar_string
  209. display_tool_bar_line
  210. tool_bar_height
  211. redisplay_tool_bar
  212. tool_bar_item_info
  213. get_tool_bar_item
  214. handle_tool_bar_click_with_device
  215. handle_tool_bar_click
  216. note_tool_bar_highlight
  217. hscroll_window_tree
  218. hscroll_windows
  219. debug_method_add
  220. text_outside_line_unchanged_p
  221. redisplay
  222. overlay_arrow_string_or_property
  223. overlay_arrow_in_current_buffer_p
  224. overlay_arrows_changed_p
  225. update_overlay_arrows
  226. overlay_arrow_at_row
  227. check_point_in_composition
  228. reconsider_clip_changes
  229. propagate_buffer_redisplay
  230. redisplay_internal
  231. unwind_redisplay_preserve_echo_area
  232. redisplay_preserve_echo_area
  233. unwind_redisplay
  234. unwind_display_working_on_window
  235. mark_window_display_accurate_1
  236. mark_window_display_accurate
  237. disp_char_vector
  238. block_buffer_flips
  239. unblock_buffer_flips
  240. buffer_flipping_blocked_p
  241. redisplay_windows
  242. redisplay_window_error
  243. redisplay_window_0
  244. redisplay_window_1
  245. update_redisplay_ticks
  246. set_cursor_from_row
  247. run_window_scroll_functions
  248. cursor_row_fully_visible_p
  249. try_scrolling
  250. compute_window_start_on_continuation_line
  251. try_cursor_movement
  252. set_vertical_scroll_bar
  253. set_horizontal_scroll_bar
  254. window_start_acceptable_p
  255. DEFUN
  256. redisplay_window
  257. try_window
  258. try_window_reusing_current_matrix
  259. find_last_row_displaying_text
  260. find_last_unchanged_at_beg_row
  261. find_first_unchanged_at_end_row
  262. sync_frame_with_window_matrix_rows
  263. row_containing_pos
  264. try_window_id
  265. dump_glyph_row
  266. dump_glyph
  267. dump_glyph_row
  268. DEFUN
  269. DEFUN
  270. DEFUN
  271. get_overlay_arrow_glyph_row
  272. insert_left_trunc_glyphs
  273. row_hash
  274. compute_line_metrics
  275. clear_position
  276. append_space_for_newline
  277. extend_face_to_end_of_line
  278. trailing_whitespace_p
  279. highlight_trailing_whitespace
  280. row_for_charpos_p
  281. cursor_row_p
  282. push_prefix_prop
  283. get_it_property
  284. get_line_prefix_it_property
  285. handle_line_prefix
  286. unproduce_glyphs
  287. find_row_edges
  288. display_count_lines_logically
  289. display_count_lines_visually
  290. maybe_produce_line_number
  291. should_produce_line_number
  292. row_text_area_empty
  293. display_line
  294. DEFUN
  295. DEFUN
  296. DEFUN
  297. display_menu_bar
  298. deep_copy_glyph_row
  299. display_tty_menu_item
  300. redisplay_mode_lines
  301. display_mode_lines
  302. display_mode_line
  303. move_elt_to_front
  304. safe_set_text_properties
  305. display_mode_element
  306. store_mode_line_string
  307. pint2str
  308. pint2hrstr
  309. decode_mode_spec_coding
  310. percent99
  311. decode_mode_spec
  312. count_lines
  313. display_count_lines
  314. display_string
  315. invisible_prop
  316. DEFUN
  317. calc_pixel_width_or_height
  318. get_font_ascent_descent
  319. dump_glyph_string
  320. init_glyph_string
  321. append_glyph_string_lists
  322. prepend_glyph_string_lists
  323. append_glyph_string
  324. get_char_face_and_encoding
  325. get_glyph_face_and_encoding
  326. get_char_glyph_code
  327. fill_composite_glyph_string
  328. fill_gstring_glyph_string
  329. fill_glyphless_glyph_string
  330. fill_glyph_string
  331. fill_image_glyph_string
  332. fill_xwidget_glyph_string
  333. fill_stretch_glyph_string
  334. get_per_char_metric
  335. normal_char_ascent_descent
  336. normal_char_height
  337. gui_get_glyph_overhangs
  338. left_overwritten
  339. left_overwriting
  340. right_overwritten
  341. right_overwriting
  342. set_glyph_string_background_width
  343. glyph_string_containing_background_width
  344. compute_overhangs_and_x
  345. draw_glyphs
  346. font_for_underline_metrics
  347. append_glyph
  348. append_composite_glyph
  349. take_vertical_position_into_account
  350. produce_image_glyph
  351. produce_xwidget_glyph
  352. append_stretch_glyph
  353. produce_stretch_glyph
  354. produce_special_glyphs
  355. calc_line_height_property
  356. append_glyphless_glyph
  357. produce_glyphless_glyph
  358. gui_produce_glyphs
  359. gui_write_glyphs
  360. gui_insert_glyphs
  361. gui_clear_end_of_line
  362. get_specified_cursor_type
  363. set_frame_cursor_types
  364. get_window_cursor_type
  365. notice_overwritten_cursor
  366. gui_fix_overlapping_area
  367. draw_phys_cursor_glyph
  368. erase_phys_cursor
  369. display_and_set_cursor
  370. update_window_cursor
  371. update_cursor_in_window_tree
  372. gui_update_cursor
  373. gui_clear_cursor
  374. draw_row_with_mouse_face
  375. show_mouse_face
  376. clear_mouse_face
  377. coords_in_mouse_face_p
  378. cursor_in_mouse_face_p
  379. rows_from_pos_range
  380. mouse_face_from_buffer_pos
  381. fast_find_string_pos
  382. mouse_face_from_string_pos
  383. on_hot_spot_p
  384. find_hot_spot
  385. define_frame_cursor1
  386. note_mode_line_or_margin_highlight
  387. note_mouse_highlight
  388. gui_clear_window_mouse_face
  389. cancel_mouse_face
  390. expose_area
  391. expose_line
  392. expose_overlaps
  393. phys_cursor_in_rect_p
  394. gui_draw_vertical_border
  395. gui_draw_right_divider
  396. gui_draw_bottom_divider
  397. expose_window
  398. expose_window_tree
  399. expose_frame
  400. gui_intersect_rectangles
  401. syms_of_xdisp
  402. init_xdisp
  403. show_hourglass
  404. start_hourglass
  405. cancel_hourglass
  406. adjust_glyph_width_for_mouse_face
  407. get_cursor_offset_for_mouse_face

     1 /* Display generation from window structure and buffer text.
     2 
     3 Copyright (C) 1985-2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     7 GNU Emacs is free software: you can redistribute it and/or modify
     8 it under the terms of the GNU General Public License as published by
     9 the Free Software Foundation, either version 3 of the License, or (at
    10 your option) any later version.
    11 
    12 GNU Emacs is distributed in the hope that it will be useful,
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 GNU General Public License for more details.
    16 
    17 You should have received a copy of the GNU General Public License
    18 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
    21 
    22    Redisplay.
    23 
    24    Emacs separates the task of updating the display -- which we call
    25    "redisplay" -- from the code modifying global state, e.g. buffer
    26    text.  This way functions operating on buffers don't also have to
    27    be concerned with updating the display as result of their
    28    operations.
    29 
    30    Redisplay is triggered by the Lisp interpreter when it decides it's
    31    time to do it.  This is done either automatically for you as part
    32    of the interpreter's command loop, or as the result of calling Lisp
    33    functions like `sit-for'.  The C function `redisplay_internal' in
    34    xdisp.c is the only entry into the inner redisplay code.
    35 
    36    The following diagram shows how redisplay code is invoked.  As you
    37    can see, Lisp calls redisplay and vice versa.
    38 
    39    Under window systems like X, some portions of the redisplay code
    40    are also called asynchronously, due to mouse movement or expose
    41    events.  "Asynchronously" in this context means that any C function
    42    which calls maybe_quit or process_pending_signals could enter
    43    redisplay via expose_frame and/or note_mouse_highlight, if X events
    44    were recently reported to Emacs about mouse movements or frame(s)
    45    that were exposed.  And such redisplay could invoke the Lisp
    46    interpreter, e.g. via the :eval forms in mode-line-format, and as
    47    result the global state could change.  It is therefore very
    48    important that C functions which might cause such "asynchronous"
    49    redisplay, but cannot tolerate the results, use
    50    block_input/unblock_input around code fragments which assume that
    51    global Lisp state doesn't change.  If you don't follow this rule,
    52    you will encounter bugs which are very hard to explain.  One place
    53    that needs to take such precautions is timer_check, some of whose
    54    code cannot tolerate changes in timer alists while it processes
    55    timers.
    56 
    57    +--------------+   redisplay     +----------------+
    58    | Lisp machine |---------------->| Redisplay code |<--+
    59    +--------------+   (xdisp.c)     +----------------+   |
    60           ^                                  |           |
    61           +----------------------------------+           |
    62             Block input to prevent this when             |
    63             called asynchronously!                       |
    64                                                          |
    65                     note_mouse_highlight (asynchronous)  |
    66                                                          |
    67                                     X mouse events  -----+
    68                                                          |
    69                             expose_frame (asynchronous)  |
    70                                                          |
    71                                    X expose events  -----+
    72 
    73    What does redisplay do?  Obviously, it has to figure out somehow what
    74    has been changed since the last time the display has been updated,
    75    and to make these changes visible.  Preferably it would do that in
    76    a moderately intelligent way, i.e. fast.
    77 
    78    At its highest level, redisplay can be divided into 3 distinct
    79    steps, all of which are visible in `redisplay_internal':
    80 
    81     . decide which frames need their windows to be considered for redisplay
    82     . for each window whose display might need to be updated, compute
    83       a structure, called "glyph matrix", which describes how it
    84       should look on display
    85     . actually update the display of windows on the glass where the
    86       newly obtained glyph matrix differs from the one produced by the
    87       previous redisplay cycle
    88 
    89    The first of these steps is done by `redisplay_internal' itself, by
    90    looping through all the frames and testing their various flags,
    91    such as their visibility.  The result of this could be that only
    92    the selected window on the selected frame must be redisplayed, or
    93    it could conclude that other windows need to be considered as well.
    94 
    95    The second step considers each window that might need to be
    96    redisplayed.  This could be only the selected window, or the window
    97    trees of one or more frames.  The function which considers a window
    98    and decides whether it actually needs redisplay is
    99    `redisplay_window'.  It does so by looking at the changes in
   100    position of point, in buffer text, in text properties, overlays,
   101    etc.  These changes can be deduced from window and buffer
   102    structures, and from some global variables like `beg_unchanged' and
   103    `end_unchanged'.  The current contents of the display are recorded
   104    in a `glyph matrix', a two-dimensional matrix of glyph structures.
   105    Each row in such a matrix corresponds to a line on the display, and
   106    each glyph in a row corresponds to a column displaying a character,
   107    an image, or what else.  This matrix is called the `current glyph
   108    matrix', or `current matrix', in redisplay terminology.
   109 
   110    For buffer parts that have been changed since the last redisplay,
   111    `redisplay_window' constructs a second glyph matrix, the so called
   112    `desired glyph matrix' or short `desired matrix'.  It does so in
   113    the most optimal way possible, avoiding the examination of text
   114    that didn't change, reusing portions of the current matrix if
   115    possible, etc.  It could, in particular, decide that a window
   116    doesn't need to be redisplayed at all.
   117 
   118    This second step of redisplay also updates the parts of the desired
   119    matrix that correspond to the mode lines, header lines, and
   120    tab-lines of the windows which need that; see `display_mode_lines'.
   121 
   122    In the third and last step, the current and desired matrix are then
   123    compared to find a cheap way to update the display, e.g. by reusing
   124    part of the display by scrolling lines.  The actual update of the
   125    display of each window, by comparing the desired and the current
   126    matrix, is done by `update_window', which calls functions which
   127    draw to the glass (those functions are specific to the type of the
   128    window's frame: X, w32, NS, etc.).
   129 
   130    Once the display of a window on the glass has been updated, its
   131    desired matrix is used to update the corresponding rows of the
   132    current matrix, and then the desired matrix is discarded.
   133 
   134    You will find a lot of redisplay optimizations when you start
   135    looking at the innards of `redisplay_window'.  The overall goal of
   136    all these optimizations is to make redisplay fast because it is
   137    done frequently.  Some of these optimizations are implemented by
   138    the following functions:
   139 
   140     . try_cursor_movement
   141 
   142       This optimization is applicable if the text in the window did
   143       not change and did not scroll, only point moved, and it did not
   144       move off the displayed portion of the text.  In that case, the
   145       window's glyph matrix is still valid, and only the position of
   146       the cursor might need to be updated.
   147 
   148     . try_window_reusing_current_matrix
   149 
   150       This function reuses the current glyph matrix of a window when
   151       text has not changed, but the window start changed (e.g., due to
   152       scrolling).
   153 
   154     . try_window_id
   155 
   156       This function attempts to update a window's glyph matrix by
   157       reusing parts of its current glyph matrix.  It finds and reuses
   158       the part that was not changed, and regenerates the rest.  (The
   159       "id" part in the function's name stands for "insert/delete", not
   160       for "identification" or somesuch.)
   161 
   162     . try_window
   163 
   164       This function performs the full, unoptimized, generation of a
   165       single window's glyph matrix, assuming that its fonts were not
   166       changed and that the cursor will not end up in the scroll
   167       margins.  (Loading fonts requires re-adjustment of dimensions of
   168       glyph matrices, which makes this method impossible to use.)
   169 
   170    The optimizations are tried in sequence (some can be skipped if
   171    it is known that they are not applicable).  If none of the
   172    optimizations were successful, redisplay calls redisplay_windows,
   173    which performs a full redisplay of all windows.
   174 
   175    Note that there's one more important optimization up Emacs's
   176    sleeve, but it is related to actually redrawing the potentially
   177    changed portions of the window/frame as part of the third step, not
   178    to generating the desired matrices of those potentially changed
   179    portions.  Namely, the function `update_frame' and its subroutines,
   180    which you will find in dispnew.c, compare the desired matrices with
   181    the current matrices, and only redraw the portions that changed.
   182    So it could happen that the functions in this file for some reason
   183    decide that the entire desired matrix needs to be regenerated from
   184    scratch, and still only parts of the Emacs display, or even nothing
   185    at all, will be actually delivered to the glass, because
   186    `update_frame' has found that the new and the old screen contents
   187    are similar or identical.
   188 
   189    Desired matrices.
   190 
   191    Desired matrices are always built per Emacs window.  It is
   192    important to know that a desired matrix is in general "sparse": it
   193    only has some of the glyph rows "enabled".  This is because
   194    redisplay tries to optimize its work, and thus only generates
   195    glyphs for rows that need to be updated on the screen.  Rows that
   196    don't need to be updated are left "disabled", and their contents
   197    in the desired matrix should be ignored.
   198 
   199    The function `display_line' is the central function to look at if
   200    you are interested in how the rows of the desired matrix are
   201    produced.  It constructs one row in a desired matrix given an
   202    iterator structure containing both a buffer position and a
   203    description of the environment in which the text is to be
   204    displayed.  But this is too early, read on.
   205 
   206    Glyph rows.
   207 
   208    A glyph row is an array of `struct glyph', where each glyph element
   209    describes a "display element" to be shown on the screen.  More
   210    accurately, a glyph row can have up to 3 different arrays of
   211    glyphs: one each for every display margins, and one for the "text
   212    area", where buffer text is displayed.  The text-area glyph array
   213    is always present, whereas the arrays for the marginal areas are
   214    present (non-empty) only if the corresponding display margin is
   215    shown in the window.  If the glyph array for a marginal area is not
   216    present its beginning and end coincide, i.e. such arrays are
   217    actually empty (they contain no glyphs).  Frame glyph matrices, used
   218    on text-mode terminals (see below) never have marginal areas; they
   219    treat the entire frame-wide row of glyphs as a single large "text
   220    area".
   221 
   222    Iteration over buffer and strings.
   223 
   224    Characters and pixmaps displayed for a range of buffer text depend
   225    on various settings of buffers and windows, on overlays and text
   226    properties, on display tables, on selective display.  The good news
   227    is that all this hairy stuff is hidden behind a small set of
   228    interface functions taking an iterator structure (`struct it')
   229    argument.
   230 
   231    Iteration over things to be displayed is then simple.  It is
   232    started by initializing an iterator with a call to `init_iterator',
   233    passing it the buffer position where to start iteration.  For
   234    iteration over strings, pass -1 as the position to `init_iterator',
   235    and call `reseat_to_string' when the string is ready, to initialize
   236    the iterator for that string.  Thereafter, calls to
   237    `get_next_display_element' fill the iterator structure with
   238    relevant information about the next thing to display.  Calls to
   239    `set_iterator_to_next' move the iterator to the next thing.
   240 
   241    Besides this, an iterator also contains information about the
   242    display environment in which glyphs for display elements are to be
   243    produced.  It has fields for the width and height of the display,
   244    the information whether long lines are truncated or continued, a
   245    current X and Y position, the face currently in effect, and lots of
   246    other stuff you can better see in dispextern.h.
   247 
   248    The "stop position".
   249 
   250    Some of the fields maintained by the iterator change relatively
   251    infrequently.  These include the face of the characters, whether
   252    text is invisible, the object (buffer or display or overlay string)
   253    being iterated, character composition info, etc.  For any given
   254    buffer or string position, the sources of information that affects
   255    the display can be determined by calling the appropriate
   256    primitives, such as `Fnext_single_property_change', but both these
   257    calls and the processing of their return values is relatively
   258    expensive.  To optimize redisplay, the display engine checks these
   259    sources of display information only when needed, not for every
   260    character.  To that end, it always maintains the position of the
   261    next place where it must stop and re-examine all those potential
   262    sources.  This is called "the stop position" and is stored in the
   263    `stop_charpos' field of the iterator.  The stop position is updated
   264    by `compute_stop_pos', which is called whenever the iteration
   265    reaches the current stop position and processes it.  Processing a
   266    stop position is done by `handle_stop', which invokes a series of
   267    handlers, one each for every potential source of display-related
   268    information; see the `it_props' array for those handlers.  For
   269    example, one handler is `handle_face_prop', which detects changes
   270    in face properties, and supplies the face ID that the iterator will
   271    use for all the glyphs it generates up to the next stop position;
   272    this face ID is the result of "realizing" the face specified by the
   273    relevant text properties at this position (see xfaces.c).  Each
   274    handler called by `handle_stop' processes the sources of display
   275    information for which it is "responsible", and returns a value
   276    which tells `handle_stop' what to do next.
   277 
   278    Once `handle_stop' returns, the information it stores in the
   279    iterator fields will not be refreshed until the iteration reaches
   280    the next stop position, which is computed by `compute_stop_pos'
   281    called at the end of `handle_stop'.  `compute_stop_pos' examines
   282    the buffer's or string's interval tree to determine where the text
   283    properties change, finds the next position where overlays and
   284    character composition can change, and stores in `stop_charpos' the
   285    closest position where any of these factors should be reconsidered.
   286 
   287    Handling of the stop position is done as part of the code in
   288    `get_next_display_element'.
   289 
   290    Producing glyphs.
   291 
   292    Glyphs in a desired matrix are normally constructed in a loop
   293    calling `get_next_display_element' and then `PRODUCE_GLYPHS'.  The
   294    call to `PRODUCE_GLYPHS' will fill the iterator structure with
   295    pixel information about the element being displayed and at the same
   296    time will produce glyphs for it.  If the display element fits on
   297    the line being displayed, `set_iterator_to_next' is called next,
   298    otherwise the glyphs produced are discarded, and `display_line'
   299    marks this glyph row as a "continued line".  The function
   300    `display_line' is the workhorse of filling glyph rows in the
   301    desired matrix with glyphs.  In addition to producing glyphs, it
   302    also handles line truncation and continuation, word wrap, and
   303    cursor positioning (for the latter, see `set_cursor_from_row').
   304 
   305    Frame matrices.
   306 
   307    That just couldn't be all, could it?  What about terminal types not
   308    supporting operations on sub-windows of the screen (a.k.a. "TTY" or
   309    "text-mode terminals")?  To update the display on such a terminal,
   310    window-based glyph matrices are not well suited.  To be able to
   311    reuse part of the display (scrolling lines up and down), we must
   312    instead have a view of the whole screen.  This is what `frame
   313    matrices' are for.  They are a trick.
   314 
   315    Frames on text terminals have a glyph pool.  Windows on such a
   316    frame sub-allocate their glyph memory from their frame's glyph
   317    pool.  The frame itself is given its own glyph matrices.  By
   318    coincidence---or maybe something else---rows in window glyph
   319    matrices are slices of corresponding rows in frame matrices.  Thus
   320    writing to window matrices implicitly updates a frame matrix which
   321    provides us with the view of the whole screen that we originally
   322    wanted to have without having to move many bytes around.  Then
   323    updating all the visible windows on text-terminal frames is done by
   324    using the frame matrices, which allows frame-global optimization of
   325    what is actually written to the glass.
   326 
   327    Frame matrices don't have marginal areas, only a text area.  That
   328    is, the entire row of glyphs that spans the width of a text-mode
   329    frame is treated as a single large "text area" for the purposes of
   330    manipulating and updating a frame glyph matrix.
   331 
   332    To be honest, there is a little bit more done for frame matrices,
   333    but not much more.  If you plan to extend that code, take a look at
   334    dispnew.c.  The function build_frame_matrix is a good starting
   335    point.
   336 
   337    Simulating display.
   338 
   339    Some of Emacs commands and functions need to take display layout
   340    into consideration.  For example, C-n moves to the next screen
   341    line, but to implement that, Emacs needs to find the buffer
   342    position which is directly below the cursor position on display.
   343    This is not trivial when buffer display includes variable-size
   344    elements such as different fonts, tall images, etc.
   345 
   346    To solve this problem, the display engine implements several
   347    functions that can move through buffer text in the same manner as
   348    `display_line' and `display_string' do, but without producing any
   349    glyphs for the glyph matrices.  The workhorse of this is
   350    `move_it_in_display_line_to'.  Its code and logic are very similar
   351    to `display_line', but it differs in two important aspects: it
   352    doesn't produce glyphs for any glyph matrix, and it returns a
   353    status telling the caller how it ended the iteration: whether it
   354    reached the required position, hit the end of line, arrived at the
   355    window edge without exhausting the buffer's line, etc.  Since the
   356    glyphs are not produced, the layout information available to the
   357    callers of this function is what is recorded in `struct it' by the
   358    iteration process.
   359 
   360    Several higher-level functions call `move_it_in_display_line_to' to
   361    perform more complex tasks: `move_it_by_lines' can move N lines up
   362    or down from a given buffer position and `move_it_to' can move to a
   363    given buffer position or to a given X or Y pixel coordinate.
   364 
   365    These functions are called by the display engine itself as well,
   366    when it needs to make layout decisions before producing the glyphs.
   367    For example, one of the first things to decide when redisplaying a
   368    window is where to put the `window-start' position; if the window
   369    is to be recentered (the default), Emacs makes that decision by
   370    starting from the position of point, then moving up the number of
   371    lines corresponding to half the window height using
   372    `move_it_by_lines'.
   373 
   374    Bidirectional display.
   375 
   376    Bidirectional display adds quite some hair to this already complex
   377    design.  The good news are that a large portion of that hairy stuff
   378    is hidden in bidi.c behind only 3 interfaces.  bidi.c implements a
   379    reordering engine which is called by `set_iterator_to_next' and
   380    returns the next character to display in the visual order.  See
   381    commentary on bidi.c for more details.  As far as redisplay is
   382    concerned, the effect of calling `bidi_move_to_visually_next', the
   383    main interface of the reordering engine, is that the iterator gets
   384    magically placed on the buffer or string position that is to be
   385    displayed next in the visual order.  In other words, a linear
   386    iteration through the buffer/string is replaced with a non-linear
   387    one.  All the rest of the redisplay is oblivious to the bidi
   388    reordering.
   389 
   390    Well, almost oblivious---there are still complications, most of
   391    them due to the fact that buffer and string positions no longer
   392    change monotonously with glyph indices in a glyph row.  Moreover,
   393    for continued lines, the buffer positions may not even be
   394    monotonously changing with vertical positions.  Also, accounting
   395    for face changes, overlays, etc. becomes more complex because
   396    non-linear iteration could potentially skip many positions with
   397    such changes, and then cross them again on the way back (see
   398    `handle_stop_backwards')...
   399 
   400    One other prominent effect of bidirectional display is that some
   401    paragraphs of text need to be displayed starting at the right
   402    margin of the window---the so-called right-to-left, or R2L
   403    paragraphs.  R2L paragraphs are displayed with R2L glyph rows,
   404    which have their `reversed_p' flag set.  The bidi reordering engine
   405    produces characters in such rows starting from the character which
   406    should be the rightmost on display.  `PRODUCE_GLYPHS' then reverses
   407    the order, when it fills up the glyph row whose `reversed_p' flag
   408    is set, by prepending each new glyph to what is already there,
   409    instead of appending it.  When the glyph row is complete, the
   410    function `extend_face_to_end_of_line' fills the empty space to the
   411    left of the leftmost character with special glyphs, which will
   412    display as, well, empty.  On text terminals, these special glyphs
   413    are simply blank characters.  On graphics terminals, there's a
   414    single stretch glyph of a suitably computed width.  Both the blanks
   415    and the stretch glyph are given the face of the background of the
   416    line.  This way, the terminal-specific back-end can still draw the
   417    glyphs left to right, even for R2L lines.
   418 
   419    Bidirectional display and character compositions.
   420 
   421    Some scripts cannot be displayed by drawing each character
   422    individually, because adjacent characters change each other's shape
   423    on display.  For example, Arabic and Indic scripts belong to this
   424    category.
   425 
   426    Emacs display supports this by providing "character compositions",
   427    most of which is implemented in composite.c.  During the buffer
   428    scan that delivers characters to `PRODUCE_GLYPHS', if the next
   429    character to be delivered is a composed character, the iteration
   430    calls `composition_reseat_it' and `next_element_from_composition'.
   431    If they succeed to compose the character with one or more of the
   432    following characters, the whole sequence of characters that were
   433    composed is recorded in the `struct composition_it' object that is
   434    part of the buffer iterator.  The composed sequence could produce
   435    one or more font glyphs (called "grapheme clusters") on the screen.
   436    Each of these grapheme clusters is then delivered to
   437    `PRODUCE_GLYPHS' in the direction corresponding to the current bidi
   438    scan direction (recorded in the `scan_dir' member of the `struct
   439    bidi_it' object that is part of the iterator).  In particular, if
   440    the bidi iterator currently scans the buffer backwards, the
   441    grapheme clusters are delivered back to front.  This reorders the
   442    grapheme clusters as appropriate for the current bidi context.
   443    Note that this means that the grapheme clusters are always stored
   444    in the `LGSTRING' object (see composite.c) in the logical order.
   445 
   446    Moving an iterator in bidirectional text
   447    without producing glyphs.
   448 
   449    Note one important detail mentioned above: that the bidi reordering
   450    engine, driven by the iterator, produces characters in R2L rows
   451    starting at the character that will be the rightmost on display.
   452    As far as the iterator is concerned, the geometry of such rows is
   453    still left to right, i.e. the iterator "thinks" the first character
   454    is at the leftmost pixel position.  The iterator does not know that
   455    `PRODUCE_GLYPHS' reverses the order of the glyphs that the iterator
   456    delivers.  This is important when functions from the `move_it_*'
   457    family are used to get to certain screen position or to match
   458    screen coordinates with buffer coordinates: these functions use the
   459    iterator geometry, which is left to right even in R2L paragraphs.
   460    This works well with most callers of `move_it_*', because they need
   461    to get to a specific column, and columns are still numbered in the
   462    reading order, i.e. the rightmost character in a R2L paragraph is
   463    still column zero.  But some callers do not get well with this; a
   464    notable example is mouse clicks that need to find the character
   465    that corresponds to certain pixel coordinates.  See
   466    `buffer_posn_from_coords' in dispnew.c for how this is handled.  */
   467 
   468 #include <config.h>
   469 #include <stdlib.h>
   470 #include <limits.h>
   471 #include <math.h>
   472 
   473 #include "lisp.h"
   474 #include "atimer.h"
   475 #include "composite.h"
   476 #include "keyboard.h"
   477 #include "sysstdio.h"
   478 #include "systime.h"
   479 #include "frame.h"
   480 #include "window.h"
   481 #include "termchar.h"
   482 #include "dispextern.h"
   483 #include "character.h"
   484 #include "category.h"
   485 #include "buffer.h"
   486 #include "charset.h"
   487 #include "indent.h"
   488 #include "commands.h"
   489 #include "keymap.h"
   490 #include "disptab.h"
   491 #include "termhooks.h"
   492 #include "termopts.h"
   493 #include "intervals.h"
   494 #include "coding.h"
   495 #include "region-cache.h"
   496 #include "font.h"
   497 #include "fontset.h"
   498 #include "blockinput.h"
   499 #include "xwidget.h"
   500 #ifdef HAVE_WINDOW_SYSTEM
   501 #include TERM_HEADER
   502 #endif /* HAVE_WINDOW_SYSTEM */
   503 
   504 #ifndef FRAME_OUTPUT_DATA
   505 #define FRAME_OUTPUT_DATA(f) (NULL)
   506 #endif
   507 
   508 #define DISP_INFINITY 10000000
   509 
   510 /* Holds the list (error).  */
   511 static Lisp_Object list_of_error;
   512 
   513 #ifdef HAVE_WINDOW_SYSTEM
   514 
   515 /* Test if overflow newline into fringe.  Called with iterator IT
   516    at or past right window margin, and with IT->current_x set.  */
   517 
   518 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT)             \
   519   (!NILP (Voverflow_newline_into_fringe)                \
   520    && FRAME_WINDOW_P ((IT)->f)                          \
   521    && ((IT)->bidi_it.paragraph_dir == R2L               \
   522        ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0)       \
   523        : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0))     \
   524    && (IT)->current_x == (IT)->last_visible_x)
   525 
   526 #else /* !HAVE_WINDOW_SYSTEM */
   527 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
   528 #endif /* HAVE_WINDOW_SYSTEM */
   529 
   530 /* Test if the display element loaded in IT, or the underlying buffer
   531    or string character, is a space or a TAB character.  This is used
   532    to determine where word wrapping can occur.  */
   533 
   534 #define IT_DISPLAYING_WHITESPACE(it)                                    \
   535   ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))        \
   536    || ((STRINGP (it->string)                                            \
   537         && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' '           \
   538             || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t'))     \
   539        || (it->s                                                        \
   540            && (it->s[IT_BYTEPOS (*it)] == ' '                           \
   541                || it->s[IT_BYTEPOS (*it)] == '\t'))                     \
   542        || (IT_BYTEPOS (*it) < ZV_BYTE                                   \
   543            && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' '                 \
   544                || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t'))))
   545 
   546 /* These are the category sets we use.  They are defined by
   547    kinsoku.el and characters.el.  */
   548 #define NOT_AT_EOL '<'
   549 #define NOT_AT_BOL '>'
   550 #define LINE_BREAKABLE '|'
   551 
   552 static bool
   553 it_char_has_category(struct it *it, int cat)
   554 {
   555   int ch = 0;
   556   if (it->what == IT_CHARACTER)
   557     ch = it->c;
   558   else if (STRINGP (it->string))
   559     ch = SREF (it->string, IT_STRING_BYTEPOS (*it));
   560   else if (it->s)
   561     ch = it->s[IT_BYTEPOS (*it)];
   562   else if (IT_BYTEPOS (*it) < ZV_BYTE)
   563     ch = *BYTE_POS_ADDR (IT_BYTEPOS (*it));
   564 
   565   if (ch == 0)
   566     return false;
   567   else
   568     return CHAR_HAS_CATEGORY (ch, cat);
   569 }
   570 
   571 /* Return true if the current character allows wrapping before it.   */
   572 static bool
   573 char_can_wrap_before (struct it *it)
   574 {
   575   if (!word_wrap_by_category)
   576     return !IT_DISPLAYING_WHITESPACE (it);
   577 
   578   /* For CJK (LTR) text in RTL paragraph, EOL and BOL are flipped.
   579      Because in RTL paragraph, each glyph is prepended to the last
   580      one, effectively drawing right to left.  */
   581   int not_at_bol;
   582   if (it->glyph_row && it->glyph_row->reversed_p)
   583     not_at_bol = NOT_AT_EOL;
   584   else
   585     not_at_bol = NOT_AT_BOL;
   586   /* You cannot wrap before a space or tab because that way you'll
   587      have space and tab at the beginning of next line.  */
   588   return (!IT_DISPLAYING_WHITESPACE (it)
   589           /* Can be at BOL.  */
   590           && !it_char_has_category (it, not_at_bol));
   591 }
   592 
   593 /* Return true if the current character allows wrapping after it.   */
   594 static bool
   595 char_can_wrap_after (struct it *it)
   596 {
   597   if (!word_wrap_by_category)
   598     return IT_DISPLAYING_WHITESPACE (it);
   599 
   600   /* For CJK (LTR) text in RTL paragraph, EOL and BOL are flipped.
   601      Because in RTL paragraph, each glyph is prepended to the last
   602      one, effectively drawing right to left.  */
   603   int not_at_eol;
   604   if (it->glyph_row && it->glyph_row->reversed_p)
   605     not_at_eol = NOT_AT_BOL;
   606   else
   607     not_at_eol = NOT_AT_EOL;
   608 
   609   return (IT_DISPLAYING_WHITESPACE (it)
   610           /* Can break after && can be at EOL.  */
   611           || (it_char_has_category (it, LINE_BREAKABLE)
   612               && !it_char_has_category (it, not_at_eol)));
   613 }
   614 
   615 #undef IT_DISPLAYING_WHITESPACE
   616 #undef NOT_AT_EOL
   617 #undef NOT_AT_BOL
   618 #undef LINE_BREAKABLE
   619 
   620 /* If all the conditions needed to print the fill column indicator are
   621    met, return the (nonnegative) column number, else return a negative
   622    value.  */
   623 static int
   624 fill_column_indicator_column (struct it *it, int char_width)
   625 {
   626   if (display_fill_column_indicator
   627       && !it->w->pseudo_window_p
   628       && it->continuation_lines_width == 0
   629       && CHARACTERP (Vdisplay_fill_column_indicator_character))
   630     {
   631       Lisp_Object col = (EQ (Vdisplay_fill_column_indicator_column, Qt)
   632                          ? BVAR (current_buffer, fill_column)
   633                          : Vdisplay_fill_column_indicator_column);
   634 
   635       /* The stretch width needs to consider the latter
   636          added glyph in append_space_for_newline.  */
   637       if (RANGED_FIXNUMP (0, col, INT_MAX))
   638         {
   639           int icol = XFIXNUM (col);
   640           if (!INT_MULTIPLY_WRAPV (char_width, icol, &icol)
   641               && !INT_ADD_WRAPV (it->lnum_pixel_width, icol, &icol))
   642             return icol;
   643         }
   644     }
   645   return -1;
   646 }
   647 
   648 /* True means print newline to stdout before next mini-buffer message.  */
   649 
   650 bool noninteractive_need_newline;
   651 
   652 /* True means print newline to message log before next message.  */
   653 
   654 static bool message_log_need_newline;
   655 
   656 /* Three markers that message_dolog uses.
   657    It could allocate them itself, but that causes trouble
   658    in handling memory-full errors.  */
   659 static Lisp_Object message_dolog_marker1;
   660 static Lisp_Object message_dolog_marker2;
   661 static Lisp_Object message_dolog_marker3;
   662 
   663 /* The buffer position of the first character appearing entirely or
   664    partially on the line of the selected window which contains the
   665    cursor; <= 0 if not known.  Set by set_cursor_from_row, used for
   666    redisplay optimization in redisplay_internal.  */
   667 
   668 static struct text_pos this_line_start_pos;
   669 
   670 /* Number of characters past the end of the line above, including the
   671    terminating newline.  */
   672 
   673 static struct text_pos this_line_end_pos;
   674 
   675 /* The vertical positions and the height of this line.  */
   676 
   677 static int this_line_vpos;
   678 static int this_line_y;
   679 static int this_line_pixel_height;
   680 
   681 /* X position at which this display line starts.  Usually zero;
   682    negative if first character is partially visible.  */
   683 
   684 static int this_line_start_x;
   685 
   686 /* The smallest character position seen by move_it_* functions as they
   687    move across display lines.  Used to set MATRIX_ROW_START_CHARPOS of
   688    hscrolled lines, see display_line.  */
   689 
   690 static struct text_pos this_line_min_pos;
   691 
   692 /* Buffer that this_line_.* variables are referring to.  */
   693 
   694 static struct buffer *this_line_buffer;
   695 
   696 /* True if an overlay arrow has been displayed in this window.  */
   697 
   698 static bool overlay_arrow_seen;
   699 
   700 /* Vector containing glyphs for an ellipsis `...'.  */
   701 
   702 static Lisp_Object default_invis_vector[3];
   703 
   704 /* This is the window where the echo area message was displayed.  It
   705    is always a mini-buffer window, but it may not be the same window
   706    currently active as a mini-buffer.  */
   707 
   708 Lisp_Object echo_area_window;
   709 
   710 /* Stack of messages, which are pushed by push_message and popped and
   711    displayed by restore_message.  */
   712 
   713 static Lisp_Object Vmessage_stack;
   714 
   715 /* True means multibyte characters were enabled when the echo area
   716    message was specified.  */
   717 
   718 static bool message_enable_multibyte;
   719 
   720 /* At each redisplay cycle, we should refresh everything there is to refresh.
   721    To do that efficiently, we use many optimizations that try to make sure we
   722    don't waste too much time updating things that haven't changed.
   723    The coarsest such optimization is that, in the most common cases, we only
   724    look at the selected-window.
   725 
   726    To know whether other windows should be considered for redisplay, we use the
   727    variable windows_or_buffers_changed: as long as it is 0, it means that we
   728    have not noticed anything that should require updating anything else than
   729    the selected-window.  If it is set to REDISPLAY_SOME, it means that since
   730    last redisplay, some changes have been made which could impact other
   731    windows.  To know which ones need redisplay, every buffer, window, and frame
   732    has a `redisplay' bit, which (if true) means that this object needs to be
   733    redisplayed.  If windows_or_buffers_changed is 0, we know there's no point
   734    looking for those `redisplay' bits (actually, there might be some such bits
   735    set, but then only on objects which aren't displayed anyway).
   736 
   737    OTOH if it's non-zero we will have to loop through all windows and then
   738    check the `redisplay' bit of the corresponding window, frame, and buffer, in
   739    order to decide whether that window needs attention or not.  Note that we
   740    can't just look at the frame's redisplay bit to decide that the whole frame
   741    can be skipped, since even if the frame's redisplay bit is unset, some of
   742    its windows's redisplay bits may be set.
   743 
   744    Mostly for historical reasons, windows_or_buffers_changed can also take
   745    other non-zero values.  In that case, the precise value doesn't matter (it
   746    encodes the cause of the setting but is only used for debugging purposes),
   747    and what it means is that we shouldn't pay attention to any `redisplay' bits
   748    and we should simply try and redisplay every window out there.  */
   749 
   750 int windows_or_buffers_changed;
   751 
   752 /* Nonzero if we should redraw the mode lines on the next redisplay.
   753    Similarly to `windows_or_buffers_changed', if it has value REDISPLAY_SOME,
   754    then only redisplay the mode lines in those buffers/windows/frames where the
   755    `redisplay' bit has been set.
   756    For any other value, redisplay all mode lines (the number used is then only
   757    used to track down the cause for this full-redisplay).
   758 
   759    Since the frame title uses the same %-constructs as the mode line
   760    (except %c, %C, and %l), if this variable is non-zero, we also consider
   761    redisplaying the title of each frame, see gui_consider_frame_title.
   762 
   763    The `redisplay' bits are the same as those used for
   764    windows_or_buffers_changed, and setting windows_or_buffers_changed also
   765    causes recomputation of the mode lines of all those windows.  IOW this
   766    variable only has an effect if windows_or_buffers_changed is zero, in which
   767    case we should only need to redisplay the mode-line of those objects with
   768    a `redisplay' bit set but not the window's text content (tho we may still
   769    need to refresh the text content of the selected-window).  */
   770 
   771 int update_mode_lines;
   772 
   773 /* True after display_mode_line if %l was used and it displayed a
   774    line number.  */
   775 
   776 static bool line_number_displayed;
   777 
   778 /* Current, index 0, and last displayed echo area message.  Either
   779    buffers from echo_buffers, or nil to indicate no message.  */
   780 
   781 Lisp_Object echo_area_buffer[2];
   782 
   783 /* The buffers referenced from echo_area_buffer.  */
   784 
   785 static Lisp_Object echo_buffer[2];
   786 
   787 /* A vector saved used in with_area_buffer to reduce consing.  */
   788 
   789 static Lisp_Object Vwith_echo_area_save_vector;
   790 
   791 /* True means display_echo_area should display the last echo area
   792    message again.  Set by redisplay_preserve_echo_area.  */
   793 
   794 static bool display_last_displayed_message_p;
   795 
   796 /* True if echo area is being used by print; false if being used by
   797    message.  */
   798 
   799 static bool message_buf_print;
   800 
   801 /* Set to true in clear_message to make redisplay_internal aware
   802    of an emptied echo area.  */
   803 
   804 static bool message_cleared_p;
   805 
   806 /* A scratch glyph row with contents used for generating truncation
   807    glyphs and overlay-arrow glyphs.  */
   808 
   809 #define MAX_SCRATCH_GLYPHS 100
   810 static struct glyph_row scratch_glyph_row;
   811 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
   812 
   813 /* Ascent and height of the last line processed by move_it_to.  */
   814 
   815 static int last_height;
   816 
   817 /* True if there's a help-echo in the echo area.  */
   818 
   819 bool help_echo_showing_p;
   820 
   821 /* The maximum distance to look ahead for text properties.  Values
   822    that are too small let us call compute_char_face and similar
   823    functions too often which is expensive.  Values that are too large
   824    let us call compute_char_face and alike too often because we
   825    might not be interested in text properties that far away.  */
   826 
   827 #define TEXT_PROP_DISTANCE_LIMIT 100
   828 
   829 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
   830    iterator state and later restore it.  This is needed because the
   831    bidi iterator on bidi.c keeps a stacked cache of its states, which
   832    is really a singleton.  When we use scratch iterator objects to
   833    move around the buffer, we can cause the bidi cache to be pushed or
   834    popped, and therefore we need to restore the cache state when we
   835    return to the original iterator.  */
   836 #define SAVE_IT(ITCOPY, ITORIG, CACHE)          \
   837   do {                                          \
   838     if (CACHE)                                  \
   839       bidi_unshelve_cache (CACHE, true);        \
   840     ITCOPY = ITORIG;                            \
   841     CACHE = bidi_shelve_cache ();               \
   842   } while (false)
   843 
   844 #define RESTORE_IT(pITORIG, pITCOPY, CACHE)     \
   845   do {                                          \
   846     if (pITORIG != pITCOPY)                     \
   847       *(pITORIG) = *(pITCOPY);                  \
   848     bidi_unshelve_cache (CACHE, false);         \
   849     CACHE = NULL;                               \
   850   } while (false)
   851 
   852 /* Functions to mark elements as needing redisplay.  */
   853 enum { REDISPLAY_SOME = 2};     /* Arbitrary choice.  */
   854 
   855 static bool calc_pixel_width_or_height (double *, struct it *, Lisp_Object,
   856                                         struct font *, bool, int *);
   857 
   858 void
   859 redisplay_other_windows (void)
   860 {
   861   if (!windows_or_buffers_changed)
   862     windows_or_buffers_changed = REDISPLAY_SOME;
   863 }
   864 
   865 void
   866 wset_redisplay (struct window *w)
   867 {
   868   /* Beware: selected_window can be nil during early stages.  */
   869   if (!BASE_EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
   870     redisplay_other_windows ();
   871   w->redisplay = true;
   872 }
   873 
   874 void
   875 fset_redisplay (struct frame *f)
   876 {
   877   redisplay_other_windows ();
   878   f->redisplay = true;
   879 }
   880 
   881 void
   882 bset_redisplay (struct buffer *b)
   883 {
   884   int count = buffer_window_count (b);
   885   if (count > 0)
   886     {
   887       /* ... it's visible in other window than selected,  */
   888       if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
   889         redisplay_other_windows ();
   890       /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
   891          so that if we later set windows_or_buffers_changed, this buffer will
   892          not be omitted.  */
   893       b->text->redisplay = true;
   894     }
   895 }
   896 
   897 void
   898 bset_update_mode_line (struct buffer *b)
   899 {
   900   if (!update_mode_lines)
   901     update_mode_lines = REDISPLAY_SOME;
   902   b->text->redisplay = true;
   903 }
   904 
   905 void
   906 wset_update_mode_line (struct window *w)
   907 {
   908   w->update_mode_line = true;
   909   /* When a window's mode line needs to be updated, the window's frame's
   910      title may also need to be updated, but we don't need to worry about it
   911      here.  Instead, `gui_consider_frame_title' is automatically called
   912      whenever w->update_mode_line is set for that frame's selected window.
   913      But for this to work reliably, we have to make sure the window
   914      is considered, so we have to mark it for redisplay.  */
   915   wset_redisplay (w);
   916 }
   917 
   918 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
   919        Sset_buffer_redisplay, 4, 4, 0,
   920        doc: /* Mark the current buffer for redisplay.
   921 This function may be passed to `add-variable-watcher'.  */)
   922   (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
   923 {
   924   bset_update_mode_line (current_buffer);
   925   current_buffer->prevent_redisplay_optimizations_p = true;
   926   return Qnil;
   927 }
   928 
   929 /* redisplay_trace is for displaying traces of redisplay.
   930    If Emacs was compiled with GLYPH_DEBUG defined, the variable
   931    trace_redisplay_p can be set to a non-zero value in debugging
   932    sessions to activate traces.  */
   933 #ifdef GLYPH_DEBUG
   934 extern bool trace_redisplay_p EXTERNALLY_VISIBLE;
   935 bool trace_redisplay_p;
   936 #else
   937 enum { trace_redisplay_p = false };
   938 #endif
   939 static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
   940 redisplay_trace (char const *fmt, ...)
   941 {
   942   if (trace_redisplay_p)
   943     {
   944       va_list ap;
   945       va_start (ap, fmt);
   946       vprintf (fmt, ap);
   947       va_end (ap);
   948     }
   949 }
   950 
   951 #ifdef DEBUG_TRACE_MOVE
   952 extern bool trace_move EXTERNALLY_VISIBLE;
   953 bool trace_move;
   954 #else
   955 enum { trace_move = false };
   956 #endif
   957 static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
   958 move_trace (char const *fmt, ...)
   959 {
   960   if (trace_move)
   961     {
   962       va_list ap;
   963       va_start (ap, fmt);
   964       vprintf (fmt, ap);
   965       va_end (ap);
   966     }
   967 }
   968 
   969 /* Buffer being redisplayed -- for redisplay_window_error.  */
   970 
   971 static struct buffer *displayed_buffer;
   972 
   973 /* Value returned from text property handlers (see below).  */
   974 
   975 enum prop_handled
   976 {
   977   HANDLED_NORMALLY,
   978   HANDLED_RECOMPUTE_PROPS,
   979   HANDLED_OVERLAY_STRING_CONSUMED,
   980   HANDLED_RETURN
   981 };
   982 
   983 /* A description of text properties that redisplay is interested
   984    in.  */
   985 
   986 struct props
   987 {
   988   /* The symbol index of the name of the property.  */
   989   short name;
   990 
   991   /* A unique index for the property.  */
   992   enum prop_idx idx;
   993 
   994   /* A handler function called to set up iterator IT from the property
   995      at IT's current position.  Value is used to steer handle_stop.  */
   996   enum prop_handled (*handler) (struct it *it);
   997 };
   998 
   999 static enum prop_handled handle_face_prop (struct it *);
  1000 static enum prop_handled handle_invisible_prop (struct it *);
  1001 static enum prop_handled handle_display_prop (struct it *);
  1002 static enum prop_handled handle_composition_prop (struct it *);
  1003 static enum prop_handled handle_overlay_change (struct it *);
  1004 static enum prop_handled handle_fontified_prop (struct it *);
  1005 
  1006 /* Properties handled by iterators.  */
  1007 
  1008 static struct props it_props[] =
  1009 {
  1010   {SYMBOL_INDEX (Qfontified),   FONTIFIED_PROP_IDX,     handle_fontified_prop},
  1011   /* Handle `face' before `display' because some sub-properties of
  1012      `display' need to know the face.  */
  1013   {SYMBOL_INDEX (Qface),        FACE_PROP_IDX,          handle_face_prop},
  1014   {SYMBOL_INDEX (Qdisplay),     DISPLAY_PROP_IDX,       handle_display_prop},
  1015   {SYMBOL_INDEX (Qinvisible),   INVISIBLE_PROP_IDX,     handle_invisible_prop},
  1016   {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
  1017   {0,                           0,                      NULL}
  1018 };
  1019 
  1020 /* Enumeration returned by some move_it_.* functions internally.  */
  1021 
  1022 enum move_it_result
  1023 {
  1024   /* Not used.  Undefined value.  */
  1025   MOVE_UNDEFINED,
  1026 
  1027   /* Move ended at the requested buffer position or ZV.  */
  1028   MOVE_POS_MATCH_OR_ZV,
  1029 
  1030   /* Move ended at the requested X pixel position.  */
  1031   MOVE_X_REACHED,
  1032 
  1033   /* Move within a line ended at the end of a line that must be
  1034      continued.  */
  1035   MOVE_LINE_CONTINUED,
  1036 
  1037   /* Move within a line ended at the end of a line that would
  1038      be displayed truncated.  */
  1039   MOVE_LINE_TRUNCATED,
  1040 
  1041   /* Move within a line ended at a line end.  */
  1042   MOVE_NEWLINE_OR_CR
  1043 };
  1044 
  1045 /* This counter is used to clear the face cache every once in a while
  1046    in redisplay_internal.  It is incremented for each redisplay.
  1047    Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
  1048    cleared.  */
  1049 
  1050 #define CLEAR_FACE_CACHE_COUNT  500
  1051 static int clear_face_cache_count;
  1052 
  1053 /* Similarly for the image cache.  */
  1054 
  1055 #ifdef HAVE_WINDOW_SYSTEM
  1056 #define CLEAR_IMAGE_CACHE_COUNT 101
  1057 static int clear_image_cache_count;
  1058 
  1059 /* Null glyph slice */
  1060 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
  1061 #endif
  1062 
  1063 /* True while redisplay_internal is in progress.  */
  1064 
  1065 bool redisplaying_p;
  1066 
  1067 /* True while some display-engine code is working on layout of some
  1068    window.
  1069 
  1070    WARNING: Use sparingly, preferably only in top level of commands
  1071    and important functions, because using it in nested calls might
  1072    reset the flag when the inner call returns, behind the back of
  1073    the callers.  */
  1074 bool display_working_on_window_p;
  1075 
  1076 /* If a string, XTread_socket generates an event to display that string.
  1077    (The display is done in read_char.)  */
  1078 
  1079 Lisp_Object help_echo_string;
  1080 Lisp_Object help_echo_window;
  1081 Lisp_Object help_echo_object;
  1082 ptrdiff_t help_echo_pos;
  1083 
  1084 /* Temporary variable for XTread_socket.  */
  1085 
  1086 Lisp_Object previous_help_echo_string;
  1087 
  1088 /* Platform-independent portion of hourglass implementation.  */
  1089 
  1090 #ifdef HAVE_WINDOW_SYSTEM
  1091 
  1092 /* True means an hourglass cursor is currently shown.  */
  1093 static bool hourglass_shown_p;
  1094 
  1095 /* If non-null, an asynchronous timer that, when it expires, displays
  1096    an hourglass cursor on all frames.  */
  1097 static struct atimer *hourglass_atimer;
  1098 
  1099 #endif /* HAVE_WINDOW_SYSTEM */
  1100 
  1101 /* Default number of seconds to wait before displaying an hourglass
  1102    cursor.  */
  1103 #define DEFAULT_HOURGLASS_DELAY 1
  1104 
  1105 #ifdef HAVE_WINDOW_SYSTEM
  1106 
  1107 /* Default pixel width of `thin-space' display method.  */
  1108 #define THIN_SPACE_WIDTH 1
  1109 
  1110 #endif /* HAVE_WINDOW_SYSTEM */
  1111 
  1112 /* Function prototypes.  */
  1113 
  1114 static void setup_for_ellipsis (struct it *, int);
  1115 static void set_iterator_to_next (struct it *, bool);
  1116 static void mark_window_display_accurate_1 (struct window *, bool);
  1117 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
  1118 static bool cursor_row_p (struct glyph_row *);
  1119 static int redisplay_mode_lines (Lisp_Object, bool);
  1120 
  1121 static void handle_line_prefix (struct it *);
  1122 
  1123 static void handle_stop_backwards (struct it *, ptrdiff_t);
  1124 static void unwind_with_echo_area_buffer (Lisp_Object);
  1125 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
  1126 static bool current_message_1 (void *, Lisp_Object);
  1127 static bool truncate_message_1 (void *, Lisp_Object);
  1128 static void set_message (Lisp_Object);
  1129 static bool set_message_1 (void *, Lisp_Object);
  1130 static bool display_echo_area_1 (void *, Lisp_Object);
  1131 static bool resize_mini_window_1 (void *, Lisp_Object);
  1132 static void unwind_redisplay (void);
  1133 static void extend_face_to_end_of_line (struct it *);
  1134 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
  1135 static void push_it (struct it *, struct text_pos *);
  1136 static void iterate_out_of_display_property (struct it *);
  1137 static void pop_it (struct it *);
  1138 static void redisplay_internal (void);
  1139 static void echo_area_display (bool);
  1140 static void block_buffer_flips (void);
  1141 static void unblock_buffer_flips (void);
  1142 static void redisplay_windows (Lisp_Object);
  1143 static void redisplay_window (Lisp_Object, bool);
  1144 static Lisp_Object redisplay_window_error (Lisp_Object);
  1145 static Lisp_Object redisplay_window_0 (Lisp_Object);
  1146 static Lisp_Object redisplay_window_1 (Lisp_Object);
  1147 static bool set_cursor_from_row (struct window *, struct glyph_row *,
  1148                                  struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
  1149                                  int, int);
  1150 static bool cursor_row_fully_visible_p (struct window *, bool, bool, bool);
  1151 static bool update_menu_bar (struct frame *, bool, bool);
  1152 static bool try_window_reusing_current_matrix (struct window *);
  1153 static int try_window_id (struct window *);
  1154 static void maybe_produce_line_number (struct it *);
  1155 static bool should_produce_line_number (struct it *);
  1156 static bool display_line (struct it *, int);
  1157 static int display_mode_lines (struct window *);
  1158 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
  1159 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
  1160                                  Lisp_Object, bool);
  1161 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
  1162                                    Lisp_Object);
  1163 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
  1164 static void display_menu_bar (struct window *);
  1165 static void display_tab_bar (struct window *);
  1166 static void update_tab_bar (struct frame *, bool);
  1167 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
  1168                                       ptrdiff_t *);
  1169 static void pint2str (register char *, register int, register ptrdiff_t);
  1170 
  1171 static int display_string (const char *, Lisp_Object, Lisp_Object,
  1172                            ptrdiff_t, ptrdiff_t, struct it *, int, int, int,
  1173                            int);
  1174 static void compute_line_metrics (struct it *);
  1175 static bool get_overlay_strings (struct it *, ptrdiff_t);
  1176 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
  1177 static void next_overlay_string (struct it *);
  1178 static void reseat (struct it *, struct text_pos, bool);
  1179 static void reseat_1 (struct it *, struct text_pos, bool);
  1180 static bool next_element_from_display_vector (struct it *);
  1181 static bool next_element_from_string (struct it *);
  1182 static bool next_element_from_c_string (struct it *);
  1183 static bool next_element_from_buffer (struct it *);
  1184 static bool next_element_from_composition (struct it *);
  1185 static bool next_element_from_image (struct it *);
  1186 static bool next_element_from_stretch (struct it *);
  1187 static bool next_element_from_xwidget (struct it *);
  1188 static void load_overlay_strings (struct it *, ptrdiff_t);
  1189 static bool get_next_display_element (struct it *);
  1190 static enum move_it_result
  1191        move_it_in_display_line_to (struct it *, ptrdiff_t, int,
  1192                                    enum move_operation_enum);
  1193 static void get_visually_first_element (struct it *);
  1194 static void compute_stop_pos (struct it *);
  1195 static int face_before_or_after_it_pos (struct it *, bool);
  1196 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
  1197                                 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
  1198 static int handle_single_display_spec (struct it *, Lisp_Object, Lisp_Object,
  1199                                        Lisp_Object, struct text_pos *,
  1200                                        ptrdiff_t, int, bool, bool);
  1201 static int underlying_face_id (const struct it *);
  1202 
  1203 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
  1204 #define face_after_it_pos(IT)  face_before_or_after_it_pos (IT, false)
  1205 
  1206 #ifdef HAVE_WINDOW_SYSTEM
  1207 
  1208 static void update_tool_bar (struct frame *, bool);
  1209 static void gui_draw_bottom_divider (struct window *w);
  1210 static void notice_overwritten_cursor (struct window *,
  1211                                        enum glyph_row_area,
  1212                                        int, int, int, int);
  1213 static int  normal_char_height (struct font *, int);
  1214 static void normal_char_ascent_descent (struct font *, int, int *, int *);
  1215 
  1216 static void append_stretch_glyph (struct it *, Lisp_Object,
  1217                                   int, int, int);
  1218 
  1219 static Lisp_Object get_it_property (struct it *, Lisp_Object);
  1220 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
  1221                                               struct font *, int, bool);
  1222 static int adjust_glyph_width_for_mouse_face (struct glyph *,
  1223                                               struct glyph_row *,
  1224                                               struct window *, struct face *,
  1225                                               struct face *);
  1226 static void get_cursor_offset_for_mouse_face (struct window *w,
  1227                                               struct glyph_row *row,
  1228                                               int *offset);
  1229 #endif /* HAVE_WINDOW_SYSTEM */
  1230 
  1231 static void produce_special_glyphs (struct it *, enum display_element_type);
  1232 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
  1233 static bool coords_in_mouse_face_p (struct window *, int, int);
  1234 static void reset_box_start_end_flags (struct it *);
  1235 
  1236 
  1237 
  1238 /***********************************************************************
  1239                       Window display dimensions
  1240  ***********************************************************************/
  1241 
  1242 /* Return the bottom boundary y-position for text lines in window W.
  1243    This is the first y position at which a line cannot start.
  1244    It is relative to the top of the window.
  1245 
  1246    This is the height of W minus the height of a mode line, if any.  */
  1247 
  1248 int
  1249 window_text_bottom_y (struct window *w)
  1250 {
  1251   int height = WINDOW_PIXEL_HEIGHT (w);
  1252 
  1253   height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  1254 
  1255   if (window_wants_mode_line (w))
  1256     height -= CURRENT_MODE_LINE_HEIGHT (w);
  1257 
  1258   height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
  1259 
  1260   return height;
  1261 }
  1262 
  1263 /* Return the pixel width of display area AREA of window W.
  1264    ANY_AREA means return the total width of W, not including
  1265    fringes to the left and right of the window.  */
  1266 
  1267 int
  1268 window_box_width (struct window *w, enum glyph_row_area area)
  1269 {
  1270   int width = w->pixel_width;
  1271 
  1272   if (!w->pseudo_window_p)
  1273     {
  1274       width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
  1275       width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
  1276 
  1277       if (area == TEXT_AREA)
  1278         width -= (WINDOW_MARGINS_WIDTH (w)
  1279                    + WINDOW_FRINGES_WIDTH (w));
  1280       else if (area == LEFT_MARGIN_AREA)
  1281         width = WINDOW_LEFT_MARGIN_WIDTH (w);
  1282       else if (area == RIGHT_MARGIN_AREA)
  1283         width = WINDOW_RIGHT_MARGIN_WIDTH (w);
  1284     }
  1285 
  1286   /* With wide margins, fringes, etc. we might end up with a negative
  1287      width, correct that here.  */
  1288   return max (0, width);
  1289 }
  1290 
  1291 
  1292 /* Return the pixel height of the display area of window W, not
  1293    including mode lines of W, if any.  */
  1294 
  1295 int
  1296 window_box_height (struct window *w)
  1297 {
  1298   struct frame *f = XFRAME (w->frame);
  1299   int height = WINDOW_PIXEL_HEIGHT (w);
  1300 
  1301   eassert (height >= 0);
  1302 
  1303   height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  1304   height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
  1305 
  1306   /* Note: the code below that determines the mode-line/header-line/tab-line
  1307      height is essentially the same as that contained in the macro
  1308      CURRENT_{MODE,HEADER,TAB}_LINE_HEIGHT, except that it checks whether
  1309      the appropriate glyph row has its `mode_line_p' flag set, and if
  1310      it doesn't, uses estimate_mode_line_height instead.  */
  1311 
  1312   if (window_wants_mode_line (w))
  1313     {
  1314       if (w->mode_line_height >= 0)
  1315         height -= w->mode_line_height;
  1316       else
  1317         {
  1318           struct glyph_row *ml_row
  1319             = (w->current_matrix && w->current_matrix->rows
  1320                ? MATRIX_MODE_LINE_ROW (w->current_matrix)
  1321                : 0);
  1322           if (ml_row && ml_row->mode_line_p)
  1323             height -= ml_row->height;
  1324           else
  1325             height -= estimate_mode_line_height
  1326               (f, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w));
  1327         }
  1328     }
  1329 
  1330   if (window_wants_tab_line (w))
  1331     {
  1332       if (w->tab_line_height >= 0)
  1333         height -= w->tab_line_height;
  1334       else
  1335         {
  1336           struct glyph_row *tl_row
  1337             = (w->current_matrix && w->current_matrix->rows
  1338                ? MATRIX_TAB_LINE_ROW (w->current_matrix)
  1339                : 0);
  1340           if (tl_row && tl_row->mode_line_p)
  1341             height -= tl_row->height;
  1342           else
  1343             height -= estimate_mode_line_height (f, TAB_LINE_FACE_ID);
  1344         }
  1345     }
  1346 
  1347   if (window_wants_header_line (w))
  1348     {
  1349       if (w->header_line_height >= 0)
  1350         height -= w->header_line_height;
  1351       else
  1352         {
  1353           struct glyph_row *hl_row
  1354             = (w->current_matrix && w->current_matrix->rows
  1355                ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
  1356                : 0);
  1357           if (hl_row && hl_row->mode_line_p)
  1358             height -= hl_row->height;
  1359           else
  1360             height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
  1361         }
  1362     }
  1363 
  1364   /* With a very small font and a mode-line that's taller than
  1365      default, we might end up with a negative height.  */
  1366   return max (0, height);
  1367 }
  1368 
  1369 /* Return the window-relative coordinate of the left edge of display
  1370    area AREA of window W.  ANY_AREA means return the left edge of the
  1371    whole window, to the right of the left fringe of W.  */
  1372 
  1373 int
  1374 window_box_left_offset (struct window *w, enum glyph_row_area area)
  1375 {
  1376   int x;
  1377 
  1378   if (w->pseudo_window_p)
  1379     return 0;
  1380 
  1381   x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
  1382 
  1383   if (area == TEXT_AREA)
  1384     x += (WINDOW_LEFT_FRINGE_WIDTH (w)
  1385           + window_box_width (w, LEFT_MARGIN_AREA));
  1386   else if (area == RIGHT_MARGIN_AREA)
  1387     x += (WINDOW_LEFT_FRINGE_WIDTH (w)
  1388           + window_box_width (w, LEFT_MARGIN_AREA)
  1389           + window_box_width (w, TEXT_AREA)
  1390           + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  1391              ? 0
  1392              : WINDOW_RIGHT_FRINGE_WIDTH (w)));
  1393   else if (area == LEFT_MARGIN_AREA
  1394            && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
  1395     x += WINDOW_LEFT_FRINGE_WIDTH (w);
  1396 
  1397   /* Don't return more than the window's pixel width.  */
  1398   return min (x, w->pixel_width);
  1399 }
  1400 
  1401 
  1402 /* Return the window-relative coordinate of the right edge of display
  1403    area AREA of window W.  ANY_AREA means return the right edge of the
  1404    whole window, to the left of the right fringe of W.  */
  1405 
  1406 static int
  1407 window_box_right_offset (struct window *w, enum glyph_row_area area)
  1408 {
  1409   /* Don't return more than the window's pixel width.  */
  1410   return min (window_box_left_offset (w, area) + window_box_width (w, area),
  1411               w->pixel_width);
  1412 }
  1413 
  1414 /* Return the frame-relative coordinate of the left edge of display
  1415    area AREA of window W.  ANY_AREA means return the left edge of the
  1416    whole window, to the right of the left fringe of W.  */
  1417 
  1418 int
  1419 window_box_left (struct window *w, enum glyph_row_area area)
  1420 {
  1421   struct frame *f = XFRAME (w->frame);
  1422   int x;
  1423 
  1424   if (w->pseudo_window_p)
  1425     return FRAME_INTERNAL_BORDER_WIDTH (f);
  1426 
  1427   x = (WINDOW_LEFT_EDGE_X (w)
  1428        + window_box_left_offset (w, area));
  1429 
  1430   return x;
  1431 }
  1432 
  1433 
  1434 /* Return the frame-relative coordinate of the right edge of display
  1435    area AREA of window W.  ANY_AREA means return the right edge of the
  1436    whole window, to the left of the right fringe of W.  */
  1437 
  1438 int
  1439 window_box_right (struct window *w, enum glyph_row_area area)
  1440 {
  1441   return window_box_left (w, area) + window_box_width (w, area);
  1442 }
  1443 
  1444 /* Get the bounding box of the display area AREA of window W, without
  1445    mode lines, in frame-relative coordinates.  ANY_AREA means the
  1446    whole window, not including the left and right fringes of
  1447    the window.  Return in *BOX_X and *BOX_Y the frame-relative pixel
  1448    coordinates of the upper-left corner of the box.  Return in
  1449    *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box.  */
  1450 
  1451 void
  1452 window_box (struct window *w, enum glyph_row_area area, int *box_x,
  1453             int *box_y, int *box_width, int *box_height)
  1454 {
  1455   if (box_width)
  1456     *box_width = window_box_width (w, area);
  1457   if (box_height)
  1458     *box_height = window_box_height (w);
  1459   if (box_x)
  1460     *box_x = window_box_left (w, area);
  1461   if (box_y)
  1462     {
  1463       *box_y = WINDOW_TOP_EDGE_Y (w);
  1464       if (window_wants_tab_line (w))
  1465         *box_y += CURRENT_TAB_LINE_HEIGHT (w);
  1466       if (window_wants_header_line (w))
  1467         *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
  1468     }
  1469 }
  1470 
  1471 #ifdef HAVE_WINDOW_SYSTEM
  1472 
  1473 /* Get the bounding box of the display area AREA of window W, without
  1474    mode lines and both fringes of the window.  Return in *TOP_LEFT_X
  1475    and TOP_LEFT_Y the frame-relative pixel coordinates of the
  1476    upper-left corner of the box.  Return in *BOTTOM_RIGHT_X, and
  1477    *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
  1478    box.  */
  1479 
  1480 static void
  1481 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
  1482                   int *bottom_right_x, int *bottom_right_y)
  1483 {
  1484   window_box (w, ANY_AREA, top_left_x, top_left_y,
  1485               bottom_right_x, bottom_right_y);
  1486   *bottom_right_x += *top_left_x;
  1487   *bottom_right_y += *top_left_y;
  1488 }
  1489 
  1490 #endif /* HAVE_WINDOW_SYSTEM */
  1491 
  1492 /***********************************************************************
  1493                               Utilities
  1494  ***********************************************************************/
  1495 
  1496 /* Return the bottom y-position of the line the iterator IT is in.
  1497    This can modify IT's settings.  */
  1498 
  1499 int
  1500 line_bottom_y (struct it *it)
  1501 {
  1502   int line_height = it->max_ascent + it->max_descent;
  1503   int line_top_y = it->current_y;
  1504 
  1505   if (line_height == 0)
  1506     {
  1507       if (last_height)
  1508         line_height = last_height;
  1509       else if (IT_CHARPOS (*it) < ZV)
  1510         {
  1511           move_it_by_lines (it, 1);
  1512           line_height = (it->max_ascent || it->max_descent
  1513                          ? it->max_ascent + it->max_descent
  1514                          : last_height);
  1515         }
  1516       else
  1517         {
  1518           struct glyph_row *row = it->glyph_row;
  1519 
  1520           /* Use the default character height.  */
  1521           it->glyph_row = NULL;
  1522           it->what = IT_CHARACTER;
  1523           it->c = ' ';
  1524           it->len = 1;
  1525           PRODUCE_GLYPHS (it);
  1526           line_height = it->ascent + it->descent;
  1527           it->glyph_row = row;
  1528         }
  1529     }
  1530 
  1531   return line_top_y + line_height;
  1532 }
  1533 
  1534 DEFUN ("line-pixel-height", Fline_pixel_height,
  1535        Sline_pixel_height, 0, 0, 0,
  1536        doc: /* Return height in pixels of text line in the selected window.
  1537 
  1538 Value is the height in pixels of the line at point.  */)
  1539   (void)
  1540 {
  1541   struct it it;
  1542   struct text_pos pt;
  1543   struct window *w = XWINDOW (selected_window);
  1544   struct buffer *old_buffer = NULL;
  1545   Lisp_Object result;
  1546 
  1547   if (XBUFFER (w->contents) != current_buffer)
  1548     {
  1549       old_buffer = current_buffer;
  1550       set_buffer_internal_1 (XBUFFER (w->contents));
  1551     }
  1552   SET_TEXT_POS (pt, PT, PT_BYTE);
  1553   void *itdata = bidi_shelve_cache ();
  1554   start_display (&it, w, pt);
  1555   /* Start from the beginning of the screen line, to make sure we
  1556      traverse all of its display elements, and thus capture the
  1557      correct metrics.  */
  1558   move_it_by_lines (&it, 0);
  1559   it.vpos = it.current_y = 0;
  1560   last_height = 0;
  1561   result = make_fixnum (line_bottom_y (&it));
  1562   if (old_buffer)
  1563     set_buffer_internal_1 (old_buffer);
  1564 
  1565   bidi_unshelve_cache (itdata, false);
  1566   return result;
  1567 }
  1568 
  1569 /* Return the default pixel height of text lines in window W.  The
  1570    value is the canonical height of the W frame's default font, plus
  1571    any extra space required by the line-spacing variable or frame
  1572    parameter.
  1573 
  1574    Implementation note: this ignores any line-spacing text properties
  1575    put on the newline characters.  This is because those properties
  1576    only affect the _screen_ line ending in the newline (i.e., in a
  1577    continued line, only the last screen line will be affected), which
  1578    means only a small number of lines in a buffer can ever use this
  1579    feature.  Since this function is used to compute the default pixel
  1580    equivalent of text lines in a window, we can safely ignore those
  1581    few lines.  For the same reasons, we ignore the line-height
  1582    properties.  */
  1583 int
  1584 default_line_pixel_height (struct window *w)
  1585 {
  1586   struct frame *f = WINDOW_XFRAME (w);
  1587   int height = FRAME_LINE_HEIGHT (f);
  1588 
  1589   if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
  1590     {
  1591       struct buffer *b = XBUFFER (w->contents);
  1592       Lisp_Object val = BVAR (b, extra_line_spacing);
  1593 
  1594       if (NILP (val))
  1595         val = BVAR (&buffer_defaults, extra_line_spacing);
  1596       if (!NILP (val))
  1597         {
  1598           if (RANGED_FIXNUMP (0, val, INT_MAX))
  1599             height += XFIXNAT (val);
  1600           else if (FLOATP (val))
  1601             {
  1602               int addon = XFLOAT_DATA (val) * height + 0.5;
  1603 
  1604               if (addon >= 0)
  1605                 height += addon;
  1606             }
  1607         }
  1608       else
  1609         height += f->extra_line_spacing;
  1610     }
  1611 
  1612   return height;
  1613 }
  1614 
  1615 /* Subroutine of pos_visible_p below.  Extracts a display string, if
  1616    any, from the display spec given as its argument.  */
  1617 static Lisp_Object
  1618 string_from_display_spec (Lisp_Object spec)
  1619 {
  1620   if (VECTORP (spec))
  1621     {
  1622       for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
  1623         if (STRINGP (AREF (spec, i)))
  1624           return AREF (spec, i);
  1625     }
  1626   else
  1627     {
  1628       for (; CONSP (spec); spec = XCDR (spec))
  1629         if (STRINGP (XCAR (spec)))
  1630           return XCAR (spec);
  1631     }
  1632   return spec;
  1633 }
  1634 
  1635 
  1636 /* Limit insanely large values of W->hscroll on frame F to the largest
  1637    value that will still prevent first_visible_x and last_visible_x of
  1638    'struct it' from overflowing an int.  */
  1639 static int
  1640 window_hscroll_limited (struct window *w, struct frame *f)
  1641 {
  1642   ptrdiff_t window_hscroll = w->hscroll;
  1643   int window_text_width = window_box_width (w, TEXT_AREA);
  1644   int colwidth = FRAME_COLUMN_WIDTH (f);
  1645 
  1646   if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
  1647     window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
  1648 
  1649   return window_hscroll;
  1650 }
  1651 
  1652 /* Reset the box-face start and end flags in the iterator.  This is
  1653    called after producing glyphs, such that we reset these flags only
  1654    after producing a glyph with the flag set.  */
  1655 
  1656 static void
  1657 reset_box_start_end_flags (struct it *it)
  1658 {
  1659   /* Don't reset if we've drawn the glyph in the display margins --
  1660      those don't count as "produced glyphs".  */
  1661   if (it->area == TEXT_AREA
  1662       /* Don't reset if we displayed a fringe bitmap.  */
  1663       && !(it->what == IT_IMAGE && it->image_id < 0))
  1664     {
  1665       /* Don't reset if the face is not a box face: that might mean we
  1666          are iterating some overlay or display string, and the first
  1667          character to have the box face is yet to be seen, when we pop
  1668          the iterator stack. */
  1669       if (it->face_box_p)
  1670         it->start_of_box_run_p = false;
  1671       it->end_of_box_run_p = false;
  1672     }
  1673 }
  1674 
  1675 /* Return true if position CHARPOS is visible in window W.
  1676    CHARPOS < 0 means return info about WINDOW_END position.
  1677    If visible, set *X and *Y to pixel coordinates of top left corner.
  1678    Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
  1679    Set *ROWH and *VPOS to row's visible height and VPOS (row number).  */
  1680 
  1681 bool
  1682 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
  1683                int *rtop, int *rbot, int *rowh, int *vpos)
  1684 {
  1685   struct it it;
  1686   void *itdata = bidi_shelve_cache ();
  1687   struct text_pos top;
  1688   bool visible_p = false;
  1689   struct buffer *old_buffer = NULL;
  1690   bool r2l = false;
  1691 
  1692   if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
  1693     return visible_p;
  1694 
  1695   if (XBUFFER (w->contents) != current_buffer)
  1696     {
  1697       old_buffer = current_buffer;
  1698       set_buffer_internal_1 (XBUFFER (w->contents));
  1699     }
  1700 
  1701   SET_TEXT_POS_FROM_MARKER (top, w->start);
  1702   /* Scrolling a minibuffer window via scroll bar when the echo area
  1703      shows long text sometimes resets the minibuffer contents behind
  1704      our backs.  Also, someone might narrow-to-region and immediately
  1705      call a scroll function.  */
  1706   if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
  1707     SET_TEXT_POS (top, BEGV, BEGV_BYTE);
  1708 
  1709   /* If the top of the window is after CHARPOS, the latter is surely
  1710      not visible.  */
  1711   if (charpos >= 0 && CHARPOS (top) > charpos)
  1712     return visible_p;
  1713 
  1714   /* Some Lisp hook could call us in the middle of redisplaying this
  1715      very window.  If, by some bad luck, we are retrying redisplay
  1716      because we found that the mode-line height and/or tab/header-line
  1717      height needs to be updated, the assignment of mode_line_height
  1718      and header_line_height below could disrupt that, due to the
  1719      selected/nonselected window dance during mode-line display, and
  1720      we could infloop.  Avoid that.  */
  1721   int prev_mode_line_height = w->mode_line_height;
  1722   int prev_header_line_height = w->header_line_height;
  1723   int prev_tab_line_height = w->tab_line_height;
  1724   /* Compute exact mode line heights.  */
  1725   if (window_wants_mode_line (w))
  1726     {
  1727       Lisp_Object window_mode_line_format
  1728         = window_parameter (w, Qmode_line_format);
  1729 
  1730       w->mode_line_height
  1731         = display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
  1732                              NILP (window_mode_line_format)
  1733                              ? BVAR (current_buffer, mode_line_format)
  1734                              : window_mode_line_format);
  1735     }
  1736 
  1737   if (window_wants_tab_line (w))
  1738     {
  1739       Lisp_Object window_tab_line_format
  1740         = window_parameter (w, Qtab_line_format);
  1741 
  1742       w->tab_line_height
  1743         = display_mode_line (w, TAB_LINE_FACE_ID,
  1744                              NILP (window_tab_line_format)
  1745                              ? BVAR (current_buffer, tab_line_format)
  1746                              : window_tab_line_format);
  1747     }
  1748 
  1749   if (window_wants_header_line (w))
  1750     {
  1751       Lisp_Object window_header_line_format
  1752         = window_parameter (w, Qheader_line_format);
  1753 
  1754       w->header_line_height
  1755         = display_mode_line (w, HEADER_LINE_FACE_ID,
  1756                              NILP (window_header_line_format)
  1757                              ? BVAR (current_buffer, header_line_format)
  1758                              : window_header_line_format);
  1759     }
  1760 
  1761   start_display (&it, w, top);
  1762   move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
  1763               (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
  1764 
  1765   /* Adjust for line numbers, if CHARPOS is at or beyond first_visible_x,
  1766      but we didn't yet produce the line-number glyphs.  */
  1767   if (!NILP (Vdisplay_line_numbers)
  1768       && it.current_x >= it.first_visible_x
  1769       && IT_CHARPOS (it) == charpos
  1770       && !it.line_number_produced_p)
  1771     {
  1772       /* If the pixel width of line numbers was not yet known, compute
  1773          it now.  This usually happens in the first display line of a
  1774          window.  */
  1775       if (!it.lnum_pixel_width)
  1776         {
  1777           struct it it2;
  1778           void *it2data = NULL;
  1779 
  1780           SAVE_IT (it2, it, it2data);
  1781           move_it_by_lines (&it, 1);
  1782           it2.lnum_pixel_width = it.lnum_pixel_width;
  1783           RESTORE_IT (&it, &it2, it2data);
  1784         }
  1785       it.current_x += it.lnum_pixel_width;
  1786     }
  1787 
  1788   if (charpos >= 0
  1789       && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
  1790            && IT_CHARPOS (it) >= charpos)
  1791           /* When scanning backwards under bidi iteration, move_it_to
  1792              stops at or _before_ CHARPOS, because it stops at or to
  1793              the _right_ of the character at CHARPOS.  */
  1794           || (it.bidi_p && it.bidi_it.scan_dir == -1
  1795               && IT_CHARPOS (it) <= charpos)))
  1796     {
  1797       /* We have reached CHARPOS, or passed it.  How the call to
  1798          move_it_to can overshoot: (i) If CHARPOS is on invisible text
  1799          or covered by a display property, move_it_to stops at the end
  1800          of the invisible text, to the right of CHARPOS.  (ii) If
  1801          CHARPOS is in a display vector, move_it_to stops on its last
  1802          glyph.  */
  1803       int top_x = it.current_x;
  1804       int top_y = it.current_y;
  1805       int window_top_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  1806       int bottom_y;
  1807       struct it save_it;
  1808       void *save_it_data = NULL;
  1809 
  1810       /* Calling line_bottom_y may change it.method, it.position, etc.  */
  1811       SAVE_IT (save_it, it, save_it_data);
  1812       last_height = 0;
  1813       bottom_y = line_bottom_y (&it);
  1814       if (top_y < window_top_y)
  1815         visible_p = bottom_y > window_top_y;
  1816       else if (top_y < it.last_visible_y)
  1817         visible_p = true;
  1818       if (bottom_y >= it.last_visible_y
  1819           && it.bidi_p && it.bidi_it.scan_dir == -1
  1820           && IT_CHARPOS (it) < charpos)
  1821         {
  1822           /* When the last line of the window is scanned backwards
  1823              under bidi iteration, we could be duped into thinking
  1824              that we have passed CHARPOS, when in fact move_it_to
  1825              simply stopped short of CHARPOS because it reached
  1826              last_visible_y.  To see if that's what happened, we call
  1827              move_it_to again with a slightly larger vertical limit,
  1828              and see if it actually moved vertically; if it did, we
  1829              didn't really reach CHARPOS, which is beyond window end.  */
  1830           /* Why 10? because we don't know how many canonical lines
  1831              will the height of the next line(s) be.  So we guess.  */
  1832           int ten_more_lines = 10 * default_line_pixel_height (w);
  1833 
  1834           move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
  1835                       MOVE_TO_POS | MOVE_TO_Y);
  1836           if (it.current_y > top_y)
  1837             visible_p = false;
  1838 
  1839         }
  1840       RESTORE_IT (&it, &save_it, save_it_data);
  1841       if (visible_p)
  1842         {
  1843           if (it.method == GET_FROM_DISPLAY_VECTOR)
  1844             {
  1845               /* We stopped on the last glyph of a display vector.
  1846                  Try and recompute.  Hack alert!  */
  1847               if (charpos < 2 || top.charpos >= charpos)
  1848                 top_x = it.glyph_row->x;
  1849               else
  1850                 {
  1851                   struct it it2, it2_prev;
  1852                   /* The idea is to get to the previous buffer
  1853                      position, consume the character there, and use
  1854                      the pixel coordinates we get after that.  But if
  1855                      the previous buffer position is also displayed
  1856                      from a display vector, we need to consume all of
  1857                      the glyphs from that display vector.  */
  1858                   start_display (&it2, w, top);
  1859                   it2.glyph_row = NULL;
  1860                   move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
  1861                   /* If we didn't get to CHARPOS - 1, there's some
  1862                      replacing display property at that position, and
  1863                      we stopped after it.  That is exactly the place
  1864                      whose coordinates we want.  */
  1865                   if (IT_CHARPOS (it2) != charpos - 1)
  1866                     it2_prev = it2;
  1867                   else
  1868                     {
  1869                       /* Iterate until we get out of the display
  1870                          vector that displays the character at
  1871                          CHARPOS - 1.  */
  1872                       do {
  1873                         get_next_display_element (&it2);
  1874                         PRODUCE_GLYPHS (&it2);
  1875                         it2_prev = it2;
  1876                         set_iterator_to_next (&it2, true);
  1877                       } while (it2.method == GET_FROM_DISPLAY_VECTOR
  1878                                && IT_CHARPOS (it2) < charpos);
  1879                     }
  1880                   if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
  1881                       || it2_prev.current_x > it2_prev.last_visible_x)
  1882                     top_x = it.glyph_row->x;
  1883                   else
  1884                     {
  1885                       top_x = it2_prev.current_x;
  1886                       top_y = it2_prev.current_y;
  1887                     }
  1888                 }
  1889             }
  1890           else if (IT_CHARPOS (it) != charpos)
  1891             {
  1892               Lisp_Object cpos = make_fixnum (charpos);
  1893               Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
  1894               Lisp_Object string = string_from_display_spec (spec);
  1895               struct text_pos tpos;
  1896               bool newline_in_string
  1897                 = (STRINGP (string)
  1898                    && memchr (SDATA (string), '\n', SBYTES (string)));
  1899 
  1900               SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
  1901               bool replacing_spec_p
  1902                 = (!NILP (spec)
  1903                    && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
  1904                                            charpos, FRAME_WINDOW_P (it.f)));
  1905               /* The tricky code below is needed because there's a
  1906                  discrepancy between move_it_to and how we set cursor
  1907                  when PT is at the beginning of a portion of text
  1908                  covered by a display property or an overlay with a
  1909                  display property, or the display line ends in a
  1910                  newline from a display string.  move_it_to will stop
  1911                  _after_ such display strings, whereas
  1912                  set_cursor_from_row conspires with cursor_row_p to
  1913                  place the cursor on the first glyph produced from the
  1914                  display string.  */
  1915 
  1916               /* We have overshoot PT because it is covered by a
  1917                  display property that replaces the text it covers.
  1918                  If the string includes embedded newlines, we are also
  1919                  in the wrong display line.  Backtrack to the correct
  1920                  line, where the display property begins.  */
  1921               if (replacing_spec_p)
  1922                 {
  1923                   Lisp_Object startpos, endpos;
  1924                   EMACS_INT start, end;
  1925                   struct it it3;
  1926 
  1927                   /* Find the first and the last buffer positions
  1928                      covered by the display string.  */
  1929                   endpos =
  1930                     Fnext_single_char_property_change (cpos, Qdisplay,
  1931                                                        Qnil, Qnil);
  1932                   startpos =
  1933                     Fprevious_single_char_property_change (endpos, Qdisplay,
  1934                                                            Qnil, Qnil);
  1935                   start = XFIXNAT (startpos);
  1936                   end = XFIXNAT (endpos);
  1937                   /* Move to the last buffer position before the
  1938                      display property.  */
  1939                   start_display (&it3, w, top);
  1940                   if (start > CHARPOS (top))
  1941                     move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
  1942                   /* Move forward one more line if the position before
  1943                      the display string is a newline or if it is the
  1944                      rightmost character on a line that is
  1945                      continued or word-wrapped.  */
  1946                   if (it3.method == GET_FROM_BUFFER
  1947                       && (it3.c == '\n'
  1948                           || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
  1949                     move_it_by_lines (&it3, 1);
  1950                   else if (move_it_in_display_line_to (&it3, -1,
  1951                                                        it3.current_x
  1952                                                        + it3.pixel_width,
  1953                                                        MOVE_TO_X)
  1954                            == MOVE_LINE_CONTINUED)
  1955                     {
  1956                       move_it_by_lines (&it3, 1);
  1957                       /* When we are under word-wrap, the #$@%!
  1958                          move_it_by_lines moves 2 lines, so we need to
  1959                          fix that up.  */
  1960                       if (it3.line_wrap == WORD_WRAP)
  1961                         move_it_by_lines (&it3, -1);
  1962                     }
  1963 
  1964                   /* Record the vertical coordinate of the display
  1965                      line where we wound up.  */
  1966                   top_y = it3.current_y;
  1967                   if (it3.bidi_p)
  1968                     {
  1969                       /* When characters are reordered for display,
  1970                          the character displayed to the left of the
  1971                          display string could be _after_ the display
  1972                          property in the logical order.  Use the
  1973                          smallest vertical position of these two.  */
  1974                       start_display (&it3, w, top);
  1975                       move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
  1976                       if (it3.current_y < top_y)
  1977                         top_y = it3.current_y;
  1978                     }
  1979                   /* Move from the top of the window to the beginning
  1980                      of the display line where the display string
  1981                      begins.  */
  1982                   start_display (&it3, w, top);
  1983                   it3.glyph_row = NULL;
  1984                   move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
  1985                   /* If it3_moved stays false after the 'while' loop
  1986                      below, that means we already were at a newline
  1987                      before the loop (e.g., the display string begins
  1988                      with a newline), so we don't need to return to
  1989                      the last position before the display string,
  1990                      because PRODUCE_GLYPHS will not produce anything
  1991                      for a newline.  */
  1992                   bool it3_moved = false;
  1993                   int top_x_before_string = it3.current_x;
  1994                   /* Finally, advance the iterator until we hit the
  1995                      first display element whose character position is
  1996                      at or beyond CHARPOS, or until the first newline
  1997                      from the display string, which signals the end of
  1998                      the display line.  */
  1999                   while (get_next_display_element (&it3))
  2000                     {
  2001                       if (!EQ (it3.object, string))
  2002                         top_x_before_string = it3.current_x;
  2003                       PRODUCE_GLYPHS (&it3);
  2004                       if ((it3.bidi_it.scan_dir == 1
  2005                            && IT_CHARPOS (it3) >= charpos)
  2006                           || (it3.bidi_it.scan_dir == -1
  2007                               && IT_CHARPOS (it3) <= charpos)
  2008                           || ITERATOR_AT_END_OF_LINE_P (&it3))
  2009                         break;
  2010                       it3_moved = true;
  2011                       set_iterator_to_next (&it3, false);
  2012                     }
  2013                   top_x = it3.current_x - it3.pixel_width;
  2014                   /* Account for line-number display, if IT3 still
  2015                      didn't.  This can happen if START - 1 is the
  2016                      first or the last character on its display line.  */
  2017                   if (!it3.line_number_produced_p)
  2018                     {
  2019                       if (it3.lnum_pixel_width > 0)
  2020                         {
  2021                           top_x += it3.lnum_pixel_width;
  2022                           top_x_before_string += it3.lnum_pixel_width;
  2023                         }
  2024                       else if (it.line_number_produced_p)
  2025                         {
  2026                           top_x += it.lnum_pixel_width;
  2027                           top_x_before_string += it3.lnum_pixel_width;
  2028                         }
  2029                     }
  2030                   /* Normally, we would exit the above loop because we
  2031                      found the display element whose character
  2032                      position is CHARPOS.  For the contingency that we
  2033                      didn't, and stopped at the first newline from the
  2034                      display string, reset top_x to the coordinate of
  2035                      the rightmost glyph not from the string.  */
  2036                   if (it3_moved
  2037                       && newline_in_string
  2038                       && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
  2039                     top_x = top_x_before_string;
  2040                 }
  2041             }
  2042 
  2043           *x = top_x;
  2044           /* The condition below is a heuristic fix for the situation
  2045              where move_it_to stops just after finishing the display
  2046              of a fringe bitmap, which resets it.ascent to zero, and
  2047              thus causes Y to be offset by it.max_ascent.  */
  2048           if (it.ascent == 0 && it.what == IT_IMAGE
  2049               && it.method != GET_FROM_IMAGE
  2050               && it.image_id < 0
  2051               && it.max_ascent > 0)
  2052             *y = max (top_y, window_top_y);
  2053           else
  2054             *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
  2055           *rtop = max (0, window_top_y - top_y);
  2056           *rbot = max (0, bottom_y - it.last_visible_y);
  2057           *rowh = max (0, (min (bottom_y, it.last_visible_y)
  2058                            - max (top_y, window_top_y)));
  2059           *vpos = it.vpos;
  2060           if (it.bidi_it.paragraph_dir == R2L)
  2061             r2l = true;
  2062         }
  2063     }
  2064   else
  2065     {
  2066       /* Either we were asked to provide info about WINDOW_END, or
  2067          CHARPOS is in the partially visible glyph row at end of
  2068          window.  */
  2069       struct it it2;
  2070       void *it2data = NULL;
  2071 
  2072       SAVE_IT (it2, it, it2data);
  2073       if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
  2074         move_it_by_lines (&it, 1);
  2075       if (charpos < IT_CHARPOS (it)
  2076           || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
  2077         {
  2078           visible_p = true;
  2079           RESTORE_IT (&it2, &it2, it2data);
  2080           move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
  2081           *x = it2.current_x;
  2082           if (it2.ascent == 0 && it2.what == IT_IMAGE
  2083               && it2.method != GET_FROM_IMAGE
  2084               && it2.image_id < 0
  2085               && it2.max_ascent > 0)
  2086             *y = it2.current_y;
  2087           else
  2088             *y = it2.current_y + it2.max_ascent - it2.ascent;
  2089           *rtop = max (0, -it2.current_y);
  2090           *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
  2091                            - it.last_visible_y));
  2092           *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
  2093                                 it.last_visible_y)
  2094                            - max (max (it2.current_y,
  2095                                        WINDOW_TAB_LINE_HEIGHT (w)),
  2096                                   WINDOW_HEADER_LINE_HEIGHT (w))));
  2097           *vpos = it2.vpos;
  2098           if (it2.bidi_it.paragraph_dir == R2L)
  2099             r2l = true;
  2100         }
  2101       else
  2102         bidi_unshelve_cache (it2data, true);
  2103     }
  2104   bidi_unshelve_cache (itdata, false);
  2105 
  2106   if (old_buffer)
  2107     set_buffer_internal_1 (old_buffer);
  2108 
  2109   if (visible_p)
  2110     {
  2111       if (w->hscroll > 0)
  2112         *x -=
  2113           window_hscroll_limited (w, WINDOW_XFRAME (w))
  2114           * WINDOW_FRAME_COLUMN_WIDTH (w);
  2115       /* For lines in an R2L paragraph, we need to mirror the X pixel
  2116          coordinate wrt the text area.  For the reasons, see the
  2117          commentary in buffer_posn_from_coords and the explanation of
  2118          the geometry used by the move_it_* functions at the end of
  2119          the large commentary near the beginning of this file.  */
  2120       if (r2l)
  2121         *x = window_box_width (w, TEXT_AREA) - *x - 1;
  2122     }
  2123 
  2124 #if false
  2125   /* Debugging code.  */
  2126   if (visible_p)
  2127     fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
  2128              charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
  2129   else
  2130     fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
  2131 #endif
  2132 
  2133   /* Restore potentially overwritten values.  */
  2134   w->mode_line_height = prev_mode_line_height;
  2135   w->header_line_height = prev_header_line_height;
  2136   w->tab_line_height = prev_tab_line_height;
  2137 
  2138   return visible_p;
  2139 }
  2140 
  2141 
  2142 /* Return the next character from STR.  Return in *LEN the length of
  2143    the character.  This is like string_char_and_length but never
  2144    returns an invalid character.  If we find one, we return a `?', but
  2145    with the length of the invalid character.  */
  2146 
  2147 static int
  2148 check_char_and_length (const unsigned char *str, int *len)
  2149 {
  2150   int c = string_char_and_length (str, len);
  2151   if (!CHAR_VALID_P (c))
  2152     /* We may not change the length here because other places in Emacs
  2153        don't use this function, i.e. they silently accept invalid
  2154        characters.  */
  2155     c = '?';
  2156 
  2157   return c;
  2158 }
  2159 
  2160 
  2161 
  2162 /* Given a position POS containing a valid character and byte position
  2163    in STRING, return the position NCHARS ahead (NCHARS >= 0).  */
  2164 
  2165 static struct text_pos
  2166 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
  2167 {
  2168   eassert (STRINGP (string) && nchars >= 0);
  2169 
  2170   if (STRING_MULTIBYTE (string))
  2171     {
  2172       const unsigned char *p = SDATA (string) + BYTEPOS (pos);
  2173 
  2174       while (nchars--)
  2175         {
  2176           int len = BYTES_BY_CHAR_HEAD (*p);
  2177           p += len;
  2178           CHARPOS (pos) += 1;
  2179           BYTEPOS (pos) += len;
  2180         }
  2181     }
  2182   else
  2183     SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
  2184 
  2185   return pos;
  2186 }
  2187 
  2188 
  2189 /* Value is the text position, i.e. character and byte position,
  2190    for character position CHARPOS in STRING.  */
  2191 
  2192 static struct text_pos
  2193 string_pos (ptrdiff_t charpos, Lisp_Object string)
  2194 {
  2195   struct text_pos pos;
  2196   eassert (STRINGP (string));
  2197   eassert (charpos >= 0);
  2198   SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
  2199   return pos;
  2200 }
  2201 
  2202 
  2203 /* Value is a text position, i.e. character and byte position, for
  2204    character position CHARPOS in C string S.  MULTIBYTE_P
  2205    means recognize multibyte characters.  */
  2206 
  2207 static struct text_pos
  2208 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
  2209 {
  2210   struct text_pos pos;
  2211 
  2212   eassert (s != NULL);
  2213   eassert (charpos >= 0);
  2214 
  2215   if (multibyte_p)
  2216     {
  2217       SET_TEXT_POS (pos, 0, 0);
  2218       while (charpos--)
  2219         {
  2220           int len = BYTES_BY_CHAR_HEAD (*s);
  2221           s += len;
  2222           CHARPOS (pos) += 1;
  2223           BYTEPOS (pos) += len;
  2224         }
  2225     }
  2226   else
  2227     SET_TEXT_POS (pos, charpos, charpos);
  2228 
  2229   return pos;
  2230 }
  2231 
  2232 
  2233 /* Value is the number of characters in C string S.  MULTIBYTE_P
  2234    means recognize multibyte characters.  */
  2235 
  2236 static ptrdiff_t
  2237 number_of_chars (const char *s, bool multibyte_p)
  2238 {
  2239   ptrdiff_t nchars;
  2240 
  2241   if (multibyte_p)
  2242     {
  2243       ptrdiff_t rest = strlen (s);
  2244       const unsigned char *p = (const unsigned char *) s;
  2245 
  2246       for (nchars = 0; rest > 0; ++nchars)
  2247         {
  2248           int len = BYTES_BY_CHAR_HEAD (*p);
  2249           rest -= len, p += len;
  2250         }
  2251     }
  2252   else
  2253     nchars = strlen (s);
  2254 
  2255   return nchars;
  2256 }
  2257 
  2258 
  2259 /* Compute byte position NEWPOS->bytepos corresponding to
  2260    NEWPOS->charpos.  POS is a known position in string STRING.
  2261    NEWPOS->charpos must be >= POS.charpos.  */
  2262 
  2263 static void
  2264 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
  2265 {
  2266   eassert (STRINGP (string));
  2267   eassert (CHARPOS (*newpos) >= CHARPOS (pos));
  2268 
  2269   if (STRING_MULTIBYTE (string))
  2270     *newpos = string_pos_nchars_ahead (pos, string,
  2271                                        CHARPOS (*newpos) - CHARPOS (pos));
  2272   else
  2273     BYTEPOS (*newpos) = CHARPOS (*newpos);
  2274 }
  2275 
  2276 /* EXPORT:
  2277    Return an estimation of the pixel height of mode or header lines on
  2278    frame F.  FACE_ID specifies what line's height to estimate.  */
  2279 
  2280 int
  2281 estimate_mode_line_height (struct frame *f, enum face_id face_id)
  2282 {
  2283 #ifdef HAVE_WINDOW_SYSTEM
  2284   if (FRAME_WINDOW_P (f))
  2285     {
  2286       int height = FONT_HEIGHT (FRAME_FONT (f));
  2287 
  2288       /* This function is called so early when Emacs starts that the face
  2289          cache and mode line face are not yet initialized.  */
  2290       if (FRAME_FACE_CACHE (f))
  2291         {
  2292           struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
  2293           if (face)
  2294             {
  2295               if (face->font)
  2296                 height = normal_char_height (face->font, -1);
  2297               if (face->box_horizontal_line_width > 0)
  2298                 height += 2 * face->box_horizontal_line_width;
  2299             }
  2300         }
  2301 
  2302       return height;
  2303     }
  2304 #endif
  2305 
  2306   return 1;
  2307 }
  2308 
  2309 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
  2310    coordinates in (*X, *Y).  Set *BOUNDS to the rectangle that the
  2311    glyph at X, Y occupies, if BOUNDS != 0.  If NOCLIP, do
  2312    not force the value into range.  */
  2313 
  2314 void
  2315 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
  2316                        NativeRectangle *bounds, bool noclip)
  2317 {
  2318 
  2319 #ifdef HAVE_WINDOW_SYSTEM
  2320   if (FRAME_WINDOW_P (f))
  2321     {
  2322       /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
  2323          even for negative values.  */
  2324       if (pix_x < 0)
  2325         pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
  2326       if (pix_y < 0)
  2327         pix_y -= FRAME_LINE_HEIGHT (f) - 1;
  2328 
  2329       pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
  2330       pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
  2331 
  2332       if (bounds)
  2333         STORE_NATIVE_RECT (*bounds,
  2334                            FRAME_COL_TO_PIXEL_X (f, pix_x),
  2335                            FRAME_LINE_TO_PIXEL_Y (f, pix_y),
  2336                            FRAME_COLUMN_WIDTH (f) - 1,
  2337                            FRAME_LINE_HEIGHT (f) - 1);
  2338 
  2339       /* PXW: Should we clip pixels before converting to columns/lines?  */
  2340       if (!noclip)
  2341         {
  2342           if (pix_x < 0)
  2343             pix_x = 0;
  2344           else if (pix_x > FRAME_TOTAL_COLS (f))
  2345             pix_x = FRAME_TOTAL_COLS (f);
  2346 
  2347           if (pix_y < 0)
  2348             pix_y = 0;
  2349           else if (pix_y > FRAME_TOTAL_LINES (f))
  2350             pix_y = FRAME_TOTAL_LINES (f);
  2351         }
  2352     }
  2353 #endif
  2354 
  2355   *x = pix_x;
  2356   *y = pix_y;
  2357 }
  2358 
  2359 
  2360 /* Find the glyph under window-relative coordinates X/Y in window W.
  2361    Consider only glyphs from buffer text, i.e. no glyphs from overlay
  2362    strings.  Return in *HPOS and *VPOS the row and column number of
  2363    the glyph found.  Return in *AREA the glyph area containing X.
  2364    Value is a pointer to the glyph found or null if X/Y is not on
  2365    text, or we can't tell because W's current matrix is not up to
  2366    date.  */
  2367 
  2368 struct glyph *
  2369 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
  2370                   int *dx, int *dy, int *area)
  2371 {
  2372   struct glyph *glyph, *end;
  2373   struct glyph_row *row = NULL;
  2374   int x0, i;
  2375 
  2376   /* Find row containing Y.  Give up if some row is not enabled.  */
  2377   for (i = 0; i < w->current_matrix->nrows; ++i)
  2378     {
  2379       row = MATRIX_ROW (w->current_matrix, i);
  2380       if (!row->enabled_p)
  2381         return NULL;
  2382       if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
  2383         break;
  2384     }
  2385 
  2386   *vpos = i;
  2387   *hpos = 0;
  2388 
  2389   /* Give up if Y is not in the window.  */
  2390   if (i == w->current_matrix->nrows)
  2391     return NULL;
  2392 
  2393   /* Get the glyph area containing X.  */
  2394   if (w->pseudo_window_p)
  2395     {
  2396       *area = TEXT_AREA;
  2397       x0 = 0;
  2398     }
  2399   else
  2400     {
  2401       if (x < window_box_left_offset (w, TEXT_AREA))
  2402         {
  2403           *area = LEFT_MARGIN_AREA;
  2404           x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
  2405         }
  2406       else if (x < window_box_right_offset (w, TEXT_AREA))
  2407         {
  2408           *area = TEXT_AREA;
  2409           x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
  2410         }
  2411       else
  2412         {
  2413           *area = RIGHT_MARGIN_AREA;
  2414           x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
  2415         }
  2416     }
  2417 
  2418   /* Find glyph containing X.  */
  2419   glyph = row->glyphs[*area];
  2420   end = glyph + row->used[*area];
  2421   x -= x0;
  2422   while (glyph < end && x >= glyph->pixel_width)
  2423     {
  2424       x -= glyph->pixel_width;
  2425       ++glyph;
  2426     }
  2427 
  2428   if (glyph == end)
  2429     return NULL;
  2430 
  2431   if (dx)
  2432     {
  2433       *dx = x;
  2434       *dy = y - (row->y + row->ascent - glyph->ascent);
  2435     }
  2436 
  2437   *hpos = glyph - row->glyphs[*area];
  2438   return glyph;
  2439 }
  2440 
  2441 /* Convert frame-relative x/y to coordinates relative to window W.
  2442    Takes pseudo-windows into account.  */
  2443 
  2444 static void
  2445 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
  2446 {
  2447   if (w->pseudo_window_p)
  2448     {
  2449       /* A pseudo-window is always full-width, and starts at the
  2450          left edge of the frame, plus a frame border.  */
  2451       struct frame *f = XFRAME (w->frame);
  2452       *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
  2453       *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
  2454     }
  2455   else
  2456     {
  2457       *x -= WINDOW_LEFT_EDGE_X (w);
  2458       *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
  2459     }
  2460 }
  2461 
  2462 #ifdef HAVE_WINDOW_SYSTEM
  2463 
  2464 /* EXPORT:
  2465    Return in RECTS[] at most N clipping rectangles for glyph string S.
  2466    Return the number of stored rectangles.  */
  2467 
  2468 int
  2469 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
  2470 {
  2471   Emacs_Rectangle r;
  2472 
  2473   if (n <= 0)
  2474     return 0;
  2475 
  2476   if (s->row->full_width_p)
  2477     {
  2478       /* Draw full-width.  X coordinates are relative to S->w->left_col.  */
  2479       r.x = WINDOW_LEFT_EDGE_X (s->w);
  2480       if (s->row->mode_line_p)
  2481         r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
  2482       else
  2483         r.width = WINDOW_PIXEL_WIDTH (s->w);
  2484 
  2485       /* Unless displaying a mode or menu bar line, which are always
  2486          fully visible, clip to the visible part of the row.  */
  2487       if (s->w->pseudo_window_p)
  2488         r.height = s->row->visible_height;
  2489       else
  2490         r.height = s->height;
  2491     }
  2492   else
  2493     {
  2494       /* This is a text line that may be partially visible.  */
  2495       r.x = window_box_left (s->w, s->area);
  2496       r.width = window_box_width (s->w, s->area);
  2497       r.height = s->row->visible_height;
  2498     }
  2499 
  2500   if (s->clip_head)
  2501     if (r.x < s->clip_head->x)
  2502       {
  2503         if (r.width >= s->clip_head->x - r.x)
  2504           r.width -= s->clip_head->x - r.x;
  2505         else
  2506           r.width = 0;
  2507         r.x = s->clip_head->x;
  2508       }
  2509   if (s->clip_tail)
  2510     if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
  2511       {
  2512         if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
  2513           r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
  2514         else
  2515           r.width = 0;
  2516       }
  2517 
  2518   /* If S draws overlapping rows, it's sufficient to use the top and
  2519      bottom of the window for clipping because this glyph string
  2520      intentionally draws over other lines.  */
  2521   if (s->for_overlaps)
  2522     {
  2523       r.y = WINDOW_TAB_LINE_HEIGHT (s->w) + WINDOW_HEADER_LINE_HEIGHT (s->w);
  2524       r.height = window_text_bottom_y (s->w) - r.y;
  2525 
  2526       /* Alas, the above simple strategy does not work for the
  2527          environments with anti-aliased text: if the same text is
  2528          drawn onto the same place multiple times, it gets thicker.
  2529          If the overlap we are processing is for the erased cursor, we
  2530          take the intersection with the rectangle of the cursor.  */
  2531       if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
  2532         {
  2533           Emacs_Rectangle rc, r_save = r;
  2534 
  2535           rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
  2536           rc.y = s->w->phys_cursor.y;
  2537           rc.width = s->w->phys_cursor_width;
  2538           rc.height = s->w->phys_cursor_height;
  2539 
  2540           gui_intersect_rectangles (&r_save, &rc, &r);
  2541         }
  2542     }
  2543   else
  2544     {
  2545       /* Don't use S->y for clipping because it doesn't take partially
  2546          visible lines into account.  For example, it can be negative for
  2547          partially visible lines at the top of a window.  */
  2548       if (!s->row->full_width_p
  2549           && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
  2550         r.y = WINDOW_TAB_LINE_HEIGHT (s->w) + WINDOW_HEADER_LINE_HEIGHT (s->w);
  2551       else
  2552         r.y = max (0, s->row->y);
  2553     }
  2554 
  2555   r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
  2556 
  2557   /* If drawing the cursor, don't let glyph draw outside its
  2558      advertised boundaries. Cleartype does this under some circumstances.  */
  2559   if (s->hl == DRAW_CURSOR)
  2560     {
  2561       struct glyph *glyph = s->first_glyph;
  2562       int height, max_y;
  2563 
  2564       if (s->x > r.x)
  2565         {
  2566           if (r.width >= s->x - r.x)
  2567             r.width -= s->x - r.x;
  2568           else  /* R2L hscrolled row with cursor outside text area */
  2569             r.width = 0;
  2570           r.x = s->x;
  2571         }
  2572       r.width = min (r.width, glyph->pixel_width);
  2573 
  2574       /* If r.y is below window bottom, ensure that we still see a cursor.  */
  2575       height = min (glyph->ascent + glyph->descent,
  2576                     min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
  2577       max_y = window_text_bottom_y (s->w) - height;
  2578       max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
  2579       if (s->ybase - glyph->ascent > max_y)
  2580         {
  2581           r.y = max_y;
  2582           r.height = height;
  2583         }
  2584       else
  2585         {
  2586           /* Don't draw cursor glyph taller than our actual glyph.  */
  2587           height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
  2588           if (height < r.height)
  2589             {
  2590               max_y = r.y + r.height;
  2591               r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
  2592               r.height = min (max_y - r.y, height);
  2593             }
  2594         }
  2595     }
  2596 
  2597   if (s->row->clip)
  2598     {
  2599       Emacs_Rectangle r_save = r;
  2600 
  2601       if (! gui_intersect_rectangles (&r_save, s->row->clip, &r))
  2602         r.width = 0;
  2603     }
  2604 
  2605   if ((s->for_overlaps & OVERLAPS_BOTH) == 0
  2606       || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
  2607     {
  2608 #ifdef CONVERT_FROM_EMACS_RECT
  2609       CONVERT_FROM_EMACS_RECT (r, *rects);
  2610 #else
  2611       *rects = r;
  2612 #endif
  2613       return 1;
  2614     }
  2615   else
  2616     {
  2617       /* If we are processing overlapping and allowed to return
  2618          multiple clipping rectangles, we exclude the row of the glyph
  2619          string from the clipping rectangle.  This is to avoid drawing
  2620          the same text on the environment with anti-aliasing.  */
  2621 #ifdef CONVERT_FROM_EMACS_RECT
  2622       Emacs_Rectangle rs[2];
  2623 #else
  2624       Emacs_Rectangle *rs = rects;
  2625 #endif
  2626       int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
  2627 
  2628       if (s->for_overlaps & OVERLAPS_PRED)
  2629         {
  2630           rs[i] = r;
  2631           if (r.y + r.height > row_y)
  2632             {
  2633               if (r.y < row_y)
  2634                 rs[i].height = row_y - r.y;
  2635               else
  2636                 rs[i].height = 0;
  2637             }
  2638           i++;
  2639         }
  2640       if (s->for_overlaps & OVERLAPS_SUCC)
  2641         {
  2642           rs[i] = r;
  2643           if (r.y < row_y + s->row->visible_height)
  2644             {
  2645               if (r.y + r.height > row_y + s->row->visible_height)
  2646                 {
  2647                   rs[i].y = row_y + s->row->visible_height;
  2648                   rs[i].height = r.y + r.height - rs[i].y;
  2649                 }
  2650               else
  2651                 rs[i].height = 0;
  2652             }
  2653           i++;
  2654         }
  2655 
  2656       n = i;
  2657 #ifdef CONVERT_FROM_EMACS_RECT
  2658       for (i = 0; i < n; i++)
  2659         CONVERT_FROM_EMACS_RECT (rs[i], rects[i]);
  2660 #endif
  2661       return n;
  2662     }
  2663 }
  2664 
  2665 /* EXPORT:
  2666    Return in *NR the clipping rectangle for glyph string S.  */
  2667 
  2668 void
  2669 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
  2670 {
  2671   get_glyph_string_clip_rects (s, nr, 1);
  2672 }
  2673 
  2674 
  2675 /* EXPORT:
  2676    Return the position and height of the phys cursor in window W.
  2677    Set w->phys_cursor_width to width of phys cursor.
  2678 */
  2679 
  2680 void
  2681 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
  2682                           struct glyph *glyph, int *xp, int *yp, int *heightp)
  2683 {
  2684   struct frame *f = XFRAME (WINDOW_FRAME (w));
  2685   int x, y, wd, h, h0, y0, ascent;
  2686 
  2687   /* Compute the width of the rectangle to draw.  If on a stretch
  2688      glyph, and `x-stretch-cursor' is nil, don't draw a rectangle
  2689      as wide as the glyph, but use a canonical character width
  2690      instead.  */
  2691   wd = glyph->pixel_width;
  2692 
  2693   x = w->phys_cursor.x;
  2694   if (x < 0)
  2695     {
  2696       wd += x;
  2697       x = 0;
  2698     }
  2699 
  2700   if (glyph->type == STRETCH_GLYPH
  2701       && !x_stretch_cursor_p)
  2702     wd = min (FRAME_COLUMN_WIDTH (f), wd);
  2703   w->phys_cursor_width = wd;
  2704 
  2705   /* Don't let the hollow cursor glyph descend below the glyph row's
  2706      ascent value, lest the hollow cursor looks funny.  */
  2707   y = w->phys_cursor.y;
  2708   ascent = row->ascent;
  2709   /* The test for row at ZV is for when line numbers are displayed and
  2710      point is at EOB: the cursor could then be smaller or larger than
  2711      the default face's font.  */
  2712   if (!row->ends_at_zv_p && row->ascent < glyph->ascent)
  2713     {
  2714       y -= glyph->ascent - row->ascent;
  2715       ascent = glyph->ascent;
  2716     }
  2717 
  2718   /* If y is below window bottom, ensure that we still see a cursor.  */
  2719   h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
  2720 
  2721   h = max (h0, ascent + glyph->descent);
  2722   /* Don't let the cursor exceed the dimensions of the row, so that
  2723      the upper/lower side of the box aren't clipped.  */
  2724   h = min (h, row->height);
  2725   h0 = min (h0, ascent + glyph->descent);
  2726 
  2727   y0 = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  2728   if (y < y0)
  2729     {
  2730       h = max (h - (y0 - y) + 1, h0);
  2731       y = y0 - 1;
  2732     }
  2733   else
  2734     {
  2735       y0 = window_text_bottom_y (w) - h0;
  2736       if (y > y0)
  2737         {
  2738           h += y - y0;
  2739           y = y0;
  2740         }
  2741     }
  2742 
  2743   *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
  2744   *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
  2745   *heightp = h;
  2746 }
  2747 
  2748 /*
  2749  * Remember which glyph the mouse is over.
  2750  */
  2751 
  2752 void
  2753 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
  2754 {
  2755   Lisp_Object window;
  2756   struct window *w;
  2757   struct glyph_row *r, *gr, *end_row;
  2758   enum window_part part;
  2759   enum glyph_row_area area;
  2760   int x, y, width, height;
  2761 
  2762   if (mouse_fine_grained_tracking)
  2763     {
  2764       STORE_NATIVE_RECT (*rect, gx, gy, 1, 1);
  2765       return;
  2766     }
  2767 
  2768   /* Try to determine frame pixel position and size of the glyph under
  2769      frame pixel coordinates X/Y on frame F.  */
  2770 
  2771   if (window_resize_pixelwise)
  2772     {
  2773       width = height = 1;
  2774       goto virtual_glyph;
  2775     }
  2776   else if (!f->glyphs_initialized_p
  2777            || (window = window_from_coordinates (f, gx, gy, &part, false, false),
  2778                NILP (window)))
  2779     {
  2780       width = FRAME_SMALLEST_CHAR_WIDTH (f);
  2781       height = FRAME_SMALLEST_FONT_HEIGHT (f);
  2782       goto virtual_glyph;
  2783     }
  2784 
  2785   w = XWINDOW (window);
  2786   width = WINDOW_FRAME_COLUMN_WIDTH (w);
  2787   height = WINDOW_FRAME_LINE_HEIGHT (w);
  2788 
  2789   x = window_relative_x_coord (w, part, gx);
  2790   y = gy - WINDOW_TOP_EDGE_Y (w);
  2791 
  2792   r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
  2793   end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
  2794 
  2795   if (w->pseudo_window_p)
  2796     {
  2797       area = TEXT_AREA;
  2798       part = ON_MODE_LINE; /* Don't adjust margin. */
  2799       goto text_glyph;
  2800     }
  2801 
  2802   switch (part)
  2803     {
  2804     case ON_LEFT_MARGIN:
  2805       area = LEFT_MARGIN_AREA;
  2806       goto text_glyph;
  2807 
  2808     case ON_RIGHT_MARGIN:
  2809       area = RIGHT_MARGIN_AREA;
  2810       goto text_glyph;
  2811 
  2812     case ON_TAB_LINE:
  2813     case ON_HEADER_LINE:
  2814     case ON_MODE_LINE:
  2815       gr = (part == ON_TAB_LINE
  2816             ? MATRIX_TAB_LINE_ROW (w->current_matrix)
  2817             : (part == ON_HEADER_LINE
  2818                ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
  2819                : MATRIX_MODE_LINE_ROW (w->current_matrix)));
  2820       gy = gr->y;
  2821       area = TEXT_AREA;
  2822       goto text_glyph_row_found;
  2823 
  2824     case ON_TEXT:
  2825       area = TEXT_AREA;
  2826 
  2827     text_glyph:
  2828       gr = 0; gy = 0;
  2829       for (; r <= end_row && r->enabled_p; ++r)
  2830         if (r->y + r->height > y)
  2831           {
  2832             gr = r; gy = r->y;
  2833             break;
  2834           }
  2835 
  2836     text_glyph_row_found:
  2837       if (gr && gy <= y)
  2838         {
  2839           struct glyph *g = gr->glyphs[area];
  2840           struct glyph *end = g + gr->used[area];
  2841 
  2842           height = gr->height;
  2843           for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
  2844             if (gx + g->pixel_width > x)
  2845               break;
  2846 
  2847           if (g < end)
  2848             {
  2849               if (g->type == IMAGE_GLYPH)
  2850                 {
  2851                   /* Don't remember when mouse is over image, as
  2852                      image may have hot-spots.  */
  2853                   STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
  2854                   return;
  2855                 }
  2856               width = g->pixel_width;
  2857             }
  2858           else
  2859             {
  2860               /* Use nominal char spacing at end of line.  */
  2861               x -= gx;
  2862               gx += (x / width) * width;
  2863             }
  2864 
  2865           if (part != ON_MODE_LINE && part != ON_HEADER_LINE
  2866               && part != ON_TAB_LINE)
  2867             {
  2868               gx += window_box_left_offset (w, area);
  2869               /* Don't expand over the modeline to make sure the vertical
  2870                  drag cursor is shown early enough.  */
  2871               height = min (height,
  2872                             max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
  2873             }
  2874         }
  2875       else
  2876         {
  2877           /* Use nominal line height at end of window.  */
  2878           gx = (x / width) * width;
  2879           y -= gy;
  2880           gy += (y / height) * height;
  2881           if (part != ON_MODE_LINE && part != ON_HEADER_LINE
  2882               && part != ON_TAB_LINE)
  2883             /* See comment above.  */
  2884             height = min (height,
  2885                           max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
  2886         }
  2887       break;
  2888 
  2889     case ON_LEFT_FRINGE:
  2890       gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2891             ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
  2892             : window_box_right_offset (w, LEFT_MARGIN_AREA));
  2893       width = WINDOW_LEFT_FRINGE_WIDTH (w);
  2894       goto row_glyph;
  2895 
  2896     case ON_RIGHT_FRINGE:
  2897       gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2898             ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
  2899             : window_box_right_offset (w, TEXT_AREA));
  2900       if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
  2901           && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
  2902           && !WINDOW_RIGHTMOST_P (w))
  2903         if (gx < WINDOW_PIXEL_WIDTH (w) - width)
  2904           /* Make sure the vertical border can get her own glyph to the
  2905              right of the one we build here.  */
  2906           width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
  2907         else
  2908           width = WINDOW_PIXEL_WIDTH (w) - gx;
  2909       else
  2910         width = WINDOW_RIGHT_FRINGE_WIDTH (w);
  2911 
  2912       goto row_glyph;
  2913 
  2914     case ON_VERTICAL_BORDER:
  2915       gx = WINDOW_PIXEL_WIDTH (w) - width;
  2916       goto row_glyph;
  2917 
  2918     case ON_VERTICAL_SCROLL_BAR:
  2919       gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
  2920             ? 0
  2921             : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
  2922                + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2923                   ? WINDOW_RIGHT_FRINGE_WIDTH (w)
  2924                   : 0)));
  2925       width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
  2926 
  2927     row_glyph:
  2928       gr = 0, gy = 0;
  2929       for (; r <= end_row && r->enabled_p; ++r)
  2930         if (r->y + r->height > y)
  2931           {
  2932             gr = r; gy = r->y;
  2933             break;
  2934           }
  2935 
  2936       if (gr && gy <= y)
  2937         height = gr->height;
  2938       else
  2939         {
  2940           /* Use nominal line height at end of window.  */
  2941           y -= gy;
  2942           gy += (y / height) * height;
  2943         }
  2944       break;
  2945 
  2946     case ON_RIGHT_DIVIDER:
  2947       gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
  2948       width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
  2949       gy = 0;
  2950       /* The bottom divider prevails. */
  2951       height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2952       goto add_edge;
  2953 
  2954     case ON_BOTTOM_DIVIDER:
  2955       gx = 0;
  2956       width = WINDOW_PIXEL_WIDTH (w);
  2957       gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2958       height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2959       goto add_edge;
  2960 
  2961     default:
  2962       ;
  2963     virtual_glyph:
  2964       /* If there is no glyph under the mouse, then we divide the screen
  2965          into a grid of the smallest glyph in the frame, and use that
  2966          as our "glyph".  */
  2967 
  2968       /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
  2969          round down even for negative values.  */
  2970       if (gx < 0)
  2971         gx -= width - 1;
  2972       if (gy < 0)
  2973         gy -= height - 1;
  2974 
  2975       gx = (gx / width) * width;
  2976       gy = (gy / height) * height;
  2977 
  2978       goto store_rect;
  2979     }
  2980 
  2981  add_edge:
  2982   gx += WINDOW_LEFT_EDGE_X (w);
  2983   gy += WINDOW_TOP_EDGE_Y (w);
  2984 
  2985  store_rect:
  2986   STORE_NATIVE_RECT (*rect, gx, gy, width, height);
  2987 
  2988   /* Visible feedback for debugging.  */
  2989 #if false && defined HAVE_X_WINDOWS
  2990   XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
  2991                   f->output_data.x->normal_gc,
  2992                   gx, gy, width, height);
  2993 #endif
  2994 }
  2995 
  2996 
  2997 #endif /* HAVE_WINDOW_SYSTEM */
  2998 
  2999 static void
  3000 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
  3001 {
  3002   eassert (w);
  3003   w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
  3004   w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
  3005   w->window_end_vpos
  3006     = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
  3007 }
  3008 
  3009 static bool
  3010 hscrolling_current_line_p (struct window *w)
  3011 {
  3012   return (!w->suspend_auto_hscroll
  3013           && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
  3014                  Qcurrent_line));
  3015 }
  3016 
  3017 /***********************************************************************
  3018                         Lisp form evaluation
  3019  ***********************************************************************/
  3020 
  3021 /* Error handler for safe_eval and safe_call.  */
  3022 
  3023 static Lisp_Object
  3024 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
  3025 {
  3026   add_to_log ("Error during redisplay: %S signaled %S",
  3027               Flist (nargs, args), arg);
  3028   return Qnil;
  3029 }
  3030 
  3031 /* Call function FUNC with the rest of NARGS - 1 arguments
  3032    following.  Return the result, or nil if something went
  3033    wrong.  Prevent redisplay during the evaluation.  */
  3034 
  3035 static Lisp_Object
  3036 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
  3037 {
  3038   Lisp_Object val;
  3039 
  3040   if (inhibit_eval_during_redisplay)
  3041     val = Qnil;
  3042   else
  3043     {
  3044       ptrdiff_t i;
  3045       specpdl_ref count = SPECPDL_INDEX ();
  3046       Lisp_Object *args;
  3047       USE_SAFE_ALLOCA;
  3048       SAFE_ALLOCA_LISP (args, nargs);
  3049 
  3050       args[0] = func;
  3051       for (i = 1; i < nargs; i++)
  3052         args[i] = va_arg (ap, Lisp_Object);
  3053 
  3054       specbind (Qinhibit_redisplay, Qt);
  3055       if (inhibit_quit)
  3056         specbind (Qinhibit_quit, Qt);
  3057       /* Use Qt to ensure debugger does not run,
  3058          so there is no possibility of wanting to redisplay.  */
  3059       val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
  3060                                        safe_eval_handler);
  3061       val = SAFE_FREE_UNBIND_TO (count, val);
  3062     }
  3063 
  3064   return val;
  3065 }
  3066 
  3067 Lisp_Object
  3068 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
  3069 {
  3070   Lisp_Object retval;
  3071   va_list ap;
  3072 
  3073   va_start (ap, func);
  3074   retval = safe__call (false, nargs, func, ap);
  3075   va_end (ap);
  3076   return retval;
  3077 }
  3078 
  3079 /* Call function FN with one argument ARG.
  3080    Return the result, or nil if something went wrong.  */
  3081 
  3082 Lisp_Object
  3083 safe_call1 (Lisp_Object fn, Lisp_Object arg)
  3084 {
  3085   return safe_call (2, fn, arg);
  3086 }
  3087 
  3088 static Lisp_Object
  3089 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
  3090 {
  3091   Lisp_Object retval;
  3092   va_list ap;
  3093 
  3094   va_start (ap, fn);
  3095   retval = safe__call (inhibit_quit, 2, fn, ap);
  3096   va_end (ap);
  3097   return retval;
  3098 }
  3099 
  3100 Lisp_Object
  3101 safe_eval (Lisp_Object sexpr)
  3102 {
  3103   return safe__call1 (false, Qeval, sexpr);
  3104 }
  3105 
  3106 static Lisp_Object
  3107 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
  3108 {
  3109   return safe__call1 (inhibit_quit, Qeval, sexpr);
  3110 }
  3111 
  3112 /* Call function FN with two arguments ARG1 and ARG2.
  3113    Return the result, or nil if something went wrong.  */
  3114 
  3115 Lisp_Object
  3116 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
  3117 {
  3118   return safe_call (3, fn, arg1, arg2);
  3119 }
  3120 
  3121 
  3122 
  3123 /***********************************************************************
  3124                               Debugging
  3125  ***********************************************************************/
  3126 
  3127 /* Define CHECK_IT to perform sanity checks on iterators.
  3128    This is for debugging.  It is too slow to do unconditionally.  */
  3129 
  3130 static void
  3131 CHECK_IT (struct it *it)
  3132 {
  3133 #if false
  3134   if (it->method == GET_FROM_STRING)
  3135     {
  3136       eassert (STRINGP (it->string));
  3137       eassert (IT_STRING_CHARPOS (*it) >= 0);
  3138     }
  3139   else
  3140     {
  3141       eassert (IT_STRING_CHARPOS (*it) < 0);
  3142       if (it->method == GET_FROM_BUFFER)
  3143         {
  3144           /* Check that character and byte positions agree.  */
  3145           eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
  3146         }
  3147     }
  3148 
  3149   if (it->dpvec)
  3150     eassert (it->current.dpvec_index >= 0);
  3151   else
  3152     eassert (it->current.dpvec_index < 0);
  3153 #endif
  3154 }
  3155 
  3156 
  3157 /* Check that the window end of window W is what we expect it
  3158    to be---the last row in the current matrix displaying text.  */
  3159 
  3160 static void
  3161 CHECK_WINDOW_END (struct window *w)
  3162 {
  3163 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
  3164   if (!MINI_WINDOW_P (w) && w->window_end_valid)
  3165     {
  3166       struct glyph_row *row;
  3167       eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
  3168                 !row->enabled_p
  3169                 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
  3170                 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
  3171     }
  3172 #endif
  3173 }
  3174 
  3175 /***********************************************************************
  3176                        Iterator initialization
  3177  ***********************************************************************/
  3178 
  3179 /* Initialize IT for displaying current_buffer in window W, starting
  3180    at character position CHARPOS.  CHARPOS < 0 means that no buffer
  3181    position is specified which is useful when the iterator is assigned
  3182    a position later.  BYTEPOS is the byte position corresponding to
  3183    CHARPOS.
  3184 
  3185    If ROW is not null, calls to produce_glyphs with IT as parameter
  3186    will produce glyphs in that row.
  3187 
  3188    BASE_FACE_ID is the id of a base face to use.  It must be one of
  3189    DEFAULT_FACE_ID for normal text, MODE_LINE_ACTIVE_FACE_ID,
  3190    MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
  3191    mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
  3192 
  3193    If ROW is null and BASE_FACE_ID is equal to MODE_LINE_ACTIVE_FACE_ID,
  3194    MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
  3195    will be initialized to use the corresponding mode line glyph row of
  3196    the desired matrix of W.  */
  3197 
  3198 void
  3199 init_iterator (struct it *it, struct window *w,
  3200                ptrdiff_t charpos, ptrdiff_t bytepos,
  3201                struct glyph_row *row, enum face_id base_face_id)
  3202 {
  3203   enum face_id remapped_base_face_id = base_face_id;
  3204   int body_width = 0, body_height = 0;
  3205 
  3206   /* Some precondition checks.  */
  3207   eassert (w != NULL && it != NULL);
  3208   eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
  3209                            && charpos <= ZV));
  3210 
  3211   /* If face attributes have been changed since the last redisplay,
  3212      free realized faces now because they depend on face definitions
  3213      that might have changed.  Don't free faces while there might be
  3214      desired matrices pending which reference these faces.  */
  3215   if (!inhibit_free_realized_faces)
  3216     {
  3217       if (face_change)
  3218         {
  3219           face_change = false;
  3220           XFRAME (w->frame)->face_change = 0;
  3221           free_all_realized_faces (Qnil);
  3222         }
  3223       else if (XFRAME (w->frame)->face_change)
  3224         {
  3225           XFRAME (w->frame)->face_change = 0;
  3226           free_all_realized_faces (w->frame);
  3227         }
  3228     }
  3229 
  3230   /* Perhaps remap BASE_FACE_ID to a user-specified alternative.  */
  3231   if (! NILP (Vface_remapping_alist))
  3232     remapped_base_face_id
  3233       = lookup_basic_face (w, XFRAME (w->frame), base_face_id);
  3234 
  3235   /* Use one of the mode line rows of W's desired matrix if
  3236      appropriate.  */
  3237   if (row == NULL)
  3238     {
  3239       if (base_face_id == MODE_LINE_ACTIVE_FACE_ID
  3240           || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
  3241         row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
  3242       else if (base_face_id == TAB_LINE_FACE_ID)
  3243         row = MATRIX_TAB_LINE_ROW (w->desired_matrix);
  3244       else if (base_face_id == HEADER_LINE_FACE_ID)
  3245         {
  3246           /* Header line row depends on whether tab line is enabled.  */
  3247           w->desired_matrix->tab_line_p = window_wants_tab_line (w);
  3248           row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
  3249         }
  3250     }
  3251 
  3252   /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
  3253      Other parts of redisplay rely on that.  */
  3254   memclear (it, sizeof *it);
  3255   it->current.overlay_string_index = -1;
  3256   it->current.dpvec_index = -1;
  3257   it->base_face_id = remapped_base_face_id;
  3258   IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
  3259   it->paragraph_embedding = L2R;
  3260   it->bidi_it.w = w;
  3261 
  3262   /* The window in which we iterate over current_buffer:  */
  3263   XSETWINDOW (it->window, w);
  3264   it->w = w;
  3265   it->f = XFRAME (w->frame);
  3266 
  3267   it->cmp_it.id = -1;
  3268   it->cmp_it.parent_it = it;
  3269 
  3270   if (max_redisplay_ticks > 0)
  3271     update_redisplay_ticks (0, w);
  3272 
  3273   /* Extra space between lines (on window systems only).  */
  3274   if (base_face_id == DEFAULT_FACE_ID
  3275       && FRAME_WINDOW_P (it->f))
  3276     {
  3277       if (FIXNATP (BVAR (current_buffer, extra_line_spacing)))
  3278         it->extra_line_spacing = XFIXNAT (BVAR (current_buffer, extra_line_spacing));
  3279       else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
  3280         it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
  3281                                   * FRAME_LINE_HEIGHT (it->f));
  3282       else if (it->f->extra_line_spacing > 0)
  3283         it->extra_line_spacing = it->f->extra_line_spacing;
  3284     }
  3285 
  3286   /* If realized faces have been removed, e.g. because of face
  3287      attribute changes of named faces, recompute them.  When running
  3288      in batch mode, the face cache of the initial frame is null.  If
  3289      we happen to get called, make a dummy face cache.  */
  3290   if (FRAME_FACE_CACHE (it->f) == NULL)
  3291     init_frame_faces (it->f);
  3292   if (FRAME_FACE_CACHE (it->f)->used == 0)
  3293     recompute_basic_faces (it->f);
  3294 
  3295   it->override_ascent = -1;
  3296 
  3297   /* Are control characters displayed as `^C'?  */
  3298   it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
  3299 
  3300   /* -1 means everything between a CR and the following line end
  3301      is invisible.  >0 means lines indented more than this value are
  3302      invisible.  */
  3303   it->selective = (FIXNUMP (BVAR (current_buffer, selective_display))
  3304                    ? (clip_to_bounds
  3305                       (-1, XFIXNUM (BVAR (current_buffer, selective_display)),
  3306                        PTRDIFF_MAX))
  3307                    : (!NILP (BVAR (current_buffer, selective_display))
  3308                       ? -1 : 0));
  3309   it->selective_display_ellipsis_p
  3310     = !NILP (BVAR (current_buffer, selective_display_ellipses));
  3311 
  3312   /* Display table to use.  */
  3313   it->dp = window_display_table (w);
  3314 
  3315   /* Are multibyte characters enabled in current_buffer?  */
  3316   it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  3317 
  3318   it->tab_width = SANE_TAB_WIDTH (current_buffer);
  3319 
  3320   /* Are lines in the display truncated?  */
  3321   if (TRUNCATE != 0)
  3322     it->line_wrap = TRUNCATE;
  3323   if (base_face_id == DEFAULT_FACE_ID
  3324       && !it->w->hscroll
  3325       && (WINDOW_FULL_WIDTH_P (it->w)
  3326           || NILP (Vtruncate_partial_width_windows)
  3327           || (FIXNUMP (Vtruncate_partial_width_windows)
  3328               /* PXW: Shall we do something about this?  */
  3329               && (XFIXNUM (Vtruncate_partial_width_windows)
  3330                   <= WINDOW_TOTAL_COLS (it->w))))
  3331       && NILP (BVAR (current_buffer, truncate_lines)))
  3332     it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
  3333       ? WINDOW_WRAP : WORD_WRAP;
  3334 
  3335   /* Get dimensions of truncation and continuation glyphs.  These are
  3336      displayed as fringe bitmaps under X, but we need them for such
  3337      frames when the fringes are turned off.  The no_special_glyphs slot
  3338      of the iterator's frame, when set, suppresses their display - by
  3339      default for tooltip frames and when set via the 'no-special-glyphs'
  3340      frame parameter.  */
  3341 #ifdef HAVE_WINDOW_SYSTEM
  3342   if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
  3343 #endif
  3344     {
  3345       if (it->line_wrap == TRUNCATE)
  3346         {
  3347           /* We will need the truncation glyph.  */
  3348           eassert (it->glyph_row == NULL);
  3349           produce_special_glyphs (it, IT_TRUNCATION);
  3350           it->truncation_pixel_width = it->pixel_width;
  3351         }
  3352       else
  3353         {
  3354           /* We will need the continuation glyph.  */
  3355           eassert (it->glyph_row == NULL);
  3356           produce_special_glyphs (it, IT_CONTINUATION);
  3357           it->continuation_pixel_width = it->pixel_width;
  3358         }
  3359     }
  3360 
  3361   /* Reset these values to zero because the produce_special_glyphs
  3362      above has changed them.  */
  3363   it->pixel_width = it->ascent = it->descent = 0;
  3364   it->phys_ascent = it->phys_descent = 0;
  3365 
  3366   /* Set this after getting the dimensions of truncation and
  3367      continuation glyphs, so that we don't produce glyphs when calling
  3368      produce_special_glyphs, above.  */
  3369   it->glyph_row = row;
  3370   it->area = TEXT_AREA;
  3371 
  3372   /* Get the dimensions of the display area.  The display area
  3373      consists of the visible window area plus a horizontally scrolled
  3374      part to the left of the window.  All x-values are relative to the
  3375      start of this total display area.  */
  3376   if (base_face_id != DEFAULT_FACE_ID)
  3377     {
  3378       /* Mode lines, menu bar in terminal frames.  */
  3379       it->first_visible_x = 0;
  3380       it->last_visible_x =
  3381         WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
  3382     }
  3383   else
  3384     {
  3385       /* When hscrolling only the current line, don't apply the
  3386          hscroll here, it will be applied by display_line when it gets
  3387          to laying out the line showing point.  However, if the
  3388          window's min_hscroll is positive, the user specified a lower
  3389          bound for automatic hscrolling, so they expect the
  3390          non-current lines to obey that hscroll amount.  */
  3391       if (hscrolling_current_line_p (w))
  3392         {
  3393           if (w->min_hscroll > 0)
  3394             it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
  3395           else
  3396             it->first_visible_x = 0;
  3397         }
  3398       else
  3399         it->first_visible_x =
  3400           window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
  3401 
  3402       body_width = window_box_width (w, TEXT_AREA);
  3403       if (!w->pseudo_window_p && !MINI_WINDOW_P (w)
  3404           && body_width != w->old_body_pixel_width)
  3405         FRAME_WINDOW_CHANGE (it->f) = true;
  3406       it->last_visible_x = it->first_visible_x + body_width;
  3407 
  3408       /* If we truncate lines, leave room for the truncation glyph(s) at
  3409          the right margin.  Otherwise, leave room for the continuation
  3410          glyph(s).  Done only if the window has no right fringe.  */
  3411       if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
  3412         {
  3413           if (it->line_wrap == TRUNCATE)
  3414             it->last_visible_x -= it->truncation_pixel_width;
  3415           else
  3416             it->last_visible_x -= it->continuation_pixel_width;
  3417         }
  3418 
  3419       it->tab_line_p = window_wants_tab_line (w);
  3420       it->header_line_p = window_wants_header_line (w);
  3421       body_height = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  3422       it->current_y =  body_height + w->vscroll;
  3423     }
  3424 
  3425   /* Leave room for a border glyph.  */
  3426   if (!FRAME_WINDOW_P (it->f)
  3427       && !WINDOW_RIGHTMOST_P (it->w))
  3428     it->last_visible_x -= 1;
  3429 
  3430   it->last_visible_y = window_text_bottom_y (w);
  3431   body_height += it->last_visible_y;
  3432   if (!w->pseudo_window_p && !MINI_WINDOW_P (w)
  3433       && body_height != w->old_body_pixel_height)
  3434     FRAME_WINDOW_CHANGE (it->f) = true;
  3435 
  3436   /* For mode lines and alike, arrange for the first glyph having a
  3437      left box line if the face specifies a box.  */
  3438   if (base_face_id != DEFAULT_FACE_ID)
  3439     {
  3440       struct face *face;
  3441 
  3442       it->face_id = remapped_base_face_id;
  3443 
  3444       /* If we have a boxed mode line, make the first character appear
  3445          with a left box line.  */
  3446       face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
  3447       if (face && face->box != FACE_NO_BOX)
  3448         {
  3449           int box_thickness = face->box_vertical_line_width;
  3450           it->face_box_p = true;
  3451           it->start_of_box_run_p = true;
  3452           /* Make sure we will have enough horizontal space to add the
  3453              right box line at the end.  */
  3454           if (box_thickness > 0)
  3455             it->last_visible_x -= box_thickness;
  3456         }
  3457     }
  3458 
  3459   /* If a buffer position was specified, set the iterator there,
  3460      getting overlays and face properties from that position.  */
  3461   if (charpos >= BUF_BEG (current_buffer))
  3462     {
  3463       it->stop_charpos = charpos;
  3464       it->end_charpos = ZV;
  3465       eassert (charpos == BYTE_TO_CHAR (bytepos));
  3466       IT_CHARPOS (*it) = charpos;
  3467       IT_BYTEPOS (*it) = bytepos;
  3468 
  3469       /* We will rely on `reseat' to set this up properly, via
  3470          handle_face_prop.  */
  3471       it->face_id = it->base_face_id;
  3472 
  3473       it->start = it->current;
  3474       /* Do we need to reorder bidirectional text?  Not if this is a
  3475          unibyte buffer: by definition, none of the single-byte
  3476          characters are strong R2L, so no reordering is needed.  And
  3477          bidi.c doesn't support unibyte buffers anyway.  Also, don't
  3478          reorder while we are loading loadup.el, since the tables of
  3479          character properties needed for reordering are not yet
  3480          available.  */
  3481       it->bidi_p =
  3482         !redisplay__inhibit_bidi
  3483         && !NILP (BVAR (current_buffer, bidi_display_reordering))
  3484         && it->multibyte_p;
  3485 
  3486       /* If we are to reorder bidirectional text, init the bidi
  3487          iterator.  */
  3488       if (it->bidi_p)
  3489         {
  3490           /*  Since we don't know at this point whether there will be
  3491               any R2L lines in the window, we reserve space for
  3492               truncation/continuation glyphs even if only the left
  3493               fringe is absent.  */
  3494           if (base_face_id == DEFAULT_FACE_ID
  3495               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
  3496               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
  3497             {
  3498               if (it->line_wrap == TRUNCATE)
  3499                 it->last_visible_x -= it->truncation_pixel_width;
  3500               else
  3501                 it->last_visible_x -= it->continuation_pixel_width;
  3502             }
  3503           /* Note the paragraph direction that this buffer wants to
  3504              use.  */
  3505           if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
  3506                   Qleft_to_right))
  3507             it->paragraph_embedding = L2R;
  3508           else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
  3509                        Qright_to_left))
  3510             it->paragraph_embedding = R2L;
  3511           else
  3512             it->paragraph_embedding = NEUTRAL_DIR;
  3513           bidi_unshelve_cache (NULL, false);
  3514           bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  3515                         &it->bidi_it);
  3516         }
  3517 
  3518       /* This is set only when long_line_optimizations_p is non-zero
  3519          for the current buffer.  */
  3520       it->medium_narrowing_begv = 0;
  3521 
  3522       /* Compute faces etc.  */
  3523       reseat (it, it->current.pos, true);
  3524     }
  3525 
  3526   CHECK_IT (it);
  3527 }
  3528 
  3529 /* How Emacs deals with long lines.
  3530 
  3531    (1) When a buffer is about to be (re)displayed, 'redisplay_window'
  3532    detects, with a heuristic, whether it contains long lines.
  3533 
  3534    This happens in 'redisplay_window' because it is only displaying
  3535    buffers with long lines that is problematic.  In other words, none
  3536    of the optimizations described below is ever used in buffers that
  3537    are never displayed.
  3538 
  3539    This happens with a heuristic, which checks whether a buffer
  3540    contains long lines, each time its contents have changed "enough"
  3541    between two redisplay cycles, because a buffer without long lines
  3542    can become a buffer with long lines at any time, for example after
  3543    a yank command, or after a replace command, or while the output of
  3544    an external process is inserted in a buffer.
  3545 
  3546    When Emacs has detected that a buffer contains long lines, the
  3547    buffer-local variable 'long_line_optimizations_p' (in 'struct
  3548    buffer') is set, and Emacs does not try to detect whether the
  3549    buffer does or does not contain long lines anymore.
  3550 
  3551    What a long line is depends on the variable 'long-line-threshold',
  3552    whose default value is 50000 (characters).
  3553 
  3554    (2) When a buffer with long lines is (re)displayed, the amount of
  3555    data that the display routines consider is, in a few well-chosen
  3556    places, limited with a temporary restriction, whose bounds are
  3557    calculated with the functions below.
  3558 
  3559    (2.1) 'get_small_narrowing_begv' is used to create a restriction
  3560    which starts a few hundred characters before point.  The exact
  3561    number of characters depends on the width of the window in which
  3562    the buffer is displayed.
  3563 
  3564    There is no corresponding 'get_small_narrowing_zv' function,
  3565    because it is not necessary to set the end limit of that
  3566    restriction.
  3567 
  3568    This restriction is used in four places, namely:
  3569    'back_to_previous_line_start' and 'move_it_vertically_backward'
  3570    (with the 'SET_WITH_NARROWED_BEGV' macro), and in
  3571    'composition_compute_stop_pos' and 'find_automatic_composition' (in
  3572    a conditional statement depending on 'long_line_optimizations_p').
  3573 
  3574    (2.2) 'get_medium_narrowing_begv' is used to create a restriction
  3575    which starts a few thousand characters before point.  The exact
  3576    number of characters depends on the size (width and height) of the
  3577    window in which the buffer is displayed.  For performance reasons,
  3578    the return value of that function is cached in 'struct it', in the
  3579    'medium_narrowing_begv' field.
  3580 
  3581    The corresponding function 'get_medium_narrowing_zv' (and
  3582    'medium_narrowing_zv' field in 'struct it') is not used to set the
  3583    end limit of the restriction, which is again unnecessary, but to
  3584    determine, in 'reseat', whether the iterator has moved far enough
  3585    from its original position, and whether the start position of the
  3586    restriction must be computed anew.
  3587 
  3588    This restriction is used in a single place:
  3589    'get_visually_first_element', with the 'SET_WITH_NARROWED_BEGV'
  3590    macro.
  3591 
  3592    (2.3) 'get_large_narrowing_begv' and 'get_large_narrowing_zv' are
  3593    used to create a restriction which starts a few hundred thousand
  3594    characters before point and ends a few hundred thousand characters
  3595    after point.  The size of that restriction depends on the variable
  3596    'long-line-optimizations-region-size', whose default value is
  3597    500000 (characters); it can be adjusted by a few hundred characters
  3598    depending on 'long-line-optimizations-bol-search-limit', whose
  3599    default value is 128 (characters).
  3600 
  3601    For performance reasons again, the return values of these functions
  3602    are stored in the 'large_narrowing_begv' and 'large_narrowing_zv'
  3603    fields in 'struct it'.
  3604 
  3605    The restriction defined by these values is used around three
  3606    low-level hooks: around 'fontification-functions', in
  3607    'handle_fontified_prop', and around 'pre-command-hook' and
  3608    'post-command-hook', in 'safe_run_hooks_maybe_narrowed', which is
  3609    called in 'command_loop_1'.  These restrictions are set around
  3610    these hooks with 'labeled_narrow_to_region'; the restrictions are
  3611    labeled, and cannot be removed with a call to 'widen', but can be
  3612    removed with 'without-restriction' with a :label argument.
  3613 */
  3614 
  3615 static int
  3616 get_narrowed_width (struct window *w)
  3617 {
  3618   /* In a character-only terminal, only one font size is used, so we
  3619      can use a smaller factor.  */
  3620   int fact = FRAME_WINDOW_P (XFRAME (w->frame)) ? 3 : 2;
  3621   /* If the window has no fringes (in a character-only terminal or in
  3622      a GUI frame without fringes), subtract 1 from the width for the
  3623      '\' line wrapping character.  */
  3624   int width = window_body_width (w, WINDOW_BODY_IN_CANONICAL_CHARS)
  3625     - ((WINDOW_RIGHT_FRINGE_WIDTH (w) == 0
  3626         || WINDOW_LEFT_FRINGE_WIDTH (w) == 0) ? 1 : 0);
  3627   return fact * max (1, width);
  3628 }
  3629 
  3630 static int
  3631 get_narrowed_len (struct window *w)
  3632 {
  3633   int height = window_body_height (w, WINDOW_BODY_IN_CANONICAL_CHARS);
  3634   return get_narrowed_width (w) * max (1, height);
  3635 }
  3636 
  3637 static ptrdiff_t
  3638 get_medium_narrowing_begv (struct window *w, ptrdiff_t pos)
  3639 {
  3640   int len = get_narrowed_len (w);
  3641   return max ((pos / len - 1) * len, BEGV);
  3642 }
  3643 
  3644 static ptrdiff_t
  3645 get_medium_narrowing_zv (struct window *w, ptrdiff_t pos)
  3646 {
  3647   int len = get_narrowed_len (w);
  3648   return min ((pos / len + 1) * len, ZV);
  3649 }
  3650 
  3651 /* Find the position of the last BOL before POS, unless it is too far
  3652    away.  The buffer portion in which the search occurs is gradually
  3653    enlarged: [POS-500..POS], [POS-5500..POS-500],
  3654    [POS-55500..POS-5500], and finally [POS-555500..POS-55500].  Return
  3655    BEGV-1 if no BOL was found in [POS-555500..POS].  */
  3656 static ptrdiff_t
  3657 get_nearby_bol_pos (ptrdiff_t pos)
  3658 {
  3659   ptrdiff_t start, pos_bytepos, cur, next, found, bol = BEGV - 1, init_pos = pos;
  3660   int dist;
  3661   for (dist = 500; dist <= 500000; dist *= 10)
  3662     {
  3663       pos_bytepos = pos == BEGV ? BEGV_BYTE : CHAR_TO_BYTE (pos);
  3664       start = pos - dist < BEGV ? BEGV : pos - dist;
  3665       for (cur = start; cur < pos; cur = next)
  3666         {
  3667           next = find_newline1 (cur, CHAR_TO_BYTE (cur),
  3668                                 pos, pos_bytepos,
  3669                                 1, &found, NULL, false);
  3670           if (found)
  3671             bol = next;
  3672           else
  3673             break;
  3674         }
  3675       if (bol >= BEGV || start == BEGV)
  3676         break;
  3677       else
  3678         pos = pos - dist < BEGV ? BEGV : pos - dist;
  3679     }
  3680   eassert (bol <= init_pos);
  3681   return bol;
  3682 }
  3683 
  3684 ptrdiff_t
  3685 get_small_narrowing_begv (struct window *w, ptrdiff_t pos)
  3686 {
  3687   int len = get_narrowed_width (w);
  3688   ptrdiff_t bol_pos = max (get_nearby_bol_pos (pos), BEGV);
  3689   return max (bol_pos + ((pos - bol_pos) / len - 1) * len, BEGV);
  3690 }
  3691 
  3692 ptrdiff_t
  3693 get_large_narrowing_begv (ptrdiff_t pos)
  3694 {
  3695   if (long_line_optimizations_region_size <= 0)
  3696     return BEGV;
  3697   int len = long_line_optimizations_region_size / 2;
  3698   int begv = max (pos - len, BEGV);
  3699   int limit = long_line_optimizations_bol_search_limit;
  3700   while (limit > 0)
  3701     {
  3702       if (begv == BEGV || FETCH_BYTE (CHAR_TO_BYTE (begv) - 1) == '\n')
  3703         return begv;
  3704       begv--;
  3705       limit--;
  3706     }
  3707   return begv;
  3708 }
  3709 
  3710 ptrdiff_t
  3711 get_large_narrowing_zv (ptrdiff_t pos)
  3712 {
  3713   if (long_line_optimizations_region_size <= 0)
  3714     return ZV;
  3715   int len = long_line_optimizations_region_size / 2;
  3716   return min (pos + len, ZV);
  3717 }
  3718 
  3719 static void
  3720 unwind_narrowed_begv (Lisp_Object point_min)
  3721 {
  3722   SET_BUF_BEGV (current_buffer, XFIXNUM (point_min));
  3723 }
  3724 
  3725 /* Set DST to EXPR.  When IT indicates that BEGV should temporarily be
  3726    updated to optimize display, evaluate EXPR with BEGV set to BV.  */
  3727 
  3728 #define SET_WITH_NARROWED_BEGV(IT,DST,EXPR,BV)                          \
  3729   do {                                                                  \
  3730     if (IT->medium_narrowing_begv)                                      \
  3731       {                                                                 \
  3732         specpdl_ref count = SPECPDL_INDEX ();                           \
  3733         record_unwind_protect (unwind_narrowed_begv, Fpoint_min ());    \
  3734         SET_BUF_BEGV (current_buffer, BV);                              \
  3735         DST = EXPR;                                                     \
  3736         unbind_to (count, Qnil);                                        \
  3737       }                                                                 \
  3738     else                                                                \
  3739       DST = EXPR;                                                       \
  3740   } while (0)
  3741 
  3742 /* Initialize IT for the display of window W with window start POS.  */
  3743 
  3744 void
  3745 start_display (struct it *it, struct window *w, struct text_pos pos)
  3746 {
  3747   struct glyph_row *row;
  3748   int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
  3749 
  3750   row = w->desired_matrix->rows + first_vpos;
  3751   init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
  3752   it->first_vpos = first_vpos;
  3753 
  3754   /* Don't reseat to previous visible line start if current start
  3755      position is in a string or image.  */
  3756   if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
  3757     {
  3758       int first_y = it->current_y;
  3759 
  3760       /* If window start is not at a line start, skip forward to POS to
  3761          get the correct continuation lines width.  */
  3762       bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
  3763                                   || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
  3764       if (!start_at_line_beg_p)
  3765         {
  3766           int new_x;
  3767 
  3768           reseat_at_previous_visible_line_start (it);
  3769           move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
  3770 
  3771           new_x = it->current_x + it->pixel_width;
  3772 
  3773           /* If lines are continued, this line may end in the middle
  3774              of a multi-glyph character (e.g. a control character
  3775              displayed as \003, or in the middle of an overlay
  3776              string).  In this case move_it_to above will not have
  3777              taken us to the start of the continuation line but to the
  3778              end of the continued line.  */
  3779           if (it->current_x > 0
  3780               && it->line_wrap != TRUNCATE /* Lines are continued.  */
  3781               && (/* And glyph doesn't fit on the line.  */
  3782                   new_x > it->last_visible_x
  3783                   /* Or it fits exactly and we're on a window
  3784                      system frame.  */
  3785                   || (new_x == it->last_visible_x
  3786                       && FRAME_WINDOW_P (it->f)
  3787                       && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  3788                           ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
  3789                           : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
  3790             {
  3791               if ((it->current.dpvec_index >= 0
  3792                    || it->current.overlay_string_index >= 0)
  3793                   /* If we are on a newline from a display vector or
  3794                      overlay string, then we are already at the end of
  3795                      a screen line; no need to go to the next line in
  3796                      that case, as this line is not really continued.
  3797                      (If we do go to the next line, C-e will not DTRT.)  */
  3798                   && it->c != '\n')
  3799                 {
  3800                   set_iterator_to_next (it, true);
  3801                   move_it_in_display_line_to (it, -1, -1, 0);
  3802                 }
  3803 
  3804               it->continuation_lines_width += it->current_x;
  3805             }
  3806           /* If the character at POS is displayed via a display
  3807              vector, move_it_to above stops at the final glyph of
  3808              IT->dpvec.  To make the caller redisplay that character
  3809              again (a.k.a. start at POS), we need to reset the
  3810              dpvec_index to the beginning of IT->dpvec.  */
  3811           else if (it->current.dpvec_index >= 0)
  3812             it->current.dpvec_index = 0;
  3813 
  3814           /* We're starting a new display line, not affected by the
  3815              height of the continued line, so clear the appropriate
  3816              fields in the iterator structure.  */
  3817           it->max_ascent = it->max_descent = 0;
  3818           it->max_phys_ascent = it->max_phys_descent = 0;
  3819 
  3820           it->current_y = first_y;
  3821           it->vpos = 0;
  3822           it->current_x = it->hpos = 0;
  3823         }
  3824     }
  3825 }
  3826 
  3827 
  3828 /* Return true if POS is a position in ellipses displayed for invisible
  3829    text.  W is the window we display, for text property lookup.  */
  3830 
  3831 static bool
  3832 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
  3833 {
  3834   Lisp_Object prop, window;
  3835   bool ellipses_p = false;
  3836   ptrdiff_t charpos = CHARPOS (pos->pos);
  3837 
  3838   /* If POS specifies a position in a display vector, this might
  3839      be for an ellipsis displayed for invisible text.  We won't
  3840      get the iterator set up for delivering that ellipsis unless
  3841      we make sure that it gets aware of the invisible text.  */
  3842   if (pos->dpvec_index >= 0
  3843       && pos->overlay_string_index < 0
  3844       && CHARPOS (pos->string_pos) < 0
  3845       && charpos > BEGV
  3846       && (XSETWINDOW (window, w),
  3847           prop = Fget_char_property (make_fixnum (charpos),
  3848                                      Qinvisible, window),
  3849           TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
  3850     {
  3851       prop = Fget_char_property (make_fixnum (charpos - 1), Qinvisible,
  3852                                  window);
  3853       ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
  3854     }
  3855 
  3856   return ellipses_p;
  3857 }
  3858 
  3859 
  3860 /* Initialize IT for stepping through current_buffer in window W,
  3861    starting at position POS that includes overlay string and display
  3862    vector/ control character translation position information.  Value
  3863    is false if there are overlay strings with newlines at POS.  */
  3864 
  3865 static bool
  3866 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
  3867 {
  3868   ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
  3869   int i;
  3870   bool overlay_strings_with_newlines = false;
  3871 
  3872   /* If POS specifies a position in a display vector, this might
  3873      be for an ellipsis displayed for invisible text.  We won't
  3874      get the iterator set up for delivering that ellipsis unless
  3875      we make sure that it gets aware of the invisible text.  */
  3876   if (in_ellipses_for_invisible_text_p (pos, w))
  3877     {
  3878       --charpos;
  3879       bytepos = 0;
  3880     }
  3881 
  3882   /* Keep in mind: the call to reseat in init_iterator skips invisible
  3883      text, so we might end up at a position different from POS.  This
  3884      is only a problem when POS is a row start after a newline and an
  3885      overlay starts there with an after-string, and the overlay has an
  3886      invisible property.  Since we don't skip invisible text in
  3887      display_line and elsewhere immediately after consuming the
  3888      newline before the row start, such a POS will not be in a string,
  3889      but the call to init_iterator below will move us to the
  3890      after-string.  */
  3891   init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
  3892 
  3893   /* This only scans the current chunk -- it should scan all chunks.
  3894      However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
  3895      to 16 in 22.1 to make this a lesser problem.  */
  3896   for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
  3897     {
  3898       const char *s = SSDATA (it->overlay_strings[i]);
  3899       const char *e = s + SBYTES (it->overlay_strings[i]);
  3900 
  3901       while (s < e && *s != '\n')
  3902         ++s;
  3903 
  3904       if (s < e)
  3905         {
  3906           overlay_strings_with_newlines = true;
  3907           break;
  3908         }
  3909     }
  3910 
  3911   /* If position is within an overlay string, set up IT to the right
  3912      overlay string.  */
  3913   if (pos->overlay_string_index >= 0)
  3914     {
  3915       int relative_index;
  3916 
  3917       /* If the first overlay string happens to have a `display'
  3918          property for an image, the iterator will be set up for that
  3919          image, and we have to undo that setup first before we can
  3920          correct the overlay string index.  */
  3921       if (it->method == GET_FROM_IMAGE)
  3922         pop_it (it);
  3923 
  3924       /* We already have the first chunk of overlay strings in
  3925          IT->overlay_strings.  Load more until the one for
  3926          pos->overlay_string_index is in IT->overlay_strings.  */
  3927       if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
  3928         {
  3929           ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
  3930           it->current.overlay_string_index = 0;
  3931           while (n--)
  3932             {
  3933               load_overlay_strings (it, 0);
  3934               it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
  3935             }
  3936         }
  3937 
  3938       it->current.overlay_string_index = pos->overlay_string_index;
  3939       relative_index = (it->current.overlay_string_index
  3940                         % OVERLAY_STRING_CHUNK_SIZE);
  3941       it->string = it->overlay_strings[relative_index];
  3942       eassert (STRINGP (it->string));
  3943       it->current.string_pos = pos->string_pos;
  3944       it->method = GET_FROM_STRING;
  3945       it->end_charpos = SCHARS (it->string);
  3946       /* Set up the bidi iterator for this overlay string.  */
  3947       if (it->bidi_p)
  3948         {
  3949           it->bidi_it.string.lstring = it->string;
  3950           it->bidi_it.string.s = NULL;
  3951           it->bidi_it.string.schars = SCHARS (it->string);
  3952           it->bidi_it.string.bufpos = it->overlay_strings_charpos;
  3953           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  3954           it->bidi_it.string.unibyte = !it->multibyte_p;
  3955           it->bidi_it.w = it->w;
  3956           bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
  3957                         FRAME_WINDOW_P (it->f), &it->bidi_it);
  3958 
  3959           /* Synchronize the state of the bidi iterator with
  3960              pos->string_pos.  For any string position other than
  3961              zero, this will be done automagically when we resume
  3962              iteration over the string and get_visually_first_element
  3963              is called.  But if string_pos is zero, and the string is
  3964              to be reordered for display, we need to resync manually,
  3965              since it could be that the iteration state recorded in
  3966              pos ended at string_pos of 0 moving backwards in string.  */
  3967           if (CHARPOS (pos->string_pos) == 0)
  3968             {
  3969               get_visually_first_element (it);
  3970               if (IT_STRING_CHARPOS (*it) != 0)
  3971                 do {
  3972                   /* Paranoia.  */
  3973                   eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
  3974                   bidi_move_to_visually_next (&it->bidi_it);
  3975                 } while (it->bidi_it.charpos != 0);
  3976             }
  3977           eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
  3978                    && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
  3979         }
  3980     }
  3981 
  3982   if (CHARPOS (pos->string_pos) >= 0)
  3983     {
  3984       /* Recorded position is not in an overlay string, but in another
  3985          string.  This can only be a string from a `display' property.
  3986          IT should already be filled with that string.  */
  3987       it->current.string_pos = pos->string_pos;
  3988       eassert (STRINGP (it->string));
  3989       if (it->bidi_p)
  3990         bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
  3991                       FRAME_WINDOW_P (it->f), &it->bidi_it);
  3992     }
  3993 
  3994   /* Restore position in display vector translations, control
  3995      character translations or ellipses.  */
  3996   if (pos->dpvec_index >= 0)
  3997     {
  3998       if (it->dpvec == NULL)
  3999         get_next_display_element (it);
  4000       eassert (it->dpvec && it->current.dpvec_index == 0);
  4001       it->current.dpvec_index = pos->dpvec_index;
  4002     }
  4003 
  4004   CHECK_IT (it);
  4005   return !overlay_strings_with_newlines;
  4006 }
  4007 
  4008 
  4009 /* Initialize IT for stepping through current_buffer in window W
  4010    starting at ROW->start.  */
  4011 
  4012 static void
  4013 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
  4014 {
  4015   init_from_display_pos (it, w, &row->start);
  4016   it->start = row->start;
  4017   it->continuation_lines_width = row->continuation_lines_width;
  4018   CHECK_IT (it);
  4019 }
  4020 
  4021 
  4022 /* Initialize IT for stepping through current_buffer in window W
  4023    starting in the line following ROW, i.e. starting at ROW->end.
  4024    Value is false if there are overlay strings with newlines at ROW's
  4025    end position, or if the following row begins with bidi-reordered
  4026    characters that could be composed.  */
  4027 
  4028 static bool
  4029 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
  4030 {
  4031   bool success = false;
  4032 
  4033   if (init_from_display_pos (it, w, &row->end))
  4034     {
  4035       if (row->continued_p)
  4036         it->continuation_lines_width
  4037           = row->continuation_lines_width + row->pixel_width;
  4038       CHECK_IT (it);
  4039       /* Initializing IT in the presence of compositions in reordered
  4040          rows is tricky: row->end above will generally cause us to
  4041          start at position that is not the first one in the logical
  4042          order, and we might therefore miss the composition earlier in
  4043          the buffer that affects how glypsh are laid out in this row.
  4044          So we punt instead.  Note: the test below works because
  4045          get_next_display_element calls get_visually_first_element,
  4046          which calls composition_compute_stop_pos, which populates
  4047          it->cmp_it.  */
  4048       if (get_next_display_element (it)
  4049           && (it->bidi_it.scan_dir == -1 && it->cmp_it.id >= 0))
  4050         success = false;
  4051       else
  4052         success = true;
  4053     }
  4054 
  4055   return success;
  4056 }
  4057 
  4058 
  4059 
  4060 
  4061 /***********************************************************************
  4062                            Text properties
  4063  ***********************************************************************/
  4064 
  4065 /* Called when IT reaches IT->stop_charpos.  Handle text property and
  4066    overlay changes.  Set IT->stop_charpos to the next position where
  4067    to stop.  */
  4068 
  4069 static void
  4070 handle_stop (struct it *it)
  4071 {
  4072   enum prop_handled handled;
  4073   bool handle_overlay_change_p;
  4074   struct props *p;
  4075 
  4076   it->dpvec = NULL;
  4077   it->current.dpvec_index = -1;
  4078   handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
  4079   it->ellipsis_p = false;
  4080 
  4081   /* Use face of preceding text for ellipsis (if invisible) */
  4082   if (it->selective_display_ellipsis_p)
  4083     it->saved_face_id = it->face_id;
  4084 
  4085   /* Here's the description of the semantics of, and the logic behind,
  4086      the various HANDLED_* statuses:
  4087 
  4088      HANDLED_NORMALLY means the handler did its job, and the loop
  4089      should proceed to calling the next handler in order.
  4090 
  4091      HANDLED_RECOMPUTE_PROPS means the handler caused a significant
  4092      change in the properties and overlays at current position, so the
  4093      loop should be restarted, to re-invoke the handlers that were
  4094      already called.  This happens when fontification-functions were
  4095      called by handle_fontified_prop, and actually fontified
  4096      something.  Another case where HANDLED_RECOMPUTE_PROPS is
  4097      returned is when we discover overlay strings that need to be
  4098      displayed right away.  The loop below will continue for as long
  4099      as the status is HANDLED_RECOMPUTE_PROPS.
  4100 
  4101      HANDLED_RETURN means return immediately to the caller, to
  4102      continue iteration without calling any further handlers.  This is
  4103      used when we need to act on some property right away, for example
  4104      when we need to display the ellipsis or a replacing display
  4105      property, such as display string or image.
  4106 
  4107      HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
  4108      consumed, and the handler switched to the next overlay string.
  4109      This signals the loop below to refrain from looking for more
  4110      overlays before all the overlay strings of the current overlay
  4111      are processed.
  4112 
  4113      Some of the handlers called by the loop push the iterator state
  4114      onto the stack (see 'push_it'), and arrange for the iteration to
  4115      continue with another object, such as an image, a display string,
  4116      or an overlay string.  In most such cases, it->stop_charpos is
  4117      set to the first character of the string, so that when the
  4118      iteration resumes, this function will immediately be called
  4119      again, to examine the properties at the beginning of the string.
  4120 
  4121      When a display or overlay string is exhausted, the iterator state
  4122      is popped (see 'pop_it'), and iteration continues with the
  4123      previous object.  Again, in many such cases this function is
  4124      called again to find the next position where properties might
  4125      change.  */
  4126 
  4127   do
  4128     {
  4129       handled = HANDLED_NORMALLY;
  4130 
  4131       /* Call text property handlers.  */
  4132       for (p = it_props; p->handler; ++p)
  4133         {
  4134           handled = p->handler (it);
  4135 
  4136           if (handled == HANDLED_RECOMPUTE_PROPS)
  4137             break;
  4138           else if (handled == HANDLED_RETURN)
  4139             {
  4140               /* We still want to show before and after strings from
  4141                  overlays even if the actual buffer text is replaced.  */
  4142               if (!handle_overlay_change_p
  4143                   || it->sp > 1
  4144                   /* Don't call get_overlay_strings_1 if we already
  4145                      have overlay strings loaded, because doing so
  4146                      will load them again and push the iterator state
  4147                      onto the stack one more time, which is not
  4148                      expected by the rest of the code that processes
  4149                      overlay strings.  */
  4150                   || (it->current.overlay_string_index < 0
  4151                       && !get_overlay_strings_1 (it, 0, false)))
  4152                 {
  4153                   if (it->ellipsis_p)
  4154                     setup_for_ellipsis (it, 0);
  4155                   /* When handling a display spec, we might load an
  4156                      empty string.  In that case, discard it here.  We
  4157                      used to discard it in handle_single_display_spec,
  4158                      but that causes get_overlay_strings_1, above, to
  4159                      ignore overlay strings that we must check.  */
  4160                   if (STRINGP (it->string) && !SCHARS (it->string))
  4161                     pop_it (it);
  4162                   return;
  4163                 }
  4164               else if (STRINGP (it->string) && !SCHARS (it->string))
  4165                 pop_it (it);
  4166               else
  4167                 {
  4168                   it->string_from_display_prop_p = false;
  4169                   it->from_disp_prop_p = false;
  4170                   handle_overlay_change_p = false;
  4171                 }
  4172               handled = HANDLED_RECOMPUTE_PROPS;
  4173               break;
  4174             }
  4175           else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
  4176             handle_overlay_change_p = false;
  4177         }
  4178 
  4179       if (handled != HANDLED_RECOMPUTE_PROPS)
  4180         {
  4181           /* Don't check for overlay strings below when set to deliver
  4182              characters from a display vector.  */
  4183           if (it->method == GET_FROM_DISPLAY_VECTOR)
  4184             handle_overlay_change_p = false;
  4185 
  4186           /* Handle overlay changes.
  4187              This sets HANDLED to HANDLED_RECOMPUTE_PROPS
  4188              if it finds overlays.  */
  4189           if (handle_overlay_change_p)
  4190             handled = handle_overlay_change (it);
  4191         }
  4192 
  4193       if (it->ellipsis_p)
  4194         {
  4195           setup_for_ellipsis (it, 0);
  4196           break;
  4197         }
  4198     }
  4199   while (handled == HANDLED_RECOMPUTE_PROPS);
  4200 
  4201   /* Determine where to stop next.  */
  4202   if (handled == HANDLED_NORMALLY)
  4203     compute_stop_pos (it);
  4204 }
  4205 
  4206 
  4207 /* Compute IT->stop_charpos from text property and overlay change
  4208    information for IT's current position.  */
  4209 
  4210 static void
  4211 compute_stop_pos (struct it *it)
  4212 {
  4213   register INTERVAL iv, next_iv;
  4214   Lisp_Object object, limit, position;
  4215   ptrdiff_t charpos, bytepos;
  4216 
  4217   if (STRINGP (it->string))
  4218     {
  4219       /* Strings are usually short, so don't limit the search for
  4220          properties.  */
  4221       it->stop_charpos = it->end_charpos;
  4222       object = it->string;
  4223       limit = Qnil;
  4224       charpos = IT_STRING_CHARPOS (*it);
  4225       bytepos = IT_STRING_BYTEPOS (*it);
  4226     }
  4227   else
  4228     {
  4229       ptrdiff_t pos;
  4230 
  4231       /* If end_charpos is out of range for some reason, such as a
  4232          misbehaving display function, rationalize it (Bug#5984).  */
  4233       if (it->end_charpos > ZV)
  4234         it->end_charpos = ZV;
  4235       it->stop_charpos = it->end_charpos;
  4236 
  4237       /* If next overlay change is in front of the current stop pos
  4238          (which is IT->end_charpos), stop there.  Note: value of
  4239          next_overlay_change is point-max if no overlay change
  4240          follows.  */
  4241       charpos = IT_CHARPOS (*it);
  4242       bytepos = IT_BYTEPOS (*it);
  4243       pos = next_overlay_change (charpos);
  4244       if (pos < it->stop_charpos)
  4245         it->stop_charpos = pos;
  4246       /* If we are breaking compositions at point, stop at point.  */
  4247       if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
  4248           && !NILP (Vauto_composition_mode)
  4249           && composition_break_at_point
  4250           && charpos < PT && PT < it->stop_charpos)
  4251         it->stop_charpos = PT;
  4252 
  4253       /* Set up variables for computing the stop position from text
  4254          property changes.  */
  4255       XSETBUFFER (object, current_buffer);
  4256       pos = charpos + TEXT_PROP_DISTANCE_LIMIT;
  4257       /* Make sure the above arbitrary limit position is not in the
  4258          middle of composable text, so we don't break compositions by
  4259          submitting the composable text to the shaper in separate
  4260          chunks.  We play safe here by assuming that only SPC, TAB,
  4261          FF, and NL cannot be in some composition; in particular, most
  4262          ASCII punctuation characters could be composed into ligatures.  */
  4263       if (!composition_break_at_point
  4264           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  4265           && !NILP (Vauto_composition_mode))
  4266         {
  4267           ptrdiff_t endpos = charpos + 10 * TEXT_PROP_DISTANCE_LIMIT;
  4268           bool found = false;
  4269 
  4270           if (pos > ZV)
  4271             pos = ZV;
  4272           if (endpos > ZV)
  4273             endpos = ZV;
  4274           ptrdiff_t bpos = CHAR_TO_BYTE (pos);
  4275           while (pos < endpos)
  4276             {
  4277               int ch = fetch_char_advance_no_check (&pos, &bpos);
  4278               if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\f')
  4279                 {
  4280                   found = true;
  4281                   break;
  4282                 }
  4283             }
  4284           if (found)
  4285             pos--;
  4286           else if (it->stop_charpos < endpos)
  4287             pos = it->stop_charpos;
  4288           else
  4289             {
  4290               /* Give up and use the original arbitrary limit.  */
  4291               pos = charpos + TEXT_PROP_DISTANCE_LIMIT;
  4292             }
  4293         }
  4294       limit = make_fixnum (pos);
  4295     }
  4296 
  4297   /* Get the interval containing IT's position.  Value is a null
  4298      interval if there isn't such an interval.  */
  4299   position = make_fixnum (charpos);
  4300   iv = validate_interval_range (object, &position, &position, false);
  4301   if (iv)
  4302     {
  4303       Lisp_Object values_here[LAST_PROP_IDX];
  4304       struct props *p;
  4305 
  4306       /* Get properties here.  */
  4307       for (p = it_props; p->handler; ++p)
  4308         values_here[p->idx] = textget (iv->plist,
  4309                                        builtin_lisp_symbol (p->name));
  4310 
  4311       /* Look for an interval following iv that has different
  4312          properties.  */
  4313       for (next_iv = next_interval (iv);
  4314            (next_iv
  4315             && (NILP (limit)
  4316                 || XFIXNAT (limit) > next_iv->position));
  4317            next_iv = next_interval (next_iv))
  4318         {
  4319           for (p = it_props; p->handler; ++p)
  4320             {
  4321               Lisp_Object new_value = textget (next_iv->plist,
  4322                                                builtin_lisp_symbol (p->name));
  4323               if (!EQ (values_here[p->idx], new_value))
  4324                 break;
  4325             }
  4326 
  4327           if (p->handler)
  4328             break;
  4329         }
  4330 
  4331       if (next_iv)
  4332         {
  4333           if (FIXNUMP (limit)
  4334               && next_iv->position >= XFIXNAT (limit))
  4335             /* No text property change up to limit.  */
  4336             it->stop_charpos = min (XFIXNAT (limit), it->stop_charpos);
  4337           else
  4338             /* Text properties change in next_iv.  */
  4339             it->stop_charpos = min (it->stop_charpos, next_iv->position);
  4340         }
  4341     }
  4342 
  4343   if (it->cmp_it.id < 0)
  4344     {
  4345       ptrdiff_t stoppos = it->end_charpos;
  4346 
  4347       if (it->bidi_p && it->bidi_it.scan_dir < 0)
  4348         stoppos = -1;
  4349       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
  4350                                     stoppos, it->string);
  4351     }
  4352 
  4353   eassert (STRINGP (it->string)
  4354            || (it->stop_charpos >= BEGV
  4355                && it->stop_charpos >= IT_CHARPOS (*it)));
  4356 }
  4357 
  4358 /* How many characters forward to search for a display property or
  4359    display string.  Searching too far forward makes the bidi display
  4360    sluggish, especially in small windows.  */
  4361 #define MAX_DISP_SCAN 250
  4362 
  4363 /* Return the character position of a display string at or after
  4364    position specified by POSITION.  If no display string exists at or
  4365    after POSITION, return ZV.  A display string is either an overlay
  4366    with `display' property whose value is a string, or a `display'
  4367    text property whose value is a string.  STRING is data about the
  4368    string to iterate; if STRING->lstring is nil, we are iterating a
  4369    buffer.  FRAME_WINDOW_P is true when we are displaying a window
  4370    on a GUI frame.  DISP_PROP is set to zero if we searched
  4371    MAX_DISP_SCAN characters forward without finding any display
  4372    strings, non-zero otherwise.  It is set to 2 if the display string
  4373    uses any kind of `(space ...)' spec that will produce a stretch of
  4374    white space in the text area.  */
  4375 ptrdiff_t
  4376 compute_display_string_pos (struct text_pos *position,
  4377                             struct bidi_string_data *string,
  4378                             struct window *w,
  4379                             bool frame_window_p, int *disp_prop)
  4380 {
  4381   /* OBJECT = nil means current buffer.  */
  4382   Lisp_Object object, object1;
  4383   Lisp_Object pos, spec, limpos;
  4384   bool string_p = string && (STRINGP (string->lstring) || string->s);
  4385   ptrdiff_t eob = string_p ? string->schars : ZV;
  4386   ptrdiff_t begb = string_p ? 0 : BEGV;
  4387   ptrdiff_t bufpos, charpos = CHARPOS (*position);
  4388   ptrdiff_t lim =
  4389     (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
  4390   struct text_pos tpos;
  4391   int rv = 0;
  4392 
  4393   if (string && STRINGP (string->lstring))
  4394     object1 = object = string->lstring;
  4395   else if (w && !string_p)
  4396     {
  4397       XSETWINDOW (object, w);
  4398       object1 = Qnil;
  4399     }
  4400   else
  4401     object1 = object = Qnil;
  4402 
  4403   *disp_prop = 1;
  4404 
  4405   if (charpos >= eob
  4406       /* We don't support display properties whose values are strings
  4407          that have display string properties.  */
  4408       || string->from_disp_str
  4409       /* C strings cannot have display properties.  */
  4410       || (string->s && !STRINGP (object)))
  4411     {
  4412       *disp_prop = 0;
  4413       return eob;
  4414     }
  4415 
  4416   /* If the character at CHARPOS is where the display string begins,
  4417      return CHARPOS.  */
  4418   pos = make_fixnum (charpos);
  4419   if (STRINGP (object))
  4420     bufpos = string->bufpos;
  4421   else
  4422     bufpos = charpos;
  4423   tpos = *position;
  4424   if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
  4425       && (charpos <= begb
  4426           || !EQ (Fget_char_property (make_fixnum (charpos - 1), Qdisplay,
  4427                                       object),
  4428                   spec))
  4429       && (rv = handle_display_spec (NULL, spec, object1, Qnil, &tpos, bufpos,
  4430                                     frame_window_p)))
  4431     {
  4432       if (rv == 2)
  4433         *disp_prop = 2;
  4434       return charpos;
  4435     }
  4436 
  4437   /* Look forward for the first character with a `display' property
  4438      that will replace the underlying text when displayed.  */
  4439   limpos = make_fixnum (lim);
  4440   do {
  4441     pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
  4442     CHARPOS (tpos) = XFIXNAT (pos);
  4443     if (CHARPOS (tpos) >= lim)
  4444       {
  4445         *disp_prop = 0;
  4446         break;
  4447       }
  4448     if (STRINGP (object))
  4449       BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
  4450     else
  4451       BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
  4452     spec = Fget_char_property (pos, Qdisplay, object);
  4453     if (!STRINGP (object))
  4454       bufpos = CHARPOS (tpos);
  4455   } while (NILP (spec)
  4456            || !(rv = handle_display_spec (NULL, spec, object1, Qnil, &tpos,
  4457                                           bufpos, frame_window_p)));
  4458   if (rv == 2)
  4459     *disp_prop = 2;
  4460 
  4461   return CHARPOS (tpos);
  4462 }
  4463 
  4464 /* Return the character position of the end of the display string that
  4465    started at CHARPOS.  If there's no display string at CHARPOS,
  4466    return -1.  A display string is either an overlay with `display'
  4467    property whose value is a string or a `display' text property whose
  4468    value is a string.  */
  4469 ptrdiff_t
  4470 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
  4471 {
  4472   /* OBJECT = nil means current buffer.  */
  4473   Lisp_Object object =
  4474     (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
  4475   Lisp_Object pos = make_fixnum (charpos);
  4476   ptrdiff_t eob =
  4477     (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
  4478 
  4479   if (charpos >= eob || (string->s && !STRINGP (object)))
  4480     return eob;
  4481 
  4482   /* It could happen that the display property or overlay was removed
  4483      since we found it in compute_display_string_pos above.  One way
  4484      this can happen is if JIT font-lock was called (through
  4485      handle_fontified_prop), and jit-lock-functions remove text
  4486      properties or overlays from the portion of buffer that includes
  4487      CHARPOS.  Muse mode is known to do that, for example.  In this
  4488      case, we return -1 to the caller, to signal that no display
  4489      string is actually present at CHARPOS.  See bidi_fetch_char for
  4490      how this is handled.
  4491 
  4492      An alternative would be to never look for display properties past
  4493      it->stop_charpos.  But neither compute_display_string_pos nor
  4494      bidi_fetch_char that calls it know or care where the next
  4495      stop_charpos is.  */
  4496   if (NILP (Fget_char_property (pos, Qdisplay, object)))
  4497     return -1;
  4498 
  4499   /* Look forward for the first character where the `display' property
  4500      changes.  */
  4501   pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
  4502 
  4503   return XFIXNAT (pos);
  4504 }
  4505 
  4506 
  4507 
  4508 /***********************************************************************
  4509                             Fontification
  4510  ***********************************************************************/
  4511 
  4512 /* Handle changes in the `fontified' property of the current buffer by
  4513    calling hook functions from Qfontification_functions to fontify
  4514    regions of text.  */
  4515 
  4516 static enum prop_handled
  4517 handle_fontified_prop (struct it *it)
  4518 {
  4519   Lisp_Object prop, pos;
  4520   enum prop_handled handled = HANDLED_NORMALLY;
  4521 
  4522   if (!NILP (Vmemory_full))
  4523     return handled;
  4524 
  4525   /* Get the value of the `fontified' property at IT's current buffer
  4526      position.  (The `fontified' property doesn't have a special
  4527      meaning in strings.)  If the value is nil, call functions from
  4528      Qfontification_functions.  */
  4529   if (!STRINGP (it->string)
  4530       && it->s == NULL
  4531       && !NILP (Vfontification_functions)
  4532       && !(input_was_pending && redisplay_skip_fontification_on_input)
  4533       && !NILP (Vrun_hooks)
  4534       && (pos = make_fixnum (IT_CHARPOS (*it)),
  4535           prop = Fget_char_property (pos, Qfontified, Qnil),
  4536           /* Ignore the special cased nil value always present at EOB since
  4537              no amount of fontifying will be able to change it.  */
  4538           NILP (prop) && IT_CHARPOS (*it) < Z))
  4539     {
  4540       specpdl_ref count = SPECPDL_INDEX ();
  4541       Lisp_Object val;
  4542       struct buffer *obuf = current_buffer;
  4543       ptrdiff_t begv = BEGV, zv = ZV;
  4544       bool old_clip_changed = current_buffer->clip_changed;
  4545       bool saved_inhibit_flag = it->f->inhibit_clear_image_cache;
  4546 
  4547       val = Vfontification_functions;
  4548       specbind (Qfontification_functions, Qnil);
  4549 
  4550       eassert (it->end_charpos == ZV);
  4551 
  4552       if (current_buffer->long_line_optimizations_p
  4553           && long_line_optimizations_region_size > 0)
  4554         {
  4555           ptrdiff_t begv = it->large_narrowing_begv;
  4556           ptrdiff_t zv = it->large_narrowing_zv;
  4557           ptrdiff_t charpos = IT_CHARPOS (*it);
  4558           if (charpos < begv || charpos > zv)
  4559             {
  4560               begv = get_large_narrowing_begv (charpos);
  4561               zv = get_large_narrowing_zv (charpos);
  4562             }
  4563           if (begv != BEG || zv != Z)
  4564             labeled_narrow_to_region (make_fixnum (begv), make_fixnum (zv),
  4565                                       Qlong_line_optimizations_in_fontification_functions);
  4566         }
  4567 
  4568       /* Don't allow Lisp that runs from 'fontification-functions'
  4569          clear our face and image caches behind our back.  */
  4570       it->f->inhibit_clear_image_cache = true;
  4571 
  4572       if (!CONSP (val) || EQ (XCAR (val), Qlambda))
  4573         safe_call1 (val, pos);
  4574       else
  4575         {
  4576           Lisp_Object fns, fn;
  4577 
  4578           fns = Qnil;
  4579 
  4580           for (; CONSP (val); val = XCDR (val))
  4581             {
  4582               fn = XCAR (val);
  4583 
  4584               if (EQ (fn, Qt))
  4585                 {
  4586                   /* A value of t indicates this hook has a local
  4587                      binding; it means to run the global binding too.
  4588                      In a global value, t should not occur.  If it
  4589                      does, we must ignore it to avoid an endless
  4590                      loop.  */
  4591                   for (fns = Fdefault_value (Qfontification_functions);
  4592                        CONSP (fns);
  4593                        fns = XCDR (fns))
  4594                     {
  4595                       fn = XCAR (fns);
  4596                       if (!EQ (fn, Qt))
  4597                         safe_call1 (fn, pos);
  4598                     }
  4599                 }
  4600               else
  4601                 safe_call1 (fn, pos);
  4602             }
  4603         }
  4604 
  4605       it->f->inhibit_clear_image_cache = saved_inhibit_flag;
  4606       unbind_to (count, Qnil);
  4607 
  4608       /* Fontification functions routinely call `save-restriction'.
  4609          Normally, this tags clip_changed, which can confuse redisplay
  4610          (see discussion in Bug#6671).  Since we don't perform any
  4611          special handling of fontification changes in the case where
  4612          `save-restriction' isn't called, there's no point doing so in
  4613          this case either.  So, if the buffer's restrictions are
  4614          actually left unchanged, reset clip_changed.  */
  4615       if (obuf == current_buffer)
  4616         {
  4617           if (begv == BEGV && zv == ZV)
  4618             current_buffer->clip_changed = old_clip_changed;
  4619         }
  4620       /* There isn't much we can reasonably do to protect against
  4621          misbehaving fontification, but here's a fig leaf.  */
  4622       else if (BUFFER_LIVE_P (obuf))
  4623         set_buffer_internal_1 (obuf);
  4624 
  4625       /* The fontification code may have added/removed text.
  4626          It could do even a lot worse, but let's at least protect against
  4627          the most obvious case where only the text past `pos' gets changed',
  4628          as is/was done in grep.el where some escapes sequences are turned
  4629          into face properties (bug#7876).  */
  4630       it->end_charpos = ZV;
  4631 
  4632       /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
  4633          something.  This avoids an endless loop if they failed to
  4634          fontify the text for which reason ever.  */
  4635       if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
  4636         handled = HANDLED_RECOMPUTE_PROPS;
  4637     }
  4638 
  4639   return handled;
  4640 }
  4641 
  4642 
  4643 
  4644 /***********************************************************************
  4645                                 Faces
  4646  ***********************************************************************/
  4647 
  4648 static int
  4649 face_at_pos (const struct it *it, enum lface_attribute_index attr_filter)
  4650 {
  4651   ptrdiff_t next_stop;
  4652 
  4653   if (!STRINGP (it->string))
  4654     {
  4655       return face_at_buffer_position (it->w,
  4656                                       IT_CHARPOS (*it),
  4657                                       &next_stop,
  4658                                       (IT_CHARPOS (*it)
  4659                                        + TEXT_PROP_DISTANCE_LIMIT),
  4660                                       false, it->base_face_id,
  4661                                       attr_filter);
  4662     }
  4663   else
  4664     {
  4665       int base_face_id;
  4666       ptrdiff_t bufpos;
  4667       int i;
  4668       Lisp_Object from_overlay
  4669         = (it->current.overlay_string_index >= 0
  4670            ? it->string_overlays[it->current.overlay_string_index
  4671                                  % OVERLAY_STRING_CHUNK_SIZE]
  4672            : Qnil);
  4673 
  4674       /* See if we got to this string directly or indirectly from
  4675          an overlay property.  That includes the before-string or
  4676          after-string of an overlay, strings in display properties
  4677          provided by an overlay, their text properties, etc.
  4678 
  4679          FROM_OVERLAY is the overlay that brought us here, or nil if none.  */
  4680       if (! NILP (from_overlay))
  4681         for (i = it->sp - 1; i >= 0; i--)
  4682           {
  4683             if (it->stack[i].current.overlay_string_index >= 0)
  4684               from_overlay
  4685                 = it->string_overlays[it->stack[i].current.overlay_string_index
  4686                                       % OVERLAY_STRING_CHUNK_SIZE];
  4687             else if (! NILP (it->stack[i].from_overlay))
  4688               from_overlay = it->stack[i].from_overlay;
  4689 
  4690             if (!NILP (from_overlay))
  4691               break;
  4692           }
  4693 
  4694       if (! NILP (from_overlay))
  4695         {
  4696           bufpos = IT_CHARPOS (*it);
  4697           /* For a string from an overlay, the base face depends
  4698              only on text properties and ignores overlays.  */
  4699           base_face_id
  4700             = face_for_overlay_string (it->w,
  4701                                        IT_CHARPOS (*it),
  4702                                        &next_stop,
  4703                                        (IT_CHARPOS (*it)
  4704                                         + TEXT_PROP_DISTANCE_LIMIT),
  4705                                        false,
  4706                                        from_overlay, attr_filter);
  4707         }
  4708       else
  4709         {
  4710           bufpos = 0;
  4711 
  4712           /* For strings from a `display' property, use the face at
  4713              IT's current buffer position as the base face to merge
  4714              with, so that overlay strings appear in the same face as
  4715              surrounding text, unless they specify their own faces.
  4716              For strings from wrap-prefix and line-prefix properties,
  4717              use the default face, possibly remapped via
  4718              Vface_remapping_alist.  */
  4719           /* Note that the fact that we use the face at _buffer_
  4720              position means that a 'display' property on an overlay
  4721              string will not inherit the face of that overlay string,
  4722              but will instead revert to the face of buffer text
  4723              covered by the overlay.  This is visible, e.g., when the
  4724              overlay specifies a box face, but neither the buffer nor
  4725              the display string do.  This sounds like a design bug,
  4726              but Emacs always did that since v21.1, so changing that
  4727              might be a big deal.  */
  4728           base_face_id = it->string_from_prefix_prop_p
  4729             ? (!NILP (Vface_remapping_alist)
  4730                ? lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
  4731                : DEFAULT_FACE_ID)
  4732             : underlying_face_id (it);
  4733         }
  4734 
  4735       return face_at_string_position (it->w,
  4736                                       it->string,
  4737                                       IT_STRING_CHARPOS (*it),
  4738                                       bufpos,
  4739                                       &next_stop,
  4740                                       base_face_id, false,
  4741                                       attr_filter);
  4742     } /* !STRINGP (it->string) */
  4743 }
  4744 
  4745 
  4746 /* Set up iterator IT from face properties at its current position.
  4747    Called from handle_stop.  */
  4748 static enum prop_handled
  4749 handle_face_prop (struct it *it)
  4750 {
  4751   specpdl_ref count = SPECPDL_INDEX ();
  4752   /* Don't allow the user to quit out of face-merging code, in case
  4753      this is called when redisplaying a non-selected window, with
  4754      point temporarily moved to window-point.  */
  4755   specbind (Qinhibit_quit, Qt);
  4756   const int new_face_id = face_at_pos (it, 0);
  4757   unbind_to (count, Qnil);
  4758 
  4759 
  4760   /* Is this a start of a run of characters with box face?
  4761      Caveat: this can be called for a freshly initialized
  4762      iterator; face_id is -1 in this case.  We know that the new
  4763      face will not change until limit, i.e. if the new face has a
  4764      box, all characters up to limit will have one.  But, as
  4765      usual, we don't know whether limit is really the end.  */
  4766   if (new_face_id != it->face_id)
  4767     {
  4768       struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
  4769       /* If it->face_id is -1, old_face below will be NULL, see
  4770          the definition of FACE_FROM_ID_OR_NULL.  This will happen
  4771          if this is the initial call that gets the face.  */
  4772       struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  4773 
  4774       /* If the value of face_id of the iterator is -1, we have to
  4775          look in front of IT's position and see whether there is a
  4776          face there that's different from new_face_id.  */
  4777       if (!STRINGP (it->string)
  4778           && !old_face
  4779           && IT_CHARPOS (*it) > BEG)
  4780         {
  4781           const int prev_face_id = face_before_it_pos (it);
  4782 
  4783           old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  4784         }
  4785 
  4786       /* If the new face has a box, but the old face does not,
  4787          this is the start of a run of characters with box face,
  4788          i.e. this character has a shadow on the left side.  */
  4789       it->face_id = new_face_id;
  4790       /* Don't reset the start_of_box_run_p flag, only set it if
  4791          needed.  */
  4792       if (!(it->start_of_box_run_p && old_face && old_face->box))
  4793         it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
  4794                                   && (old_face == NULL || !old_face->box));
  4795       it->face_box_p = new_face->box != FACE_NO_BOX;
  4796     }
  4797 
  4798   return HANDLED_NORMALLY;
  4799 }
  4800 
  4801 
  4802 /* Return the ID of the face ``underlying'' IT's current position,
  4803    which is in a string.  If the iterator is associated with a
  4804    buffer, return the face at IT's current buffer position.
  4805    Otherwise, use the iterator's base_face_id.  */
  4806 
  4807 static int
  4808 underlying_face_id (const struct it *it)
  4809 {
  4810   int face_id = it->base_face_id, i;
  4811 
  4812   eassert (STRINGP (it->string));
  4813 
  4814   for (i = it->sp - 1; i >= 0; --i)
  4815     if (NILP (it->stack[i].string))
  4816       face_id = it->stack[i].face_id;
  4817 
  4818   return face_id;
  4819 }
  4820 
  4821 
  4822 /* Compute the face one character before or after the current position
  4823    of IT, in the visual order.  BEFORE_P means get the face
  4824    in front (to the left in L2R paragraphs, to the right in R2L
  4825    paragraphs) of IT's screen position.  Value is the ID of the face.  */
  4826 
  4827 static int
  4828 face_before_or_after_it_pos (struct it *it, bool before_p)
  4829 {
  4830   int face_id, limit;
  4831   ptrdiff_t next_check_charpos;
  4832   struct it it_copy;
  4833   void *it_copy_data = NULL;
  4834 
  4835   eassert (it->s == NULL);
  4836 
  4837   if (STRINGP (it->string))
  4838     {
  4839       ptrdiff_t bufpos, charpos;
  4840       int base_face_id;
  4841 
  4842       /* No face change past the end of the string (for the case we
  4843          are padding with spaces).  No face change before the string
  4844          start.  Ignore face changes before the first visible
  4845          character on this display line.  */
  4846       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
  4847           || (IT_STRING_CHARPOS (*it) == 0 && before_p)
  4848           || it->current_x <= it->first_visible_x)
  4849         return it->face_id;
  4850 
  4851       if (!it->bidi_p)
  4852         {
  4853           /* Set charpos to the position before or after IT's current
  4854              position, in the logical order, which in the non-bidi
  4855              case is the same as the visual order.  */
  4856           if (before_p)
  4857             charpos = IT_STRING_CHARPOS (*it) - 1;
  4858           else if (it->what == IT_COMPOSITION)
  4859             /* For composition, we must check the character after the
  4860                composition.  */
  4861             charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
  4862           else
  4863             charpos = IT_STRING_CHARPOS (*it) + 1;
  4864         }
  4865       else
  4866         {
  4867           /* With bidi iteration, the character before the current in
  4868              the visual order cannot be found by simple iteration,
  4869              because "reverse" reordering is not supported.  Instead,
  4870              we need to start from the string beginning and go all the
  4871              way to the current string position, remembering the
  4872              visually-previous position.  We need to start from the
  4873              string beginning for the character after the current as
  4874              well, since the iterator state in IT may have been
  4875              pushed, and the bidi cache is no longer coherent with the
  4876              string's text.  */
  4877           SAVE_IT (it_copy, *it, it_copy_data);
  4878           IT_STRING_CHARPOS (it_copy) = 0;
  4879           bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
  4880           it_copy.bidi_it.scan_dir = 0;
  4881 
  4882           do
  4883             {
  4884               charpos = it_copy.bidi_it.charpos;
  4885               if (charpos >= SCHARS (it->string))
  4886                 break;
  4887               bidi_move_to_visually_next (&it_copy.bidi_it);
  4888             }
  4889           while (it_copy.bidi_it.charpos != IT_STRING_CHARPOS (*it));
  4890 
  4891           if (!before_p)
  4892             {
  4893               /* Set charpos to the string position of the character
  4894                  that comes after IT's current position in the visual
  4895                  order.  */
  4896               int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
  4897               /* If this is the first string character,
  4898                  bidi_move_to_visually_next will deliver character at
  4899                  current position without moving, so we need to enlarge N.  */
  4900               if (it_copy.bidi_it.first_elt)
  4901                 n++;
  4902               while (n--)
  4903                 bidi_move_to_visually_next (&it_copy.bidi_it);
  4904 
  4905               charpos = it_copy.bidi_it.charpos;
  4906             }
  4907 
  4908           RESTORE_IT (it, it, it_copy_data);
  4909         }
  4910       eassert (0 <= charpos && charpos <= SCHARS (it->string));
  4911 
  4912       if (it->current.overlay_string_index >= 0)
  4913         bufpos = IT_CHARPOS (*it);
  4914       else
  4915         bufpos = 0;
  4916 
  4917       base_face_id = underlying_face_id (it);
  4918 
  4919       /* Get the face for ASCII, or unibyte.  */
  4920       face_id = face_at_string_position (it->w, it->string, charpos,
  4921                                          bufpos, &next_check_charpos,
  4922                                          base_face_id, false, 0);
  4923 
  4924       /* Correct the face for charsets different from ASCII.  Do it
  4925          for the multibyte case only.  The face returned above is
  4926          suitable for unibyte text if IT->string is unibyte.  */
  4927       if (STRING_MULTIBYTE (it->string))
  4928         {
  4929           struct text_pos pos1 = string_pos (charpos, it->string);
  4930           const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
  4931           struct face *face = FACE_FROM_ID (it->f, face_id);
  4932           int len, c = check_char_and_length (p, &len);
  4933           face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
  4934         }
  4935     }
  4936   else
  4937     {
  4938       struct text_pos pos;
  4939 
  4940       if ((IT_CHARPOS (*it) >= ZV && !before_p)
  4941           || (IT_CHARPOS (*it) <= BEGV && before_p))
  4942         return it->face_id;
  4943 
  4944       limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
  4945       pos = it->current.pos;
  4946 
  4947       if (!it->bidi_p)
  4948         {
  4949           if (before_p)
  4950             DEC_TEXT_POS (pos, it->multibyte_p);
  4951           else
  4952             {
  4953               if (it->what == IT_COMPOSITION)
  4954                 {
  4955                   /* For composition, we must check the position after
  4956                      the composition.  */
  4957                   pos.charpos += it->cmp_it.nchars;
  4958                   pos.bytepos += it->len;
  4959                 }
  4960               else
  4961                 INC_TEXT_POS (pos, it->multibyte_p);
  4962             }
  4963         }
  4964       else
  4965         {
  4966           if (before_p)
  4967             {
  4968               int current_x;
  4969 
  4970               /* With bidi iteration, the character before the current
  4971                  in the visual order cannot be found by simple
  4972                  iteration, because "reverse" reordering is not
  4973                  supported.  Instead, we need to use the move_it_*
  4974                  family of functions, and move to the previous
  4975                  character starting from the beginning of the visual
  4976                  line.  */
  4977               /* Ignore face changes before the first visible
  4978                  character on this display line.  */
  4979               if (it->current_x <= it->first_visible_x)
  4980                 return it->face_id;
  4981               SAVE_IT (it_copy, *it, it_copy_data);
  4982               /* Implementation note: Since move_it_in_display_line
  4983                  works in the iterator geometry, and thinks the first
  4984                  character is always the leftmost, even in R2L lines,
  4985                  we don't need to distinguish between the R2L and L2R
  4986                  cases here.  */
  4987               current_x = it_copy.current_x;
  4988               move_it_vertically_backward (&it_copy, 0);
  4989               move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
  4990               pos = it_copy.current.pos;
  4991               RESTORE_IT (it, it, it_copy_data);
  4992             }
  4993           else
  4994             {
  4995               /* Set charpos to the buffer position of the character
  4996                  that comes after IT's current position in the visual
  4997                  order.  */
  4998               int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
  4999 
  5000               it_copy = *it;
  5001               /* If this is the first display element,
  5002                  bidi_move_to_visually_next will deliver character at
  5003                  current position without moving, so we need to enlarge N.  */
  5004               if (it->bidi_it.first_elt)
  5005                 n++;
  5006               while (n--)
  5007                 bidi_move_to_visually_next (&it_copy.bidi_it);
  5008 
  5009               SET_TEXT_POS (pos,
  5010                             it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
  5011             }
  5012         }
  5013       eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
  5014 
  5015       /* Determine face for CHARSET_ASCII, or unibyte.  */
  5016       face_id = face_at_buffer_position (it->w,
  5017                                          CHARPOS (pos),
  5018                                          &next_check_charpos,
  5019                                          limit, false, -1, 0);
  5020 
  5021       /* Correct the face for charsets different from ASCII.  Do it
  5022          for the multibyte case only.  The face returned above is
  5023          suitable for unibyte text if current_buffer is unibyte.  */
  5024       if (it->multibyte_p)
  5025         {
  5026           int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
  5027           struct face *face = FACE_FROM_ID (it->f, face_id);
  5028           face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
  5029         }
  5030     }
  5031 
  5032   return face_id;
  5033 }
  5034 
  5035 
  5036 
  5037 /***********************************************************************
  5038                             Invisible text
  5039  ***********************************************************************/
  5040 
  5041 /* Set up iterator IT from invisible properties at its current
  5042    position.  Called from handle_stop.  */
  5043 
  5044 static enum prop_handled
  5045 handle_invisible_prop (struct it *it)
  5046 {
  5047   enum prop_handled handled = HANDLED_NORMALLY;
  5048   int invis;
  5049   Lisp_Object prop;
  5050 
  5051   if (STRINGP (it->string))
  5052     {
  5053       Lisp_Object end_charpos, limit;
  5054 
  5055       /* Get the value of the invisible text property at the
  5056          current position.  Value will be nil if there is no such
  5057          property.  */
  5058       end_charpos = make_fixnum (IT_STRING_CHARPOS (*it));
  5059       prop = Fget_text_property (end_charpos, Qinvisible, it->string);
  5060       invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5061 
  5062       if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
  5063         {
  5064           /* Record whether we have to display an ellipsis for the
  5065              invisible text.  */
  5066           bool display_ellipsis_p = (invis == 2);
  5067           ptrdiff_t len, endpos;
  5068 
  5069           handled = HANDLED_RECOMPUTE_PROPS;
  5070 
  5071           /* Get the position at which the next visible text can be
  5072              found in IT->string, if any.  */
  5073           endpos = len = SCHARS (it->string);
  5074           XSETINT (limit, len);
  5075           do
  5076             {
  5077               end_charpos
  5078                 = Fnext_single_property_change (end_charpos, Qinvisible,
  5079                                                 it->string, limit);
  5080               /* Since LIMIT is always an integer, so should be the
  5081                  value returned by Fnext_single_property_change.  */
  5082               eassert (FIXNUMP (end_charpos));
  5083               if (FIXNUMP (end_charpos))
  5084                 {
  5085                   endpos = XFIXNAT (end_charpos);
  5086                   prop = Fget_text_property (end_charpos, Qinvisible, it->string);
  5087                   invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5088                   if (invis == 2)
  5089                     display_ellipsis_p = true;
  5090                 }
  5091               else /* Should never happen; but if it does, exit the loop.  */
  5092                 endpos = len;
  5093             }
  5094           while (invis != 0 && endpos < len);
  5095 
  5096           if (display_ellipsis_p)
  5097             it->ellipsis_p = true;
  5098 
  5099           if (endpos < len)
  5100             {
  5101               /* Text at END_CHARPOS is visible.  Move IT there.  */
  5102               struct text_pos old;
  5103               ptrdiff_t oldpos;
  5104 
  5105               old = it->current.string_pos;
  5106               oldpos = CHARPOS (old);
  5107               if (it->bidi_p)
  5108                 {
  5109                   if (it->bidi_it.first_elt
  5110                       && it->bidi_it.charpos < SCHARS (it->string))
  5111                     bidi_paragraph_init (it->paragraph_embedding,
  5112                                          &it->bidi_it, true);
  5113                   /* Bidi-iterate out of the invisible text.  */
  5114                   do
  5115                     {
  5116                       bidi_move_to_visually_next (&it->bidi_it);
  5117                     }
  5118                   while (oldpos <= it->bidi_it.charpos
  5119                          && it->bidi_it.charpos < endpos
  5120                          && it->bidi_it.charpos < it->bidi_it.string.schars);
  5121 
  5122                   IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  5123                   IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  5124                   if (IT_CHARPOS (*it) >= endpos)
  5125                     it->prev_stop = endpos;
  5126                 }
  5127               else
  5128                 {
  5129                   IT_STRING_CHARPOS (*it) = endpos;
  5130                   compute_string_pos (&it->current.string_pos, old, it->string);
  5131                 }
  5132             }
  5133           else
  5134             {
  5135               /* The rest of the string is invisible.  If this is an
  5136                  overlay string, proceed with the next overlay string
  5137                  or whatever comes and return a character from there.  */
  5138               if (it->current.overlay_string_index >= 0
  5139                   && !display_ellipsis_p)
  5140                 {
  5141                   next_overlay_string (it);
  5142                   /* Don't check for overlay strings when we just
  5143                      finished processing them.  */
  5144                   handled = HANDLED_OVERLAY_STRING_CONSUMED;
  5145                 }
  5146               else
  5147                 {
  5148                   IT_STRING_CHARPOS (*it) = SCHARS (it->string);
  5149                   IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
  5150                 }
  5151             }
  5152         }
  5153     }
  5154   else
  5155     {
  5156       ptrdiff_t newpos, next_stop, start_charpos, tem;
  5157       Lisp_Object pos, overlay;
  5158 
  5159       /* First of all, is there invisible text at this position?  */
  5160       tem = start_charpos = IT_CHARPOS (*it);
  5161       pos = make_fixnum (tem);
  5162       prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
  5163                                             &overlay);
  5164       invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5165 
  5166       /* If we are on invisible text, skip over it.  */
  5167       if (invis != 0 && start_charpos < it->end_charpos)
  5168         {
  5169           /* Record whether we have to display an ellipsis for the
  5170              invisible text.  */
  5171           bool display_ellipsis_p = invis == 2;
  5172 
  5173           handled = HANDLED_RECOMPUTE_PROPS;
  5174 
  5175           /* Loop skipping over invisible text.  The loop is left at
  5176              ZV or with IT on the first char being visible again.  */
  5177           do
  5178             {
  5179               /* Try to skip some invisible text.  Return value is the
  5180                  position reached which can be equal to where we start
  5181                  if there is nothing invisible there.  This skips both
  5182                  over invisible text properties and overlays with
  5183                  invisible property.  */
  5184               newpos = skip_invisible (tem, &next_stop, ZV, it->window);
  5185 
  5186               /* If we skipped nothing at all we weren't at invisible
  5187                  text in the first place.  If everything to the end of
  5188                  the buffer was skipped, end the loop.  */
  5189               if (newpos == tem || newpos >= ZV)
  5190                 invis = 0;
  5191               else
  5192                 {
  5193                   /* We skipped some characters but not necessarily
  5194                      all there are.  Check if we ended up on visible
  5195                      text.  Fget_char_property returns the property of
  5196                      the char before the given position, i.e. if we
  5197                      get invis = 0, this means that the char at
  5198                      newpos is visible.  */
  5199                   pos = make_fixnum (newpos);
  5200                   prop = Fget_char_property (pos, Qinvisible, it->window);
  5201                   invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5202                 }
  5203 
  5204               /* If we ended up on invisible text, proceed to
  5205                  skip starting with next_stop.  */
  5206               if (invis != 0)
  5207                 tem = next_stop;
  5208 
  5209               /* If there are adjacent invisible texts, don't lose the
  5210                  second one's ellipsis.  */
  5211               if (invis == 2)
  5212                 display_ellipsis_p = true;
  5213             }
  5214           while (invis != 0);
  5215 
  5216           /* The position newpos is now either ZV or on visible text.  */
  5217           if (it->bidi_p)
  5218             {
  5219               ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
  5220               bool on_newline
  5221                 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
  5222               bool after_newline
  5223                 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
  5224 
  5225               /* If the invisible text ends on a newline or on a
  5226                  character after a newline, we can avoid the costly,
  5227                  character by character, bidi iteration to NEWPOS, and
  5228                  instead simply reseat the iterator there.  That's
  5229                  because all bidi reordering information is tossed at
  5230                  the newline.  This is a big win for modes that hide
  5231                  complete lines, like Outline, Org, etc.  */
  5232               if (on_newline || after_newline)
  5233                 {
  5234                   struct text_pos tpos;
  5235                   bidi_dir_t pdir = it->bidi_it.paragraph_dir;
  5236 
  5237                   SET_TEXT_POS (tpos, newpos, bpos);
  5238                   reseat_1 (it, tpos, false);
  5239                   /* If we reseat on a newline/ZV, we need to prep the
  5240                      bidi iterator for advancing to the next character
  5241                      after the newline/EOB, keeping the current paragraph
  5242                      direction (so that PRODUCE_GLYPHS does TRT wrt
  5243                      prepending/appending glyphs to a glyph row).  */
  5244                   if (on_newline)
  5245                     {
  5246                       it->bidi_it.first_elt = false;
  5247                       it->bidi_it.paragraph_dir = pdir;
  5248                       it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
  5249                       it->bidi_it.nchars = 1;
  5250                       it->bidi_it.ch_len = 1;
  5251                     }
  5252                 }
  5253               else      /* Must use the slow method.  */
  5254                 {
  5255                   /* With bidi iteration, the region of invisible text
  5256                      could start and/or end in the middle of a
  5257                      non-base embedding level.  Therefore, we need to
  5258                      skip invisible text using the bidi iterator,
  5259                      starting at IT's current position, until we find
  5260                      ourselves outside of the invisible text.
  5261                      Skipping invisible text _after_ bidi iteration
  5262                      avoids affecting the visual order of the
  5263                      displayed text when invisible properties are
  5264                      added or removed.  */
  5265                   if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
  5266                     {
  5267                       /* If we were `reseat'ed to a new paragraph,
  5268                          determine the paragraph base direction.  We
  5269                          need to do it now because
  5270                          next_element_from_buffer may not have a
  5271                          chance to do it, if we are going to skip any
  5272                          text at the beginning, which resets the
  5273                          FIRST_ELT flag.  */
  5274                       bidi_paragraph_init (it->paragraph_embedding,
  5275                                            &it->bidi_it, true);
  5276                     }
  5277                   do
  5278                     {
  5279                       bidi_move_to_visually_next (&it->bidi_it);
  5280                     }
  5281                   while (it->stop_charpos <= it->bidi_it.charpos
  5282                          && it->bidi_it.charpos < newpos);
  5283                   IT_CHARPOS (*it) = it->bidi_it.charpos;
  5284                   IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  5285                   /* If we overstepped NEWPOS, record its position in
  5286                      the iterator, so that we skip invisible text if
  5287                      later the bidi iteration lands us in the
  5288                      invisible region again. */
  5289                   if (IT_CHARPOS (*it) >= newpos)
  5290                     it->prev_stop = newpos;
  5291                 }
  5292             }
  5293           else
  5294             {
  5295               IT_CHARPOS (*it) = newpos;
  5296               IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
  5297             }
  5298 
  5299           if (display_ellipsis_p)
  5300             {
  5301               /* Make sure that the glyphs of the ellipsis will get
  5302                  correct `charpos' values.  If we would not update
  5303                  it->position here, the glyphs would belong to the
  5304                  last visible character _before_ the invisible
  5305                  text, which confuses `set_cursor_from_row'.
  5306 
  5307                  We use the last invisible position instead of the
  5308                  first because this way the cursor is always drawn on
  5309                  the first "." of the ellipsis, whenever PT is inside
  5310                  the invisible text.  Otherwise the cursor would be
  5311                  placed _after_ the ellipsis when the point is after the
  5312                  first invisible character.  */
  5313               if (!STRINGP (it->object))
  5314                 {
  5315                   it->position.charpos = newpos - 1;
  5316                   it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
  5317                 }
  5318             }
  5319 
  5320           /* If there are before-strings at the start of invisible
  5321              text, and the text is invisible because of a text
  5322              property, arrange to show before-strings because 20.x did
  5323              it that way.  (If the text is invisible because of an
  5324              overlay property instead of a text property, this is
  5325              already handled in the overlay code.)  */
  5326           if (NILP (overlay)
  5327               && get_overlay_strings (it, it->stop_charpos))
  5328             {
  5329               handled = HANDLED_RECOMPUTE_PROPS;
  5330               if (it->sp > 0)
  5331                 {
  5332                   it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
  5333                   /* The call to get_overlay_strings above recomputes
  5334                      it->stop_charpos, but it only considers changes
  5335                      in properties and overlays beyond iterator's
  5336                      current position.  This causes us to miss changes
  5337                      that happen exactly where the invisible property
  5338                      ended.  So we play it safe here and force the
  5339                      iterator to check for potential stop positions
  5340                      immediately after the invisible text.  Note that
  5341                      if get_overlay_strings returns true, it
  5342                      normally also pushed the iterator stack, so we
  5343                      need to update the stop position in the slot
  5344                      below the current one.  */
  5345                   it->stack[it->sp - 1].stop_charpos
  5346                     = CHARPOS (it->stack[it->sp - 1].current.pos);
  5347                 }
  5348             }
  5349           else if (display_ellipsis_p)
  5350             {
  5351               it->ellipsis_p = true;
  5352               /* Let the ellipsis display before
  5353                  considering any properties of the following char.
  5354                  Fixes jasonr@gnu.org 01 Oct 07 bug.  */
  5355               handled = HANDLED_RETURN;
  5356             }
  5357         }
  5358     }
  5359 
  5360   return handled;
  5361 }
  5362 
  5363 
  5364 /* Make iterator IT return `...' next.
  5365    Replaces LEN characters from buffer.  */
  5366 
  5367 static void
  5368 setup_for_ellipsis (struct it *it, int len)
  5369 {
  5370   /* Use the display table definition for `...'.  Invalid glyphs
  5371      will be handled by the method returning elements from dpvec.  */
  5372   if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
  5373     {
  5374       struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
  5375       it->dpvec = v->contents;
  5376       it->dpend = v->contents + v->header.size;
  5377     }
  5378   else
  5379     {
  5380       /* Default `...'.  */
  5381       it->dpvec = default_invis_vector;
  5382       it->dpend = default_invis_vector + 3;
  5383     }
  5384 
  5385   it->dpvec_char_len = len;
  5386   it->current.dpvec_index = 0;
  5387   it->dpvec_face_id = -1;
  5388 
  5389   /* Use IT->saved_face_id for the ellipsis, so that it has the same
  5390      face as the preceding text.  IT->saved_face_id was set in
  5391      handle_stop to the face of the preceding character, and will be
  5392      different from IT->face_id only if the invisible text skipped in
  5393      handle_invisible_prop has some non-default face on its first
  5394      character.  We thus ignore the face of the invisible text when we
  5395      display the ellipsis.  IT's face is restored in set_iterator_to_next.  */
  5396   if (it->saved_face_id >= 0)
  5397     it->face_id = it->saved_face_id;
  5398 
  5399   /* If the ellipsis represents buffer text, it means we advanced in
  5400      the buffer, so we should no longer ignore overlay strings.  */
  5401   if (it->method == GET_FROM_BUFFER)
  5402     it->ignore_overlay_strings_at_pos_p = false;
  5403 
  5404   it->method = GET_FROM_DISPLAY_VECTOR;
  5405   it->ellipsis_p = true;
  5406 }
  5407 
  5408 
  5409 static Lisp_Object
  5410 find_display_property (Lisp_Object disp, Lisp_Object prop)
  5411 {
  5412   if (NILP (disp))
  5413     return Qnil;
  5414   /* We have a vector of display specs.  */
  5415   if (VECTORP (disp))
  5416     {
  5417       for (ptrdiff_t i = 0; i < ASIZE (disp); i++)
  5418         {
  5419           Lisp_Object elem = AREF (disp, i);
  5420           if (CONSP (elem)
  5421               && CONSP (XCDR (elem))
  5422               && EQ (XCAR (elem), prop))
  5423             return XCAR (XCDR (elem));
  5424         }
  5425       return Qnil;
  5426     }
  5427   /* We have a list of display specs.  */
  5428   else if (CONSP (disp)
  5429            && CONSP (XCAR (disp)))
  5430     {
  5431       while (!NILP (disp))
  5432         {
  5433           Lisp_Object elem = XCAR (disp);
  5434           if (CONSP (elem)
  5435               && CONSP (XCDR (elem))
  5436               && EQ (XCAR (elem), prop))
  5437             return XCAR (XCDR (elem));
  5438 
  5439           /* Check that we have a proper list before going to the next
  5440              element.  */
  5441           if (CONSP (XCDR (disp)))
  5442             disp = XCDR (disp);
  5443           else
  5444             disp = Qnil;
  5445         }
  5446       return Qnil;
  5447     }
  5448   /* A simple display spec.  */
  5449   else if (CONSP (disp)
  5450            && CONSP (XCDR (disp))
  5451            && EQ (XCAR (disp), prop))
  5452     return XCAR (XCDR (disp));
  5453   else
  5454     return Qnil;
  5455 }
  5456 
  5457 static Lisp_Object
  5458 get_display_property (ptrdiff_t bufpos, Lisp_Object prop, Lisp_Object object)
  5459 {
  5460   return find_display_property (Fget_text_property (make_fixnum (bufpos),
  5461                                                     Qdisplay, object),
  5462                                 prop);
  5463 }
  5464 
  5465 static void
  5466 display_min_width (struct it *it, ptrdiff_t bufpos,
  5467                    Lisp_Object object, Lisp_Object width_spec)
  5468 {
  5469   /* We're being called at the end of the `min-width' sequence,
  5470      probably. */
  5471   if (!NILP (it->min_width_property)
  5472       && !EQ (width_spec, it->min_width_property))
  5473     {
  5474       if (!it->glyph_row)
  5475         return;
  5476 
  5477       /* When called from display_string (i.e., the mode line),
  5478          we're being called with a string as the object, and we
  5479          may be called with many sub-strings belonging to the same
  5480          :propertize run. */
  5481       if ((bufpos == 0
  5482            && !EQ (it->min_width_property,
  5483                    get_display_property (0, Qmin_width, object)))
  5484           /* In a buffer -- check that we're really right after the
  5485              sequence of characters covered by this `min-width'.  */
  5486           || (bufpos > BEGV
  5487               && EQ (it->min_width_property,
  5488                      get_display_property (bufpos - 1, Qmin_width, object))))
  5489         {
  5490           Lisp_Object w = Qnil;
  5491           double width;
  5492 #ifdef HAVE_WINDOW_SYSTEM
  5493           if (FRAME_WINDOW_P (it->f))
  5494             {
  5495               struct font *font = NULL;
  5496               struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5497               font = face->font ? face->font : FRAME_FONT (it->f);
  5498               calc_pixel_width_or_height (&width, it,
  5499                                           XCAR (it->min_width_property),
  5500                                           font, true, NULL);
  5501               width -= it->current_x - it->min_width_start;
  5502               w = list1 (make_int (width));
  5503             }
  5504           else
  5505 #endif
  5506             {
  5507               calc_pixel_width_or_height (&width, it,
  5508                                           XCAR (it->min_width_property),
  5509                                           NULL, true, NULL);
  5510               width -= (it->current_x - it->min_width_start) /
  5511                 FRAME_COLUMN_WIDTH (it->f);
  5512               w = make_int (width);
  5513             }
  5514 
  5515           /* Insert the stretch glyph.  */
  5516           it->object = list3 (Qspace, QCwidth, w);
  5517           produce_stretch_glyph (it);
  5518           if (it->area == TEXT_AREA)
  5519             it->current_x += it->pixel_width;
  5520           it->min_width_property = Qnil;
  5521         }
  5522     }
  5523 
  5524   /* We're at the start of a `min-width' sequence -- record the
  5525      position and the property, so that we can later see if we're at
  5526      the end.  */
  5527   if (CONSP (width_spec))
  5528     {
  5529       if (bufpos == BEGV
  5530           /* Mode line (see above).  */
  5531           || (bufpos == 0
  5532               && !EQ (it->min_width_property,
  5533                       get_display_property (0, Qmin_width, object)))
  5534           /* Buffer.  */
  5535           || (bufpos > BEGV
  5536               && !EQ (width_spec,
  5537                       get_display_property (bufpos - 1, Qmin_width, object))))
  5538         {
  5539           it->min_width_property = width_spec;
  5540           it->min_width_start = it->current_x;
  5541         }
  5542     }
  5543 }
  5544 
  5545 DEFUN ("get-display-property", Fget_display_property,
  5546        Sget_display_property, 2, 4, 0,
  5547        doc: /* Get the value of the `display' property PROP at POSITION.
  5548 If OBJECT, this should be a buffer or string where the property is
  5549 fetched from.  If omitted, OBJECT defaults to the current buffer.
  5550 
  5551 If PROPERTIES, look for value of PROP in PROPERTIES instead of the
  5552 properties at POSITION.  */)
  5553   (Lisp_Object position, Lisp_Object prop, Lisp_Object object,
  5554    Lisp_Object properties)
  5555 {
  5556   if (NILP (properties))
  5557     properties = Fget_text_property (position, Qdisplay, object);
  5558   else
  5559     CHECK_LIST (properties);
  5560 
  5561   return find_display_property (properties, prop);
  5562 }
  5563 
  5564 
  5565 
  5566 /***********************************************************************
  5567                             'display' property
  5568  ***********************************************************************/
  5569 
  5570 /* Set up iterator IT from `display' property at its current position.
  5571    Called from handle_stop.
  5572    We return HANDLED_RETURN if some part of the display property
  5573    overrides the display of the buffer text itself.
  5574    Otherwise we return HANDLED_NORMALLY.  */
  5575 
  5576 static enum prop_handled
  5577 handle_display_prop (struct it *it)
  5578 {
  5579   Lisp_Object propval, object, overlay;
  5580   struct text_pos *position;
  5581   ptrdiff_t bufpos;
  5582   /* Nonzero if some property replaces the display of the text itself.  */
  5583   int display_replaced = 0;
  5584 
  5585   if (STRINGP (it->string))
  5586     {
  5587       object = it->string;
  5588       position = &it->current.string_pos;
  5589       bufpos = CHARPOS (it->current.pos);
  5590     }
  5591   else
  5592     {
  5593       XSETWINDOW (object, it->w);
  5594       position = &it->current.pos;
  5595       bufpos = CHARPOS (*position);
  5596     }
  5597 
  5598   /* Reset those iterator values set from display property values.  */
  5599   it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
  5600   it->space_width = Qnil;
  5601   it->font_height = Qnil;
  5602   it->voffset = 0;
  5603 
  5604   /* We don't support recursive `display' properties, i.e. string
  5605      values that have a string `display' property, that have a string
  5606      `display' property etc.  */
  5607   if (!it->string_from_display_prop_p)
  5608     it->area = TEXT_AREA;
  5609 
  5610   propval = get_char_property_and_overlay (make_fixnum (position->charpos),
  5611                                            Qdisplay, object, &overlay);
  5612 
  5613   /* Rest of the code must have OBJECT be either a string or a buffer.  */
  5614   if (!STRINGP (it->string))
  5615     object = it->w->contents;
  5616 
  5617   /* Handle min-width ends. */
  5618   if (!NILP (it->min_width_property)
  5619       && NILP (find_display_property (propval, Qmin_width)))
  5620     display_min_width (it, bufpos, object, Qnil);
  5621 
  5622   if (NILP (propval))
  5623     return HANDLED_NORMALLY;
  5624   /* Now OVERLAY is the overlay that gave us this property, or nil
  5625      if it was a text property.  */
  5626 
  5627   display_replaced = handle_display_spec (it, propval, object, overlay,
  5628                                           position, bufpos,
  5629                                           FRAME_WINDOW_P (it->f));
  5630   return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
  5631 }
  5632 
  5633 /* Subroutine of handle_display_prop.  Returns non-zero if the display
  5634    specification in SPEC is a replacing specification, i.e. it would
  5635    replace the text covered by `display' property with something else,
  5636    such as an image or a display string.  If SPEC includes any kind or
  5637    `(space ...) specification, the value is 2; this is used by
  5638    compute_display_string_pos, which see.
  5639 
  5640    See handle_single_display_spec for documentation of arguments.
  5641    FRAME_WINDOW_P is true if the window being redisplayed is on a
  5642    GUI frame; this argument is used only if IT is NULL, see below.
  5643 
  5644    IT can be NULL, if this is called by the bidi reordering code
  5645    through compute_display_string_pos, which see.  In that case, this
  5646    function only examines SPEC, but does not otherwise "handle" it, in
  5647    the sense that it doesn't set up members of IT from the display
  5648    spec.  */
  5649 static int
  5650 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
  5651                      Lisp_Object overlay, struct text_pos *position,
  5652                      ptrdiff_t bufpos, bool frame_window_p)
  5653 {
  5654   int replacing = 0;
  5655   bool enable_eval = true;
  5656 
  5657   /* Support (disable-eval PROP) which is used by enriched.el.  */
  5658   if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
  5659     {
  5660       enable_eval = false;
  5661       spec = CONSP (XCDR (spec)) ? XCAR (XCDR (spec)) : Qnil;
  5662     }
  5663 
  5664   if (CONSP (spec)
  5665       /* Simple specifications.  */
  5666       && !EQ (XCAR (spec), Qimage)
  5667 #ifdef HAVE_XWIDGETS
  5668       && !EQ (XCAR (spec), Qxwidget)
  5669 #endif
  5670       && !EQ (XCAR (spec), Qspace)
  5671       && !EQ (XCAR (spec), Qwhen)
  5672       && !EQ (XCAR (spec), Qslice)
  5673       && !EQ (XCAR (spec), Qspace_width)
  5674       && !EQ (XCAR (spec), Qheight)
  5675       && !EQ (XCAR (spec), Qraise)
  5676       /* Marginal area specifications.  */
  5677       && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
  5678       && !EQ (XCAR (spec), Qleft_fringe)
  5679       && !EQ (XCAR (spec), Qright_fringe)
  5680       && !EQ (XCAR (spec), Qmin_width)
  5681       && !NILP (XCAR (spec)))
  5682     {
  5683       for (; CONSP (spec); spec = XCDR (spec))
  5684         {
  5685           int rv = handle_single_display_spec (it, XCAR (spec), object,
  5686                                                overlay, position, bufpos,
  5687                                                replacing, frame_window_p,
  5688                                                enable_eval);
  5689           if (rv != 0)
  5690             {
  5691               replacing = rv;
  5692               /* If some text in a string is replaced, `position' no
  5693                  longer points to the position of `object'.  */
  5694               if (!it || STRINGP (object))
  5695                 break;
  5696             }
  5697         }
  5698     }
  5699   else if (VECTORP (spec))
  5700     {
  5701       ptrdiff_t i;
  5702       for (i = 0; i < ASIZE (spec); ++i)
  5703         {
  5704           int rv = handle_single_display_spec (it, AREF (spec, i), object,
  5705                                                overlay, position, bufpos,
  5706                                                replacing, frame_window_p,
  5707                                                enable_eval);
  5708           if (rv != 0)
  5709             {
  5710               replacing = rv;
  5711               /* If some text in a string is replaced, `position' no
  5712                  longer points to the position of `object'.  */
  5713               if (!it || STRINGP (object))
  5714                 break;
  5715             }
  5716         }
  5717     }
  5718   else
  5719     replacing = handle_single_display_spec (it, spec, object, overlay, position,
  5720                                             bufpos, 0, frame_window_p,
  5721                                             enable_eval);
  5722   return replacing;
  5723 }
  5724 
  5725 /* Value is the position of the end of the `display' property starting
  5726    at START_POS in OBJECT.  */
  5727 
  5728 static struct text_pos
  5729 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
  5730 {
  5731   Lisp_Object end;
  5732   struct text_pos end_pos;
  5733 
  5734   end = Fnext_single_char_property_change (make_fixnum (CHARPOS (start_pos)),
  5735                                            Qdisplay, object, Qnil);
  5736   CHARPOS (end_pos) = XFIXNAT (end);
  5737   if (STRINGP (object))
  5738     compute_string_pos (&end_pos, start_pos, it->string);
  5739   else
  5740     BYTEPOS (end_pos) = CHAR_TO_BYTE (XFIXNAT (end));
  5741 
  5742   return end_pos;
  5743 }
  5744 
  5745 
  5746 /* Set up IT from a single `display' property specification SPEC.  OBJECT
  5747    is the object in which the `display' property was found.  *POSITION
  5748    is the position in OBJECT at which the `display' property was found.
  5749    BUFPOS is the buffer position of OBJECT (different from POSITION if
  5750    OBJECT is not a buffer).  DISPLAY_REPLACED non-zero means that we
  5751    previously saw a display specification which already replaced text
  5752    display with something else, for example an image; we ignore such
  5753    properties after the first one has been processed.
  5754 
  5755    OVERLAY is the overlay this `display' property came from,
  5756    or nil if it was a text property.
  5757 
  5758    If SPEC is a `space' or `image' specification, and in some other
  5759    cases too, set *POSITION to the position where the `display'
  5760    property ends.
  5761 
  5762    If IT is NULL, only examine the property specification in SPEC, but
  5763    don't set up IT.  In that case, FRAME_WINDOW_P means SPEC
  5764    is intended to be displayed in a window on a GUI frame.
  5765 
  5766    Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
  5767 
  5768    Value is non-zero if something was found which replaces the display
  5769    of buffer or string text.  */
  5770 
  5771 static int
  5772 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
  5773                             Lisp_Object overlay, struct text_pos *position,
  5774                             ptrdiff_t bufpos, int display_replaced,
  5775                             bool frame_window_p, bool enable_eval_p)
  5776 {
  5777   Lisp_Object form;
  5778   Lisp_Object location, value;
  5779   struct text_pos start_pos = *position;
  5780   void *itdata = NULL;
  5781 
  5782   /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
  5783      If the result is non-nil, use VALUE instead of SPEC.  */
  5784   form = Qt;
  5785   if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
  5786     {
  5787       spec = XCDR (spec);
  5788       if (!CONSP (spec))
  5789         return 0;
  5790       form = XCAR (spec);
  5791       spec = XCDR (spec);
  5792     }
  5793 
  5794   if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
  5795     form = Qnil;
  5796   if (!NILP (form) && !EQ (form, Qt))
  5797     {
  5798       specpdl_ref count = SPECPDL_INDEX ();
  5799 
  5800       /* Bind `object' to the object having the `display' property, a
  5801          buffer or string.  Bind `position' to the position in the
  5802          object where the property was found, and `buffer-position'
  5803          to the current position in the buffer.  */
  5804 
  5805       if (NILP (object))
  5806         XSETBUFFER (object, current_buffer);
  5807       specbind (Qobject, object);
  5808       specbind (Qposition, make_fixnum (CHARPOS (*position)));
  5809       specbind (Qbuffer_position, make_fixnum (bufpos));
  5810       /* Save and restore the bidi cache, since FORM could be crazy
  5811          enough to re-enter redisplay, e.g., by calling 'message'.  */
  5812       itdata = bidi_shelve_cache ();
  5813       form = safe_eval (form);
  5814       bidi_unshelve_cache (itdata, false);
  5815       form = unbind_to (count, form);
  5816     }
  5817 
  5818   if (NILP (form))
  5819     return 0;
  5820 
  5821   /* Handle `(height HEIGHT)' specifications.  */
  5822   if (CONSP (spec)
  5823       && EQ (XCAR (spec), Qheight)
  5824       && CONSP (XCDR (spec)))
  5825     {
  5826       if (it)
  5827         {
  5828           if (!FRAME_WINDOW_P (it->f))
  5829             return 0;
  5830 
  5831           it->font_height = XCAR (XCDR (spec));
  5832           if (!NILP (it->font_height))
  5833             {
  5834               int new_height = -1;
  5835 
  5836               if (CONSP (it->font_height)
  5837                   && (EQ (XCAR (it->font_height), Qplus)
  5838                       || EQ (XCAR (it->font_height), Qminus))
  5839                   && CONSP (XCDR (it->font_height))
  5840                   && RANGED_FIXNUMP (0, XCAR (XCDR (it->font_height)), INT_MAX))
  5841                 {
  5842                   /* `(+ N)' or `(- N)' where N is an integer.  */
  5843                   int steps = XFIXNUM (XCAR (XCDR (it->font_height)));
  5844                   if (EQ (XCAR (it->font_height), Qplus))
  5845                     steps = - steps;
  5846                   it->face_id = smaller_face (it->f, it->face_id, steps);
  5847                 }
  5848               else if (FUNCTIONP (it->font_height) && enable_eval_p)
  5849                 {
  5850                   /* Call function with current height as argument.
  5851                      Value is the new height.  */
  5852                   struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5853                   Lisp_Object height;
  5854                   itdata = bidi_shelve_cache ();
  5855                   height = safe_call1 (it->font_height,
  5856                                        face->lface[LFACE_HEIGHT_INDEX]);
  5857                   bidi_unshelve_cache (itdata, false);
  5858                   if (NUMBERP (height))
  5859                     new_height = XFLOATINT (height);
  5860                 }
  5861               else if (NUMBERP (it->font_height))
  5862                 {
  5863                   /* Value is a multiple of the canonical char height.  */
  5864                   struct face *f;
  5865 
  5866                   f = FACE_FROM_ID (it->f,
  5867                                     lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID));
  5868                   new_height = (XFLOATINT (it->font_height)
  5869                                 * XFIXNUM (f->lface[LFACE_HEIGHT_INDEX]));
  5870                 }
  5871               else if (enable_eval_p)
  5872                 {
  5873                   /* Evaluate IT->font_height with `height' bound to the
  5874                      current specified height to get the new height.  */
  5875                   specpdl_ref count = SPECPDL_INDEX ();
  5876                   struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5877 
  5878                   specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
  5879                   itdata = bidi_shelve_cache ();
  5880                   value = safe_eval (it->font_height);
  5881                   bidi_unshelve_cache (itdata, false);
  5882                   value = unbind_to (count, value);
  5883 
  5884                   if (NUMBERP (value))
  5885                     new_height = XFLOATINT (value);
  5886                 }
  5887 
  5888               if (new_height > 0)
  5889                 it->face_id = face_with_height (it->f, it->face_id, new_height);
  5890             }
  5891         }
  5892 
  5893       return 0;
  5894     }
  5895 
  5896   /* Handle `(space-width WIDTH)'.  */
  5897   if (CONSP (spec)
  5898       && EQ (XCAR (spec), Qspace_width)
  5899       && CONSP (XCDR (spec)))
  5900     {
  5901       if (it)
  5902         {
  5903           if (!FRAME_WINDOW_P (it->f))
  5904             return 0;
  5905 
  5906           value = XCAR (XCDR (spec));
  5907           if (NUMBERP (value) && XFLOATINT (value) > 0)
  5908             it->space_width = value;
  5909         }
  5910 
  5911       return 0;
  5912     }
  5913 
  5914   /* Handle `(min-width (WIDTH))'.  */
  5915   if (CONSP (spec)
  5916       && EQ (XCAR (spec), Qmin_width)
  5917       && CONSP (XCDR (spec))
  5918       && CONSP (XCAR (XCDR (spec))))
  5919     {
  5920       if (it)
  5921         display_min_width (it, bufpos, object, XCAR (XCDR (spec)));
  5922       return 0;
  5923     }
  5924 
  5925   /* Handle `(slice X Y WIDTH HEIGHT)'.  */
  5926   if (CONSP (spec)
  5927       && EQ (XCAR (spec), Qslice))
  5928     {
  5929       Lisp_Object tem;
  5930 
  5931       if (it)
  5932         {
  5933           if (!FRAME_WINDOW_P (it->f))
  5934             return 0;
  5935 
  5936           if (tem = XCDR (spec), CONSP (tem))
  5937             {
  5938               it->slice.x = XCAR (tem);
  5939               if (tem = XCDR (tem), CONSP (tem))
  5940                 {
  5941                   it->slice.y = XCAR (tem);
  5942                   if (tem = XCDR (tem), CONSP (tem))
  5943                     {
  5944                       it->slice.width = XCAR (tem);
  5945                       if (tem = XCDR (tem), CONSP (tem))
  5946                         it->slice.height = XCAR (tem);
  5947                     }
  5948                 }
  5949             }
  5950         }
  5951 
  5952       return 0;
  5953     }
  5954 
  5955   /* Handle `(raise FACTOR)'.  */
  5956   if (CONSP (spec)
  5957       && EQ (XCAR (spec), Qraise)
  5958       && CONSP (XCDR (spec)))
  5959     {
  5960       if (it)
  5961         {
  5962           if (!FRAME_WINDOW_P (it->f))
  5963             return 0;
  5964 
  5965 #ifdef HAVE_WINDOW_SYSTEM
  5966           value = XCAR (XCDR (spec));
  5967           if (NUMBERP (value))
  5968             {
  5969               struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5970               it->voffset = - (XFLOATINT (value)
  5971                                * (normal_char_height (face->font, -1)));
  5972             }
  5973 #endif /* HAVE_WINDOW_SYSTEM */
  5974         }
  5975 
  5976       return 0;
  5977     }
  5978 
  5979   /* Don't handle the other kinds of display specifications
  5980      inside a string that we got from a `display' property.  */
  5981   if (it && it->string_from_display_prop_p)
  5982     return 0;
  5983 
  5984   /* Characters having this form of property are not displayed, so
  5985      we have to find the end of the property.  */
  5986   if (it)
  5987     {
  5988       start_pos = *position;
  5989       *position = display_prop_end (it, object, start_pos);
  5990       /* If the display property comes from an overlay, don't consider
  5991          any potential stop_charpos values before the end of that
  5992          overlay.  Since display_prop_end will happily find another
  5993          'display' property coming from some other overlay or text
  5994          property on buffer positions before this overlay's end, we
  5995          need to ignore them, or else we risk displaying this
  5996          overlay's display string/image twice.  */
  5997       if (!NILP (overlay))
  5998         {
  5999           ptrdiff_t ovendpos = OVERLAY_END (overlay);
  6000 
  6001           /* Some borderline-sane Lisp might call us with the current
  6002              buffer narrowed so that overlay-end is outside the
  6003              POINT_MIN..POINT_MAX region, which will then cause
  6004              various assertion violations and crashes down the road,
  6005              starting with pop_it when it will attempt to use POSITION
  6006              set below.  Prevent that.  */
  6007           ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
  6008 
  6009           if (ovendpos > CHARPOS (*position))
  6010             SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
  6011         }
  6012     }
  6013   value = Qnil;
  6014 
  6015   /* Stop the scan at that end position--we assume that all
  6016      text properties change there.  */
  6017   if (it)
  6018     it->stop_charpos = position->charpos;
  6019 
  6020   /* Handle `(left-fringe BITMAP [FACE])'
  6021      and `(right-fringe BITMAP [FACE])'.  */
  6022   if (CONSP (spec)
  6023       && (EQ (XCAR (spec), Qleft_fringe)
  6024           || EQ (XCAR (spec), Qright_fringe))
  6025       && CONSP (XCDR (spec)))
  6026     {
  6027       if (it)
  6028         {
  6029           if (!FRAME_WINDOW_P (it->f))
  6030             /* If we return here, POSITION has been advanced
  6031                across the text with this property.  */
  6032             {
  6033               /* Synchronize the bidi iterator with POSITION.  This is
  6034                  needed because we are not going to push the iterator
  6035                  on behalf of this display property, so there will be
  6036                  no pop_it call to do this synchronization for us.  */
  6037               if (it->bidi_p)
  6038                 {
  6039                   it->position = *position;
  6040                   iterate_out_of_display_property (it);
  6041                   *position = it->position;
  6042                 }
  6043               return 1;
  6044             }
  6045         }
  6046       else if (!frame_window_p)
  6047         return 1;
  6048 
  6049 #ifdef HAVE_WINDOW_SYSTEM
  6050       value = XCAR (XCDR (spec));
  6051       int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
  6052       if (! fringe_bitmap)
  6053         /* If we return here, POSITION has been advanced
  6054            across the text with this property.  */
  6055         {
  6056           if (it && it->bidi_p)
  6057             {
  6058               it->position = *position;
  6059               iterate_out_of_display_property (it);
  6060               *position = it->position;
  6061             }
  6062           return 1;
  6063         }
  6064 
  6065       if (it)
  6066         {
  6067           int face_id = lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
  6068 
  6069           if (CONSP (XCDR (XCDR (spec))))
  6070             {
  6071               Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
  6072               int face_id2;
  6073               /* Don't allow quitting from lookup_derived_face, for when
  6074                  we are displaying a non-selected window, and the buffer's
  6075                  point was temporarily moved to the window-point.  */
  6076               specpdl_ref count1 = SPECPDL_INDEX ();
  6077               specbind (Qinhibit_quit, Qt);
  6078               face_id2 = lookup_derived_face (it->w, it->f, face_name,
  6079                                               FRINGE_FACE_ID, false);
  6080               unbind_to (count1, Qnil);
  6081               if (face_id2 >= 0)
  6082                 face_id = face_id2;
  6083             }
  6084 
  6085           /* Save current settings of IT so that we can restore them
  6086              when we are finished with the glyph property value.  */
  6087           push_it (it, position);
  6088 
  6089           it->area = TEXT_AREA;
  6090           it->what = IT_IMAGE;
  6091           it->image_id = -1; /* no image */
  6092           it->position = start_pos;
  6093           it->object = NILP (object) ? it->w->contents : object;
  6094           it->method = GET_FROM_IMAGE;
  6095           it->from_overlay = Qnil;
  6096           it->face_id = face_id;
  6097           it->from_disp_prop_p = true;
  6098 
  6099           /* Say that we haven't consumed the characters with
  6100              `display' property yet.  The call to pop_it in
  6101              set_iterator_to_next will clean this up.  */
  6102           *position = start_pos;
  6103 
  6104           if (EQ (XCAR (spec), Qleft_fringe))
  6105             {
  6106               it->left_user_fringe_bitmap = fringe_bitmap;
  6107               it->left_user_fringe_face_id = face_id;
  6108             }
  6109           else
  6110             {
  6111               it->right_user_fringe_bitmap = fringe_bitmap;
  6112               it->right_user_fringe_face_id = face_id;
  6113             }
  6114         }
  6115 #endif /* HAVE_WINDOW_SYSTEM */
  6116       return 1;
  6117     }
  6118 
  6119   /* Prepare to handle `((margin left-margin) ...)',
  6120      `((margin right-margin) ...)' and `((margin nil) ...)'
  6121      prefixes for display specifications.  */
  6122   location = Qunbound;
  6123   if (CONSP (spec) && CONSP (XCAR (spec)))
  6124     {
  6125       Lisp_Object tem;
  6126 
  6127       value = XCDR (spec);
  6128       if (CONSP (value))
  6129         value = XCAR (value);
  6130 
  6131       tem = XCAR (spec);
  6132       if (EQ (XCAR (tem), Qmargin)
  6133           && (tem = XCDR (tem),
  6134               tem = CONSP (tem) ? XCAR (tem) : Qnil,
  6135               (NILP (tem)
  6136                || EQ (tem, Qleft_margin)
  6137                || EQ (tem, Qright_margin))))
  6138         location = tem;
  6139     }
  6140 
  6141   if (BASE_EQ (location, Qunbound))
  6142     {
  6143       location = Qnil;
  6144       value = spec;
  6145     }
  6146 
  6147   /* After this point, VALUE is the property after any
  6148      margin prefix has been stripped.  It must be a string,
  6149      an image specification, or `(space ...)'.
  6150 
  6151      LOCATION specifies where to display: `left-margin',
  6152      `right-margin' or nil.  */
  6153 
  6154   bool valid_p = (STRINGP (value)
  6155 #ifdef HAVE_WINDOW_SYSTEM
  6156                   || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
  6157                       && valid_image_p (value))
  6158 #endif /* not HAVE_WINDOW_SYSTEM */
  6159              || (CONSP (value) && EQ (XCAR (value), Qspace))
  6160              || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
  6161                  && valid_xwidget_spec_p (value)));
  6162 
  6163   if (valid_p && display_replaced == 0)
  6164     {
  6165       int retval = 1;
  6166 
  6167       if (!it)
  6168         {
  6169           /* Callers need to know whether the display spec is any kind
  6170              of `(space ...)' spec that is about to affect text-area
  6171              display.  */
  6172           if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
  6173             retval = 2;
  6174           return retval;
  6175         }
  6176 
  6177       /* Save current settings of IT so that we can restore them
  6178          when we are finished with the glyph property value.  */
  6179       push_it (it, position);
  6180       it->from_overlay = overlay;
  6181       it->from_disp_prop_p = true;
  6182 
  6183       if (NILP (location))
  6184         it->area = TEXT_AREA;
  6185       else if (EQ (location, Qleft_margin))
  6186         it->area = LEFT_MARGIN_AREA;
  6187       else
  6188         it->area = RIGHT_MARGIN_AREA;
  6189 
  6190       if (STRINGP (value))
  6191         {
  6192           it->string = value;
  6193           it->multibyte_p = STRING_MULTIBYTE (it->string);
  6194           it->current.overlay_string_index = -1;
  6195           IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
  6196           it->end_charpos = it->string_nchars = SCHARS (it->string);
  6197           it->method = GET_FROM_STRING;
  6198           it->stop_charpos = 0;
  6199           it->prev_stop = 0;
  6200           it->base_level_stop = 0;
  6201           it->string_from_display_prop_p = true;
  6202           it->cmp_it.id = -1;
  6203           /* Say that we haven't consumed the characters with
  6204              `display' property yet.  The call to pop_it in
  6205              set_iterator_to_next will clean this up.  */
  6206           if (BUFFERP (object))
  6207             *position = start_pos;
  6208 
  6209           /* Force paragraph direction to be that of the parent
  6210              object.  If the parent object's paragraph direction is
  6211              not yet determined, default to L2R.  */
  6212           if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  6213             it->paragraph_embedding = it->bidi_it.paragraph_dir;
  6214           else
  6215             it->paragraph_embedding = L2R;
  6216 
  6217           /* Set up the bidi iterator for this display string.  */
  6218           if (it->bidi_p)
  6219             {
  6220               it->bidi_it.string.lstring = it->string;
  6221               it->bidi_it.string.s = NULL;
  6222               it->bidi_it.string.schars = it->end_charpos;
  6223               it->bidi_it.string.bufpos = bufpos;
  6224               it->bidi_it.string.from_disp_str = true;
  6225               it->bidi_it.string.unibyte = !it->multibyte_p;
  6226               it->bidi_it.w = it->w;
  6227               bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6228             }
  6229         }
  6230       else if (CONSP (value) && EQ (XCAR (value), Qspace))
  6231         {
  6232           it->method = GET_FROM_STRETCH;
  6233           it->object = value;
  6234           *position = it->position = start_pos;
  6235           retval = 1 + (it->area == TEXT_AREA);
  6236         }
  6237       else if (valid_xwidget_spec_p (value))
  6238         {
  6239           it->what = IT_XWIDGET;
  6240           it->method = GET_FROM_XWIDGET;
  6241           it->position = start_pos;
  6242           it->object = NILP (object) ? it->w->contents : object;
  6243           *position = start_pos;
  6244           it->xwidget = lookup_xwidget (value);
  6245         }
  6246 #ifdef HAVE_WINDOW_SYSTEM
  6247       else
  6248         {
  6249           specpdl_ref count = SPECPDL_INDEX ();
  6250 
  6251           it->what = IT_IMAGE;
  6252           /* Don't allow quitting from lookup_image, for when we are
  6253              displaying a non-selected window, and the buffer's point
  6254              was temporarily moved to the window-point.  */
  6255           specbind (Qinhibit_quit, Qt);
  6256           it->image_id = lookup_image (it->f, value, it->face_id);
  6257           unbind_to (count, Qnil);
  6258           it->position = start_pos;
  6259           it->object = NILP (object) ? it->w->contents : object;
  6260           it->method = GET_FROM_IMAGE;
  6261 
  6262           /* Say that we haven't consumed the characters with
  6263              `display' property yet.  The call to pop_it in
  6264              set_iterator_to_next will clean this up.  */
  6265           *position = start_pos;
  6266         }
  6267 #endif /* HAVE_WINDOW_SYSTEM */
  6268 
  6269       return retval;
  6270     }
  6271 
  6272   /* Invalid property or property not supported.  Restore
  6273      POSITION to what it was before.  */
  6274   *position = start_pos;
  6275   return 0;
  6276 }
  6277 
  6278 /* Check if PROP is a display property value whose text should be
  6279    treated as intangible.  OVERLAY is the overlay from which PROP
  6280    came, or nil if it came from a text property.  CHARPOS and BYTEPOS
  6281    specify the buffer position covered by PROP.  */
  6282 
  6283 bool
  6284 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
  6285                            ptrdiff_t charpos, ptrdiff_t bytepos)
  6286 {
  6287   bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
  6288   struct text_pos position;
  6289 
  6290   SET_TEXT_POS (position, charpos, bytepos);
  6291   return (handle_display_spec (NULL, prop, Qnil, overlay,
  6292                                &position, charpos, frame_window_p)
  6293           != 0);
  6294 }
  6295 
  6296 
  6297 /* Return true if PROP is a display sub-property value containing STRING.
  6298 
  6299    Implementation note: this and the following function are really
  6300    special cases of handle_display_spec and
  6301    handle_single_display_spec, and should ideally use the same code.
  6302    Until they do, these two pairs must be consistent and must be
  6303    modified in sync.  */
  6304 
  6305 static bool
  6306 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
  6307 {
  6308   if (EQ (string, prop))
  6309     return true;
  6310 
  6311   /* Skip over `when FORM'.  */
  6312   if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
  6313     {
  6314       prop = XCDR (prop);
  6315       if (!CONSP (prop))
  6316         return false;
  6317       /* Actually, the condition following `when' should be eval'ed,
  6318          like handle_single_display_spec does, and we should return
  6319          false if it evaluates to nil.  However, this function is
  6320          called only when the buffer was already displayed and some
  6321          glyph in the glyph matrix was found to come from a display
  6322          string.  Therefore, the condition was already evaluated, and
  6323          the result was non-nil, otherwise the display string wouldn't
  6324          have been displayed and we would have never been called for
  6325          this property.  Thus, we can skip the evaluation and assume
  6326          its result is non-nil.  */
  6327       prop = XCDR (prop);
  6328     }
  6329 
  6330   if (CONSP (prop))
  6331     /* Skip over `margin LOCATION'.  */
  6332     if (EQ (XCAR (prop), Qmargin))
  6333       {
  6334         prop = XCDR (prop);
  6335         if (!CONSP (prop))
  6336           return false;
  6337 
  6338         prop = XCDR (prop);
  6339         if (!CONSP (prop))
  6340           return false;
  6341       }
  6342 
  6343   return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
  6344 }
  6345 
  6346 
  6347 /* Return true if STRING appears in the `display' property PROP.  */
  6348 
  6349 static bool
  6350 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
  6351 {
  6352   if (CONSP (prop)
  6353       && !EQ (XCAR (prop), Qwhen)
  6354       && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
  6355     {
  6356       /* A list of sub-properties.  */
  6357       while (CONSP (prop))
  6358         {
  6359           if (single_display_spec_string_p (XCAR (prop), string))
  6360             return true;
  6361           prop = XCDR (prop);
  6362         }
  6363     }
  6364   else if (VECTORP (prop))
  6365     {
  6366       /* A vector of sub-properties.  */
  6367       ptrdiff_t i;
  6368       for (i = 0; i < ASIZE (prop); ++i)
  6369         if (single_display_spec_string_p (AREF (prop, i), string))
  6370           return true;
  6371     }
  6372   else
  6373     return single_display_spec_string_p (prop, string);
  6374 
  6375   return false;
  6376 }
  6377 
  6378 /* Look for STRING in overlays and text properties in the current
  6379    buffer, between character positions FROM and TO (excluding TO).
  6380    BACK_P means look back (in this case, TO is supposed to be
  6381    less than FROM).
  6382    Value is the first character position where STRING was found, or
  6383    zero if it wasn't found before hitting TO.
  6384 
  6385    This function may only use code that doesn't eval because it is
  6386    called asynchronously from note_mouse_highlight.  */
  6387 
  6388 static ptrdiff_t
  6389 string_buffer_position_lim (Lisp_Object string,
  6390                             ptrdiff_t from, ptrdiff_t to, bool back_p)
  6391 {
  6392   Lisp_Object limit, prop, pos;
  6393   bool found = false;
  6394 
  6395   pos = make_fixnum (max (from, BEGV));
  6396 
  6397   if (!back_p)  /* looking forward */
  6398     {
  6399       limit = make_fixnum (min (to, ZV));
  6400       while (!found && !EQ (pos, limit))
  6401         {
  6402           prop = Fget_char_property (pos, Qdisplay, Qnil);
  6403           if (!NILP (prop) && display_prop_string_p (prop, string))
  6404             found = true;
  6405           else
  6406             pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
  6407                                                      limit);
  6408         }
  6409     }
  6410   else          /* looking back */
  6411     {
  6412       limit = make_fixnum (max (to, BEGV));
  6413       while (!found && !EQ (pos, limit))
  6414         {
  6415           prop = Fget_char_property (pos, Qdisplay, Qnil);
  6416           if (!NILP (prop) && display_prop_string_p (prop, string))
  6417             found = true;
  6418           else
  6419             pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
  6420                                                          limit);
  6421         }
  6422     }
  6423 
  6424   return found ? XFIXNUM (pos) : 0;
  6425 }
  6426 
  6427 /* Determine which buffer position in current buffer STRING comes from.
  6428    AROUND_CHARPOS is an approximate position where it could come from.
  6429    Value is the buffer position or 0 if it couldn't be determined.
  6430 
  6431    This function is necessary because we don't record buffer positions
  6432    in glyphs generated from strings (to keep struct glyph small).
  6433    This function may only use code that doesn't eval because it is
  6434    called asynchronously from note_mouse_highlight.  */
  6435 
  6436 static ptrdiff_t
  6437 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
  6438 {
  6439   const int MAX_DISTANCE = 1000;
  6440   ptrdiff_t forward_limit = min (around_charpos + MAX_DISTANCE, ZV);
  6441   ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
  6442                                                 forward_limit, false);
  6443 
  6444   if (!found)
  6445     {
  6446       ptrdiff_t backward_limit = max (around_charpos - MAX_DISTANCE, BEGV);
  6447       found = string_buffer_position_lim (string, around_charpos,
  6448                                           backward_limit, true);
  6449     }
  6450   return found;
  6451 }
  6452 
  6453 
  6454 
  6455 /***********************************************************************
  6456                         `composition' property
  6457  ***********************************************************************/
  6458 
  6459 /* Set up iterator IT from `composition' property at its current
  6460    position.  Called from handle_stop.  */
  6461 
  6462 static enum prop_handled
  6463 handle_composition_prop (struct it *it)
  6464 {
  6465   Lisp_Object prop, string;
  6466   ptrdiff_t pos, pos_byte, start, end;
  6467 
  6468   if (STRINGP (it->string))
  6469     {
  6470       unsigned char *s;
  6471 
  6472       pos = IT_STRING_CHARPOS (*it);
  6473       pos_byte = IT_STRING_BYTEPOS (*it);
  6474       string = it->string;
  6475       s = SDATA (string) + pos_byte;
  6476       if (STRING_MULTIBYTE (string))
  6477         it->c = STRING_CHAR (s);
  6478       else
  6479         it->c = *s;
  6480     }
  6481   else
  6482     {
  6483       pos = IT_CHARPOS (*it);
  6484       pos_byte = IT_BYTEPOS (*it);
  6485       string = Qnil;
  6486       it->c = FETCH_CHAR (pos_byte);
  6487     }
  6488 
  6489   /* If there's a valid composition and point is not inside of the
  6490      composition (in the case that the composition is from the current
  6491      buffer), draw a glyph composed from the composition components.  */
  6492   if (find_composition (pos, -1, &start, &end, &prop, string)
  6493       && composition_valid_p (start, end, prop)
  6494       && (STRINGP (it->string) || (PT <= start || PT >= end)))
  6495     {
  6496       if (start < pos)
  6497         /* As we can't handle this situation (perhaps font-lock added
  6498            a new composition), we just return here hoping that next
  6499            redisplay will detect this composition much earlier.  */
  6500         return HANDLED_NORMALLY;
  6501       if (start != pos)
  6502         {
  6503           if (STRINGP (it->string))
  6504             pos_byte = string_char_to_byte (it->string, start);
  6505           else
  6506             pos_byte = CHAR_TO_BYTE (start);
  6507         }
  6508       it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
  6509                                                prop, string);
  6510 
  6511       if (it->cmp_it.id >= 0)
  6512         {
  6513           it->cmp_it.ch = -1;
  6514           it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
  6515           it->cmp_it.nglyphs = -1;
  6516         }
  6517     }
  6518 
  6519   return HANDLED_NORMALLY;
  6520 }
  6521 
  6522 
  6523 
  6524 /***********************************************************************
  6525                            Overlay strings
  6526  ***********************************************************************/
  6527 
  6528 /* The following structure is used to record overlay strings for
  6529    later sorting in load_overlay_strings.  */
  6530 
  6531 struct overlay_entry
  6532 {
  6533   Lisp_Object overlay;
  6534   Lisp_Object string;
  6535   EMACS_INT priority;
  6536   bool after_string_p;
  6537 };
  6538 
  6539 
  6540 /* Set up iterator IT from overlay strings at its current position.
  6541    Called from handle_stop.  */
  6542 
  6543 static enum prop_handled
  6544 handle_overlay_change (struct it *it)
  6545 {
  6546   if (!STRINGP (it->string) && get_overlay_strings (it, 0))
  6547     return HANDLED_RECOMPUTE_PROPS;
  6548   else
  6549     return HANDLED_NORMALLY;
  6550 }
  6551 
  6552 
  6553 /* Set up the next overlay string for delivery by IT, if there is an
  6554    overlay string to deliver.  Called by set_iterator_to_next when the
  6555    end of the current overlay string is reached.  If there are more
  6556    overlay strings to display, IT->string and
  6557    IT->current.overlay_string_index are set appropriately here.
  6558    Otherwise IT->string is set to nil.  */
  6559 
  6560 static void
  6561 next_overlay_string (struct it *it)
  6562 {
  6563   ++it->current.overlay_string_index;
  6564   if (it->current.overlay_string_index == it->n_overlay_strings)
  6565     {
  6566       /* No more overlay strings.  Restore IT's settings to what
  6567          they were before overlay strings were processed, and
  6568          continue to deliver from current_buffer.  */
  6569 
  6570       it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
  6571       pop_it (it);
  6572       eassert (it->sp > 0
  6573                || (NILP (it->string)
  6574                    && it->method == GET_FROM_BUFFER
  6575                    && it->stop_charpos >= BEGV
  6576                    && it->stop_charpos <= it->end_charpos));
  6577       it->current.overlay_string_index = -1;
  6578       it->n_overlay_strings = 0;
  6579       /* If there's an empty display string on the stack, pop the
  6580          stack, to resync the bidi iterator with IT's position.  Such
  6581          empty strings are pushed onto the stack in
  6582          get_overlay_strings_1.  */
  6583       if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
  6584         pop_it (it);
  6585 
  6586       /* Since we've exhausted overlay strings at this buffer
  6587          position, set the flag to ignore overlays until we move to
  6588          another position.  (The flag will be reset in
  6589          next_element_from_buffer.)  But don't do that if the overlay
  6590          strings were loaded at position other than the current one,
  6591          which could happen if we called pop_it above, or if the
  6592          overlay strings were loaded by handle_invisible_prop at the
  6593          beginning of invisible text.  */
  6594       if (it->overlay_strings_charpos == IT_CHARPOS (*it))
  6595         it->ignore_overlay_strings_at_pos_p = true;
  6596 
  6597       /* If we're at the end of the buffer, record that we have
  6598          processed the overlay strings there already, so that
  6599          next_element_from_buffer doesn't try it again.  */
  6600       if (NILP (it->string)
  6601           && IT_CHARPOS (*it) >= it->end_charpos
  6602           && it->overlay_strings_charpos >= it->end_charpos)
  6603         it->overlay_strings_at_end_processed_p = true;
  6604       /* Note: we reset overlay_strings_charpos only here, to make
  6605          sure the just-processed overlays were indeed at EOB.
  6606          Otherwise, overlays on text with invisible text property,
  6607          which are processed with IT's position past the invisible
  6608          text, might fool us into thinking the overlays at EOB were
  6609          already processed (linum-mode can cause this, for
  6610          example).  */
  6611       it->overlay_strings_charpos = -1;
  6612     }
  6613   else
  6614     {
  6615       /* There are more overlay strings to process.  If
  6616          IT->current.overlay_string_index has advanced to a position
  6617          where we must load IT->overlay_strings with more strings, do
  6618          it.  We must load at the IT->overlay_strings_charpos where
  6619          IT->n_overlay_strings was originally computed; when invisible
  6620          text is present, this might not be IT_CHARPOS (Bug#7016).  */
  6621       int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
  6622 
  6623       if (it->current.overlay_string_index && i == 0)
  6624         load_overlay_strings (it, it->overlay_strings_charpos);
  6625 
  6626       /* Initialize IT to deliver display elements from the overlay
  6627          string.  */
  6628       it->string = it->overlay_strings[i];
  6629       it->multibyte_p = STRING_MULTIBYTE (it->string);
  6630       SET_TEXT_POS (it->current.string_pos, 0, 0);
  6631       it->method = GET_FROM_STRING;
  6632       it->stop_charpos = 0;
  6633       it->end_charpos = SCHARS (it->string);
  6634       if (it->cmp_it.stop_pos >= 0)
  6635         it->cmp_it.stop_pos = 0;
  6636       it->prev_stop = 0;
  6637       it->base_level_stop = 0;
  6638 
  6639       /* Set up the bidi iterator for this overlay string.  */
  6640       if (it->bidi_p)
  6641         {
  6642           it->bidi_it.string.lstring = it->string;
  6643           it->bidi_it.string.s = NULL;
  6644           it->bidi_it.string.schars = SCHARS (it->string);
  6645           it->bidi_it.string.bufpos = it->overlay_strings_charpos;
  6646           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  6647           it->bidi_it.string.unibyte = !it->multibyte_p;
  6648           it->bidi_it.w = it->w;
  6649           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6650         }
  6651     }
  6652 
  6653   CHECK_IT (it);
  6654 }
  6655 
  6656 
  6657 /* Compare two overlay_entry structures E1 and E2.  Used as a
  6658    comparison function for qsort in load_overlay_strings.  Overlay
  6659    strings for the same position are sorted so that
  6660 
  6661    1. All after-strings come in front of before-strings, except
  6662    when they come from the same overlay.
  6663 
  6664    2. Within after-strings, strings are sorted so that overlay strings
  6665    from overlays with higher priorities come first.
  6666 
  6667    2. Within before-strings, strings are sorted so that overlay
  6668    strings from overlays with higher priorities come last.
  6669 
  6670    Value is analogous to strcmp.  */
  6671 
  6672 
  6673 static int
  6674 compare_overlay_entries (const void *e1, const void *e2)
  6675 {
  6676   struct overlay_entry const *entry1 = e1;
  6677   struct overlay_entry const *entry2 = e2;
  6678   int result;
  6679 
  6680   if (entry1->after_string_p != entry2->after_string_p)
  6681     {
  6682       /* Let after-strings appear in front of before-strings if
  6683          they come from different overlays.  */
  6684       if (EQ (entry1->overlay, entry2->overlay))
  6685         result = entry1->after_string_p ? 1 : -1;
  6686       else
  6687         result = entry1->after_string_p ? -1 : 1;
  6688     }
  6689   else if (entry1->priority != entry2->priority)
  6690     {
  6691       if (entry1->after_string_p)
  6692         /* After-strings sorted in order of decreasing priority.  */
  6693         result = entry2->priority < entry1->priority ? -1 : 1;
  6694       else
  6695         /* Before-strings sorted in order of increasing priority.  */
  6696         result = entry1->priority < entry2->priority ? -1 : 1;
  6697     }
  6698   else
  6699     result = 0;
  6700 
  6701   return result;
  6702 }
  6703 
  6704 
  6705 /* Load the vector IT->overlay_strings with overlay strings from IT's
  6706    current buffer position, or from CHARPOS if that is > 0.  Set
  6707    IT->n_overlays to the total number of overlay strings found.
  6708 
  6709    Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
  6710    a time.  On entry into load_overlay_strings,
  6711    IT->current.overlay_string_index gives the number of overlay
  6712    strings that have already been loaded by previous calls to this
  6713    function.
  6714 
  6715    IT->add_overlay_start contains an additional overlay start
  6716    position to consider for taking overlay strings from, if non-zero.
  6717    This position comes into play when the overlay has an `invisible'
  6718    property, and both before and after-strings.  When we've skipped to
  6719    the end of the overlay, because of its `invisible' property, we
  6720    nevertheless want its before-string to appear.
  6721    IT->add_overlay_start will contain the overlay start position
  6722    in this case.
  6723 
  6724    Overlay strings are sorted so that after-string strings come in
  6725    front of before-string strings.  Within before and after-strings,
  6726    strings are sorted by overlay priority.  See also function
  6727    compare_overlay_entries.  */
  6728 
  6729 static void
  6730 load_overlay_strings (struct it *it, ptrdiff_t charpos)
  6731 {
  6732   ptrdiff_t n = 0;
  6733   struct overlay_entry entriesbuf[20];
  6734   ptrdiff_t size = ARRAYELTS (entriesbuf);
  6735   struct overlay_entry *entries = entriesbuf;
  6736   struct itree_node *node;
  6737 
  6738   USE_SAFE_ALLOCA;
  6739 
  6740   if (charpos <= 0)
  6741     charpos = IT_CHARPOS (*it);
  6742 
  6743   /* Append the overlay string STRING of overlay OVERLAY to vector
  6744      `entries' which has size `size' and currently contains `n'
  6745      elements.  AFTER_P means STRING is an after-string of
  6746      OVERLAY.  */
  6747 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P)                 \
  6748   do                                                                    \
  6749     {                                                                   \
  6750       Lisp_Object priority;                                             \
  6751                                                                         \
  6752       if (n == size)                                                    \
  6753         {                                                               \
  6754           struct overlay_entry *old = entries;                          \
  6755           SAFE_NALLOCA (entries, 2, size);                              \
  6756           memcpy (entries, old, size * sizeof *entries);                \
  6757           size *= 2;                                                    \
  6758         }                                                               \
  6759                                                                         \
  6760       entries[n].string = (STRING);                                     \
  6761       entries[n].overlay = (OVERLAY);                                   \
  6762       priority = Foverlay_get ((OVERLAY), Qpriority);                   \
  6763       entries[n].priority = FIXNUMP (priority) ? XFIXNUM (priority) : 0;  \
  6764       entries[n].after_string_p = (AFTER_P);                            \
  6765       ++n;                                                              \
  6766     }                                                                   \
  6767   while (false)
  6768 
  6769 
  6770   /* Process overlays.  */
  6771   ITREE_FOREACH (node, current_buffer->overlays, charpos - 1, charpos + 1, DESCENDING)
  6772     {
  6773       Lisp_Object overlay = node->data;
  6774       eassert (OVERLAYP (overlay));
  6775       ptrdiff_t start = node->begin;
  6776       ptrdiff_t end = node->end;
  6777 
  6778       /* Skip this overlay if it doesn't start or end at IT's current
  6779          position.  */
  6780       if (end != charpos && start != charpos)
  6781         continue;
  6782 
  6783       /* Skip this overlay if it doesn't apply to IT->w.  */
  6784       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  6785       if (WINDOWP (window) && XWINDOW (window) != it->w)
  6786         continue;
  6787 
  6788       /* If the text ``under'' the overlay is invisible, both before-
  6789          and after-strings from this overlay are visible; start and
  6790          end position are indistinguishable.  */
  6791       Lisp_Object invisible = Foverlay_get (overlay, Qinvisible);
  6792       int invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
  6793 
  6794       /* If overlay has a non-empty before-string, record it.  */
  6795       Lisp_Object str;
  6796       if ((start == charpos || (end == charpos && invis != 0))
  6797           && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
  6798           && SCHARS (str))
  6799         RECORD_OVERLAY_STRING (overlay, str, false);
  6800 
  6801       /* If overlay has a non-empty after-string, record it.  */
  6802       if ((end == charpos || (start == charpos && invis != 0))
  6803           && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
  6804           && SCHARS (str))
  6805         RECORD_OVERLAY_STRING (overlay, str, true);
  6806     }
  6807 
  6808 #undef RECORD_OVERLAY_STRING
  6809 
  6810   /* Sort entries.  */
  6811   if (n > 1)
  6812     qsort (entries, n, sizeof *entries, compare_overlay_entries);
  6813 
  6814   /* Record number of overlay strings, and where we computed it.  */
  6815   it->n_overlay_strings = n;
  6816   it->overlay_strings_charpos = charpos;
  6817 
  6818   /* IT->current.overlay_string_index is the number of overlay strings
  6819      that have already been consumed by IT.  Copy some of the
  6820      remaining overlay strings to IT->overlay_strings.  */
  6821   ptrdiff_t j = it->current.overlay_string_index;
  6822   for (ptrdiff_t i = 0; i < OVERLAY_STRING_CHUNK_SIZE && j < n; i++, j++)
  6823     {
  6824       it->overlay_strings[i] = entries[j].string;
  6825       it->string_overlays[i] = entries[j].overlay;
  6826     }
  6827 
  6828   CHECK_IT (it);
  6829   SAFE_FREE ();
  6830 }
  6831 
  6832 
  6833 /* Get the first chunk of overlay strings at IT's current buffer
  6834    position, or at CHARPOS if that is > 0.  Value is true if at
  6835    least one overlay string was found.  */
  6836 
  6837 static bool
  6838 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
  6839 {
  6840   /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
  6841      process.  This fills IT->overlay_strings with strings, and sets
  6842      IT->n_overlay_strings to the total number of strings to process.
  6843      IT->pos.overlay_string_index has to be set temporarily to zero
  6844      because load_overlay_strings needs this; it must be set to -1
  6845      when no overlay strings are found because a zero value would
  6846      indicate a position in the first overlay string.  */
  6847   it->current.overlay_string_index = 0;
  6848   load_overlay_strings (it, charpos);
  6849 
  6850   /* If we found overlay strings, set up IT to deliver display
  6851      elements from the first one.  Otherwise set up IT to deliver
  6852      from current_buffer.  */
  6853   if (it->n_overlay_strings)
  6854     {
  6855       /* Make sure we know settings in current_buffer, so that we can
  6856          restore meaningful values when we're done with the overlay
  6857          strings.  */
  6858       if (compute_stop_p)
  6859         compute_stop_pos (it);
  6860       eassert (it->face_id >= 0);
  6861 
  6862       /* Save IT's settings.  They are restored after all overlay
  6863          strings have been processed.  */
  6864       eassert (!compute_stop_p || it->sp == 0);
  6865 
  6866       /* When called from handle_stop, there might be an empty display
  6867          string loaded.  In that case, don't bother saving it.  But
  6868          don't use this optimization with the bidi iterator, since we
  6869          need the corresponding pop_it call to resync the bidi
  6870          iterator's position with IT's position, after we are done
  6871          with the overlay strings.  (The corresponding call to pop_it
  6872          in case of an empty display string is in
  6873          next_overlay_string.)  */
  6874       if (!(!it->bidi_p
  6875             && STRINGP (it->string) && !SCHARS (it->string)))
  6876         push_it (it, NULL);
  6877 
  6878       /* Set up IT to deliver display elements from the first overlay
  6879          string.  */
  6880       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
  6881       it->string = it->overlay_strings[0];
  6882       it->from_overlay = Qnil;
  6883       it->stop_charpos = 0;
  6884       eassert (STRINGP (it->string));
  6885       it->end_charpos = SCHARS (it->string);
  6886       it->prev_stop = 0;
  6887       it->base_level_stop = 0;
  6888       it->multibyte_p = STRING_MULTIBYTE (it->string);
  6889       it->method = GET_FROM_STRING;
  6890       it->from_disp_prop_p = 0;
  6891       it->cmp_it.id = -1;
  6892 
  6893       /* Force paragraph direction to be that of the parent
  6894          buffer.  */
  6895       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  6896         it->paragraph_embedding = it->bidi_it.paragraph_dir;
  6897       else
  6898         it->paragraph_embedding = L2R;
  6899 
  6900       /* Set up the bidi iterator for this overlay string.  */
  6901       if (it->bidi_p)
  6902         {
  6903           ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
  6904 
  6905           it->bidi_it.string.lstring = it->string;
  6906           it->bidi_it.string.s = NULL;
  6907           it->bidi_it.string.schars = SCHARS (it->string);
  6908           it->bidi_it.string.bufpos = pos;
  6909           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  6910           it->bidi_it.string.unibyte = !it->multibyte_p;
  6911           it->bidi_it.w = it->w;
  6912           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6913         }
  6914       return true;
  6915     }
  6916 
  6917   it->current.overlay_string_index = -1;
  6918   return false;
  6919 }
  6920 
  6921 static bool
  6922 get_overlay_strings (struct it *it, ptrdiff_t charpos)
  6923 {
  6924   it->string = Qnil;
  6925   it->method = GET_FROM_BUFFER;
  6926 
  6927   get_overlay_strings_1 (it, charpos, true);
  6928 
  6929   CHECK_IT (it);
  6930 
  6931   /* Value is true if we found at least one overlay string.  */
  6932   return STRINGP (it->string);
  6933 }
  6934 
  6935 
  6936 
  6937 /***********************************************************************
  6938                       Saving and restoring state
  6939  ***********************************************************************/
  6940 
  6941 /* Save current settings of IT on IT->stack.  Called, for example,
  6942    before setting up IT for an overlay string, to be able to restore
  6943    IT's settings to what they were after the overlay string has been
  6944    processed.  If POSITION is non-NULL, it is the position to save on
  6945    the stack instead of IT->position.  */
  6946 
  6947 static void
  6948 push_it (struct it *it, struct text_pos *position)
  6949 {
  6950   struct iterator_stack_entry *p;
  6951 
  6952   eassert (it->sp < IT_STACK_SIZE);
  6953   p = it->stack + it->sp;
  6954 
  6955   p->stop_charpos = it->stop_charpos;
  6956   p->prev_stop = it->prev_stop;
  6957   p->base_level_stop = it->base_level_stop;
  6958   p->cmp_it = it->cmp_it;
  6959   eassert (it->face_id >= 0);
  6960   p->face_id = it->face_id;
  6961   p->string = it->string;
  6962   p->method = it->method;
  6963   p->from_overlay = it->from_overlay;
  6964   switch (p->method)
  6965     {
  6966     case GET_FROM_IMAGE:
  6967       p->u.image.object = it->object;
  6968       p->u.image.image_id = it->image_id;
  6969       p->u.image.slice = it->slice;
  6970       break;
  6971     case GET_FROM_STRETCH:
  6972       p->u.stretch.object = it->object;
  6973       break;
  6974     case GET_FROM_XWIDGET:
  6975       p->u.xwidget.object = it->object;
  6976       break;
  6977     case GET_FROM_BUFFER:
  6978     case GET_FROM_DISPLAY_VECTOR:
  6979     case GET_FROM_STRING:
  6980     case GET_FROM_C_STRING:
  6981       break;
  6982     default:
  6983       emacs_abort ();
  6984     }
  6985   p->position = position ? *position : it->position;
  6986   p->current = it->current;
  6987   p->end_charpos = it->end_charpos;
  6988   p->string_nchars = it->string_nchars;
  6989   p->area = it->area;
  6990   p->multibyte_p = it->multibyte_p;
  6991   p->avoid_cursor_p = it->avoid_cursor_p;
  6992   p->space_width = it->space_width;
  6993   p->font_height = it->font_height;
  6994   p->voffset = it->voffset;
  6995   p->string_from_display_prop_p = it->string_from_display_prop_p;
  6996   p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
  6997   p->display_ellipsis_p = false;
  6998   p->line_wrap = it->line_wrap;
  6999   p->bidi_p = it->bidi_p;
  7000   p->paragraph_embedding = it->paragraph_embedding;
  7001   p->from_disp_prop_p = it->from_disp_prop_p;
  7002   ++it->sp;
  7003 
  7004   /* Save the state of the bidi iterator as well. */
  7005   if (it->bidi_p)
  7006     bidi_push_it (&it->bidi_it);
  7007 }
  7008 
  7009 static void
  7010 iterate_out_of_display_property (struct it *it)
  7011 {
  7012   bool buffer_p = !STRINGP (it->string);
  7013   ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
  7014   ptrdiff_t bob = (buffer_p ? BEGV : 0);
  7015 
  7016   eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
  7017 
  7018   /* Maybe initialize paragraph direction.  If we are at the beginning
  7019      of a new paragraph, next_element_from_buffer may not have a
  7020      chance to do that.  */
  7021   if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
  7022     bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  7023   /* prev_stop can be zero, so check against BEGV as well.  */
  7024   while (it->bidi_it.charpos >= bob
  7025          && it->prev_stop <= it->bidi_it.charpos
  7026          && it->bidi_it.charpos < CHARPOS (it->position)
  7027          && it->bidi_it.charpos < eob)
  7028     bidi_move_to_visually_next (&it->bidi_it);
  7029   /* Record the stop_pos we just crossed, for when we cross it
  7030      back, maybe.  */
  7031   if (it->bidi_it.charpos > CHARPOS (it->position))
  7032     it->prev_stop = CHARPOS (it->position);
  7033   /* If we ended up not where pop_it put us, resync IT's
  7034      positional members with the bidi iterator. */
  7035   if (it->bidi_it.charpos != CHARPOS (it->position))
  7036     SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
  7037   if (buffer_p)
  7038     it->current.pos = it->position;
  7039   else
  7040     it->current.string_pos = it->position;
  7041 }
  7042 
  7043 /* Restore the IT->face_box_p flag, since it could have been
  7044    overwritten by the face of the object that we just finished
  7045    displaying.  Also, set the IT->start_of_box_run_p flag if the
  7046    change in faces requires that.  */
  7047 static void
  7048 restore_face_box_flags (struct it *it, int prev_face_id)
  7049 {
  7050   struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  7051 
  7052   if (face)
  7053     {
  7054       struct face *prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  7055 
  7056       if (!(it->start_of_box_run_p && prev_face && prev_face->box))
  7057         it->start_of_box_run_p = (face->box != FACE_NO_BOX
  7058                                   && (prev_face == NULL
  7059                                       || prev_face->box == FACE_NO_BOX));
  7060       it->face_box_p = face->box != FACE_NO_BOX;
  7061     }
  7062 }
  7063 
  7064 /* Restore IT's settings from IT->stack.  Called, for example, when no
  7065    more overlay strings must be processed, and we return to delivering
  7066    display elements from a buffer, or when the end of a string from a
  7067    `display' property is reached and we return to delivering display
  7068    elements from an overlay string, or from a buffer.  */
  7069 
  7070 static void
  7071 pop_it (struct it *it)
  7072 {
  7073   struct iterator_stack_entry *p;
  7074   bool from_display_prop = it->from_disp_prop_p;
  7075   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  7076   int prev_face_id = it->face_id;
  7077 
  7078   eassert (it->sp > 0);
  7079   --it->sp;
  7080   p = it->stack + it->sp;
  7081   it->stop_charpos = p->stop_charpos;
  7082   it->prev_stop = p->prev_stop;
  7083   it->base_level_stop = p->base_level_stop;
  7084   it->cmp_it = p->cmp_it;
  7085   it->face_id = p->face_id;
  7086   it->current = p->current;
  7087   it->position = p->position;
  7088   it->string = p->string;
  7089   it->from_overlay = p->from_overlay;
  7090   if (NILP (it->string))
  7091     SET_TEXT_POS (it->current.string_pos, -1, -1);
  7092   it->method = p->method;
  7093   switch (it->method)
  7094     {
  7095     case GET_FROM_IMAGE:
  7096       it->image_id = p->u.image.image_id;
  7097       it->object = p->u.image.object;
  7098       it->slice = p->u.image.slice;
  7099       break;
  7100     case GET_FROM_XWIDGET:
  7101       it->object = p->u.xwidget.object;
  7102       break;
  7103     case GET_FROM_STRETCH:
  7104       it->object = p->u.stretch.object;
  7105       break;
  7106     case GET_FROM_BUFFER:
  7107       {
  7108         restore_face_box_flags (it, prev_face_id);
  7109         it->object = it->w->contents;
  7110       }
  7111       break;
  7112     case GET_FROM_STRING:
  7113       {
  7114         restore_face_box_flags (it, prev_face_id);
  7115         it->object = it->string;
  7116       }
  7117       break;
  7118     case GET_FROM_DISPLAY_VECTOR:
  7119       if (it->s)
  7120         it->method = GET_FROM_C_STRING;
  7121       else if (STRINGP (it->string))
  7122         it->method = GET_FROM_STRING;
  7123       else
  7124         {
  7125           it->method = GET_FROM_BUFFER;
  7126           it->object = it->w->contents;
  7127         }
  7128       break;
  7129     case GET_FROM_C_STRING:
  7130       break;
  7131     default:
  7132       emacs_abort ();
  7133     }
  7134   it->end_charpos = p->end_charpos;
  7135   it->string_nchars = p->string_nchars;
  7136   it->area = p->area;
  7137   it->multibyte_p = p->multibyte_p;
  7138   it->avoid_cursor_p = p->avoid_cursor_p;
  7139   it->space_width = p->space_width;
  7140   it->font_height = p->font_height;
  7141   it->voffset = p->voffset;
  7142   it->string_from_display_prop_p = p->string_from_display_prop_p;
  7143   it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
  7144   it->line_wrap = p->line_wrap;
  7145   it->bidi_p = p->bidi_p;
  7146   it->paragraph_embedding = p->paragraph_embedding;
  7147   it->from_disp_prop_p = p->from_disp_prop_p;
  7148   if (it->bidi_p)
  7149     {
  7150       bidi_pop_it (&it->bidi_it);
  7151       /* Bidi-iterate until we get out of the portion of text, if any,
  7152          covered by a `display' text property or by an overlay with
  7153          `display' property.  (We cannot just jump there, because the
  7154          internal coherency of the bidi iterator state can not be
  7155          preserved across such jumps.)  We also must determine the
  7156          paragraph base direction if the overlay we just processed is
  7157          at the beginning of a new paragraph.  */
  7158       if (from_display_prop
  7159           && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
  7160         iterate_out_of_display_property (it);
  7161 
  7162       eassert ((BUFFERP (it->object)
  7163                 && IT_CHARPOS (*it) == it->bidi_it.charpos
  7164                 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
  7165                || (STRINGP (it->object)
  7166                    && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
  7167                    && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
  7168                || (CONSP (it->object) && it->method == GET_FROM_STRETCH)
  7169                /* We could be in the middle of handling a list or a
  7170                   vector of several 'display' properties, in which
  7171                   case we should only verify the above conditions when
  7172                   we pop the iterator stack the last time, because
  7173                   higher stack levels cannot "iterate out of the
  7174                   display property".  */
  7175                || it->sp > 0);
  7176     }
  7177   /* If we move the iterator over text covered by a display property
  7178      to a new buffer position, any info about previously seen overlays
  7179      is no longer valid.  */
  7180   if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
  7181     it->ignore_overlay_strings_at_pos_p = false;
  7182 }
  7183 
  7184 
  7185 
  7186 /***********************************************************************
  7187                           Moving over lines
  7188  ***********************************************************************/
  7189 
  7190 /* Set IT's current position to the previous line start.  */
  7191 
  7192 static void
  7193 back_to_previous_line_start (struct it *it)
  7194 {
  7195   ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
  7196 
  7197   dec_both (&cp, &bp);
  7198   SET_WITH_NARROWED_BEGV (it, IT_CHARPOS (*it),
  7199                           find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it)),
  7200                           get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
  7201 }
  7202 
  7203 /* Find in the current buffer the first display or overlay string
  7204    between STARTPOS and ENDPOS that includes embedded newlines.
  7205    Consider only overlays that apply to window W.
  7206    Value is non-zero if such a display/overlay string is found.  */
  7207 static bool
  7208 strings_with_newlines (ptrdiff_t startpos, ptrdiff_t endpos, struct window *w)
  7209 {
  7210   struct itree_node *node;
  7211   /* Process overlays.  */
  7212   ITREE_FOREACH (node, current_buffer->overlays, startpos, endpos, DESCENDING)
  7213     {
  7214       Lisp_Object overlay = node->data;
  7215       eassert (OVERLAYP (overlay));
  7216 
  7217       /* Skip this overlay if it doesn't apply to our window.  */
  7218       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  7219       if (WINDOWP (window) && XWINDOW (window) != w)
  7220         continue;
  7221 
  7222       ptrdiff_t ostart = node->begin;
  7223       ptrdiff_t oend = node->end;
  7224 
  7225       /* Skip overlays that don't overlap the range.  */
  7226       if (!((startpos < oend && ostart < endpos)
  7227             || (ostart == oend
  7228                 && (startpos == oend || (endpos == ZV && oend == endpos)))))
  7229         continue;
  7230 
  7231       Lisp_Object str;
  7232       str = Foverlay_get (overlay, Qbefore_string);
  7233       if (STRINGP (str) && SCHARS (str)
  7234           && memchr (SDATA (str), '\n', SBYTES (str)))
  7235         return true;
  7236       str = Foverlay_get (overlay, Qafter_string);
  7237       if (STRINGP (str) && SCHARS (str)
  7238           && memchr (SDATA (str), '\n', SBYTES (str)))
  7239         return true;
  7240     }
  7241 
  7242   /* Check for 'display' properties whose values include strings.  */
  7243   Lisp_Object cpos = make_fixnum (startpos);
  7244   Lisp_Object limpos = make_fixnum (endpos);
  7245 
  7246   while ((cpos = Fnext_single_property_change (cpos, Qdisplay, Qnil, limpos),
  7247           !(NILP (cpos) || XFIXNAT (cpos) >= endpos)))
  7248     {
  7249       Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
  7250       Lisp_Object string = string_from_display_spec (spec);
  7251       if (STRINGP (string)
  7252           && memchr (SDATA (string), '\n', SBYTES (string)))
  7253         return true;
  7254     }
  7255 
  7256   return false;
  7257 }
  7258 
  7259 
  7260 /* Move IT to the next line start.
  7261 
  7262    Value is true if a newline was found.  Set *SKIPPED_P to true if
  7263    we skipped over part of the text (as opposed to moving the iterator
  7264    continuously over the text).  Otherwise, don't change the value
  7265    of *SKIPPED_P.
  7266 
  7267    If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
  7268    iterator on the newline, if it was found.
  7269 
  7270    Newlines may come from buffer text, overlay strings, or strings
  7271    displayed via the `display' property.  That's the reason we can't
  7272    simply use find_newline_no_quit.
  7273 
  7274    Note that this function may not skip over invisible text that is so
  7275    because of text properties and immediately follows a newline.  If
  7276    it would, function reseat_at_next_visible_line_start, when called
  7277    from set_iterator_to_next, would effectively make invisible
  7278    characters following a newline part of the wrong glyph row, which
  7279    leads to wrong cursor motion.  */
  7280 
  7281 static bool
  7282 forward_to_next_line_start (struct it *it, bool *skipped_p,
  7283                             struct bidi_it *bidi_it_prev)
  7284 {
  7285   ptrdiff_t old_selective;
  7286   bool newline_found_p = false;
  7287   int n;
  7288   const int MAX_NEWLINE_DISTANCE = 500;
  7289 
  7290   /* If already on a newline, just consume it to avoid unintended
  7291      skipping over invisible text below.  */
  7292   if (it->what == IT_CHARACTER
  7293       && it->c == '\n'
  7294       && CHARPOS (it->position) == IT_CHARPOS (*it))
  7295     {
  7296       if (it->bidi_p && bidi_it_prev)
  7297         *bidi_it_prev = it->bidi_it;
  7298       set_iterator_to_next (it, false);
  7299       it->c = 0;
  7300       return true;
  7301     }
  7302 
  7303   /* Don't handle selective display in the following.  It's (a)
  7304      unnecessary because it's done by the caller, and (b) leads to an
  7305      infinite recursion because next_element_from_ellipsis indirectly
  7306      calls this function.  */
  7307   old_selective = it->selective;
  7308   it->selective = 0;
  7309 
  7310   /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
  7311      from buffer text, or till the end of the string if iterating a
  7312      string.  */
  7313   for (n = 0;
  7314        !newline_found_p && n < MAX_NEWLINE_DISTANCE;
  7315        n += !STRINGP (it->string))
  7316     {
  7317       if (!get_next_display_element (it))
  7318         return false;
  7319       newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
  7320       if (newline_found_p && it->bidi_p && bidi_it_prev)
  7321         *bidi_it_prev = it->bidi_it;
  7322       set_iterator_to_next (it, false);
  7323     }
  7324 
  7325   /* If we didn't find a newline near enough, see if we can use a
  7326      short-cut.  */
  7327   if (!newline_found_p)
  7328     {
  7329       ptrdiff_t bytepos, start = IT_CHARPOS (*it);
  7330       ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
  7331                                               1, &bytepos);
  7332       eassert (!STRINGP (it->string));
  7333 
  7334       /* it->stop_charpos >= limit means we already know there's no
  7335          stop position up until the newline at LIMIT, so there's no
  7336          need for any further checks.  */
  7337       bool no_strings_with_newlines = it->stop_charpos >= limit;
  7338 
  7339       if (!no_strings_with_newlines)
  7340         {
  7341           if (!(current_buffer->long_line_optimizations_p
  7342                 && it->line_wrap == TRUNCATE))
  7343             {
  7344               /* Quick-and-dirty check: if there isn't any `display'
  7345                  property in sight, and no overlays, we're done.  */
  7346               Lisp_Object pos =
  7347                 Fnext_single_property_change (make_fixnum (start),
  7348                                               Qdisplay, Qnil,
  7349                                               make_fixnum (limit));
  7350               no_strings_with_newlines =
  7351                 (NILP (pos) || XFIXNAT (pos) == limit) /* no 'display' props */
  7352                 && next_overlay_change (start) == ZV;  /* no overlays */
  7353             }
  7354           else
  7355             {
  7356               /* For buffers with very long and truncated lines we try
  7357                  harder, because it's worth our while to spend some
  7358                  time looking into the overlays and 'display' properties
  7359                  if we can then avoid iterating through all of them.  */
  7360               no_strings_with_newlines =
  7361                 !strings_with_newlines (start, limit, it->w);
  7362             }
  7363         }
  7364 
  7365       /* If there's no display or overlay strings with embedded
  7366          newlines until the position of the newline in buffer text, we
  7367          can just use that position.  */
  7368       if (no_strings_with_newlines)
  7369         {
  7370           if (!it->bidi_p || !bidi_it_prev)
  7371             {
  7372               /* The optimal case: just jump there.  */
  7373               IT_CHARPOS (*it) = limit;
  7374               IT_BYTEPOS (*it) = bytepos;
  7375             }
  7376           else
  7377             {
  7378               /* The less optimal case: need to bidi-walk there, but
  7379                  this is still cheaper that the full iteration using
  7380                  get_next_display_element and set_iterator_to_next.  */
  7381               struct bidi_it bprev;
  7382 
  7383               /* Help bidi.c avoid expensive searches for display
  7384                  properties and overlays, by telling it that there are
  7385                  none up to `limit'.  */
  7386               if (it->bidi_it.disp_pos < limit)
  7387                 {
  7388                   it->bidi_it.disp_pos = limit;
  7389                   it->bidi_it.disp_prop = 0;
  7390                 }
  7391               do {
  7392                 bprev = it->bidi_it;
  7393                 bidi_move_to_visually_next (&it->bidi_it);
  7394               } while (it->bidi_it.charpos != limit);
  7395               IT_CHARPOS (*it) = limit;
  7396               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  7397               if (bidi_it_prev)
  7398                 *bidi_it_prev = bprev;
  7399             }
  7400           *skipped_p = newline_found_p = true;
  7401         }
  7402       else
  7403         {
  7404           /* The slow case.  */
  7405           while (!newline_found_p)
  7406             {
  7407               if (!get_next_display_element (it))
  7408                 break;
  7409               newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
  7410               if (newline_found_p && it->bidi_p && bidi_it_prev)
  7411                 *bidi_it_prev = it->bidi_it;
  7412               set_iterator_to_next (it, false);
  7413             }
  7414         }
  7415     }
  7416 
  7417   it->selective = old_selective;
  7418   return newline_found_p;
  7419 }
  7420 
  7421 
  7422 /* Set IT's current position to the previous visible line start.  Skip
  7423    invisible text that is so either due to text properties or due to
  7424    selective display.  Caution: this does not change IT->current_x and
  7425    IT->hpos.  */
  7426 
  7427 static void
  7428 back_to_previous_visible_line_start (struct it *it)
  7429 {
  7430   while (IT_CHARPOS (*it) > BEGV)
  7431     {
  7432       back_to_previous_line_start (it);
  7433 
  7434       if (IT_CHARPOS (*it) <= BEGV)
  7435         break;
  7436 
  7437       /* If selective > 0, then lines indented more than its value are
  7438          invisible.  */
  7439       if (it->selective > 0
  7440           && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
  7441                                 it->selective))
  7442         continue;
  7443 
  7444       /* Check the newline before point for invisibility.  */
  7445       {
  7446         Lisp_Object prop;
  7447         prop = Fget_char_property (make_fixnum (IT_CHARPOS (*it) - 1),
  7448                                    Qinvisible, it->window);
  7449         if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
  7450           continue;
  7451       }
  7452 
  7453       if (IT_CHARPOS (*it) <= BEGV)
  7454         break;
  7455 
  7456       {
  7457         struct it it2;
  7458         void *it2data = NULL;
  7459         ptrdiff_t pos;
  7460         ptrdiff_t beg, end;
  7461         Lisp_Object val, overlay;
  7462 
  7463         SAVE_IT (it2, *it, it2data);
  7464 
  7465         /* If newline is part of a composition, continue from start of composition */
  7466         if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
  7467             && beg < IT_CHARPOS (*it))
  7468           goto replaced;
  7469 
  7470         /* If newline is replaced by a display property, find start of overlay
  7471            or interval and continue search from that point.  */
  7472         pos = --IT_CHARPOS (it2);
  7473         --IT_BYTEPOS (it2);
  7474         it2.sp = 0;
  7475         bidi_unshelve_cache (NULL, false);
  7476         it2.string_from_display_prop_p = false;
  7477         it2.from_disp_prop_p = false;
  7478         if (handle_display_prop (&it2) == HANDLED_RETURN
  7479             && !NILP (val = get_char_property_and_overlay
  7480                       (make_fixnum (pos), Qdisplay, Qnil, &overlay))
  7481             && (OVERLAYP (overlay)
  7482                 ? (beg = OVERLAY_START (overlay))
  7483                 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
  7484           {
  7485             RESTORE_IT (it, it, it2data);
  7486             goto replaced;
  7487           }
  7488 
  7489         /* Newline is not replaced by anything -- so we are done.  */
  7490         RESTORE_IT (it, it, it2data);
  7491         break;
  7492 
  7493       replaced:
  7494         if (beg < BEGV)
  7495           beg = BEGV;
  7496         IT_CHARPOS (*it) = beg;
  7497         IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
  7498       }
  7499     }
  7500 
  7501   it->continuation_lines_width = 0;
  7502 
  7503   eassert (IT_CHARPOS (*it) >= BEGV);
  7504   eassert (it->medium_narrowing_begv > 0 /* long-line optimizations: all bets off */
  7505            || IT_CHARPOS (*it) == BEGV
  7506            || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
  7507   CHECK_IT (it);
  7508 }
  7509 
  7510 
  7511 /* Reseat iterator IT at the previous visible line start.  Skip
  7512    invisible text that is so either due to text properties or due to
  7513    selective display.  At the end, update IT's overlay information,
  7514    face information etc.  */
  7515 
  7516 void
  7517 reseat_at_previous_visible_line_start (struct it *it)
  7518 {
  7519   back_to_previous_visible_line_start (it);
  7520   reseat (it, it->current.pos, true);
  7521   CHECK_IT (it);
  7522 }
  7523 
  7524 
  7525 /* Reseat iterator IT on the next visible line start in the current
  7526    buffer.  ON_NEWLINE_P means position IT on the newline
  7527    preceding the line start.  Skip over invisible text that is so
  7528    because of selective display.  Compute faces, overlays etc at the
  7529    new position.  Note that this function does not skip over text that
  7530    is invisible because of text properties.  */
  7531 
  7532 static void
  7533 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
  7534 {
  7535   bool skipped_p = false;
  7536   struct bidi_it bidi_it_prev;
  7537   bool newline_found_p
  7538     = forward_to_next_line_start (it, &skipped_p,
  7539                                   on_newline_p ? &bidi_it_prev : NULL);
  7540 
  7541   /* Skip over lines that are invisible because they are indented
  7542      more than the value of IT->selective.  */
  7543   if (it->selective > 0)
  7544     while (IT_CHARPOS (*it) < ZV
  7545            && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
  7546                                  it->selective))
  7547       {
  7548         eassert (IT_BYTEPOS (*it) == BEGV
  7549                  || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
  7550         newline_found_p =
  7551           forward_to_next_line_start (it, &skipped_p,
  7552                                       on_newline_p ? &bidi_it_prev : NULL);
  7553       }
  7554 
  7555   /* Position on the newline if that's what's requested.  */
  7556   if (on_newline_p && newline_found_p)
  7557     {
  7558       if (STRINGP (it->string))
  7559         {
  7560           if (IT_STRING_CHARPOS (*it) > 0)
  7561             {
  7562               if (!it->bidi_p)
  7563                 {
  7564                   --IT_STRING_CHARPOS (*it);
  7565                   --IT_STRING_BYTEPOS (*it);
  7566                 }
  7567               else
  7568                 {
  7569                   /* We need to restore the bidi iterator to the state
  7570                      it had on the newline, and resync the IT's
  7571                      position with that.  */
  7572                   it->bidi_it = bidi_it_prev;
  7573                   IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  7574                   IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  7575                 }
  7576             }
  7577         }
  7578       else if (IT_CHARPOS (*it) > BEGV)
  7579         {
  7580           if (!it->bidi_p)
  7581             {
  7582               --IT_CHARPOS (*it);
  7583               --IT_BYTEPOS (*it);
  7584             }
  7585           else
  7586             {
  7587               /* We need to restore the bidi iterator to the state it
  7588                  had on the newline and resync IT with that.  */
  7589               it->bidi_it = bidi_it_prev;
  7590               IT_CHARPOS (*it) = it->bidi_it.charpos;
  7591               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  7592             }
  7593           reseat (it, it->current.pos, false);
  7594         }
  7595     }
  7596   else if (skipped_p)
  7597     reseat (it, it->current.pos, false);
  7598 
  7599   CHECK_IT (it);
  7600 }
  7601 
  7602 
  7603 
  7604 /***********************************************************************
  7605                    Changing an iterator's position
  7606 ***********************************************************************/
  7607 
  7608 /* Change IT's current position to POS in current_buffer.
  7609    If FORCE_P, always check for text properties at the new position.
  7610    Otherwise, text properties are only looked up if POS >=
  7611    IT->check_charpos of a property.  */
  7612 
  7613 static void
  7614 reseat (struct it *it, struct text_pos pos, bool force_p)
  7615 {
  7616   ptrdiff_t original_pos = IT_CHARPOS (*it);
  7617 
  7618   reseat_1 (it, pos, false);
  7619 
  7620   if (current_buffer->long_line_optimizations_p)
  7621     {
  7622       if (!it->medium_narrowing_begv)
  7623         {
  7624           it->medium_narrowing_begv
  7625             = get_medium_narrowing_begv (it->w, window_point (it->w));
  7626           it->medium_narrowing_zv
  7627             = get_medium_narrowing_zv (it->w, window_point (it->w));
  7628           it->large_narrowing_begv
  7629             = get_large_narrowing_begv (window_point (it->w));
  7630           it->large_narrowing_zv
  7631             = get_large_narrowing_zv (window_point (it->w));
  7632         }
  7633       else if ((pos.charpos < it->medium_narrowing_begv
  7634                 || pos.charpos > it->medium_narrowing_zv)
  7635                 && (!redisplaying_p || it->line_wrap == TRUNCATE))
  7636         {
  7637           it->medium_narrowing_begv
  7638             = get_medium_narrowing_begv (it->w, pos.charpos);
  7639           it->medium_narrowing_zv
  7640             = get_medium_narrowing_zv (it->w, pos.charpos);
  7641           it->large_narrowing_begv
  7642             = get_large_narrowing_begv (window_point (it->w));
  7643           it->large_narrowing_zv
  7644             = get_large_narrowing_zv (window_point (it->w));
  7645         }
  7646     }
  7647 
  7648   /* Determine where to check text properties.  Avoid doing it
  7649      where possible because text property lookup is very expensive.  */
  7650   if (force_p
  7651       || CHARPOS (pos) > it->stop_charpos
  7652       || CHARPOS (pos) < original_pos)
  7653     {
  7654       if (it->bidi_p)
  7655         {
  7656           /* For bidi iteration, we need to prime prev_stop and
  7657              base_level_stop with our best estimations.  */
  7658           /* Implementation note: Of course, POS is not necessarily a
  7659              stop position, so assigning prev_pos to it is a lie; we
  7660              should have called compute_stop_backwards.  However, if
  7661              the current buffer does not include any R2L characters,
  7662              that call would be a waste of cycles, because the
  7663              iterator will never move back, and thus never cross this
  7664              "fake" stop position.  So we delay that backward search
  7665              until the time we really need it, in next_element_from_buffer.  */
  7666           if (CHARPOS (pos) != it->prev_stop)
  7667             it->prev_stop = CHARPOS (pos);
  7668           if (CHARPOS (pos) < it->base_level_stop)
  7669             it->base_level_stop = 0; /* meaning it's unknown */
  7670           handle_stop (it);
  7671         }
  7672       else
  7673         {
  7674           handle_stop (it);
  7675           it->prev_stop = it->base_level_stop = 0;
  7676         }
  7677 
  7678     }
  7679 
  7680   CHECK_IT (it);
  7681 }
  7682 
  7683 
  7684 /* Change IT's buffer position to POS.  SET_STOP_P means set
  7685    IT->stop_pos to POS, also.  */
  7686 
  7687 static void
  7688 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
  7689 {
  7690   /* Don't call this function when scanning a C string.  */
  7691   eassert (it->s == NULL);
  7692 
  7693   /* POS must be a reasonable value.  */
  7694   eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
  7695 
  7696   it->current.pos = it->position = pos;
  7697   it->end_charpos = ZV;
  7698   it->dpvec = NULL;
  7699   it->current.dpvec_index = -1;
  7700   it->current.overlay_string_index = -1;
  7701   IT_STRING_CHARPOS (*it) = -1;
  7702   IT_STRING_BYTEPOS (*it) = -1;
  7703   it->string = Qnil;
  7704   it->method = GET_FROM_BUFFER;
  7705   it->object = it->w->contents;
  7706   it->area = TEXT_AREA;
  7707   it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  7708   it->sp = 0;
  7709   it->string_from_display_prop_p = false;
  7710   it->string_from_prefix_prop_p = false;
  7711 
  7712   it->from_disp_prop_p = false;
  7713   it->face_before_selective_p = false;
  7714   if (it->bidi_p)
  7715     {
  7716       bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  7717                     &it->bidi_it);
  7718       bidi_unshelve_cache (NULL, false);
  7719       it->bidi_it.paragraph_dir = NEUTRAL_DIR;
  7720       it->bidi_it.string.s = NULL;
  7721       it->bidi_it.string.lstring = Qnil;
  7722       it->bidi_it.string.bufpos = 0;
  7723       it->bidi_it.string.from_disp_str = false;
  7724       it->bidi_it.string.unibyte = false;
  7725       it->bidi_it.w = it->w;
  7726     }
  7727 
  7728   if (set_stop_p)
  7729     {
  7730       it->stop_charpos = CHARPOS (pos);
  7731       it->base_level_stop = CHARPOS (pos);
  7732     }
  7733   /* This make the information stored in it->cmp_it invalidate.  */
  7734   it->cmp_it.id = -1;
  7735   it->min_width_property = Qnil;
  7736 }
  7737 
  7738 
  7739 /* Set up IT for displaying a string, starting at CHARPOS in window W.
  7740    If S is non-null, it is a C string to iterate over.  Otherwise,
  7741    STRING gives a Lisp string to iterate over.
  7742 
  7743    If PRECISION > 0, don't return more then PRECISION number of
  7744    characters from the string.
  7745 
  7746    If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
  7747    characters have been returned.  FIELD_WIDTH < 0 means an infinite
  7748    field width.
  7749 
  7750    MULTIBYTE = 0 means disable processing of multibyte characters,
  7751    MULTIBYTE > 0 means enable it,
  7752    MULTIBYTE < 0 means use IT->multibyte_p.
  7753 
  7754    IT must be initialized via a prior call to init_iterator before
  7755    calling this function.  */
  7756 
  7757 static void
  7758 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
  7759                   ptrdiff_t charpos, ptrdiff_t precision, int field_width,
  7760                   int multibyte)
  7761 {
  7762   /* No text property checks performed by default, but see below.  */
  7763   it->stop_charpos = -1;
  7764 
  7765   /* Set iterator position and end position.  */
  7766   memset (&it->current, 0, sizeof it->current);
  7767   it->current.overlay_string_index = -1;
  7768   it->current.dpvec_index = -1;
  7769   eassert (charpos >= 0);
  7770 
  7771   /* If STRING is specified, use its multibyteness, otherwise use the
  7772      setting of MULTIBYTE, if specified.  */
  7773   if (multibyte >= 0)
  7774     it->multibyte_p = multibyte > 0;
  7775 
  7776   /* Bidirectional reordering of strings is controlled by the default
  7777      value of bidi-display-reordering.  Don't try to reorder while
  7778      loading loadup.el, as the necessary character property tables are
  7779      not yet available.  */
  7780   it->bidi_p =
  7781     !redisplay__inhibit_bidi
  7782     && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
  7783 
  7784   if (s == NULL)
  7785     {
  7786       eassert (STRINGP (string));
  7787       it->string = string;
  7788       it->s = NULL;
  7789       it->end_charpos = it->string_nchars = SCHARS (string);
  7790       it->method = GET_FROM_STRING;
  7791       it->current.string_pos = string_pos (charpos, string);
  7792 
  7793       if (it->bidi_p)
  7794         {
  7795           it->bidi_it.string.lstring = string;
  7796           it->bidi_it.string.s = NULL;
  7797           it->bidi_it.string.schars = it->end_charpos;
  7798           it->bidi_it.string.bufpos = 0;
  7799           it->bidi_it.string.from_disp_str = false;
  7800           it->bidi_it.string.unibyte = !it->multibyte_p;
  7801           it->bidi_it.w = it->w;
  7802           bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
  7803                         FRAME_WINDOW_P (it->f), &it->bidi_it);
  7804         }
  7805     }
  7806   else
  7807     {
  7808       it->s = (const unsigned char *) s;
  7809       it->string = Qnil;
  7810 
  7811       /* Note that we use IT->current.pos, not it->current.string_pos,
  7812          for displaying C strings.  */
  7813       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
  7814       if (it->multibyte_p)
  7815         {
  7816           it->current.pos = c_string_pos (charpos, s, true);
  7817           it->end_charpos = it->string_nchars = number_of_chars (s, true);
  7818         }
  7819       else
  7820         {
  7821           IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
  7822           it->end_charpos = it->string_nchars = strlen (s);
  7823         }
  7824 
  7825       if (it->bidi_p)
  7826         {
  7827           it->bidi_it.string.lstring = Qnil;
  7828           it->bidi_it.string.s = (const unsigned char *) s;
  7829           it->bidi_it.string.schars = it->end_charpos;
  7830           it->bidi_it.string.bufpos = 0;
  7831           it->bidi_it.string.from_disp_str = false;
  7832           it->bidi_it.string.unibyte = !it->multibyte_p;
  7833           it->bidi_it.w = it->w;
  7834           bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  7835                         &it->bidi_it);
  7836         }
  7837       it->method = GET_FROM_C_STRING;
  7838     }
  7839 
  7840   /* PRECISION > 0 means don't return more than PRECISION characters
  7841      from the string.  */
  7842   if (precision > 0 && it->end_charpos - charpos > precision)
  7843     {
  7844       it->end_charpos = it->string_nchars = charpos + precision;
  7845       if (it->bidi_p)
  7846         it->bidi_it.string.schars = it->end_charpos;
  7847     }
  7848 
  7849   /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
  7850      characters have been returned.  FIELD_WIDTH == 0 means don't pad,
  7851      FIELD_WIDTH < 0 means infinite field width.  This is useful for
  7852      padding with `-' at the end of a mode line.  */
  7853   if (field_width < 0)
  7854     field_width = DISP_INFINITY;
  7855   /* Implementation note: We deliberately don't enlarge
  7856      it->bidi_it.string.schars here to fit it->end_charpos, because
  7857      the bidi iterator cannot produce characters out of thin air.  */
  7858   if (field_width > it->end_charpos - charpos)
  7859     it->end_charpos = charpos + field_width;
  7860 
  7861   /* Use the standard display table for displaying strings.  */
  7862   if (DISP_TABLE_P (Vstandard_display_table))
  7863     it->dp = XCHAR_TABLE (Vstandard_display_table);
  7864 
  7865   it->stop_charpos = charpos;
  7866   it->prev_stop = charpos;
  7867   it->base_level_stop = 0;
  7868   if (it->bidi_p)
  7869     {
  7870       it->bidi_it.first_elt = true;
  7871       it->bidi_it.paragraph_dir = NEUTRAL_DIR;
  7872       it->bidi_it.disp_pos = -1;
  7873     }
  7874   if (s == NULL && it->multibyte_p)
  7875     {
  7876       ptrdiff_t endpos = SCHARS (it->string);
  7877       if (endpos > it->end_charpos)
  7878         endpos = it->end_charpos;
  7879       composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
  7880                                     it->string);
  7881     }
  7882   CHECK_IT (it);
  7883 }
  7884 
  7885 
  7886 
  7887 /***********************************************************************
  7888                               Iteration
  7889 ***********************************************************************/
  7890 
  7891 /* Map enum it_method value to corresponding next_element_from_* function.  */
  7892 
  7893 typedef bool (*next_element_function) (struct it *);
  7894 
  7895 static next_element_function const get_next_element[NUM_IT_METHODS] =
  7896 {
  7897   next_element_from_buffer,
  7898   next_element_from_display_vector,
  7899   next_element_from_string,
  7900   next_element_from_c_string,
  7901   next_element_from_image,
  7902   next_element_from_stretch,
  7903   next_element_from_xwidget,
  7904 };
  7905 
  7906 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
  7907 
  7908 
  7909 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
  7910    (possibly with the following characters).
  7911 
  7912   Note: we pass -1 as the "resolved bidi level" when the iterator
  7913   doesn't have the bidi_p flag set, because in that case we really
  7914   don't know what is the directionality of the text, so we leave it to
  7915   the shaping engine to figure that out.  */
  7916 
  7917 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS)                 \
  7918   ((IT)->cmp_it.id >= 0                                                 \
  7919    || ((IT)->cmp_it.stop_pos == (CHARPOS)                               \
  7920        && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS,       \
  7921                                  END_CHARPOS, (IT)->w,                  \
  7922                                  (IT)->bidi_p                           \
  7923                                  ? (IT)->bidi_it.resolved_level         \
  7924                                  : -1,                                  \
  7925                                  FACE_FROM_ID_OR_NULL ((IT)->f,         \
  7926                                                        (IT)->face_id),  \
  7927                                  (IT)->string)))
  7928 
  7929 
  7930 /* Lookup the char-table Vglyphless_char_display for character C (-1
  7931    if we want information for no-font case), and return the display
  7932    method symbol.  By side-effect, update it->what and
  7933    it->glyphless_method.  This function is called from
  7934    get_next_display_element for each character element, and from
  7935    gui_produce_glyphs when no suitable font was found.  */
  7936 
  7937 Lisp_Object
  7938 lookup_glyphless_char_display (int c, struct it *it)
  7939 {
  7940   Lisp_Object glyphless_method = Qnil;
  7941 
  7942   if (CHAR_TABLE_P (Vglyphless_char_display)
  7943       && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
  7944     {
  7945       if (c >= 0)
  7946         glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
  7947       else
  7948         glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
  7949 
  7950       if (CONSP (glyphless_method))
  7951         glyphless_method = FRAME_WINDOW_P (it->f)
  7952           ? XCAR (glyphless_method)
  7953           : XCDR (glyphless_method);
  7954     }
  7955 
  7956  retry:
  7957   if (NILP (glyphless_method))
  7958     {
  7959       if (c >= 0)
  7960         /* The default is to display the character by a proper font.  */
  7961         return Qnil;
  7962       /* The default for the no-font case is to display an empty box.  */
  7963       glyphless_method = Qempty_box;
  7964     }
  7965   if (EQ (glyphless_method, Qzero_width))
  7966     {
  7967       if (c >= 0)
  7968         return glyphless_method;
  7969       /* This method can't be used for the no-font case.  */
  7970       glyphless_method = Qempty_box;
  7971     }
  7972   if (EQ (glyphless_method, Qthin_space))
  7973     it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
  7974   else if (EQ (glyphless_method, Qempty_box))
  7975     it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
  7976   else if (EQ (glyphless_method, Qhex_code))
  7977     it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
  7978   else if (STRINGP (glyphless_method))
  7979     it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
  7980   else
  7981     {
  7982       /* Invalid value.  We use the default method.  */
  7983       glyphless_method = Qnil;
  7984       goto retry;
  7985     }
  7986   it->what = IT_GLYPHLESS;
  7987   return glyphless_method;
  7988 }
  7989 
  7990 /* Merge escape glyph face and cache the result.  */
  7991 
  7992 static struct frame *last_escape_glyph_frame = NULL;
  7993 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  7994 static int last_escape_glyph_merged_face_id = 0;
  7995 
  7996 static int
  7997 merge_escape_glyph_face (struct it *it)
  7998 {
  7999   int face_id;
  8000 
  8001   if (it->f == last_escape_glyph_frame
  8002       && it->face_id == last_escape_glyph_face_id)
  8003     face_id = last_escape_glyph_merged_face_id;
  8004   else
  8005     {
  8006       /* Merge the `escape-glyph' face into the current face.  */
  8007       face_id = merge_faces (it->w, Qescape_glyph, 0, it->face_id);
  8008       last_escape_glyph_frame = it->f;
  8009       last_escape_glyph_face_id = it->face_id;
  8010       last_escape_glyph_merged_face_id = face_id;
  8011     }
  8012   return face_id;
  8013 }
  8014 
  8015 /* Likewise for glyphless glyph face.  */
  8016 
  8017 static struct frame *last_glyphless_glyph_frame = NULL;
  8018 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  8019 static int last_glyphless_glyph_merged_face_id = 0;
  8020 
  8021 int
  8022 merge_glyphless_glyph_face (struct it *it)
  8023 {
  8024   int face_id;
  8025 
  8026   if (it->f == last_glyphless_glyph_frame
  8027       && it->face_id == last_glyphless_glyph_face_id)
  8028     face_id = last_glyphless_glyph_merged_face_id;
  8029   else
  8030     {
  8031       /* Merge the `glyphless-char' face into the current face.  */
  8032       face_id = merge_faces (it->w, Qglyphless_char, 0, it->face_id);
  8033       last_glyphless_glyph_frame = it->f;
  8034       last_glyphless_glyph_face_id = it->face_id;
  8035       last_glyphless_glyph_merged_face_id = face_id;
  8036     }
  8037   return face_id;
  8038 }
  8039 
  8040 /* Forget the `escape-glyph' and `glyphless-char' faces.  This should
  8041    be called before redisplaying windows, and when the frame's face
  8042    cache is freed.  */
  8043 void
  8044 forget_escape_and_glyphless_faces (void)
  8045 {
  8046   last_escape_glyph_frame = NULL;
  8047   last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  8048   last_glyphless_glyph_frame = NULL;
  8049   last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  8050 }
  8051 
  8052 /* Load IT's display element fields with information about the next
  8053    display element from the current position of IT.  Value is false if
  8054    end of buffer (or C string) is reached.  */
  8055 
  8056 static bool
  8057 get_next_display_element (struct it *it)
  8058 {
  8059   /* True means that we found a display element.  False means that
  8060      we hit the end of what we iterate over.  Performance note: the
  8061      function pointer `method' used here turns out to be faster than
  8062      using a sequence of if-statements.  */
  8063   bool success_p;
  8064 
  8065  get_next:
  8066   success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  8067 
  8068   if (it->what == IT_CHARACTER)
  8069     {
  8070       /* UAX#9, L4: "A character is depicted by a mirrored glyph if
  8071          and only if (a) the resolved directionality of that character
  8072          is R..."  */
  8073       /* FIXME: Do we need an exception for characters from display
  8074          tables?  */
  8075       if (it->bidi_p && it->bidi_it.type == STRONG_R
  8076           && !inhibit_bidi_mirroring)
  8077         it->c = bidi_mirror_char (it->c);
  8078       /* Map via display table or translate control characters.
  8079          IT->c, IT->len etc. have been set to the next character by
  8080          the function call above.  If we have a display table, and it
  8081          contains an entry for IT->c, translate it.  Don't do this if
  8082          IT->c itself comes from a display table, otherwise we could
  8083          end up in an infinite recursion.  (An alternative could be to
  8084          count the recursion depth of this function and signal an
  8085          error when a certain maximum depth is reached.)  Is it worth
  8086          it?  */
  8087       if (success_p && it->dpvec == NULL)
  8088         {
  8089           Lisp_Object dv;
  8090           struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
  8091           bool nonascii_space_p = false;
  8092           bool nonascii_hyphen_p = false;
  8093           int c = it->c;        /* This is the character to display.  */
  8094 
  8095           if (! it->multibyte_p && ! ASCII_CHAR_P (c))
  8096             {
  8097               eassert (SINGLE_BYTE_CHAR_P (c));
  8098               if (unibyte_display_via_language_environment)
  8099                 {
  8100                   c = DECODE_CHAR (unibyte, c);
  8101                   if (c < 0)
  8102                     c = BYTE8_TO_CHAR (it->c);
  8103                 }
  8104               else
  8105                 c = BYTE8_TO_CHAR (it->c);
  8106             }
  8107 
  8108           if (it->dp
  8109               && (dv = DISP_CHAR_VECTOR (it->dp, c),
  8110                   VECTORP (dv)))
  8111             {
  8112               struct Lisp_Vector *v = XVECTOR (dv);
  8113 
  8114               /* Return the first character from the display table
  8115                  entry, if not empty.  If empty, don't display the
  8116                  current character.  */
  8117               if (v->header.size)
  8118                 {
  8119                   it->dpvec_char_len = it->len;
  8120                   it->dpvec = v->contents;
  8121                   it->dpend = v->contents + v->header.size;
  8122                   it->current.dpvec_index = 0;
  8123                   it->dpvec_face_id = -1;
  8124                   it->saved_face_id = it->face_id;
  8125                   it->method = GET_FROM_DISPLAY_VECTOR;
  8126                   it->ellipsis_p = false;
  8127                 }
  8128               else
  8129                 {
  8130                   set_iterator_to_next (it, false);
  8131                 }
  8132               goto get_next;
  8133             }
  8134 
  8135           if (! NILP (lookup_glyphless_char_display (c, it)))
  8136             {
  8137               if (it->what == IT_GLYPHLESS)
  8138                 goto done;
  8139               /* Don't display this character.  */
  8140               set_iterator_to_next (it, false);
  8141               goto get_next;
  8142             }
  8143 
  8144           /* If `nobreak-char-display' is non-nil, we display
  8145              non-ASCII spaces and hyphens specially.  */
  8146           if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
  8147             {
  8148               if (blankp (c))
  8149                 nonascii_space_p = true;
  8150               else if (c == SOFT_HYPHEN || c == HYPHEN
  8151                        || c == NON_BREAKING_HYPHEN)
  8152                 nonascii_hyphen_p = true;
  8153             }
  8154 
  8155           /* Translate control characters into `\003' or `^C' form.
  8156              Control characters coming from a display table entry are
  8157              currently not translated because we use IT->dpvec to hold
  8158              the translation.  This could easily be changed but I
  8159              don't believe that it is worth doing.
  8160 
  8161              The characters handled by `nobreak-char-display' must be
  8162              translated too.
  8163 
  8164              Non-printable characters and raw-byte characters are also
  8165              translated to octal or hexadecimal form.  */
  8166           if (((c < ' ' || c == 127) /* ASCII control chars.  */
  8167                ? (it->area != TEXT_AREA
  8168                   /* In mode line, treat \n, \t like other crl chars.  */
  8169                   || (c != '\t'
  8170                       && it->glyph_row
  8171                       && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
  8172                   || (c != '\n' && c != '\t'))
  8173                : (nonascii_space_p
  8174                   || nonascii_hyphen_p
  8175                   || CHAR_BYTE8_P (c)
  8176                   || ! CHAR_PRINTABLE_P (c))))
  8177             {
  8178               /* C is a control character, non-ASCII space/hyphen,
  8179                  raw-byte, or a non-printable character which must be
  8180                  displayed either as '\003' or as `^C' where the '\\'
  8181                  and '^' can be defined in the display table.  Fill
  8182                  IT->ctl_chars with glyphs for what we have to
  8183                  display.  Then, set IT->dpvec to these glyphs.  */
  8184               Lisp_Object gc;
  8185               int ctl_len;
  8186               int face_id;
  8187               int lface_id = 0;
  8188               int escape_glyph;
  8189 
  8190               /* Handle control characters with ^.  */
  8191 
  8192               if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
  8193                 {
  8194                   int g;
  8195 
  8196                   g = '^';           /* default glyph for Control */
  8197                   /* Set IT->ctl_chars[0] to the glyph for `^'.  */
  8198                   if (it->dp
  8199                       && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
  8200                     {
  8201                       g = GLYPH_CODE_CHAR (gc);
  8202                       lface_id = GLYPH_CODE_FACE (gc);
  8203                     }
  8204 
  8205                   face_id = (lface_id
  8206                              ? merge_faces (it->w, Qt, lface_id, it->face_id)
  8207                              : merge_escape_glyph_face (it));
  8208 
  8209                   XSETINT (it->ctl_chars[0], g);
  8210                   XSETINT (it->ctl_chars[1], c ^ 0100);
  8211                   ctl_len = 2;
  8212                   goto display_control;
  8213                 }
  8214 
  8215               /* Handle non-ascii space in the mode where it only gets
  8216                  highlighting.  */
  8217 
  8218               if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
  8219                 {
  8220                   /* Merge `nobreak-space' into the current face.  */
  8221                   face_id = merge_faces (it->w, Qnobreak_space, 0,
  8222                                          it->face_id);
  8223                   XSETINT (it->ctl_chars[0],
  8224                            nobreak_char_ascii_display ? ' ' : it->c);
  8225                   ctl_len = 1;
  8226                   goto display_control;
  8227                 }
  8228 
  8229               /* Handle non-ascii hyphens in the mode where it only
  8230                  gets highlighting.  */
  8231 
  8232               if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
  8233                 {
  8234                   /* Merge `nobreak-space' into the current face.  */
  8235                   face_id = merge_faces (it->w, Qnobreak_hyphen, 0,
  8236                                          it->face_id);
  8237                   XSETINT (it->ctl_chars[0],
  8238                            nobreak_char_ascii_display ? '-' : it->c);
  8239                   ctl_len = 1;
  8240                   goto display_control;
  8241                 }
  8242 
  8243               /* Handle sequences that start with the "escape glyph".  */
  8244 
  8245               /* the default escape glyph is \.  */
  8246               escape_glyph = '\\';
  8247 
  8248               if (it->dp
  8249                   && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
  8250                 {
  8251                   escape_glyph = GLYPH_CODE_CHAR (gc);
  8252                   lface_id = GLYPH_CODE_FACE (gc);
  8253                 }
  8254 
  8255               face_id = (lface_id
  8256                          ? merge_faces (it->w, Qt, lface_id, it->face_id)
  8257                          : merge_escape_glyph_face (it));
  8258 
  8259               /* Draw non-ASCII space/hyphen with escape glyph: */
  8260 
  8261               if (nonascii_space_p || nonascii_hyphen_p)
  8262                 {
  8263                   XSETINT (it->ctl_chars[0], escape_glyph);
  8264                   XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
  8265                   ctl_len = 2;
  8266                   goto display_control;
  8267                 }
  8268 
  8269               {
  8270                 char str[10];
  8271                 int len, i;
  8272 
  8273                 if (CHAR_BYTE8_P (c))
  8274                   /* Display \200 or \x80 instead of \17777600.  */
  8275                   c = CHAR_TO_BYTE8 (c);
  8276                 const char *format_string = display_raw_bytes_as_hex
  8277                                             ? "x%02x"
  8278                                             : "%03o";
  8279                 len = sprintf (str, format_string, c + 0u);
  8280 
  8281                 XSETINT (it->ctl_chars[0], escape_glyph);
  8282                 for (i = 0; i < len; i++)
  8283                   XSETINT (it->ctl_chars[i + 1], str[i]);
  8284                 ctl_len = len + 1;
  8285               }
  8286 
  8287             display_control:
  8288               /* Set up IT->dpvec and return first character from it.  */
  8289               it->dpvec_char_len = it->len;
  8290               it->dpvec = it->ctl_chars;
  8291               it->dpend = it->dpvec + ctl_len;
  8292               it->current.dpvec_index = 0;
  8293               it->dpvec_face_id = face_id;
  8294               it->saved_face_id = it->face_id;
  8295               it->method = GET_FROM_DISPLAY_VECTOR;
  8296               it->ellipsis_p = false;
  8297               goto get_next;
  8298             }
  8299           it->char_to_display = c;
  8300         }
  8301       else if (success_p)
  8302         {
  8303           it->char_to_display = it->c;
  8304         }
  8305     }
  8306 
  8307 #ifdef HAVE_WINDOW_SYSTEM
  8308   /* Adjust face id for a multibyte character.  There are no multibyte
  8309      character in unibyte text.  */
  8310   if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
  8311       && it->multibyte_p
  8312       && success_p
  8313       && FRAME_WINDOW_P (it->f))
  8314     {
  8315       struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  8316 
  8317       /* It shouldn't happen, ever, that FACE is NULL here, but
  8318          evidently some faulty fonts/fontsets can sometimes cause it.
  8319          In that case, we punt and consider the stuff undisplayable.  */
  8320       if (!face)
  8321         {
  8322           it->what = IT_GLYPHLESS;
  8323           it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
  8324         }
  8325       else if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
  8326         {
  8327           /* Automatic composition with glyph-string.   */
  8328           Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
  8329 
  8330           it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
  8331         }
  8332       else
  8333         {
  8334           ptrdiff_t pos = (it->s ? -1
  8335                      : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
  8336                      : IT_CHARPOS (*it));
  8337           int c;
  8338 
  8339           if (it->what == IT_CHARACTER)
  8340             c = it->char_to_display;
  8341           else
  8342             {
  8343               struct composition *cmp = composition_table[it->cmp_it.id];
  8344               int i;
  8345 
  8346               c = ' ';
  8347               for (i = 0; i < cmp->glyph_len; i++)
  8348                 /* TAB in a composition means display glyphs with
  8349                    padding space on the left or right.  */
  8350                 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
  8351                   break;
  8352             }
  8353           it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
  8354         }
  8355     }
  8356 #endif  /* HAVE_WINDOW_SYSTEM */
  8357 
  8358  done:
  8359   /* Is this character the last one of a run of characters with
  8360      box?  If yes, set IT->end_of_box_run_p to true.  */
  8361   if (it->face_box_p
  8362       && it->s == NULL)
  8363     {
  8364       if (it->method == GET_FROM_STRING && it->sp)
  8365         {
  8366           int face_id = underlying_face_id (it);
  8367           struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
  8368 
  8369           if (face)
  8370             {
  8371               if (face->box == FACE_NO_BOX)
  8372                 {
  8373                   /* If the box comes from face properties in a
  8374                      display string, check faces in that string.  */
  8375                   int string_face_id = face_after_it_pos (it);
  8376                   if (FACE_FROM_ID (it->f, string_face_id)->box == FACE_NO_BOX)
  8377                     it->end_of_box_run_p = true;
  8378                 }
  8379               /* Otherwise, the box comes from the underlying face.
  8380                  If this is the last string character displayed, check
  8381                  the next buffer location.  */
  8382               else if (((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
  8383                         /* For a composition, see if the string ends
  8384                            at the last character included in the
  8385                            composition.  */
  8386                         || (it->what == IT_COMPOSITION
  8387                             && (IT_STRING_CHARPOS (*it) + it->cmp_it.nchars
  8388                                 >= SCHARS (it->string))))
  8389                        /* n_overlay_strings is unreliable unless
  8390                           overlay_string_index is non-negative.  */
  8391                        && ((it->current.overlay_string_index >= 0
  8392                             && (it->current.overlay_string_index
  8393                                 == it->n_overlay_strings - 1))
  8394                            /* A string from display property.  */
  8395                            || it->from_disp_prop_p))
  8396                 {
  8397                   ptrdiff_t ignore;
  8398                   int next_face_id;
  8399                   bool text_from_string = false;
  8400                   /* Normally, the next buffer location is stored in
  8401                      IT->current.pos...  */
  8402                   struct text_pos pos = it->current.pos;
  8403 
  8404                   /* ...but for a string from a display property, the
  8405                      next buffer position is stored in the 'position'
  8406                      member of the iteration stack slot below the
  8407                      current one, see handle_single_display_spec.  By
  8408                      contrast, it->current.pos was not yet updated to
  8409                      point to that buffer position; that will happen
  8410                      in pop_it, after we finish displaying the current
  8411                      string.  Note that we already checked above that
  8412                      it->sp is positive, so subtracting one from it is
  8413                      safe.  */
  8414                   if (it->from_disp_prop_p)
  8415                     {
  8416                       int stackp = it->sp - 1;
  8417 
  8418                       /* Find the stack level with data from buffer.  */
  8419                       while (stackp >= 0
  8420                              && STRINGP ((it->stack + stackp)->string))
  8421                         stackp--;
  8422                       if (stackp < 0)
  8423                         {
  8424                           /* If no stack slot was found for iterating
  8425                              a buffer, we are displaying text from a
  8426                              string, most probably the mode line or
  8427                              the header line, and that string has a
  8428                              display string on some of its
  8429                              characters.  */
  8430                           text_from_string = true;
  8431                           pos = it->stack[it->sp - 1].position;
  8432                         }
  8433                       else
  8434                         pos = (it->stack + stackp)->position;
  8435                     }
  8436                   else
  8437                     INC_TEXT_POS (pos, it->multibyte_p);
  8438 
  8439                   if (text_from_string)
  8440                     {
  8441                       Lisp_Object base_string = it->stack[it->sp - 1].string;
  8442 
  8443                       if (CHARPOS (pos) >= SCHARS (base_string) - 1)
  8444                         it->end_of_box_run_p = true;
  8445                       else
  8446                         {
  8447                           next_face_id
  8448                             = face_at_string_position (it->w, base_string,
  8449                                                        CHARPOS (pos), 0,
  8450                                                        &ignore, face_id,
  8451                                                        false, 0);
  8452                           if (FACE_FROM_ID (it->f, next_face_id)->box
  8453                               == FACE_NO_BOX)
  8454                             it->end_of_box_run_p = true;
  8455                         }
  8456                     }
  8457                   else if (CHARPOS (pos) >= ZV)
  8458                     it->end_of_box_run_p = true;
  8459                   else
  8460                     {
  8461                       next_face_id =
  8462                         face_at_buffer_position (it->w, CHARPOS (pos),
  8463                                                  &ignore,
  8464                                                  CHARPOS (pos)
  8465                                                  + TEXT_PROP_DISTANCE_LIMIT,
  8466                                                  false, -1, 0);
  8467                       if (FACE_FROM_ID (it->f, next_face_id)->box
  8468                           == FACE_NO_BOX)
  8469                         it->end_of_box_run_p = true;
  8470                     }
  8471                 }
  8472             }
  8473         }
  8474       /* next_element_from_display_vector sets this flag according to
  8475          faces of the display vector glyphs, see there.  */
  8476       else if (it->method != GET_FROM_DISPLAY_VECTOR)
  8477         {
  8478           int face_id = face_after_it_pos (it);
  8479           if (face_id != it->face_id
  8480               && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX)
  8481             it->end_of_box_run_p = true;
  8482         }
  8483     }
  8484   /* If we reached the end of the object we've been iterating (e.g., a
  8485      display string or an overlay string), and there's something on
  8486      IT->stack, proceed with what's on the stack.  It doesn't make
  8487      sense to return false if there's unprocessed stuff on the stack,
  8488      because otherwise that stuff will never be displayed.  */
  8489   if (!success_p && it->sp > 0)
  8490     {
  8491       set_iterator_to_next (it, false);
  8492       success_p = get_next_display_element (it);
  8493     }
  8494 
  8495   /* Value is false if end of buffer or string reached.  */
  8496   return success_p;
  8497 }
  8498 
  8499 
  8500 /* Move IT to the next display element.
  8501 
  8502    RESEAT_P means if called on a newline in buffer text,
  8503    skip to the next visible line start.
  8504 
  8505    Functions get_next_display_element and set_iterator_to_next are
  8506    separate because I find this arrangement easier to handle than a
  8507    get_next_display_element function that also increments IT's
  8508    position.  The way it is we can first look at an iterator's current
  8509    display element, decide whether it fits on a line, and if it does,
  8510    increment the iterator position.  The other way around we probably
  8511    would either need a flag indicating whether the iterator has to be
  8512    incremented the next time, or we would have to implement a
  8513    decrement position function which would not be easy to write.  */
  8514 
  8515 void
  8516 set_iterator_to_next (struct it *it, bool reseat_p)
  8517 {
  8518 
  8519   if (max_redisplay_ticks > 0)
  8520     update_redisplay_ticks (1, it->w);
  8521 
  8522   switch (it->method)
  8523     {
  8524     case GET_FROM_BUFFER:
  8525       /* The current display element of IT is a character from
  8526          current_buffer.  Advance in the buffer, and maybe skip over
  8527          invisible lines that are so because of selective display.  */
  8528       if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
  8529         reseat_at_next_visible_line_start (it, false);
  8530       else if (it->cmp_it.id >= 0)
  8531         {
  8532           /* We are currently getting glyphs from a composition.  */
  8533           if (! it->bidi_p)
  8534             {
  8535               IT_CHARPOS (*it) += it->cmp_it.nchars;
  8536               IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  8537             }
  8538           else
  8539             {
  8540               int i;
  8541 
  8542               /* Update IT's char/byte positions to point to the first
  8543                  character of the next grapheme cluster, or to the
  8544                  character visually after the current composition.  */
  8545               for (i = 0; i < it->cmp_it.nchars; i++)
  8546                 bidi_move_to_visually_next (&it->bidi_it);
  8547               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8548               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8549             }
  8550 
  8551           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8552               && it->cmp_it.to < it->cmp_it.nglyphs)
  8553             {
  8554               /* Composition created while scanning forward.  Proceed
  8555                  to the next grapheme cluster.  */
  8556               it->cmp_it.from = it->cmp_it.to;
  8557             }
  8558           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8559                    && it->cmp_it.from > 0)
  8560             {
  8561               /* Composition created while scanning backward.  Proceed
  8562                  to the previous grapheme cluster.  */
  8563               it->cmp_it.to = it->cmp_it.from;
  8564             }
  8565           else
  8566             {
  8567               /* No more grapheme clusters in this composition.
  8568                  Find the next stop position.  */
  8569               ptrdiff_t stop = it->end_charpos;
  8570 
  8571               if (it->bidi_it.scan_dir < 0)
  8572                 /* Now we are scanning backward and don't know
  8573                    where to stop.  */
  8574                 stop = -1;
  8575               composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8576                                             IT_BYTEPOS (*it), stop, Qnil);
  8577             }
  8578         }
  8579       else
  8580         {
  8581           eassert (it->len != 0);
  8582 
  8583           if (!it->bidi_p)
  8584             {
  8585               IT_BYTEPOS (*it) += it->len;
  8586               IT_CHARPOS (*it) += 1;
  8587             }
  8588           else
  8589             {
  8590               int prev_scan_dir = it->bidi_it.scan_dir;
  8591               /* If this is a new paragraph, determine its base
  8592                  direction (a.k.a. its base embedding level).  */
  8593               if (it->bidi_it.new_paragraph)
  8594                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  8595                                      false);
  8596               bidi_move_to_visually_next (&it->bidi_it);
  8597               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8598               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8599               if (prev_scan_dir != it->bidi_it.scan_dir)
  8600                 {
  8601                   /* As the scan direction was changed, we must
  8602                      re-compute the stop position for composition.  */
  8603                   ptrdiff_t stop = it->end_charpos;
  8604                   if (it->bidi_it.scan_dir < 0)
  8605                     stop = -1;
  8606                   composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8607                                                 IT_BYTEPOS (*it), stop, Qnil);
  8608                 }
  8609             }
  8610           eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
  8611         }
  8612       break;
  8613 
  8614     case GET_FROM_C_STRING:
  8615       /* Current display element of IT is from a C string.  */
  8616       if (!it->bidi_p
  8617           /* If the string position is beyond string's end, it means
  8618              next_element_from_c_string is padding the string with
  8619              blanks, in which case we bypass the bidi iterator,
  8620              because it cannot deal with such virtual characters.  */
  8621           || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
  8622         {
  8623           IT_BYTEPOS (*it) += it->len;
  8624           IT_CHARPOS (*it) += 1;
  8625         }
  8626       else
  8627         {
  8628           bidi_move_to_visually_next (&it->bidi_it);
  8629           IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8630           IT_CHARPOS (*it) = it->bidi_it.charpos;
  8631         }
  8632       break;
  8633 
  8634     case GET_FROM_DISPLAY_VECTOR:
  8635       /* Current display element of IT is from a display table entry.
  8636          Advance in the display table definition.  Reset it to null if
  8637          end reached, and continue with characters from buffers/
  8638          strings.  */
  8639       ++it->current.dpvec_index;
  8640 
  8641       /* Restore face of the iterator to what they were before the
  8642          display vector entry (these entries may contain faces).  */
  8643       it->face_id = it->saved_face_id;
  8644 
  8645       if (it->dpvec + it->current.dpvec_index >= it->dpend)
  8646         {
  8647           bool recheck_faces = it->ellipsis_p;
  8648 
  8649           if (it->s)
  8650             it->method = GET_FROM_C_STRING;
  8651           else if (STRINGP (it->string))
  8652             it->method = GET_FROM_STRING;
  8653           else
  8654             {
  8655               it->method = GET_FROM_BUFFER;
  8656               it->object = it->w->contents;
  8657             }
  8658 
  8659           it->dpvec = NULL;
  8660           it->current.dpvec_index = -1;
  8661 
  8662           /* Skip over characters which were displayed via IT->dpvec.  */
  8663           if (it->dpvec_char_len < 0)
  8664             reseat_at_next_visible_line_start (it, true);
  8665           else if (it->dpvec_char_len > 0)
  8666             {
  8667               it->len = it->dpvec_char_len;
  8668               set_iterator_to_next (it, reseat_p);
  8669             }
  8670 
  8671           /* Maybe recheck faces after display vector.  */
  8672           if (recheck_faces)
  8673             {
  8674               if (it->method == GET_FROM_STRING)
  8675                 it->stop_charpos = IT_STRING_CHARPOS (*it);
  8676               else
  8677                 it->stop_charpos = IT_CHARPOS (*it);
  8678             }
  8679         }
  8680       break;
  8681 
  8682     case GET_FROM_STRING:
  8683       /* Current display element is a character from a Lisp string.  */
  8684       eassert (it->s == NULL && STRINGP (it->string));
  8685       /* Don't advance past string end.  These conditions are true
  8686          when set_iterator_to_next is called at the end of
  8687          get_next_display_element, in which case the Lisp string is
  8688          already exhausted, and all we want is pop the iterator
  8689          stack.  */
  8690       if (it->current.overlay_string_index >= 0)
  8691         {
  8692           /* This is an overlay string, so there's no padding with
  8693              spaces, and the number of characters in the string is
  8694              where the string ends.  */
  8695           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8696             goto consider_string_end;
  8697         }
  8698       else
  8699         {
  8700           /* Not an overlay string.  There could be padding, so test
  8701              against it->end_charpos.  */
  8702           if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  8703             goto consider_string_end;
  8704         }
  8705       if (it->cmp_it.id >= 0)
  8706         {
  8707           /* We are delivering display elements from a composition.
  8708              Update the string position past the grapheme cluster
  8709              we've just processed.  */
  8710           if (! it->bidi_p)
  8711             {
  8712               IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  8713               IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  8714             }
  8715           else
  8716             {
  8717               int i;
  8718 
  8719               for (i = 0; i < it->cmp_it.nchars; i++)
  8720                 bidi_move_to_visually_next (&it->bidi_it);
  8721               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8722               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8723             }
  8724 
  8725           /* Did we exhaust all the grapheme clusters of this
  8726              composition?  */
  8727           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8728               && (it->cmp_it.to < it->cmp_it.nglyphs))
  8729             {
  8730               /* Not all the grapheme clusters were processed yet;
  8731                  advance to the next cluster.  */
  8732               it->cmp_it.from = it->cmp_it.to;
  8733             }
  8734           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8735                    && it->cmp_it.from > 0)
  8736             {
  8737               /* Likewise: advance to the next cluster, but going in
  8738                  the reverse direction.  */
  8739               it->cmp_it.to = it->cmp_it.from;
  8740             }
  8741           else
  8742             {
  8743               /* This composition was fully processed; find the next
  8744                  candidate place for checking for composed
  8745                  characters.  */
  8746               /* Always limit string searches to the string length;
  8747                  any padding spaces are not part of the string, and
  8748                  there cannot be any compositions in that padding.  */
  8749               ptrdiff_t stop = SCHARS (it->string);
  8750 
  8751               if (it->bidi_p && it->bidi_it.scan_dir < 0)
  8752                 stop = -1;
  8753               else if (it->end_charpos < stop)
  8754                 {
  8755                   /* Cf. PRECISION in reseat_to_string: we might be
  8756                      limited in how many of the string characters we
  8757                      need to deliver.  */
  8758                   stop = it->end_charpos;
  8759                 }
  8760               composition_compute_stop_pos (&it->cmp_it,
  8761                                             IT_STRING_CHARPOS (*it),
  8762                                             IT_STRING_BYTEPOS (*it), stop,
  8763                                             it->string);
  8764             }
  8765         }
  8766       else
  8767         {
  8768           if (!it->bidi_p
  8769               /* If the string position is beyond string's end, it
  8770                  means next_element_from_string is padding the string
  8771                  with blanks, in which case we bypass the bidi
  8772                  iterator, because it cannot deal with such virtual
  8773                  characters.  */
  8774               || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
  8775             {
  8776               IT_STRING_BYTEPOS (*it) += it->len;
  8777               IT_STRING_CHARPOS (*it) += 1;
  8778             }
  8779           else
  8780             {
  8781               int prev_scan_dir = it->bidi_it.scan_dir;
  8782 
  8783               bidi_move_to_visually_next (&it->bidi_it);
  8784               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8785               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8786               /* If the scan direction changes, we may need to update
  8787                  the place where to check for composed characters.  */
  8788               if (prev_scan_dir != it->bidi_it.scan_dir)
  8789                 {
  8790                   ptrdiff_t stop = SCHARS (it->string);
  8791 
  8792                   if (it->bidi_it.scan_dir < 0)
  8793                     stop = -1;
  8794                   else if (it->end_charpos < stop)
  8795                     stop = it->end_charpos;
  8796 
  8797                   composition_compute_stop_pos (&it->cmp_it,
  8798                                                 IT_STRING_CHARPOS (*it),
  8799                                                 IT_STRING_BYTEPOS (*it), stop,
  8800                                                 it->string);
  8801                 }
  8802             }
  8803         }
  8804 
  8805     consider_string_end:
  8806 
  8807       if (it->current.overlay_string_index >= 0)
  8808         {
  8809           /* IT->string is an overlay string.  Advance to the
  8810              next, if there is one.  */
  8811           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8812             {
  8813               it->ellipsis_p = false;
  8814               next_overlay_string (it);
  8815               if (it->ellipsis_p)
  8816                 setup_for_ellipsis (it, 0);
  8817             }
  8818         }
  8819       else
  8820         {
  8821           /* IT->string is not an overlay string.  If we reached
  8822              its end, and there is something on IT->stack, proceed
  8823              with what is on the stack.  This can be either another
  8824              string, this time an overlay string, or a buffer.  */
  8825           if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
  8826               && it->sp > 0)
  8827             {
  8828               pop_it (it);
  8829               if (it->method == GET_FROM_STRING)
  8830                 goto consider_string_end;
  8831             }
  8832         }
  8833       break;
  8834 
  8835     case GET_FROM_IMAGE:
  8836     case GET_FROM_STRETCH:
  8837     case GET_FROM_XWIDGET:
  8838 
  8839       /* The position etc with which we have to proceed are on
  8840          the stack.  The position may be at the end of a string,
  8841          if the `display' property takes up the whole string.  */
  8842       eassert (it->sp > 0);
  8843       pop_it (it);
  8844       if (it->method == GET_FROM_STRING)
  8845         goto consider_string_end;
  8846       break;
  8847 
  8848     default:
  8849       /* There are no other methods defined, so this should be a bug.  */
  8850       emacs_abort ();
  8851     }
  8852 
  8853   eassert (it->method != GET_FROM_STRING
  8854            || (STRINGP (it->string)
  8855                && IT_STRING_CHARPOS (*it) >= 0));
  8856 }
  8857 
  8858 /* Load IT's display element fields with information about the next
  8859    display element which comes from a display table entry or from the
  8860    result of translating a control character to one of the forms `^C'
  8861    or `\003'.
  8862 
  8863    IT->dpvec holds the glyphs to return as characters.
  8864    IT->saved_face_id holds the face id before the display vector--it
  8865    is restored into IT->face_id in set_iterator_to_next.  */
  8866 
  8867 static bool
  8868 next_element_from_display_vector (struct it *it)
  8869 {
  8870   Lisp_Object gc;
  8871   int prev_face_id = it->face_id;
  8872   int next_face_id;
  8873 
  8874   /* Precondition.  */
  8875   eassert (it->dpvec && it->current.dpvec_index >= 0);
  8876 
  8877   it->face_id = it->saved_face_id;
  8878 
  8879   /* KFS: This code used to check ip->dpvec[0] instead of the current element.
  8880      That seemed totally bogus - so I changed it...  */
  8881   if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
  8882       && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
  8883     {
  8884       struct face *this_face, *prev_face, *next_face;
  8885 
  8886       it->c = GLYPH_CODE_CHAR (gc);
  8887       it->len = CHAR_BYTES (it->c);
  8888 
  8889       /* The entry may contain a face id to use.  Such a face id is
  8890          the id of a Lisp face, not a realized face.  A face id of
  8891          zero means no face is specified.  */
  8892       if (it->dpvec_face_id >= 0)
  8893         it->face_id = it->dpvec_face_id;
  8894       else
  8895         {
  8896           int lface_id = GLYPH_CODE_FACE (gc);
  8897           if (lface_id > 0)
  8898             it->face_id = merge_faces (it->w, Qt, lface_id,
  8899                                        it->saved_face_id);
  8900         }
  8901 
  8902       /* Glyphs in the display vector could have the box face, so we
  8903          need to set the related flags in the iterator, as
  8904          appropriate.  */
  8905       this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  8906       prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  8907 
  8908       /* Is this character the first character of a box-face run?  */
  8909       it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
  8910                                 && (!prev_face
  8911                                     || prev_face->box == FACE_NO_BOX));
  8912 
  8913       /* For the last character of the box-face run, we need to look
  8914          either at the next glyph from the display vector, or at the
  8915          face we saw before the display vector.  */
  8916       next_face_id = it->saved_face_id;
  8917       if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
  8918         {
  8919           if (it->dpvec_face_id >= 0)
  8920             next_face_id = it->dpvec_face_id;
  8921           else
  8922             {
  8923               Lisp_Object gc = it->dpvec[it->current.dpvec_index + 1];
  8924               int lface_id = GLYPH_CODE_P (gc) ? GLYPH_CODE_FACE (gc) : 0;
  8925 
  8926               if (lface_id > 0)
  8927                 next_face_id = merge_faces (it->w, Qt, lface_id,
  8928                                             it->saved_face_id);
  8929             }
  8930         }
  8931       next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
  8932       if (this_face && this_face->box != FACE_NO_BOX
  8933           && (!next_face || next_face->box == FACE_NO_BOX))
  8934         it->end_of_box_run_p = true;
  8935       it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
  8936     }
  8937   else
  8938     /* Display table entry is invalid.  Return a space.  */
  8939     it->c = ' ', it->len = 1;
  8940 
  8941   /* Don't change position and object of the iterator here.  They are
  8942      still the values of the character that had this display table
  8943      entry or was translated, and that's what we want.  */
  8944   it->what = IT_CHARACTER;
  8945   return true;
  8946 }
  8947 
  8948 /* Get the first element of string/buffer in the visual order, after
  8949    being reseated to a new position in a string or a buffer.  */
  8950 static void
  8951 get_visually_first_element (struct it *it)
  8952 {
  8953   bool string_p = STRINGP (it->string) || it->s;
  8954   ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
  8955   ptrdiff_t bob;
  8956   ptrdiff_t obegv = BEGV;
  8957 
  8958   SET_WITH_NARROWED_BEGV (it, bob,
  8959                           string_p ? 0 :
  8960                           IT_CHARPOS (*it) < BEGV ? obegv : BEGV,
  8961                           it->medium_narrowing_begv);
  8962 
  8963   if (STRINGP (it->string))
  8964     {
  8965       it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
  8966       it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
  8967     }
  8968   else
  8969     {
  8970       it->bidi_it.charpos = IT_CHARPOS (*it);
  8971       it->bidi_it.bytepos = IT_BYTEPOS (*it);
  8972     }
  8973 
  8974   if (it->bidi_it.charpos == eob)
  8975     {
  8976       /* Nothing to do, but reset the FIRST_ELT flag, like
  8977          bidi_paragraph_init does, because we are not going to
  8978          call it.  */
  8979       it->bidi_it.first_elt = false;
  8980     }
  8981   else if (it->bidi_it.charpos == bob
  8982            || (!string_p
  8983                && (FETCH_BYTE (it->bidi_it.bytepos - 1) == '\n'
  8984                    || FETCH_BYTE (it->bidi_it.bytepos) == '\n')))
  8985     {
  8986       /* If we are at the beginning of a line/string, we can produce
  8987          the next element right away.  */
  8988       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  8989       bidi_move_to_visually_next (&it->bidi_it);
  8990     }
  8991   else
  8992     {
  8993       ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
  8994 
  8995       /* We need to prime the bidi iterator starting at the line's or
  8996          string's beginning, before we will be able to produce the
  8997          next element.  */
  8998       if (string_p)
  8999         it->bidi_it.charpos = it->bidi_it.bytepos = 0;
  9000       else
  9001         SET_WITH_NARROWED_BEGV (it, it->bidi_it.charpos,
  9002                                 find_newline_no_quit (IT_CHARPOS (*it),
  9003                                                       IT_BYTEPOS (*it), -1,
  9004                                                       &it->bidi_it.bytepos),
  9005                                 it->medium_narrowing_begv);
  9006       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  9007       do
  9008         {
  9009           /* Now return to buffer/string position where we were asked
  9010              to get the next display element, and produce that.  */
  9011           bidi_move_to_visually_next (&it->bidi_it);
  9012         }
  9013       while (it->bidi_it.bytepos != orig_bytepos
  9014              && it->bidi_it.charpos < eob);
  9015     }
  9016 
  9017   /*  Adjust IT's position information to where we ended up.  */
  9018   if (STRINGP (it->string))
  9019     {
  9020       IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  9021       IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  9022     }
  9023   else
  9024     {
  9025       IT_CHARPOS (*it) = it->bidi_it.charpos;
  9026       IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  9027     }
  9028 
  9029   if (STRINGP (it->string) || !it->s)
  9030     {
  9031       ptrdiff_t stop, charpos, bytepos;
  9032 
  9033       if (STRINGP (it->string))
  9034         {
  9035           eassert (!it->s);
  9036           stop = SCHARS (it->string);
  9037           if (stop > it->end_charpos)
  9038             stop = it->end_charpos;
  9039           charpos = IT_STRING_CHARPOS (*it);
  9040           bytepos = IT_STRING_BYTEPOS (*it);
  9041         }
  9042       else
  9043         {
  9044           stop = it->end_charpos;
  9045           charpos = IT_CHARPOS (*it);
  9046           bytepos = IT_BYTEPOS (*it);
  9047         }
  9048       if (it->bidi_it.scan_dir < 0)
  9049         stop = -1;
  9050       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
  9051                                     it->string);
  9052     }
  9053 }
  9054 
  9055 /* Load IT with the next display element from Lisp string IT->string.
  9056    IT->current.string_pos is the current position within the string.
  9057    If IT->current.overlay_string_index >= 0, the Lisp string is an
  9058    overlay string.  */
  9059 
  9060 static bool
  9061 next_element_from_string (struct it *it)
  9062 {
  9063   struct text_pos position;
  9064 
  9065   eassert (STRINGP (it->string));
  9066   eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
  9067   eassert (IT_STRING_CHARPOS (*it) >= 0);
  9068   position = it->current.string_pos;
  9069 
  9070   /* With bidi reordering, the character to display might not be the
  9071      character at IT_STRING_CHARPOS.  BIDI_IT.FIRST_ELT means
  9072      that we were reseat()ed to a new string, whose paragraph
  9073      direction is not known.  */
  9074   if (it->bidi_p && it->bidi_it.first_elt)
  9075     {
  9076       get_visually_first_element (it);
  9077       SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
  9078     }
  9079 
  9080   /* Time to check for invisible text?  */
  9081   if (IT_STRING_CHARPOS (*it) < it->end_charpos)
  9082     {
  9083       if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
  9084         {
  9085           if (!(!it->bidi_p
  9086                 || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9087                 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
  9088             {
  9089               /* With bidi non-linear iteration, we could find
  9090                  ourselves far beyond the last computed stop_charpos,
  9091                  with several other stop positions in between that we
  9092                  missed.  Scan them all now, in buffer's logical
  9093                  order, until we find and handle the last stop_charpos
  9094                  that precedes our current position.  */
  9095               handle_stop_backwards (it, it->stop_charpos);
  9096               return GET_NEXT_DISPLAY_ELEMENT (it);
  9097             }
  9098           else
  9099             {
  9100               if (it->bidi_p)
  9101                 {
  9102                   /* Take note of the stop position we just moved
  9103                      across, for when we will move back across it.  */
  9104                   it->prev_stop = it->stop_charpos;
  9105                   /* If we are at base paragraph embedding level, take
  9106                      note of the last stop position seen at this
  9107                      level.  */
  9108                   if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9109                     it->base_level_stop = it->stop_charpos;
  9110                 }
  9111               handle_stop (it);
  9112 
  9113               /* Since a handler may have changed IT->method, we must
  9114                  recurse here.  */
  9115               return GET_NEXT_DISPLAY_ELEMENT (it);
  9116             }
  9117         }
  9118       else if (it->bidi_p
  9119                /* If we are before prev_stop, we may have overstepped
  9120                   on our way backwards a stop_pos, and if so, we need
  9121                   to handle that stop_pos.  */
  9122                && IT_STRING_CHARPOS (*it) < it->prev_stop
  9123                /* We can sometimes back up for reasons that have nothing
  9124                   to do with bidi reordering.  E.g., compositions.  The
  9125                   code below is only needed when we are above the base
  9126                   embedding level, so test for that explicitly.  */
  9127                && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9128         {
  9129           /* If we lost track of base_level_stop, we have no better
  9130              place for handle_stop_backwards to start from than string
  9131              beginning.  This happens, e.g., when we were reseated to
  9132              the previous screenful of text by vertical-motion.  */
  9133           if (it->base_level_stop <= 0
  9134               || IT_STRING_CHARPOS (*it) < it->base_level_stop)
  9135             it->base_level_stop = 0;
  9136           handle_stop_backwards (it, it->base_level_stop);
  9137           return GET_NEXT_DISPLAY_ELEMENT (it);
  9138         }
  9139     }
  9140 
  9141   if (it->current.overlay_string_index >= 0)
  9142     {
  9143       /* Get the next character from an overlay string.  In overlay
  9144          strings, there is no field width or padding with spaces to
  9145          do.  */
  9146       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  9147         {
  9148           it->what = IT_EOB;
  9149           return false;
  9150         }
  9151       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9152                                 IT_STRING_BYTEPOS (*it),
  9153                                 it->bidi_it.scan_dir < 0
  9154                                 ? -1
  9155                                 : SCHARS (it->string))
  9156                && next_element_from_composition (it))
  9157         {
  9158           return true;
  9159         }
  9160       else if (STRING_MULTIBYTE (it->string))
  9161         {
  9162           const unsigned char *s = (SDATA (it->string)
  9163                                     + IT_STRING_BYTEPOS (*it));
  9164           it->c = check_char_and_length (s, &it->len);
  9165         }
  9166       else
  9167         {
  9168           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9169           it->len = 1;
  9170         }
  9171     }
  9172   else
  9173     {
  9174       /* Get the next character from a Lisp string that is not an
  9175          overlay string.  Such strings come from the mode line, for
  9176          example.  We may have to pad with spaces, or truncate the
  9177          string.  See also next_element_from_c_string.  */
  9178       if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  9179         {
  9180           it->what = IT_EOB;
  9181           return false;
  9182         }
  9183       else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
  9184         {
  9185           /* Pad with spaces.  */
  9186           it->c = ' ', it->len = 1;
  9187           CHARPOS (position) = BYTEPOS (position) = -1;
  9188         }
  9189       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9190                                 IT_STRING_BYTEPOS (*it),
  9191                                 it->bidi_it.scan_dir < 0
  9192                                 ? -1
  9193                                 : it->string_nchars)
  9194                && next_element_from_composition (it))
  9195         {
  9196           return true;
  9197         }
  9198       else if (STRING_MULTIBYTE (it->string))
  9199         {
  9200           const unsigned char *s = (SDATA (it->string)
  9201                                     + IT_STRING_BYTEPOS (*it));
  9202           it->c = check_char_and_length (s, &it->len);
  9203         }
  9204       else
  9205         {
  9206           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9207           it->len = 1;
  9208         }
  9209     }
  9210 
  9211   /* Record what we have and where it came from.  */
  9212   it->what = IT_CHARACTER;
  9213   it->object = it->string;
  9214   it->position = position;
  9215   return true;
  9216 }
  9217 
  9218 
  9219 /* Load IT with next display element from C string IT->s.
  9220    IT->string_nchars is the maximum number of characters to return
  9221    from the string.  IT->end_charpos may be greater than
  9222    IT->string_nchars when this function is called, in which case we
  9223    may have to return padding spaces.  Value is false if end of string
  9224    reached, including padding spaces.  */
  9225 
  9226 static bool
  9227 next_element_from_c_string (struct it *it)
  9228 {
  9229   bool success_p = true;
  9230 
  9231   eassert (it->s);
  9232   eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
  9233   it->what = IT_CHARACTER;
  9234   BYTEPOS (it->position) = CHARPOS (it->position) = 0;
  9235   it->object = make_fixnum (0);
  9236 
  9237   /* With bidi reordering, the character to display might not be the
  9238      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9239      we were reseated to a new string, whose paragraph direction is
  9240      not known.  */
  9241   if (it->bidi_p && it->bidi_it.first_elt)
  9242     get_visually_first_element (it);
  9243 
  9244   /* IT's position can be greater than IT->string_nchars in case a
  9245      field width or precision has been specified when the iterator was
  9246      initialized.  */
  9247   if (IT_CHARPOS (*it) >= it->end_charpos)
  9248     {
  9249       /* End of the game.  */
  9250       it->what = IT_EOB;
  9251       success_p = false;
  9252     }
  9253   else if (IT_CHARPOS (*it) >= it->string_nchars)
  9254     {
  9255       /* Pad with spaces.  */
  9256       it->c = ' ', it->len = 1;
  9257       BYTEPOS (it->position) = CHARPOS (it->position) = -1;
  9258     }
  9259   else if (it->multibyte_p)
  9260     it->c = check_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
  9261   else
  9262     it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
  9263 
  9264   return success_p;
  9265 }
  9266 
  9267 
  9268 /* Set up IT to return characters from an ellipsis, if appropriate.
  9269    The definition of the ellipsis glyphs may come from a display table
  9270    entry.  This function fills IT with the first glyph from the
  9271    ellipsis if an ellipsis is to be displayed.  */
  9272 
  9273 static bool
  9274 next_element_from_ellipsis (struct it *it)
  9275 {
  9276   if (it->selective_display_ellipsis_p)
  9277     setup_for_ellipsis (it, it->len);
  9278   else
  9279     {
  9280       /* The face at the current position may be different from the
  9281          face we find after the invisible text.  Remember what it
  9282          was in IT->saved_face_id, and signal that it's there by
  9283          setting face_before_selective_p.  */
  9284       it->saved_face_id = it->face_id;
  9285       it->method = GET_FROM_BUFFER;
  9286       it->object = it->w->contents;
  9287       reseat_at_next_visible_line_start (it, true);
  9288       it->face_before_selective_p = true;
  9289     }
  9290 
  9291   return GET_NEXT_DISPLAY_ELEMENT (it);
  9292 }
  9293 
  9294 
  9295 /* Deliver an image display element.  The iterator IT is already
  9296    filled with image information (done in handle_display_prop).  Value
  9297    is always true.  */
  9298 
  9299 
  9300 static bool
  9301 next_element_from_image (struct it *it)
  9302 {
  9303   it->what = IT_IMAGE;
  9304   return true;
  9305 }
  9306 
  9307 static bool
  9308 next_element_from_xwidget (struct it *it)
  9309 {
  9310   it->what = IT_XWIDGET;
  9311   return true;
  9312 }
  9313 
  9314 
  9315 /* Fill iterator IT with next display element from a stretch glyph
  9316    property.  IT->object is the value of the text property.  Value is
  9317    always true.  */
  9318 
  9319 static bool
  9320 next_element_from_stretch (struct it *it)
  9321 {
  9322   it->what = IT_STRETCH;
  9323   return true;
  9324 }
  9325 
  9326 /* Scan backwards from IT's current position until we find a stop
  9327    position, or until BEGV.  This is called when we find ourself
  9328    before both the last known prev_stop and base_level_stop while
  9329    reordering bidirectional text.  */
  9330 
  9331 static void
  9332 compute_stop_pos_backwards (struct it *it)
  9333 {
  9334   const int SCAN_BACK_LIMIT = 1000;
  9335   struct text_pos pos;
  9336   struct display_pos save_current = it->current;
  9337   struct text_pos save_position = it->position;
  9338   ptrdiff_t charpos = IT_CHARPOS (*it);
  9339   ptrdiff_t where_we_are = charpos;
  9340   ptrdiff_t save_stop_pos = it->stop_charpos;
  9341   ptrdiff_t save_end_pos = it->end_charpos;
  9342 
  9343   eassert (NILP (it->string) && !it->s);
  9344   eassert (it->bidi_p);
  9345   it->bidi_p = false;
  9346   do
  9347     {
  9348       it->end_charpos = min (charpos + 1, ZV);
  9349       charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
  9350       SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
  9351       reseat_1 (it, pos, false);
  9352       compute_stop_pos (it);
  9353       /* We must advance forward, right?  */
  9354       if (it->stop_charpos <= charpos)
  9355         emacs_abort ();
  9356     }
  9357   while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
  9358 
  9359   if (it->stop_charpos <= where_we_are)
  9360     it->prev_stop = it->stop_charpos;
  9361   else
  9362     it->prev_stop = BEGV;
  9363   it->bidi_p = true;
  9364   it->current = save_current;
  9365   it->position = save_position;
  9366   it->stop_charpos = save_stop_pos;
  9367   it->end_charpos = save_end_pos;
  9368 }
  9369 
  9370 /* Scan forward from CHARPOS in the current buffer/string, until we
  9371    find a stop position > current IT's position.  Then handle the stop
  9372    position before that.  This is called when we bump into a stop
  9373    position while reordering bidirectional text.  CHARPOS should be
  9374    the last previously processed stop_pos (or BEGV/0, if none were
  9375    processed yet) whose position is less than IT's current
  9376    position.  */
  9377 
  9378 static void
  9379 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
  9380 {
  9381   bool bufp = !STRINGP (it->string);
  9382   ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
  9383   struct display_pos save_current = it->current;
  9384   struct text_pos save_position = it->position;
  9385   struct composition_it save_cmp_it = it->cmp_it;
  9386   struct text_pos pos1;
  9387   ptrdiff_t next_stop;
  9388 
  9389   /* Scan in strict logical order.  */
  9390   eassert (it->bidi_p);
  9391   it->bidi_p = false;
  9392   do
  9393     {
  9394       it->prev_stop = charpos;
  9395       if (bufp)
  9396         {
  9397           SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
  9398           reseat_1 (it, pos1, false);
  9399         }
  9400       else
  9401         it->current.string_pos = string_pos (charpos, it->string);
  9402       compute_stop_pos (it);
  9403       /* We must advance forward, right?  */
  9404       if (it->stop_charpos <= it->prev_stop)
  9405         emacs_abort ();
  9406       charpos = it->stop_charpos;
  9407     }
  9408   while (charpos <= where_we_are);
  9409 
  9410   it->bidi_p = true;
  9411   it->current = save_current;
  9412   it->position = save_position;
  9413   it->cmp_it = save_cmp_it;
  9414   next_stop = it->stop_charpos;
  9415   it->stop_charpos = it->prev_stop;
  9416   handle_stop (it);
  9417   it->stop_charpos = next_stop;
  9418 }
  9419 
  9420 /* Load IT with the next display element from current_buffer.  Value
  9421    is false if end of buffer reached.  IT->stop_charpos is the next
  9422    position at which to stop and check for text properties or buffer
  9423    end.  */
  9424 
  9425 static bool
  9426 next_element_from_buffer (struct it *it)
  9427 {
  9428   bool success_p = true;
  9429 
  9430   eassert (IT_CHARPOS (*it) >= BEGV);
  9431   eassert (NILP (it->string) && !it->s);
  9432   eassert (!it->bidi_p
  9433            || (NILP (it->bidi_it.string.lstring)
  9434                && it->bidi_it.string.s == NULL));
  9435 
  9436   /* With bidi reordering, the character to display might not be the
  9437      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9438      we were reseat()ed to a new buffer position, which is potentially
  9439      a different paragraph.  */
  9440   if (it->bidi_p && it->bidi_it.first_elt)
  9441     {
  9442       get_visually_first_element (it);
  9443       SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9444     }
  9445 
  9446   if (IT_CHARPOS (*it) >= it->stop_charpos)
  9447     {
  9448       if (IT_CHARPOS (*it) >= it->end_charpos)
  9449         {
  9450           bool overlay_strings_follow_p;
  9451 
  9452           /* End of the game, except when overlay strings follow that
  9453              haven't been returned yet.  */
  9454           if (it->overlay_strings_at_end_processed_p)
  9455             overlay_strings_follow_p = false;
  9456           else
  9457             {
  9458               it->overlay_strings_at_end_processed_p = true;
  9459               overlay_strings_follow_p = get_overlay_strings (it, 0);
  9460             }
  9461 
  9462           if (overlay_strings_follow_p)
  9463             success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  9464           else
  9465             {
  9466               it->what = IT_EOB;
  9467               it->position = it->current.pos;
  9468               success_p = false;
  9469             }
  9470         }
  9471       else if (!(!it->bidi_p
  9472                  || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9473                  || IT_CHARPOS (*it) == it->stop_charpos))
  9474         {
  9475           /* With bidi non-linear iteration, we could find ourselves
  9476              far beyond the last computed stop_charpos, with several
  9477              other stop positions in between that we missed.  Scan
  9478              them all now, in buffer's logical order, until we find
  9479              and handle the last stop_charpos that precedes our
  9480              current position.  */
  9481           handle_stop_backwards (it, it->stop_charpos);
  9482           it->ignore_overlay_strings_at_pos_p = false;
  9483           return GET_NEXT_DISPLAY_ELEMENT (it);
  9484         }
  9485       else
  9486         {
  9487           if (it->bidi_p)
  9488             {
  9489               /* Take note of the stop position we just moved across,
  9490                  for when we will move back across it.  */
  9491               it->prev_stop = it->stop_charpos;
  9492               /* If we are at base paragraph embedding level, take
  9493                  note of the last stop position seen at this
  9494                  level.  */
  9495               if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9496                 it->base_level_stop = it->stop_charpos;
  9497             }
  9498           handle_stop (it);
  9499           it->ignore_overlay_strings_at_pos_p = false;
  9500           return GET_NEXT_DISPLAY_ELEMENT (it);
  9501         }
  9502     }
  9503   else if (it->bidi_p
  9504            /* If we are before prev_stop, we may have overstepped on
  9505               our way backwards a stop_pos, and if so, we need to
  9506               handle that stop_pos.  */
  9507            && IT_CHARPOS (*it) < it->prev_stop
  9508            /* We can sometimes back up for reasons that have nothing
  9509               to do with bidi reordering.  E.g., compositions.  The
  9510               code below is only needed when we are above the base
  9511               embedding level, so test for that explicitly.  */
  9512            && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9513     {
  9514       if (it->base_level_stop <= 0
  9515           || IT_CHARPOS (*it) < it->base_level_stop)
  9516         {
  9517           /* If we lost track of base_level_stop, we need to find
  9518              prev_stop by looking backwards.  This happens, e.g., when
  9519              we were reseated to the previous screenful of text by
  9520              vertical-motion.  */
  9521           it->base_level_stop = BEGV;
  9522           compute_stop_pos_backwards (it);
  9523           handle_stop_backwards (it, it->prev_stop);
  9524         }
  9525       else
  9526         handle_stop_backwards (it, it->base_level_stop);
  9527       it->ignore_overlay_strings_at_pos_p = false;
  9528       return GET_NEXT_DISPLAY_ELEMENT (it);
  9529     }
  9530   else
  9531     {
  9532       /* No face changes, overlays etc. in sight, so just return a
  9533          character from current_buffer.  */
  9534       unsigned char *p;
  9535       ptrdiff_t stop;
  9536 
  9537       /* We moved to the next buffer position, so any info about
  9538          previously seen overlays is no longer valid.  */
  9539       it->ignore_overlay_strings_at_pos_p = false;
  9540 
  9541       if (composition_break_at_point
  9542           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  9543           && !NILP (Vauto_composition_mode))
  9544         {
  9545           /* Limit search for composable characters to point's position.  */
  9546           if (it->bidi_it.scan_dir < 0)
  9547             stop = (PT <= IT_CHARPOS (*it)) ? PT : -1;
  9548           else
  9549             stop = (IT_CHARPOS (*it) < PT
  9550                     && PT < it->end_charpos) ? PT : it->end_charpos;
  9551         }
  9552       else
  9553         stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
  9554       if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
  9555                            stop)
  9556           && next_element_from_composition (it))
  9557         {
  9558           return true;
  9559         }
  9560 
  9561       /* Get the next character, maybe multibyte.  */
  9562       p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
  9563       if (it->multibyte_p && !ASCII_CHAR_P (*p))
  9564         it->c = string_char_and_length (p, &it->len);
  9565       else
  9566         it->c = *p, it->len = 1;
  9567 
  9568       /* Record what we have and where it came from.  */
  9569       it->what = IT_CHARACTER;
  9570       it->object = it->w->contents;
  9571       it->position = it->current.pos;
  9572 
  9573       /* Normally we return the character found above, except when we
  9574          really want to return an ellipsis for selective display.  */
  9575       if (it->selective)
  9576         {
  9577           if (it->c == '\n')
  9578             {
  9579               /* A value of selective > 0 means hide lines indented more
  9580                  than that number of columns.  */
  9581               if (it->selective > 0
  9582                   && IT_CHARPOS (*it) + 1 < ZV
  9583                   && indented_beyond_p (IT_CHARPOS (*it) + 1,
  9584                                         IT_BYTEPOS (*it) + 1,
  9585                                         it->selective))
  9586                 {
  9587                   success_p = next_element_from_ellipsis (it);
  9588                   it->dpvec_char_len = -1;
  9589                 }
  9590             }
  9591           else if (it->c == '\r' && it->selective == -1)
  9592             {
  9593               /* A value of selective == -1 means that everything from the
  9594                  CR to the end of the line is invisible, with maybe an
  9595                  ellipsis displayed for it.  */
  9596               success_p = next_element_from_ellipsis (it);
  9597               it->dpvec_char_len = -1;
  9598             }
  9599         }
  9600     }
  9601 
  9602   /* Value is false if end of buffer reached.  */
  9603   eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
  9604   return success_p;
  9605 }
  9606 
  9607 
  9608 /* Deliver a composition display element.  Unlike the other
  9609    next_element_from_XXX, this function is not registered in the array
  9610    get_next_element[].  It is called from next_element_from_buffer and
  9611    next_element_from_string when necessary.  */
  9612 
  9613 static bool
  9614 next_element_from_composition (struct it *it)
  9615 {
  9616   it->what = IT_COMPOSITION;
  9617   it->len = it->cmp_it.nbytes;
  9618   if (STRINGP (it->string))
  9619     {
  9620       if (it->c < 0)
  9621         {
  9622           IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  9623           IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  9624           return false;
  9625         }
  9626       it->position = it->current.string_pos;
  9627       it->object = it->string;
  9628       it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
  9629                                      IT_STRING_BYTEPOS (*it), it->string);
  9630     }
  9631   else
  9632     {
  9633       if (it->c < 0)
  9634         {
  9635           IT_CHARPOS (*it) += it->cmp_it.nchars;
  9636           IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  9637           if (it->bidi_p)
  9638             {
  9639               if (it->bidi_it.new_paragraph)
  9640                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  9641                                      false);
  9642               /* Resync the bidi iterator with IT's new position.
  9643                  FIXME: this doesn't support bidirectional text.  */
  9644               while (it->bidi_it.charpos < IT_CHARPOS (*it))
  9645                 bidi_move_to_visually_next (&it->bidi_it);
  9646             }
  9647           return false;
  9648         }
  9649       it->position = it->current.pos;
  9650       it->object = it->w->contents;
  9651       it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
  9652                                      IT_BYTEPOS (*it), Qnil);
  9653     }
  9654   return true;
  9655 }
  9656 
  9657 
  9658 
  9659 /***********************************************************************
  9660              Moving an iterator without producing glyphs
  9661  ***********************************************************************/
  9662 
  9663 /* Check if iterator is at a position corresponding to a valid buffer
  9664    position after some move_it_ call.  */
  9665 
  9666 #define IT_POS_VALID_AFTER_MOVE_P(it)                   \
  9667   ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
  9668 
  9669 
  9670 /* Move iterator IT to a specified buffer or X position within one
  9671    line on the display without producing glyphs.
  9672 
  9673    OP should be a bit mask including some or all of these bits:
  9674     MOVE_TO_X: Stop upon reaching x-position TO_X.
  9675     MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
  9676    Regardless of OP's value, stop upon reaching the end of the display line.
  9677 
  9678    TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
  9679    This means, in particular, that TO_X includes window's horizontal
  9680    scroll amount.
  9681 
  9682    The return value has several possible values that
  9683    say what condition caused the scan to stop:
  9684 
  9685    MOVE_POS_MATCH_OR_ZV
  9686      - when TO_POS or ZV was reached.
  9687 
  9688    MOVE_X_REACHED
  9689      -when TO_X was reached before TO_POS or ZV were reached.
  9690 
  9691    MOVE_LINE_CONTINUED
  9692      - when we reached the end of the display area and the line must
  9693      be continued.
  9694 
  9695    MOVE_LINE_TRUNCATED
  9696      - when we reached the end of the display area and the line is
  9697      truncated.
  9698 
  9699    MOVE_NEWLINE_OR_CR
  9700      - when we stopped at a line end, i.e. a newline or a CR and selective
  9701      display is on.  */
  9702 
  9703 static enum move_it_result
  9704 move_it_in_display_line_to (struct it *it,
  9705                             ptrdiff_t to_charpos, int to_x,
  9706                             enum move_operation_enum op)
  9707 {
  9708   enum move_it_result result = MOVE_UNDEFINED;
  9709   struct glyph_row *saved_glyph_row;
  9710   struct it wrap_it, atpos_it, atx_it, ppos_it;
  9711   void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
  9712   void *ppos_data = NULL;
  9713   bool may_wrap = false;
  9714   enum it_method prev_method = it->method;
  9715   ptrdiff_t closest_pos UNINIT;
  9716   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  9717   bool saw_smaller_pos = prev_pos < to_charpos;
  9718   bool line_number_pending = false;
  9719 
  9720   /* Don't produce glyphs in produce_glyphs.  */
  9721   saved_glyph_row = it->glyph_row;
  9722   it->glyph_row = NULL;
  9723 
  9724   /* Use wrap_it to save a copy of IT wherever a word wrap could
  9725      occur.  Use atpos_it to save a copy of IT at the desired buffer
  9726      position, if found, so that we can scan ahead and check if the
  9727      word later overshoots the window edge.  Use atx_it similarly, for
  9728      pixel positions.  */
  9729   wrap_it.sp = -1;
  9730   atpos_it.sp = -1;
  9731   atx_it.sp = -1;
  9732 
  9733   /* Use ppos_it under bidi reordering to save a copy of IT for the
  9734      initial position.  We restore that position in IT when we have
  9735      scanned the entire display line without finding a match for
  9736      TO_CHARPOS and all the character positions are greater than
  9737      TO_CHARPOS.  We then restart the scan from the initial position,
  9738      and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
  9739      the closest to TO_CHARPOS.  */
  9740   if (it->bidi_p)
  9741     {
  9742       if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
  9743         {
  9744           SAVE_IT (ppos_it, *it, ppos_data);
  9745           closest_pos = IT_CHARPOS (*it);
  9746         }
  9747       else
  9748         closest_pos = ZV;
  9749     }
  9750 
  9751 #define BUFFER_POS_REACHED_P()                                  \
  9752   ((op & MOVE_TO_POS) != 0                                      \
  9753    && BUFFERP (it->object)                                      \
  9754    && (IT_CHARPOS (*it) == to_charpos                           \
  9755        || ((!it->bidi_p                                         \
  9756             || BIDI_AT_BASE_LEVEL (it->bidi_it))                \
  9757            && IT_CHARPOS (*it) > to_charpos)                    \
  9758        || (it->what == IT_COMPOSITION                           \
  9759            && ((IT_CHARPOS (*it) > to_charpos                   \
  9760                 && to_charpos >= it->cmp_it.charpos)            \
  9761                || (IT_CHARPOS (*it) < to_charpos                \
  9762                    && to_charpos <= it->cmp_it.charpos))))      \
  9763    && (it->method == GET_FROM_BUFFER                            \
  9764        || (it->method == GET_FROM_DISPLAY_VECTOR                \
  9765            && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
  9766 
  9767   if (it->hpos == 0)
  9768     {
  9769       /* If line numbers are being displayed, produce a line number.
  9770          But don't do that if we are to reach first_visible_x, because
  9771          line numbers are not relevant to stuff that is not visible on
  9772          display.  */
  9773       if (!((op && MOVE_TO_X) && to_x == it->first_visible_x)
  9774           && should_produce_line_number (it))
  9775         {
  9776           if (it->current_x == it->first_visible_x)
  9777             maybe_produce_line_number (it);
  9778           else
  9779             line_number_pending = true;
  9780         }
  9781       /* If there's a line-/wrap-prefix, handle it, if we didn't already.  */
  9782       if (it->area == TEXT_AREA && !it->string_from_prefix_prop_p)
  9783         handle_line_prefix (it);
  9784     }
  9785 
  9786   if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9787     SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9788 
  9789   while (true)
  9790     {
  9791       int x, i, ascent = 0, descent = 0;
  9792 
  9793 /* Utility macro to reset an iterator with x, ascent, and descent.  */
  9794 #define IT_RESET_X_ASCENT_DESCENT(IT)                   \
  9795   ((IT)->current_x = x, (IT)->max_ascent = ascent,      \
  9796    (IT)->max_descent = descent)
  9797 
  9798       /* Stop if we move beyond TO_CHARPOS (after an image or a
  9799          display string or stretch glyph).  */
  9800       if ((op & MOVE_TO_POS) != 0
  9801           && BUFFERP (it->object)
  9802           && it->method == GET_FROM_BUFFER
  9803           && (((!it->bidi_p
  9804                 /* When the iterator is at base embedding level, we
  9805                    are guaranteed that characters are delivered for
  9806                    display in strictly increasing order of their
  9807                    buffer positions.  */
  9808                 || BIDI_AT_BASE_LEVEL (it->bidi_it))
  9809                && IT_CHARPOS (*it) > to_charpos)
  9810               || (it->bidi_p
  9811                   && (prev_method == GET_FROM_IMAGE
  9812                       || prev_method == GET_FROM_STRETCH
  9813                       || prev_method == GET_FROM_STRING)
  9814                   /* Passed TO_CHARPOS from left to right.  */
  9815                   && ((prev_pos < to_charpos
  9816                        && IT_CHARPOS (*it) >= to_charpos)
  9817                       /* Passed TO_CHARPOS from right to left.  */
  9818                       || (prev_pos > to_charpos
  9819                           && IT_CHARPOS (*it) <= to_charpos)))))
  9820         {
  9821           if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9822             {
  9823               result = MOVE_POS_MATCH_OR_ZV;
  9824               break;
  9825             }
  9826           else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
  9827             /* If wrap_it is valid, the current position might be in a
  9828                word that is wrapped.  So, save the iterator in
  9829                atpos_it and continue to see if wrapping happens.  */
  9830             SAVE_IT (atpos_it, *it, atpos_data);
  9831         }
  9832 
  9833       /* Stop when ZV reached.
  9834          We used to stop here when TO_CHARPOS reached as well, but that is
  9835          too soon if this glyph does not fit on this line.  So we handle it
  9836          explicitly below.  */
  9837       if (!get_next_display_element (it))
  9838         {
  9839           result = MOVE_POS_MATCH_OR_ZV;
  9840           break;
  9841         }
  9842 
  9843       if (it->line_wrap == TRUNCATE)
  9844         {
  9845           /* If it->pixel_width is zero, the last PRODUCE_GLYPHS call
  9846              produced something that doesn't consume any screen estate
  9847              in the text area, so we don't want to exit the loop at
  9848              TO_CHARPOS, before we produce the glyph for that buffer
  9849              position.  This happens, e.g., when there's an overlay at
  9850              TO_CHARPOS that draws a fringe bitmap.  */
  9851           if (BUFFER_POS_REACHED_P ()
  9852               && (it->pixel_width > 0
  9853                   || IT_CHARPOS (*it) > to_charpos
  9854                   || it->area != TEXT_AREA))
  9855             {
  9856               result = MOVE_POS_MATCH_OR_ZV;
  9857               break;
  9858             }
  9859         }
  9860       else
  9861         {
  9862           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
  9863             {
  9864               bool next_may_wrap = may_wrap;
  9865               /* Can we wrap after this character?  */
  9866               if (char_can_wrap_after (it))
  9867                 next_may_wrap = true;
  9868               else
  9869                 next_may_wrap = false;
  9870               /* Can we wrap here? */
  9871               if (may_wrap && char_can_wrap_before (it))
  9872                 {
  9873                   /* We have reached a glyph that follows one or more
  9874                      whitespace characters or characters that allow
  9875                      wrapping after them.  If this character allows
  9876                      wrapping before it, save this position as a
  9877                      wrapping point.  */
  9878                   if (atpos_it.sp >= 0)
  9879                     {
  9880                       RESTORE_IT (it, &atpos_it, atpos_data);
  9881                       result = MOVE_POS_MATCH_OR_ZV;
  9882                       goto done;
  9883                     }
  9884                   if (atx_it.sp >= 0)
  9885                     {
  9886                       RESTORE_IT (it, &atx_it, atx_data);
  9887                       result = MOVE_X_REACHED;
  9888                       goto done;
  9889                     }
  9890                   /* Otherwise, we can wrap here.  */
  9891                   SAVE_IT (wrap_it, *it, wrap_data);
  9892                 }
  9893               /* Update may_wrap for the next iteration.  */
  9894               may_wrap = next_may_wrap;
  9895             }
  9896         }
  9897 
  9898       /* Remember the line height for the current line, in case
  9899          the next element doesn't fit on the line.  */
  9900       ascent = it->max_ascent;
  9901       descent = it->max_descent;
  9902 
  9903       /* The call to produce_glyphs will get the metrics of the
  9904          display element IT is loaded with.  Record the x-position
  9905          before this display element, in case it doesn't fit on the
  9906          line.  */
  9907       x = it->current_x;
  9908 
  9909       PRODUCE_GLYPHS (it);
  9910 
  9911       if (it->area != TEXT_AREA)
  9912         {
  9913           prev_method = it->method;
  9914           if (it->method == GET_FROM_BUFFER)
  9915             prev_pos = IT_CHARPOS (*it);
  9916           set_iterator_to_next (it, true);
  9917           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9918             SET_TEXT_POS (this_line_min_pos,
  9919                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9920           if (it->bidi_p
  9921               && (op & MOVE_TO_POS)
  9922               && IT_CHARPOS (*it) > to_charpos
  9923               && IT_CHARPOS (*it) < closest_pos)
  9924             closest_pos = IT_CHARPOS (*it);
  9925           continue;
  9926         }
  9927 
  9928       /* The number of glyphs we get back in IT->nglyphs will normally
  9929          be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
  9930          character on a terminal frame, or (iii) a line end.  For the
  9931          second case, IT->nglyphs - 1 padding glyphs will be present.
  9932          (On X frames, there is only one glyph produced for a
  9933          composite character.)
  9934 
  9935          The behavior implemented below means, for continuation lines,
  9936          that as many spaces of a TAB as fit on the current line are
  9937          displayed there.  For terminal frames, as many glyphs of a
  9938          multi-glyph character are displayed in the current line, too.
  9939          This is what the old redisplay code did, and we keep it that
  9940          way.  Under X, the whole shape of a complex character must
  9941          fit on the line or it will be completely displayed in the
  9942          next line.
  9943 
  9944          Note that both for tabs and padding glyphs, all glyphs have
  9945          the same width.  */
  9946       if (it->nglyphs)
  9947         {
  9948           /* More than one glyph or glyph doesn't fit on line.  All
  9949              glyphs have the same width.  */
  9950           int single_glyph_width = it->pixel_width / it->nglyphs;
  9951           int new_x;
  9952           int x_before_this_char = x;
  9953           int hpos_before_this_char = it->hpos;
  9954 
  9955           for (i = 0; i < it->nglyphs; ++i, x = new_x)
  9956             {
  9957               new_x = x + single_glyph_width;
  9958 
  9959               /* We want to leave anything reaching TO_X to the caller.  */
  9960               if ((op & MOVE_TO_X) && new_x > to_x)
  9961                 {
  9962                   if (BUFFER_POS_REACHED_P ())
  9963                     {
  9964                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9965                         goto buffer_pos_reached;
  9966                       if (atpos_it.sp < 0)
  9967                         {
  9968                           SAVE_IT (atpos_it, *it, atpos_data);
  9969                           IT_RESET_X_ASCENT_DESCENT (&atpos_it);
  9970                         }
  9971                     }
  9972                   else
  9973                     {
  9974                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9975                         {
  9976                           it->current_x = x;
  9977                           result = MOVE_X_REACHED;
  9978                           break;
  9979                         }
  9980                       if (atx_it.sp < 0)
  9981                         {
  9982                           SAVE_IT (atx_it, *it, atx_data);
  9983                           IT_RESET_X_ASCENT_DESCENT (&atx_it);
  9984                         }
  9985                     }
  9986                 }
  9987 
  9988               if (/* Lines are continued.  */
  9989                   it->line_wrap != TRUNCATE
  9990                   && (/* And glyph doesn't fit on the line.  */
  9991                       new_x > it->last_visible_x
  9992                       /* Or it fits exactly and we're on a window
  9993                          system frame.  */
  9994                       || (new_x == it->last_visible_x
  9995                           && FRAME_WINDOW_P (it->f)
  9996                           && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  9997                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
  9998                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
  9999                 {
 10000                   bool moved_forward = false;
 10001 
 10002                   if (/* IT->hpos == 0 means the very first glyph
 10003                          doesn't fit on the line, e.g. a wide image.  */
 10004                       it->hpos == 0
 10005                       || (new_x == it->last_visible_x
 10006                           && FRAME_WINDOW_P (it->f)))
 10007                     {
 10008                       ++it->hpos;
 10009                       it->current_x = new_x;
 10010 
 10011                       /* The character's last glyph just barely fits
 10012                          in this row.  */
 10013                       if (i == it->nglyphs - 1)
 10014                         {
 10015                           /* If this is the destination position,
 10016                              return a position *before* it in this row,
 10017                              now that we know it fits in this row.  */
 10018                           if (BUFFER_POS_REACHED_P ())
 10019                             {
 10020                               bool can_wrap = true;
 10021 
 10022                               /* If the previous character says we can
 10023                                  wrap after it, but the current
 10024                                  character says we can't wrap before
 10025                                  it, then we can't wrap here.  */
 10026                               if (it->line_wrap == WORD_WRAP
 10027                                   && wrap_it.sp >= 0
 10028                                   && may_wrap
 10029                                   && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10030                                 {
 10031                                   struct it tem_it;
 10032                                   void *tem_data = NULL;
 10033 
 10034                                   SAVE_IT (tem_it, *it, tem_data);
 10035                                   set_iterator_to_next (it, true);
 10036                                   if (get_next_display_element (it)
 10037                                       && !char_can_wrap_before (it))
 10038                                     can_wrap = false;
 10039                                   RESTORE_IT (it, &tem_it, tem_data);
 10040                                 }
 10041                               if (it->line_wrap != WORD_WRAP
 10042                                   || wrap_it.sp < 0
 10043                                   /* If we've just found whitespace
 10044                                      where we can wrap, effectively
 10045                                      ignore the previous wrap point --
 10046                                      it is no longer relevant, but we
 10047                                      won't have an opportunity to
 10048                                      update it, since we've reached
 10049                                      the edge of this screen line.  */
 10050                                   || (may_wrap && can_wrap
 10051                                       && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10052                                 {
 10053                                   it->hpos = hpos_before_this_char;
 10054                                   it->current_x = x_before_this_char;
 10055                                   result = MOVE_POS_MATCH_OR_ZV;
 10056                                   break;
 10057                                 }
 10058                               if (it->line_wrap == WORD_WRAP
 10059                                   && atpos_it.sp < 0)
 10060                                 {
 10061                                   SAVE_IT (atpos_it, *it, atpos_data);
 10062                                   atpos_it.current_x = x_before_this_char;
 10063                                   atpos_it.hpos = hpos_before_this_char;
 10064                                 }
 10065                             }
 10066 
 10067                           prev_method = it->method;
 10068                           if (it->method == GET_FROM_BUFFER)
 10069                             prev_pos = IT_CHARPOS (*it);
 10070                           set_iterator_to_next (it, true);
 10071                           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10072                             SET_TEXT_POS (this_line_min_pos,
 10073                                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10074                           /* On graphical terminals, newlines may
 10075                              "overflow" into the fringe if
 10076                              overflow-newline-into-fringe is non-nil.
 10077                              On text terminals, and on graphical
 10078                              terminals with no right margin, newlines
 10079                              may overflow into the last glyph on the
 10080                              display line.*/
 10081                           if (!FRAME_WINDOW_P (it->f)
 10082                               || ((it->bidi_p
 10083                                    && it->bidi_it.paragraph_dir == R2L)
 10084                                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10085                                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10086                               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10087                             {
 10088                               if (!get_next_display_element (it))
 10089                                 {
 10090                                   result = MOVE_POS_MATCH_OR_ZV;
 10091                                   break;
 10092                                 }
 10093                               moved_forward = true;
 10094                               if (BUFFER_POS_REACHED_P ())
 10095                                 {
 10096                                   if (ITERATOR_AT_END_OF_LINE_P (it))
 10097                                     result = MOVE_POS_MATCH_OR_ZV;
 10098                                   else
 10099                                     result = MOVE_LINE_CONTINUED;
 10100                                   break;
 10101                                 }
 10102                               if (ITERATOR_AT_END_OF_LINE_P (it)
 10103                                   && (it->line_wrap != WORD_WRAP
 10104                                       || wrap_it.sp < 0
 10105                                       || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10106                                 {
 10107                                   result = MOVE_NEWLINE_OR_CR;
 10108                                   break;
 10109                                 }
 10110                             }
 10111                         }
 10112                     }
 10113                   else
 10114                     IT_RESET_X_ASCENT_DESCENT (it);
 10115 
 10116                   /* If the screen line ends with whitespace (or
 10117                      wrap-able character), and we are under word-wrap,
 10118                      don't use wrap_it: it is no longer relevant, but
 10119                      we won't have an opportunity to update it, since
 10120                      we are done with this screen line.  */
 10121                   if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 10122                       /* If the character after the one which set the
 10123                          may_wrap flag says we can't wrap before it,
 10124                          we can't wrap here.  Therefore, wrap_it
 10125                          (previously found wrap-point) _is_ relevant
 10126                          in that case.  */
 10127                       && (!moved_forward || char_can_wrap_before (it)))
 10128                     {
 10129                       /* If we've found TO_X, go back there, as we now
 10130                          know the last word fits on this screen line.  */
 10131                       if ((op & MOVE_TO_X) && new_x == it->last_visible_x
 10132                           && atx_it.sp >= 0)
 10133                         {
 10134                           RESTORE_IT (it, &atx_it, atx_data);
 10135                           atpos_it.sp = -1;
 10136                           atx_it.sp = -1;
 10137                           result = MOVE_X_REACHED;
 10138                           break;
 10139                         }
 10140                     }
 10141                   else if (wrap_it.sp >= 0)
 10142                     {
 10143                       RESTORE_IT (it, &wrap_it, wrap_data);
 10144                       atpos_it.sp = -1;
 10145                       atx_it.sp = -1;
 10146                     }
 10147 
 10148                   move_trace ("move_it_in: continued at %td\n",
 10149                               IT_CHARPOS (*it));
 10150                   result = MOVE_LINE_CONTINUED;
 10151                   break;
 10152                 }
 10153 
 10154               if (BUFFER_POS_REACHED_P ())
 10155                 {
 10156                   if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
 10157                     goto buffer_pos_reached;
 10158                   if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
 10159                     {
 10160                       SAVE_IT (atpos_it, *it, atpos_data);
 10161                       IT_RESET_X_ASCENT_DESCENT (&atpos_it);
 10162                     }
 10163                 }
 10164 
 10165               if (new_x > it->first_visible_x)
 10166                 {
 10167                   /* If we have reached the visible portion of the
 10168                      screen line, produce the line number if needed.  */
 10169                   if (line_number_pending)
 10170                     {
 10171                       line_number_pending = false;
 10172                       it->current_x = it->first_visible_x;
 10173                       maybe_produce_line_number (it);
 10174                       it->current_x += new_x - it->first_visible_x;
 10175                     }
 10176                   /* Glyph is visible.  Increment number of glyphs that
 10177                      would be displayed.  */
 10178                   ++it->hpos;
 10179                 }
 10180             }
 10181 
 10182           if (result != MOVE_UNDEFINED)
 10183             break;
 10184         }
 10185       else if (BUFFER_POS_REACHED_P ())
 10186         {
 10187         buffer_pos_reached:
 10188           IT_RESET_X_ASCENT_DESCENT (it);
 10189           result = MOVE_POS_MATCH_OR_ZV;
 10190           break;
 10191         }
 10192       else if ((op & MOVE_TO_X) && it->current_x >= to_x)
 10193         {
 10194           /* Stop when TO_X specified and reached.  This check is
 10195              necessary here because of lines consisting of a line end,
 10196              only.  The line end will not produce any glyphs and we
 10197              would never get MOVE_X_REACHED.  */
 10198           eassert (it->nglyphs == 0);
 10199           result = MOVE_X_REACHED;
 10200           break;
 10201         }
 10202 
 10203       /* Is this a line end?  If yes, we're done.  */
 10204       if (ITERATOR_AT_END_OF_LINE_P (it))
 10205         {
 10206           /* If we are past TO_CHARPOS, but never saw any character
 10207              positions smaller than TO_CHARPOS, return
 10208              MOVE_POS_MATCH_OR_ZV, like the unidirectional display
 10209              did.  */
 10210           if (it->bidi_p && (op & MOVE_TO_POS) != 0)
 10211             {
 10212               if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
 10213                 {
 10214                   if (closest_pos < ZV)
 10215                     {
 10216                       RESTORE_IT (it, &ppos_it, ppos_data);
 10217                       /* Don't recurse if closest_pos is equal to
 10218                          to_charpos, since we have just tried that.  */
 10219                       if (closest_pos != to_charpos)
 10220                         move_it_in_display_line_to (it, closest_pos, -1,
 10221                                                     MOVE_TO_POS);
 10222                       result = MOVE_POS_MATCH_OR_ZV;
 10223                     }
 10224                   else
 10225                     goto buffer_pos_reached;
 10226                 }
 10227               else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
 10228                        && IT_CHARPOS (*it) > to_charpos)
 10229                 goto buffer_pos_reached;
 10230               else
 10231                 result = MOVE_NEWLINE_OR_CR;
 10232             }
 10233           else
 10234             result = MOVE_NEWLINE_OR_CR;
 10235           /* If lines are truncated, and the line we moved across is
 10236              completely hscrolled out of view, reset the line metrics
 10237              to those of the newline we've just processed, so that
 10238              glyphs not on display don't affect the line's height.  */
 10239           if (it->line_wrap == TRUNCATE
 10240               && it->current_x <= it->first_visible_x
 10241               && result == MOVE_NEWLINE_OR_CR
 10242               && it->char_to_display == '\n')
 10243             {
 10244               it->max_ascent = it->ascent;
 10245               it->max_descent = it->descent;
 10246             }
 10247           /* If we've processed the newline, make sure this flag is
 10248              reset, as it must only be set when the newline itself is
 10249              processed.  */
 10250           if (result == MOVE_NEWLINE_OR_CR)
 10251             it->constrain_row_ascent_descent_p = false;
 10252           break;
 10253         }
 10254 
 10255       prev_method = it->method;
 10256       if (it->method == GET_FROM_BUFFER)
 10257         prev_pos = IT_CHARPOS (*it);
 10258 
 10259       /* Detect overly-wide wrap-prefixes made of (space ...) display
 10260          properties.  When such a wrap prefix reaches past the right
 10261          margin of the window, we need to avoid the call to
 10262          set_iterator_to_next below, so that it->line_wrap is left at
 10263          its TRUNCATE value wisely set by handle_line_prefix.
 10264          Otherwise, set_iterator_to_next will pop the iterator stack,
 10265          restore it->line_wrap, and we might miss the opportunity to
 10266          exit the loop and return.  */
 10267       bool overwide_wrap_prefix =
 10268         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 10269         && it->sp > 0 && it->method == GET_FROM_STRETCH
 10270         && it->current_x >= it->last_visible_x
 10271         && it->continuation_lines_width > 0
 10272         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 10273       /* The current display element has been consumed.  Advance
 10274          to the next.  */
 10275       if (!overwide_wrap_prefix)
 10276         set_iterator_to_next (it, true);
 10277       if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10278         SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10279       if (IT_CHARPOS (*it) < to_charpos)
 10280         saw_smaller_pos = true;
 10281       if (it->bidi_p
 10282           && (op & MOVE_TO_POS)
 10283           && IT_CHARPOS (*it) >= to_charpos
 10284           && IT_CHARPOS (*it) < closest_pos)
 10285         closest_pos = IT_CHARPOS (*it);
 10286 
 10287       /* Stop if lines are truncated and IT's current x-position is
 10288          past the right edge of the window now.  */
 10289       if (it->line_wrap == TRUNCATE
 10290           && it->current_x >= it->last_visible_x)
 10291         {
 10292           if (!FRAME_WINDOW_P (it->f)
 10293               || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 10294                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10295                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10296               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10297             {
 10298               bool at_eob_p = false;
 10299 
 10300               if ((at_eob_p = !get_next_display_element (it))
 10301                   || BUFFER_POS_REACHED_P ()
 10302                   /* If we are past TO_CHARPOS, but never saw any
 10303                      character positions smaller than TO_CHARPOS,
 10304                      return MOVE_POS_MATCH_OR_ZV, like the
 10305                      unidirectional display did.  */
 10306                   || (it->bidi_p && (op & MOVE_TO_POS) != 0
 10307                       && !saw_smaller_pos
 10308                       && IT_CHARPOS (*it) > to_charpos))
 10309                 {
 10310                   if (it->bidi_p
 10311                       && !BUFFER_POS_REACHED_P ()
 10312                       && !at_eob_p && closest_pos < ZV)
 10313                     {
 10314                       RESTORE_IT (it, &ppos_it, ppos_data);
 10315                       if (closest_pos != to_charpos)
 10316                         move_it_in_display_line_to (it, closest_pos, -1,
 10317                                                     MOVE_TO_POS);
 10318                     }
 10319                   result = MOVE_POS_MATCH_OR_ZV;
 10320                   break;
 10321                 }
 10322               if (ITERATOR_AT_END_OF_LINE_P (it))
 10323                 {
 10324                   result = MOVE_NEWLINE_OR_CR;
 10325                   break;
 10326                 }
 10327             }
 10328           else if (it->bidi_p && (op & MOVE_TO_POS) != 0
 10329                    && !saw_smaller_pos
 10330                    && IT_CHARPOS (*it) > to_charpos)
 10331             {
 10332               if (closest_pos < ZV)
 10333                 {
 10334                   RESTORE_IT (it, &ppos_it, ppos_data);
 10335                   if (closest_pos != to_charpos)
 10336                     move_it_in_display_line_to (it, closest_pos, -1,
 10337                                                 MOVE_TO_POS);
 10338                 }
 10339               result = MOVE_POS_MATCH_OR_ZV;
 10340               break;
 10341             }
 10342           result = MOVE_LINE_TRUNCATED;
 10343           break;
 10344         }
 10345 #undef IT_RESET_X_ASCENT_DESCENT
 10346     }
 10347 
 10348 #undef BUFFER_POS_REACHED_P
 10349 
 10350   /* If we scanned beyond TO_POS, restore the saved iterator either to
 10351      the wrap point (if found), or to atpos/atx location.  We decide which
 10352      data to use to restore the saved iterator state by their X coordinates,
 10353      since buffer positions might increase non-monotonically with screen
 10354      coordinates due to bidi reordering.  */
 10355   if (result == MOVE_LINE_CONTINUED
 10356       && it->line_wrap == WORD_WRAP
 10357       && wrap_it.sp >= 0
 10358       && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
 10359           || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
 10360     RESTORE_IT (it, &wrap_it, wrap_data);
 10361   else if (atpos_it.sp >= 0)
 10362     RESTORE_IT (it, &atpos_it, atpos_data);
 10363   else if (atx_it.sp >= 0)
 10364     RESTORE_IT (it, &atx_it, atx_data);
 10365 
 10366  done:
 10367 
 10368   if (atpos_data)
 10369     bidi_unshelve_cache (atpos_data, true);
 10370   if (atx_data)
 10371     bidi_unshelve_cache (atx_data, true);
 10372   if (wrap_data)
 10373     bidi_unshelve_cache (wrap_data, true);
 10374   if (ppos_data)
 10375     bidi_unshelve_cache (ppos_data, true);
 10376 
 10377   /* Restore the iterator settings altered at the beginning of this
 10378      function.  */
 10379   it->glyph_row = saved_glyph_row;
 10380   return result;
 10381 }
 10382 
 10383 /* For external use.  */
 10384 void
 10385 move_it_in_display_line (struct it *it,
 10386                          ptrdiff_t to_charpos, int to_x,
 10387                          enum move_operation_enum op)
 10388 {
 10389   if (it->line_wrap == WORD_WRAP
 10390       && (op & MOVE_TO_X))
 10391     {
 10392       struct it save_it;
 10393       void *save_data = NULL;
 10394       int skip;
 10395 
 10396       SAVE_IT (save_it, *it, save_data);
 10397       skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10398       /* When word-wrap is on, TO_X may lie past the end
 10399          of a wrapped line.  Then it->current is the
 10400          character on the next line, so backtrack to the
 10401          space before the wrap point.  */
 10402       if (skip == MOVE_LINE_CONTINUED)
 10403         {
 10404           int prev_x = max (it->current_x - 1, 0);
 10405           RESTORE_IT (it, &save_it, save_data);
 10406           move_it_in_display_line_to
 10407             (it, -1, prev_x, MOVE_TO_X);
 10408         }
 10409       else
 10410         bidi_unshelve_cache (save_data, true);
 10411     }
 10412   else
 10413     move_it_in_display_line_to (it, to_charpos, to_x, op);
 10414 }
 10415 
 10416 
 10417 /* Move IT forward until it satisfies one or more of the criteria in
 10418    TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
 10419 
 10420    OP is a bit-mask that specifies where to stop, and in particular,
 10421    which of those four position arguments makes a difference.  See the
 10422    description of enum move_operation_enum.
 10423 
 10424    If TO_CHARPOS is in invisible text, e.g. a truncated part of a
 10425    screen line, this function will set IT to the next position that is
 10426    displayed to the right of TO_CHARPOS on the screen.
 10427 
 10428    Return the maximum pixel length of any line scanned but never more
 10429    than it.last_visible_x.  */
 10430 
 10431 int
 10432 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
 10433 {
 10434   enum move_it_result skip, skip2 = MOVE_X_REACHED;
 10435   int line_height, line_start_x = 0, reached = 0;
 10436   int max_current_x = 0;
 10437   void *backup_data = NULL;
 10438   ptrdiff_t orig_charpos = -1;
 10439   enum it_method orig_method = NUM_IT_METHODS;
 10440 
 10441   for (;;)
 10442     {
 10443       orig_charpos = IT_CHARPOS (*it);
 10444       orig_method = it->method;
 10445       if (op & MOVE_TO_VPOS)
 10446         {
 10447           /* If no TO_CHARPOS and no TO_X specified, stop at the
 10448              start of the line TO_VPOS.  */
 10449           if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
 10450             {
 10451               if (it->vpos == to_vpos)
 10452                 {
 10453                   reached = 1;
 10454                   break;
 10455                 }
 10456               else
 10457                 skip = move_it_in_display_line_to (it, -1, -1, 0);
 10458             }
 10459           else
 10460             {
 10461               /* TO_VPOS >= 0 means stop at TO_X in the line at
 10462                  TO_VPOS, or at TO_POS, whichever comes first.  */
 10463               if (it->vpos == to_vpos)
 10464                 {
 10465                   reached = 2;
 10466                   break;
 10467                 }
 10468 
 10469               skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10470 
 10471               if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
 10472                 {
 10473                   reached = 3;
 10474                   break;
 10475                 }
 10476               else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
 10477                 {
 10478                   /* We have reached TO_X but not in the line we want.  */
 10479                   skip = move_it_in_display_line_to (it, to_charpos,
 10480                                                      -1, MOVE_TO_POS);
 10481                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10482                     {
 10483                       reached = 4;
 10484                       break;
 10485                     }
 10486                 }
 10487             }
 10488         }
 10489       else if (op & MOVE_TO_Y)
 10490         {
 10491           struct it it_backup;
 10492 
 10493           if (it->line_wrap == WORD_WRAP)
 10494             SAVE_IT (it_backup, *it, backup_data);
 10495 
 10496           /* TO_Y specified means stop at TO_X in the line containing
 10497              TO_Y---or at TO_CHARPOS if this is reached first.  The
 10498              problem is that we can't really tell whether the line
 10499              contains TO_Y before we have completely scanned it, and
 10500              this may skip past TO_X.  What we do is to first scan to
 10501              TO_X.
 10502 
 10503              If TO_X is not specified, use a TO_X of zero.  The reason
 10504              is to make the outcome of this function more predictable.
 10505              If we didn't use TO_X == 0, we would stop at the end of
 10506              the line which is probably not what a caller would expect
 10507              to happen.  */
 10508           skip = move_it_in_display_line_to
 10509             (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
 10510              (MOVE_TO_X | (op & MOVE_TO_POS)));
 10511 
 10512           /* If TO_CHARPOS is reached or ZV, we don't have to do more.  */
 10513           if (skip == MOVE_POS_MATCH_OR_ZV)
 10514             reached = 5;
 10515           else if (skip == MOVE_X_REACHED)
 10516             {
 10517               /* If TO_X was reached, we want to know whether TO_Y is
 10518                  in the line.  We know this is the case if the already
 10519                  scanned glyphs make the line tall enough.  Otherwise,
 10520                  we must check by scanning the rest of the line.  */
 10521               line_height = it->max_ascent + it->max_descent;
 10522               if (to_y >= it->current_y
 10523                   && to_y < it->current_y + line_height)
 10524                 {
 10525                   reached = 6;
 10526                   break;
 10527                 }
 10528               SAVE_IT (it_backup, *it, backup_data);
 10529               move_trace ("move_it: from %td\n", IT_CHARPOS (*it));
 10530               skip2 = move_it_in_display_line_to (it, to_charpos, -1,
 10531                                                   op & MOVE_TO_POS);
 10532               move_trace ("move_it: to %td\n", IT_CHARPOS (*it));
 10533               line_height = it->max_ascent + it->max_descent;
 10534               move_trace ("move_it: line_height = %d\n", line_height);
 10535 
 10536               if (to_y >= it->current_y
 10537                   && to_y < it->current_y + line_height)
 10538                 {
 10539                   /* If TO_Y is in this line and TO_X was reached
 10540                      above, we scanned too far.  We have to restore
 10541                      IT's settings to the ones before skipping.  But
 10542                      keep the more accurate values of max_ascent and
 10543                      max_descent we've found while skipping the rest
 10544                      of the line, for the sake of callers, such as
 10545                      pos_visible_p, that need to know the line
 10546                      height.  */
 10547                   int max_ascent = it->max_ascent;
 10548                   int max_descent = it->max_descent;
 10549 
 10550                   RESTORE_IT (it, &it_backup, backup_data);
 10551                   it->max_ascent = max_ascent;
 10552                   it->max_descent = max_descent;
 10553                   reached = 6;
 10554                 }
 10555               else
 10556                 {
 10557                   skip = skip2;
 10558                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10559                     {
 10560                       reached = 7;
 10561                       /* If the last move_it_in_display_line_to call
 10562                          took us away from TO_CHARPOS, back up to the
 10563                          previous position, as it is a better
 10564                          approximation of TO_CHARPOS.  (Note that we
 10565                          could have both positions after TO_CHARPOS or
 10566                          both positions before it, due to bidi
 10567                          reordering.)  */
 10568                       if (to_charpos > 0
 10569                           && IT_CHARPOS (*it) != to_charpos
 10570                           && ((IT_CHARPOS (it_backup) > to_charpos)
 10571                               == (IT_CHARPOS (*it) > to_charpos)))
 10572                         {
 10573                           int max_ascent = it->max_ascent;
 10574                           int max_descent = it->max_descent;
 10575 
 10576                           RESTORE_IT (it, &it_backup, backup_data);
 10577                           it->max_ascent = max_ascent;
 10578                           it->max_descent = max_descent;
 10579                         }
 10580                     }
 10581                 }
 10582             }
 10583           else
 10584             {
 10585               /* Check whether TO_Y is in this line.  */
 10586               line_height = it->max_ascent + it->max_descent;
 10587               move_trace ("move_it: line_height = %d\n", line_height);
 10588 
 10589               if (to_y >= it->current_y
 10590                   && to_y < it->current_y + line_height)
 10591                 {
 10592                   if (to_y > it->current_y)
 10593                     max_current_x = max (it->current_x, max_current_x);
 10594 
 10595                   /* When word-wrap is on, TO_X may lie past the end
 10596                      of a wrapped line.  Then it->current is the
 10597                      character on the next line, so backtrack to the
 10598                      space before the wrap point.  */
 10599                   if (skip == MOVE_LINE_CONTINUED
 10600                       && it->line_wrap == WORD_WRAP)
 10601                     {
 10602                       int prev_x = max (it->current_x - 1, 0);
 10603                       RESTORE_IT (it, &it_backup, backup_data);
 10604                       skip = move_it_in_display_line_to
 10605                         (it, -1, prev_x, MOVE_TO_X);
 10606                     }
 10607 
 10608                   reached = 6;
 10609                 }
 10610             }
 10611 
 10612           if (reached)
 10613             {
 10614               max_current_x = max (it->current_x, max_current_x);
 10615               break;
 10616             }
 10617         }
 10618       else if (BUFFERP (it->object)
 10619                && (it->method == GET_FROM_BUFFER
 10620                    || it->method == GET_FROM_STRETCH)
 10621                && IT_CHARPOS (*it) >= to_charpos
 10622                /* Under bidi iteration, a call to set_iterator_to_next
 10623                   can scan far beyond to_charpos if the initial
 10624                   portion of the next line needs to be reordered.  In
 10625                   that case, give move_it_in_display_line_to another
 10626                   chance below.  */
 10627                && !(it->bidi_p
 10628                     && it->bidi_it.scan_dir == -1))
 10629         skip = MOVE_POS_MATCH_OR_ZV;
 10630       else
 10631         skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
 10632 
 10633       switch (skip)
 10634         {
 10635         case MOVE_POS_MATCH_OR_ZV:
 10636           max_current_x = max (it->current_x, max_current_x);
 10637           reached = 8;
 10638           goto out;
 10639 
 10640         case MOVE_NEWLINE_OR_CR:
 10641           max_current_x = max (it->current_x, max_current_x);
 10642           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10643             it->override_ascent = -1;
 10644           set_iterator_to_next (it, true);
 10645           it->continuation_lines_width = 0;
 10646           break;
 10647 
 10648         case MOVE_LINE_TRUNCATED:
 10649           max_current_x = it->last_visible_x;
 10650           it->continuation_lines_width = 0;
 10651           reseat_at_next_visible_line_start (it, false);
 10652           if ((op & MOVE_TO_POS) != 0
 10653               && (IT_CHARPOS (*it) > to_charpos
 10654                   || (IT_CHARPOS (*it) == to_charpos
 10655                       /* Consider TO_CHARPOS as REACHED if we are at
 10656                          EOB that ends in something other than a newline.  */
 10657                       && to_charpos == ZV
 10658                       && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n')
 10659                       /* But if we have a display or an overlay string
 10660                          at EOB, keep going until we exhaust all the
 10661                          characters of the string(s).  */
 10662                       && (it->sp == 0
 10663                           || (STRINGP (it->string)
 10664                               && (it->current.overlay_string_index < 0
 10665                                   || (it->current.overlay_string_index >= 0
 10666                                       && it->current.overlay_string_index
 10667                                          >= it->n_overlay_strings - 1))
 10668                               && IT_STRING_CHARPOS (*it) >= it->end_charpos)))))
 10669             {
 10670               reached = 9;
 10671               goto out;
 10672             }
 10673           break;
 10674 
 10675         case MOVE_LINE_CONTINUED:
 10676           max_current_x = it->last_visible_x;
 10677           /* For continued lines ending in a tab, some of the glyphs
 10678              associated with the tab are displayed on the current
 10679              line.  Since it->current_x does not include these glyphs,
 10680              we use it->last_visible_x instead.  */
 10681           if (it->c == '\t')
 10682             {
 10683               it->continuation_lines_width += it->last_visible_x;
 10684               /* When moving by vpos, ensure that the iterator really
 10685                  advances to the next line (bug#847, bug#969).  Fixme:
 10686                  do we need to do this in other circumstances?  */
 10687               if (it->current_x != it->last_visible_x
 10688                   && (op & MOVE_TO_VPOS)
 10689                   && !(op & (MOVE_TO_X | MOVE_TO_POS)))
 10690                 {
 10691                   line_start_x = it->current_x + it->pixel_width
 10692                     - it->last_visible_x;
 10693                   if (FRAME_WINDOW_P (it->f))
 10694                     {
 10695                       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 10696                       struct font *face_font = face->font;
 10697 
 10698                       /* When display_line produces a continued line
 10699                          that ends in a TAB, it skips a tab stop that
 10700                          is closer than the font's space character
 10701                          width (see gui_produce_glyphs where it produces
 10702                          the stretch glyph which represents a TAB).
 10703                          We need to reproduce the same logic here.  */
 10704                       eassert (face_font);
 10705                       if (face_font)
 10706                         {
 10707                           if (line_start_x < face_font->space_width)
 10708                             line_start_x
 10709                               += it->tab_width * face_font->space_width;
 10710                         }
 10711                     }
 10712                   set_iterator_to_next (it, false);
 10713                 }
 10714             }
 10715           else
 10716             {
 10717               /* Make sure we do advance, otherwise we might infloop.
 10718                  This could happen when the first display element is
 10719                  wider than the window, or if we have a wrap-prefix
 10720                  that doesn't leave enough space after it to display
 10721                  even a single character.  We only do this for moving
 10722                  through buffer text, as with display/overlay strings
 10723                  we'd need to also compare it->object's, and this is
 10724                  unlikely to happen in that case anyway.  */
 10725               if (IT_CHARPOS (*it) == orig_charpos
 10726                   && it->method == orig_method
 10727                   && orig_method == GET_FROM_BUFFER)
 10728                 set_iterator_to_next (it, false);
 10729               it->continuation_lines_width += it->current_x;
 10730             }
 10731           break;
 10732 
 10733         default:
 10734           emacs_abort ();
 10735         }
 10736 
 10737       /* Reset/increment for the next run.  */
 10738       it->current_x = line_start_x;
 10739       line_start_x = 0;
 10740       it->hpos = 0;
 10741       it->line_number_produced_p = false;
 10742       it->current_y += it->max_ascent + it->max_descent;
 10743       ++it->vpos;
 10744       last_height = it->max_ascent + it->max_descent;
 10745       it->max_ascent = it->max_descent = 0;
 10746     }
 10747 
 10748  out:
 10749 
 10750   /* On text terminals, we may stop at the end of a line in the middle
 10751      of a multi-character glyph.  If the glyph itself is continued,
 10752      i.e. it is actually displayed on the next line, don't treat this
 10753      stopping point as valid; move to the next line instead (unless
 10754      that brings us offscreen).  */
 10755   if (!FRAME_WINDOW_P (it->f)
 10756       && op & MOVE_TO_POS
 10757       && IT_CHARPOS (*it) == to_charpos
 10758       && it->what == IT_CHARACTER
 10759       && it->nglyphs > 1
 10760       && it->line_wrap == WINDOW_WRAP
 10761       && it->current_x == it->last_visible_x - 1
 10762       && it->c != '\n'
 10763       && it->c != '\t'
 10764       && it->w->window_end_valid
 10765       && it->vpos < it->w->window_end_vpos)
 10766     {
 10767       it->continuation_lines_width += it->current_x;
 10768       it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
 10769       it->current_y += it->max_ascent + it->max_descent;
 10770       ++it->vpos;
 10771       last_height = it->max_ascent + it->max_descent;
 10772     }
 10773 
 10774   if (backup_data)
 10775     bidi_unshelve_cache (backup_data, true);
 10776 
 10777   move_trace ("move_it_to: reached %d\n", reached);
 10778 
 10779   return max_current_x;
 10780 }
 10781 
 10782 
 10783 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
 10784 
 10785    If DY > 0, move IT backward that many pixels.
 10786    DY = 0 means move IT backward to the preceding line start or to BEGV.
 10787    This function may move over less or more than DY pixels if
 10788    IT->current_y - DY ends up in the middle of a line; in this case
 10789    IT->current_y will be set to the top of the line either before or
 10790    after the exact pixel coordinate.  */
 10791 
 10792 void
 10793 move_it_vertically_backward (struct it *it, int dy)
 10794 {
 10795   int nlines, h;
 10796   struct it it2, it3;
 10797   void *it2data = NULL, *it3data = NULL;
 10798   ptrdiff_t start_pos;
 10799   int nchars_per_row
 10800     = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 10801   ptrdiff_t pos_limit;
 10802 
 10803  move_further_back:
 10804   eassert (dy >= 0);
 10805 
 10806   start_pos = IT_CHARPOS (*it);
 10807 
 10808   /* Estimate how many newlines we must move back.  */
 10809   nlines = max (1, dy / default_line_pixel_height (it->w));
 10810   if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 10811     pos_limit = BEGV;
 10812   else
 10813     pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
 10814 
 10815   /* Set the iterator's position that many lines back.  But don't go
 10816      back more than NLINES full screen lines -- this wins a day with
 10817      buffers which have very long lines.  */
 10818   while (nlines-- && IT_CHARPOS (*it) > pos_limit)
 10819     back_to_previous_visible_line_start (it);
 10820 
 10821   /* Reseat the iterator here.  When moving backward, we don't want
 10822      reseat to skip forward over invisible text, set up the iterator
 10823      to deliver from overlay strings at the new position etc.  So,
 10824      use reseat_1 here.  */
 10825   reseat_1 (it, it->current.pos, true);
 10826 
 10827   /* We are now surely at a line start.  */
 10828   it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
 10829                                    reordering is in effect.  */
 10830   it->continuation_lines_width = 0;
 10831 
 10832   /* Move forward and see what y-distance we moved.  First move to the
 10833      start of the next line so that we get its height.  We need this
 10834      height to be able to tell whether we reached the specified
 10835      y-distance.  */
 10836   SAVE_IT (it2, *it, it2data);
 10837   it2.max_ascent = it2.max_descent = 0;
 10838   do
 10839     {
 10840       move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
 10841                   MOVE_TO_POS | MOVE_TO_VPOS);
 10842     }
 10843   while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
 10844            /* If we are in a display string which starts at START_POS,
 10845               and that display string includes a newline, and we are
 10846               right after that newline (i.e. at the beginning of a
 10847               display line), exit the loop, because otherwise we will
 10848               infloop, since move_it_to will see that it is already at
 10849               START_POS and will not move.  */
 10850            || (it2.method == GET_FROM_STRING
 10851                && IT_CHARPOS (it2) == start_pos
 10852                && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
 10853   eassert (IT_CHARPOS (*it) >= BEGV);
 10854   SAVE_IT (it3, it2, it3data);
 10855 
 10856   move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
 10857   eassert (IT_CHARPOS (*it) >= BEGV);
 10858   /* H is the actual vertical distance from the position in *IT
 10859      and the starting position.  */
 10860   h = it2.current_y - it->current_y;
 10861   /* NLINES is the distance in number of lines.  */
 10862   nlines = it2.vpos - it->vpos;
 10863 
 10864   /* Correct IT's y and vpos position
 10865      so that they are relative to the starting point.  */
 10866   it->vpos -= nlines;
 10867   it->current_y -= h;
 10868 
 10869   if (dy == 0)
 10870     {
 10871       /* DY == 0 means move to the start of the screen line.  The
 10872          value of nlines is > 0 if continuation lines were involved,
 10873          or if the original IT position was at start of a line.  */
 10874       RESTORE_IT (it, it, it2data);
 10875       if (nlines > 0)
 10876         move_it_by_lines (it, nlines);
 10877       /* The above code moves us to some position NLINES down,
 10878          usually to its first glyph (leftmost in an L2R line), but
 10879          that's not necessarily the start of the line, under bidi
 10880          reordering.  We want to get to the character position
 10881          that is immediately after the newline of the previous
 10882          line.  */
 10883       if (it->bidi_p
 10884           && !it->continuation_lines_width
 10885           && !STRINGP (it->string)
 10886           && IT_CHARPOS (*it) > BEGV
 10887           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 10888         {
 10889           ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
 10890 
 10891           dec_both (&cp, &bp);
 10892           SET_WITH_NARROWED_BEGV (it, cp,
 10893                                   find_newline_no_quit (cp, bp, -1, NULL),
 10894                                   get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
 10895           move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
 10896         }
 10897       bidi_unshelve_cache (it3data, true);
 10898     }
 10899   else
 10900     {
 10901       /* The y-position we try to reach, relative to *IT.
 10902          Note that H has been subtracted in front of the if-statement.  */
 10903       int target_y = it->current_y + h - dy;
 10904       int y0 = it3.current_y;
 10905       int y1;
 10906       int line_height;
 10907 
 10908       RESTORE_IT (&it3, &it3, it3data);
 10909       y1 = line_bottom_y (&it3);
 10910       line_height = y1 - y0;
 10911       RESTORE_IT (it, it, it2data);
 10912       /* If we did not reach target_y, try to move further backward if
 10913          we can.  If we moved too far backward, try to move forward.  */
 10914       if (target_y < it->current_y
 10915           /* This is heuristic.  In a window that's 3 lines high, with
 10916              a line height of 13 pixels each, recentering with point
 10917              on the bottom line will try to move -39/2 = 19 pixels
 10918              backward.  Try to avoid moving into the first line.  */
 10919           && (it->current_y - target_y
 10920               > min (window_box_height (it->w), line_height * 2 / 3))
 10921           && IT_CHARPOS (*it) > BEGV)
 10922         {
 10923           move_trace ("  not far enough -> move_vert %d\n",
 10924                       target_y - it->current_y);
 10925           dy = it->current_y - target_y;
 10926           goto move_further_back;
 10927         }
 10928       else if (target_y >= it->current_y + line_height
 10929                && IT_CHARPOS (*it) < ZV)
 10930         {
 10931           /* Should move forward by at least one line, maybe more.
 10932 
 10933              Note: Calling move_it_by_lines can be expensive on
 10934              terminal frames, where compute_motion is used (via
 10935              vmotion) to do the job, when there are very long lines
 10936              and truncate-lines is nil.  That's the reason for
 10937              treating terminal frames specially here.  */
 10938 
 10939           if (!FRAME_WINDOW_P (it->f))
 10940             move_it_vertically (it, target_y - it->current_y);
 10941           else
 10942             {
 10943               struct text_pos last_pos;
 10944               int last_y, last_vpos;
 10945               do
 10946                 {
 10947                   last_pos = it->current.pos;
 10948                   last_y = it->current_y;
 10949                   last_vpos = it->vpos;
 10950                   move_it_by_lines (it, 1);
 10951                 }
 10952               while (target_y > it->current_y && IT_CHARPOS (*it) < ZV);
 10953               if (it->current_y > target_y)
 10954                 {
 10955                   reseat (it, last_pos, true);
 10956                   it->current_y = last_y;
 10957                   it->vpos = last_vpos;
 10958                 }
 10959             }
 10960         }
 10961     }
 10962 }
 10963 
 10964 
 10965 /* Move IT by a specified amount of pixel lines DY.  DY negative means
 10966    move backwards.  DY = 0 means move to start of screen line.  At the
 10967    end, IT will be on the start of a screen line.  */
 10968 
 10969 void
 10970 move_it_vertically (struct it *it, int dy)
 10971 {
 10972   if (dy <= 0)
 10973     move_it_vertically_backward (it, -dy);
 10974   else
 10975     {
 10976       move_trace ("move_it_v: from %td, %d\n", IT_CHARPOS (*it), dy);
 10977       move_it_to (it, ZV, -1, it->current_y + dy, -1,
 10978                   MOVE_TO_POS | MOVE_TO_Y);
 10979       move_trace ("move_it_v: to %td\n", IT_CHARPOS (*it));
 10980 
 10981       /* If buffer ends in ZV without a newline, move to the start of
 10982          the line to satisfy the post-condition.  */
 10983       if (IT_CHARPOS (*it) == ZV
 10984           && ZV > BEGV
 10985           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 10986         move_it_by_lines (it, 0);
 10987     }
 10988 }
 10989 
 10990 
 10991 /* Move iterator IT past the end of the text line it is in.  */
 10992 
 10993 void
 10994 move_it_past_eol (struct it *it)
 10995 {
 10996   enum move_it_result rc;
 10997 
 10998   rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
 10999   if (rc == MOVE_NEWLINE_OR_CR)
 11000     set_iterator_to_next (it, false);
 11001 }
 11002 
 11003 
 11004 /* Move IT by a specified number DVPOS of screen lines down.  DVPOS
 11005    negative means move up.  DVPOS == 0 means move to the start of the
 11006    screen line.
 11007 
 11008    Optimization idea: If we would know that IT->f doesn't use
 11009    a face with proportional font, we could be faster for
 11010    truncate-lines nil.  */
 11011 
 11012 void
 11013 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
 11014 {
 11015 
 11016   /* The commented-out optimization uses vmotion on terminals.  This
 11017      gives bad results, because elements like it->what, on which
 11018      callers such as pos_visible_p rely, aren't updated.  */
 11019   /* struct position pos;
 11020     if (!FRAME_WINDOW_P (it->f))
 11021     {
 11022       struct text_pos textpos;
 11023 
 11024       pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
 11025       SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
 11026       reseat (it, textpos, true);
 11027       it->vpos += pos.vpos;
 11028       it->current_y += pos.vpos;
 11029     }
 11030     else */
 11031 
 11032   if (dvpos == 0)
 11033     {
 11034       /* DVPOS == 0 means move to the start of the screen line.  */
 11035       move_it_vertically_backward (it, 0);
 11036       /* Let next call to line_bottom_y calculate real line height.  */
 11037       last_height = 0;
 11038     }
 11039   else if (dvpos > 0)
 11040     {
 11041       move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
 11042       if (!IT_POS_VALID_AFTER_MOVE_P (it))
 11043         {
 11044           /* Only move to the next buffer position if we ended up in a
 11045              string from display property, not in an overlay string
 11046              (before-string or after-string).  That is because the
 11047              latter don't conceal the underlying buffer position, so
 11048              we can ask to move the iterator to the exact position we
 11049              are interested in.  Note that, even if we are already at
 11050              IT_CHARPOS (*it), the call below is not a no-op, as it
 11051              will detect that we are at the end of the string, pop the
 11052              iterator, and compute it->current_x and it->hpos
 11053              correctly.  */
 11054           move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
 11055                       -1, -1, -1, MOVE_TO_POS);
 11056         }
 11057     }
 11058   else
 11059     {
 11060       struct it it2;
 11061       void *it2data = NULL;
 11062       ptrdiff_t start_charpos, orig_charpos, i;
 11063       int nchars_per_row
 11064         = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 11065       bool hit_pos_limit = false;
 11066       ptrdiff_t pos_limit;
 11067 
 11068       /* Start at the beginning of the screen line containing IT's
 11069          position.  This may actually move vertically backwards,
 11070          in case of overlays, so adjust dvpos accordingly.  */
 11071       dvpos += it->vpos;
 11072       orig_charpos = IT_CHARPOS (*it);
 11073       move_it_vertically_backward (it, 0);
 11074       dvpos -= it->vpos;
 11075 
 11076       /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
 11077          screen lines, and reseat the iterator there.  */
 11078       start_charpos = IT_CHARPOS (*it);
 11079       if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 11080         pos_limit = BEGV;
 11081       else
 11082         pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
 11083 
 11084       for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
 11085         back_to_previous_visible_line_start (it);
 11086       if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
 11087         hit_pos_limit = true;
 11088       reseat (it, it->current.pos, true);
 11089 
 11090       /* Move further back if we end up in a string or an image.  */
 11091       while (!IT_POS_VALID_AFTER_MOVE_P (it))
 11092         {
 11093           /* First try to move to start of display line.  */
 11094           dvpos += it->vpos;
 11095           move_it_vertically_backward (it, 0);
 11096           dvpos -= it->vpos;
 11097           if (IT_POS_VALID_AFTER_MOVE_P (it))
 11098             break;
 11099           /* If start of line is still in string or image,
 11100              move further back.  */
 11101           back_to_previous_visible_line_start (it);
 11102           reseat (it, it->current.pos, true);
 11103           dvpos--;
 11104         }
 11105 
 11106       it->current_x = it->hpos = 0;
 11107 
 11108       /* Above call may have moved too far if continuation lines
 11109          are involved.  Scan forward and see if it did.  */
 11110       SAVE_IT (it2, *it, it2data);
 11111       it2.vpos = it2.current_y = 0;
 11112       move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
 11113       it->vpos -= it2.vpos;
 11114       it->current_y -= it2.current_y;
 11115       it->current_x = it->hpos = 0;
 11116 
 11117       /* If we moved too far back, move IT some lines forward.  */
 11118       if (it2.vpos > -dvpos)
 11119         {
 11120           int delta = it2.vpos + dvpos;
 11121 
 11122           RESTORE_IT (&it2, &it2, it2data);
 11123           SAVE_IT (it2, *it, it2data);
 11124           move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
 11125           /* Move back again if we got too far ahead,
 11126              or didn't move at all.  */
 11127           if (it->vpos - it2.vpos > delta || IT_CHARPOS (*it) == orig_charpos)
 11128             RESTORE_IT (it, &it2, it2data);
 11129           else
 11130             bidi_unshelve_cache (it2data, true);
 11131         }
 11132       else if (hit_pos_limit && pos_limit > BEGV
 11133                && dvpos < 0 && it2.vpos < -dvpos)
 11134         {
 11135           /* If we hit the limit, but still didn't make it far enough
 11136              back, that means there's a display string with a newline
 11137              covering a large chunk of text, and that caused
 11138              back_to_previous_visible_line_start try to go too far.
 11139              Punish those who commit such atrocities by going back
 11140              until we've reached DVPOS, after lifting the limit, which
 11141              could make it slow for very long lines.  "If it hurts,
 11142              don't do that!"  */
 11143           dvpos += it2.vpos;
 11144           RESTORE_IT (it, it, it2data);
 11145           for (i = -dvpos; i > 0; --i)
 11146             {
 11147               back_to_previous_visible_line_start (it);
 11148               it->vpos--;
 11149             }
 11150           reseat_1 (it, it->current.pos, true);
 11151         }
 11152       else
 11153         RESTORE_IT (it, it, it2data);
 11154     }
 11155 }
 11156 
 11157 int
 11158 partial_line_height (struct it *it_origin)
 11159 {
 11160   /* In a buffer with very long and truncated lines, we ignore the
 11161      possibly-partial height of the last line in the window: it is too
 11162      expensive to compute that (since in most cases that involves
 11163      going all the way to ZV), and the effect of ignoring it is
 11164      relatively minor.  */
 11165   if (XBUFFER (it_origin->w->contents)->long_line_optimizations_p
 11166       && it_origin->line_wrap == TRUNCATE)
 11167     return 0;
 11168 
 11169   int partial_height;
 11170   void *it_data = NULL;
 11171   struct it it;
 11172   SAVE_IT (it, *it_origin, it_data);
 11173   move_it_to (&it, ZV, -1, it.last_visible_y, -1,
 11174               MOVE_TO_POS | MOVE_TO_Y);
 11175   if (it.what == IT_EOB)
 11176     {
 11177       int vis_height = it.last_visible_y - it.current_y;
 11178       int height = it.ascent + it.descent;
 11179       partial_height = (vis_height < height) ? vis_height : 0;
 11180     }
 11181   else
 11182     {
 11183       int last_line_y = it.current_y;
 11184       move_it_by_lines (&it, 1);
 11185       partial_height = (it.current_y > it.last_visible_y)
 11186         ? it.last_visible_y - last_line_y : 0;
 11187     }
 11188   RESTORE_IT (&it, &it, it_data);
 11189   return partial_height;
 11190 }
 11191 
 11192 /* Approximate move_it_in_display_line_to for very long and truncated
 11193    display lines, when moving horizontally.  This is used when the
 11194    buffer's long_line_optimizations_p flag is set.  It ignores various
 11195    complications, like different font sizes, invisible text, display
 11196    and overlay strings, and, to some degree, bidirectional text.  So
 11197    caveat emptor!
 11198 
 11199    Starting from IT's position, reseat IT after skipping NCHARS
 11200    characters or to the next newline/ZV, whichever comes first.  Return
 11201    what move_it_in_display_line_to would have returned in this case.  */
 11202 
 11203 static enum move_it_result
 11204 fast_move_it_horizontally (struct it *it, ptrdiff_t nchars)
 11205 {
 11206   ptrdiff_t nl_bytepos;
 11207   ptrdiff_t nl_pos = find_newline_no_quit (IT_CHARPOS (*it), IT_BYTEPOS (*it),
 11208                                            1, &nl_bytepos);
 11209   struct text_pos new_pos;
 11210   enum move_it_result move_result;
 11211 
 11212   if (nl_pos - IT_CHARPOS (*it) > nchars)
 11213     {
 11214       SET_TEXT_POS (new_pos,
 11215                     IT_CHARPOS (*it) + nchars,
 11216                     CHAR_TO_BYTE (IT_CHARPOS (*it) + nchars));
 11217       move_result = MOVE_X_REACHED;
 11218     }
 11219   else
 11220     {
 11221       if (nl_bytepos < ZV_BYTE
 11222           || (nl_bytepos > BEGV_BYTE
 11223               && FETCH_BYTE (nl_bytepos - 1) == '\n'))
 11224         {
 11225           nl_pos--;
 11226           nl_bytepos--;
 11227           move_result = MOVE_NEWLINE_OR_CR;
 11228         }
 11229       else
 11230         move_result = MOVE_POS_MATCH_OR_ZV;
 11231       SET_TEXT_POS (new_pos, nl_pos, nl_bytepos);
 11232     }
 11233   reseat (it, new_pos, false);
 11234   return move_result;
 11235 }
 11236 
 11237 /* Return true if IT points into the middle of a display vector.  */
 11238 
 11239 bool
 11240 in_display_vector_p (struct it *it)
 11241 {
 11242   return (it->method == GET_FROM_DISPLAY_VECTOR
 11243           && it->current.dpvec_index > 0
 11244           && it->dpvec + it->current.dpvec_index != it->dpend);
 11245 }
 11246 
 11247 /* This is like Fwindow_text_pixel_size but assumes that WINDOW's buffer
 11248    is the current buffer.  Fbuffer_text_pixel_size calls it after it has
 11249    set WINDOW's buffer to the buffer specified by its BUFFER_OR_NAME
 11250    argument.  */
 11251 static Lisp_Object
 11252 window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
 11253                         Lisp_Object x_limit, Lisp_Object y_limit,
 11254                         Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11255 {
 11256   struct window *w = decode_live_window (window);
 11257   struct it it;
 11258   ptrdiff_t start, end, bpos;
 11259   struct text_pos startp;
 11260   void *itdata = NULL;
 11261   int c, max_x = 0, max_y = 0, x = 0, y = 0, vertical_offset = 0, doff = 0;
 11262 
 11263   if (NILP (from))
 11264     {
 11265       start = BEGV;
 11266       bpos = BEGV_BYTE;
 11267     }
 11268   else if (EQ (from, Qt))
 11269     {
 11270       start = BEGV;
 11271       bpos = BEGV_BYTE;
 11272       while (bpos < ZV_BYTE)
 11273         {
 11274           c = FETCH_BYTE (bpos);
 11275           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11276             break;
 11277           inc_both (&start, &bpos);
 11278         }
 11279       while (bpos > BEGV_BYTE)
 11280         {
 11281           dec_both (&start, &bpos);
 11282           c = FETCH_BYTE (bpos);
 11283           if (!(c == ' ' || c == '\t'))
 11284             break;
 11285         }
 11286     }
 11287   else if (CONSP (from))
 11288     {
 11289       start = clip_to_bounds (BEGV, fix_position (XCAR (from)), ZV);
 11290       bpos = CHAR_TO_BYTE (start);
 11291       CHECK_FIXNUM (XCDR (from));
 11292       vertical_offset = XFIXNUM (XCDR (from));
 11293     }
 11294   else
 11295     {
 11296       start = clip_to_bounds (BEGV, fix_position (from), ZV);
 11297       bpos = CHAR_TO_BYTE (start);
 11298     }
 11299 
 11300   SET_TEXT_POS (startp, start, bpos);
 11301 
 11302   if (NILP (to))
 11303     end = ZV;
 11304   else if (EQ (to, Qt))
 11305     {
 11306       end = ZV;
 11307       bpos = ZV_BYTE;
 11308       while (bpos > BEGV_BYTE)
 11309         {
 11310           dec_both (&end, &bpos);
 11311           c = FETCH_BYTE (bpos);
 11312           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11313             {
 11314               inc_both (&end, &bpos);
 11315               break;
 11316             }
 11317         }
 11318       while (bpos < ZV_BYTE)
 11319         {
 11320           c = fetch_char_advance (&end, &bpos);
 11321           if (!(c == ' ' || c == '\t'))
 11322             break;
 11323         }
 11324     }
 11325   else
 11326     end = clip_to_bounds (start, fix_position (to), ZV);
 11327 
 11328   if (RANGED_FIXNUMP (0, x_limit, INT_MAX))
 11329     max_x = XFIXNUM (x_limit);
 11330   else if (!NILP (x_limit))
 11331     max_x = INT_MAX;
 11332 
 11333   if (NILP (y_limit))
 11334     max_y = INT_MAX;
 11335   else if (RANGED_FIXNUMP (0, y_limit, INT_MAX))
 11336     max_y = XFIXNUM (y_limit);
 11337 
 11338   itdata = bidi_shelve_cache ();
 11339 
 11340   start_display (&it, w, startp);
 11341 
 11342   int start_y = it.current_y;
 11343 
 11344   /* It makes no sense to measure dimensions of region of text that
 11345      crosses the point where bidi reordering changes scan direction.
 11346      By using unidirectional movement here we at least support the use
 11347      case of measuring regions of text that have a uniformly R2L
 11348      directionality, and regions that begin and end in text of the
 11349      same directionality.  */
 11350   it.bidi_p = false;
 11351 
 11352   int start_x;
 11353   if (vertical_offset != 0)
 11354     {
 11355       int last_y;
 11356       it.current_y = 0;
 11357 
 11358       move_it_by_lines (&it, 0);
 11359 
 11360       /* `move_it_vertically_backward' is called by move_it_vertically
 11361          to move by a negative value (upwards), but it is not always
 11362          guaranteed to leave the iterator at or above the position
 11363          given by the offset, which this loop ensures.  */
 11364       if (vertical_offset < 0)
 11365         {
 11366           while (it.current_y > vertical_offset)
 11367             {
 11368               last_y = it.current_y;
 11369               move_it_vertically_backward (&it,
 11370                                            (abs (vertical_offset)
 11371                                             + it.current_y));
 11372 
 11373               if (it.current_y == last_y)
 11374                 break;
 11375             }
 11376         }
 11377       else
 11378         {
 11379           move_it_vertically (&it, vertical_offset);
 11380         }
 11381 
 11382       it.current_y = (WINDOW_TAB_LINE_HEIGHT (w)
 11383                       + WINDOW_HEADER_LINE_HEIGHT (w));
 11384       start = clip_to_bounds (BEGV, IT_CHARPOS (it), ZV);
 11385       start_y = it.current_y;
 11386       start_x = it.current_x;
 11387     }
 11388   else
 11389     {
 11390       /* Start at the beginning of the line containing FROM.  Otherwise
 11391          IT.current_x will be incorrectly set to zero at some arbitrary
 11392          non-zero X coordinate.  */
 11393       reseat_at_previous_visible_line_start (&it);
 11394       it.current_x = it.hpos = 0;
 11395       if (IT_CHARPOS (it) != start)
 11396         {
 11397           void *it1data = NULL;
 11398           struct it it1;
 11399 
 11400           SAVE_IT (it1, it, it1data);
 11401           move_it_to (&it, start, -1, -1, -1, MOVE_TO_POS);
 11402           /* We could have a display property at START, in which case
 11403              asking move_it_to to stop at START will overshoot and
 11404              stop at position after START.  So we try again, stopping
 11405              before START, and account for the width of the last
 11406              buffer position manually.  */
 11407           if (IT_CHARPOS (it) > start && start > BEGV)
 11408             {
 11409               ptrdiff_t it1pos = IT_CHARPOS (it1);
 11410               int it1_x = it1.current_x;
 11411 
 11412               RESTORE_IT (&it, &it1, it1data);
 11413               /* If START - 1 is the beginning of screen line,
 11414                  move_it_to will not move, so we need to use a
 11415                  lower-level move_it_in_display_line subroutine, and
 11416                  tell it to move just 1 pixel, so it stops at the next
 11417                  display element.  */
 11418               if (start - 1 > it1pos)
 11419                 move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11420               else
 11421                 move_it_in_display_line (&it, start, it1_x + 1,
 11422                                          MOVE_TO_POS | MOVE_TO_X);
 11423               move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11424               start_x = it.current_x;
 11425               /* If we didn't change our buffer position, the pixel
 11426                  width of what's here was not yet accounted for; do it
 11427                  manually.  */
 11428               if (IT_CHARPOS (it) == start - 1)
 11429                 start_x += it.pixel_width;
 11430             }
 11431           else
 11432             {
 11433               start_x = it.current_x;
 11434               bidi_unshelve_cache (it1data, true);
 11435             }
 11436         }
 11437       else
 11438         start_x = it.current_x;
 11439     }
 11440 
 11441   /* Now move to TO.  */
 11442   int move_op = MOVE_TO_POS | MOVE_TO_Y;
 11443   int to_x = -1;
 11444   it.current_y = start_y;
 11445   /* If FROM is on a newline, pretend that we start at the beginning
 11446      of the next line, because the newline takes no place on display.  */
 11447   if (FETCH_BYTE (start) == '\n')
 11448     it.current_x = 0;
 11449   if (!NILP (x_limit))
 11450     {
 11451       it.last_visible_x = max_x;
 11452       /* Actually, we never want move_it_to stop at to_x.  But to make
 11453          sure that move_it_in_display_line_to always moves far enough,
 11454          we set to_x to INT_MAX and specify MOVE_TO_X.  */
 11455       move_op |= MOVE_TO_X;
 11456       to_x = INT_MAX;
 11457     }
 11458 
 11459   void *it2data = NULL;
 11460   struct it it2;
 11461   SAVE_IT (it2, it, it2data);
 11462 
 11463   x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11464 
 11465   /* We could have a display property at END, in which case asking
 11466      move_it_to to stop at END will overshoot and stop at position
 11467      after END.  So we try again, stopping before END, and account for
 11468      the width of the last buffer position manually.  */
 11469   if (IT_CHARPOS (it) > end)
 11470     {
 11471       end--;
 11472       RESTORE_IT (&it, &it2, it2data);
 11473       x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11474       /* Add the width of the thing at TO, but only if we didn't
 11475          overshoot it; if we did, it is already accounted for.  Also,
 11476          account for the height of the thing at TO.  */
 11477       if (IT_CHARPOS (it) == end)
 11478         {
 11479           x += it.pixel_width;
 11480 
 11481           /* DTRT if ignore_line_at_end is t.  */
 11482           if (!NILP (ignore_line_at_end))
 11483             doff = (max (it.max_ascent, it.ascent)
 11484                     + max (it.max_descent, it.descent));
 11485           else
 11486             {
 11487               it.max_ascent = max (it.max_ascent, it.ascent);
 11488               it.max_descent = max (it.max_descent, it.descent);
 11489             }
 11490         }
 11491     }
 11492   else
 11493     bidi_unshelve_cache (it2data, true);
 11494 
 11495   if (!NILP (x_limit))
 11496     {
 11497       /* Don't return more than X-LIMIT.  */
 11498       if (x > max_x)
 11499         x = max_x;
 11500     }
 11501 
 11502   /* If text spans more than one screen line, we don't need to adjust
 11503      the x-span for start_x, since the second and subsequent lines
 11504      will begin at zero X coordinate.  */
 11505   if (it.current_y > start_y)
 11506     start_x = 0;
 11507 
 11508   /* Subtract height of header-line and tab-line which was counted
 11509      automatically by start_display.  */
 11510   if (!NILP (ignore_line_at_end))
 11511     y = (it.current_y + doff
 11512          - WINDOW_TAB_LINE_HEIGHT (w)
 11513          - WINDOW_HEADER_LINE_HEIGHT (w));
 11514   else
 11515     y = (it.current_y + it.max_ascent + it.max_descent + doff
 11516          - WINDOW_TAB_LINE_HEIGHT (w) - WINDOW_HEADER_LINE_HEIGHT (w));
 11517 
 11518   /* Don't return more than Y-LIMIT.  */
 11519   if (y > max_y)
 11520     y = max_y;
 11521 
 11522   if ((EQ (mode_lines, Qtab_line) || EQ (mode_lines, Qt))
 11523       && window_wants_tab_line (w))
 11524     /* Add height of tab-line as requested.  */
 11525     {
 11526       Lisp_Object window_tab_line_format
 11527         = window_parameter (w, Qtab_line_format);
 11528 
 11529       y = y + display_mode_line (w, TAB_LINE_FACE_ID,
 11530                                  NILP (window_tab_line_format)
 11531                                  ? BVAR (current_buffer, tab_line_format)
 11532                                  : window_tab_line_format);
 11533     }
 11534 
 11535   if ((EQ (mode_lines, Qheader_line) || EQ (mode_lines, Qt))
 11536       && window_wants_header_line (w))
 11537     {
 11538       Lisp_Object window_header_line_format
 11539         = window_parameter (w, Qheader_line_format);
 11540 
 11541       y = y + display_mode_line (w, HEADER_LINE_FACE_ID,
 11542                                  NILP (window_header_line_format)
 11543                                  ? BVAR (current_buffer, header_line_format)
 11544                                  : window_header_line_format);
 11545     }
 11546 
 11547   if ((EQ (mode_lines, Qmode_line) || EQ (mode_lines, Qt))
 11548       && window_wants_mode_line (w))
 11549     {
 11550       Lisp_Object window_mode_line_format
 11551         = window_parameter (w, Qmode_line_format);
 11552 
 11553       y = y + display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
 11554                                  NILP (window_mode_line_format)
 11555                                  ? BVAR (current_buffer, mode_line_format)
 11556                                  : window_mode_line_format);
 11557     }
 11558 
 11559   bidi_unshelve_cache (itdata, false);
 11560 
 11561   return (!vertical_offset
 11562           ? Fcons (make_fixnum (x - start_x), make_fixnum (y))
 11563           : list3i (x - start_x, y, start));
 11564 }
 11565 
 11566 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 7, 0,
 11567        doc: /* Return the size of the text of WINDOW's buffer in pixels.
 11568 WINDOW must be a live window and defaults to the selected one.  The
 11569 return value is a cons of the maximum pixel-width of any text line and
 11570 the pixel-height of all the text lines in the accessible portion of
 11571 buffer text.
 11572 
 11573 If FROM is a cons cell, the return value includes, in addition to the
 11574 dimensions, also a third element that provides the buffer position
 11575 from which measuring of the text dimensions was actually started.
 11576 
 11577 This function exists to allow Lisp programs to adjust the dimensions
 11578 of WINDOW to the buffer text it needs to display.
 11579 
 11580 The optional argument FROM, if non-nil, specifies the first text
 11581 position to consider, and defaults to the minimum accessible position
 11582 of the buffer.  If FROM is a cons, its car specifies a buffer
 11583 position, and its cdr specifies the vertical offset in pixels from
 11584 that position to the first screen line to be measured.  If FROM is t,
 11585 it stands for the minimum accessible position that starts a non-empty
 11586 line.  TO, if non-nil, specifies the last text position and defaults
 11587 to the maximum accessible position of the buffer.  If TO is t, it
 11588 stands for the maximum accessible position that ends a non-empty line.
 11589 
 11590 The optional argument X-LIMIT, if non-nil, specifies the maximum X
 11591 coordinate beyond which the text should be ignored.  It is therefore
 11592 also the maximum width that the function can return.  X-LIMIT nil or
 11593 omitted means to use the pixel-width of WINDOW's body.  This default
 11594 means text of truncated lines wider than the window will be ignored;
 11595 specify a non-nil value for X-LIMIT if lines are truncated and you need
 11596 to account for the truncated text.
 11597 
 11598 Use nil for X-LIMIT if you want to know how high WINDOW should become in
 11599 order to fit all of its buffer's text with the width of WINDOW
 11600 unaltered.  Use the maximum width WINDOW may assume if you intend to
 11601 change WINDOW's width.  Use t for the maximum possible value.  Since
 11602 calculating the width of long lines can take some time, it's always a
 11603 good idea to make this argument as small as possible; in particular, if
 11604 the buffer contains long lines that shall be truncated anyway.
 11605 
 11606 The optional argument Y-LIMIT, if non-nil, specifies the maximum Y
 11607 coordinate beyond which the text is to be ignored; it is therefore
 11608 also the maximum height that the function can return (excluding the
 11609 height of the mode- or header-line, if any).  Y-LIMIT nil or omitted
 11610 means consider all of the accessible portion of buffer text up to the
 11611 position specified by TO.  Since calculating the text height of a
 11612 large buffer can take some time, it makes sense to specify this
 11613 argument if the size of the buffer is large or unknown.
 11614 
 11615 Optional argument MODE-LINES nil or omitted means do not include the
 11616 height of the mode-, tab- or header-line of WINDOW in the return value.
 11617 If it is the symbol `mode-line', `tab-line' or `header-line', include
 11618 only the height of that line, if present, in the return value.  If t,
 11619 include the height of any of these, if present, in the return value.
 11620 
 11621 IGNORE-LINE-AT-END, if non-nil, means to not add the height of the
 11622 screen line that includes TO to the returned height of the text.  */)
 11623   (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
 11624    Lisp_Object y_limit, Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11625 {
 11626   struct window *w = decode_live_window (window);
 11627   struct buffer *b = XBUFFER (w->contents);
 11628   struct buffer *old_b = NULL;
 11629   Lisp_Object value;
 11630 
 11631   if (b != current_buffer)
 11632     {
 11633       old_b = current_buffer;
 11634       set_buffer_internal_1 (b);
 11635     }
 11636 
 11637   value = window_text_pixel_size (window, from, to, x_limit, y_limit, mode_lines,
 11638                                   ignore_line_at_end);
 11639 
 11640   if (old_b)
 11641     set_buffer_internal_1 (old_b);
 11642 
 11643   return value;
 11644 }
 11645 
 11646 DEFUN ("buffer-text-pixel-size", Fbuffer_text_pixel_size, Sbuffer_text_pixel_size, 0, 4, 0,
 11647        doc: /* Return size of whole text of BUFFER-OR-NAME in WINDOW.
 11648 BUFFER-OR-NAME must specify a live buffer or the name of a live buffer
 11649 and defaults to the current buffer.  WINDOW must be a live window and
 11650 defaults to the selected one.  The return value is a cons of the maximum
 11651 pixel-width of any text line and the pixel-height of all the text lines
 11652 of the buffer specified by BUFFER-OR-NAME.
 11653 
 11654 The optional arguments X-LIMIT and Y-LIMIT have the same meaning as with
 11655 `window-text-pixel-size'.
 11656 
 11657 Do not use this function if the buffer specified by BUFFER-OR-NAME is
 11658 already displayed in WINDOW.  `window-text-pixel-size' is cheaper in
 11659 that case because it does not have to temporarily show that buffer in
 11660 WINDOW.  */)
 11661   (Lisp_Object buffer_or_name, Lisp_Object window, Lisp_Object x_limit,
 11662    Lisp_Object y_limit)
 11663 {
 11664   struct window *w = decode_live_window (window);
 11665   struct buffer *b = (NILP (buffer_or_name)
 11666                       ? current_buffer
 11667                       : XBUFFER (Fget_buffer (buffer_or_name)));
 11668   Lisp_Object buffer, value;
 11669   specpdl_ref count = SPECPDL_INDEX ();
 11670 
 11671   XSETBUFFER (buffer, b);
 11672 
 11673   /* The unwind form of with_echo_area_buffer is what we need here to
 11674      make WINDOW temporarily show our buffer.  */
 11675   /* FIXME: Can we move this into the `if (!EQ (buffer, w->contents))`?  */
 11676   record_unwind_protect (unwind_with_echo_area_buffer,
 11677                          with_echo_area_buffer_unwind_data (w));
 11678 
 11679   set_buffer_internal_1 (b);
 11680 
 11681   if (!EQ (buffer, w->contents))
 11682     {
 11683       wset_buffer (w, buffer);
 11684       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 11685       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 11686     }
 11687 
 11688   value = window_text_pixel_size (window, Qnil, Qnil, x_limit, y_limit, Qnil,
 11689                                   Qnil);
 11690 
 11691   unbind_to (count, Qnil);
 11692 
 11693   return value;
 11694 }
 11695 
 11696 
 11697 DEFUN ("display--line-is-continued-p", Fdisplay__line_is_continued_p,
 11698        Sdisplay__line_is_continued_p, 0, 0, 0,
 11699        doc: /* Return non-nil if the current screen line is continued on display.  */)
 11700   (void)
 11701 {
 11702   struct buffer *oldb = current_buffer;
 11703   struct window *w = XWINDOW (selected_window);
 11704   enum move_it_result rc = MOVE_POS_MATCH_OR_ZV;
 11705 
 11706   set_buffer_internal_1 (XBUFFER (w->contents));
 11707 
 11708   if (PT < ZV)
 11709     {
 11710       struct text_pos startpos;
 11711       struct it it;
 11712       void *itdata;
 11713       /* Use a marker, since vertical-motion enters redisplay, which can
 11714          trigger fontifications, which in turn could modify buffer text.  */
 11715       Lisp_Object opoint = Fpoint_marker ();
 11716 
 11717       /* Make sure to start from the beginning of the current screen
 11718          line, so that move_it_in_display_line_to counts pixels correctly.  */
 11719       Fvertical_motion (make_fixnum (0), selected_window, Qnil);
 11720       SET_TEXT_POS (startpos, PT, PT_BYTE);
 11721       itdata = bidi_shelve_cache ();
 11722       start_display (&it, w, startpos);
 11723       /* If lines are truncated, no line is continued.  */
 11724       if (it.line_wrap != TRUNCATE)
 11725         {
 11726           it.glyph_row = NULL;
 11727           rc = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 11728         }
 11729       SET_PT_BOTH (marker_position (opoint), marker_byte_position (opoint));
 11730       bidi_unshelve_cache (itdata, false);
 11731     }
 11732   set_buffer_internal_1 (oldb);
 11733 
 11734   return rc == MOVE_LINE_CONTINUED ? Qt : Qnil;
 11735 }
 11736 
 11737 
 11738 /***********************************************************************
 11739                                Messages
 11740  ***********************************************************************/
 11741 
 11742 /* Return the number of arguments the format string FORMAT needs.  */
 11743 
 11744 static ptrdiff_t
 11745 format_nargs (char const *format)
 11746 {
 11747   ptrdiff_t nargs = 0;
 11748   for (char const *p = format; (p = strchr (p, '%')); p++)
 11749     if (p[1] == '%')
 11750       p++;
 11751     else
 11752       nargs++;
 11753   return nargs;
 11754 }
 11755 
 11756 /* Add a message with format string FORMAT and formatted arguments
 11757    to *Messages*.  */
 11758 
 11759 void
 11760 add_to_log (const char *format, ...)
 11761 {
 11762   va_list ap;
 11763   va_start (ap, format);
 11764   vadd_to_log (format, ap);
 11765   va_end (ap);
 11766 }
 11767 
 11768 void
 11769 vadd_to_log (char const *format, va_list ap)
 11770 {
 11771   ptrdiff_t form_nargs = format_nargs (format);
 11772   ptrdiff_t nargs = 1 + form_nargs;
 11773   Lisp_Object args[10];
 11774   eassert (nargs <= ARRAYELTS (args));
 11775   AUTO_STRING (args0, format);
 11776   args[0] = args0;
 11777   for (ptrdiff_t i = 1; i <= nargs; i++)
 11778     args[i] = va_arg (ap, Lisp_Object);
 11779   Lisp_Object msg = Qnil;
 11780   msg = Fformat_message (nargs, args);
 11781 
 11782   ptrdiff_t len = SBYTES (msg) + 1;
 11783   USE_SAFE_ALLOCA;
 11784   char *buffer = SAFE_ALLOCA (len);
 11785   memcpy (buffer, SDATA (msg), len);
 11786 
 11787   message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
 11788   SAFE_FREE ();
 11789 }
 11790 
 11791 
 11792 /* Output a newline in the *Messages* buffer if "needs" one.  */
 11793 
 11794 void
 11795 message_log_maybe_newline (void)
 11796 {
 11797   if (message_log_need_newline)
 11798     message_dolog ("", 0, true, false);
 11799 }
 11800 
 11801 
 11802 /* Add a string M of length NBYTES to the message log, optionally
 11803    terminated with a newline when NLFLAG is true.  MULTIBYTE, if
 11804    true, means interpret the contents of M as multibyte.  This
 11805    function calls low-level routines in order to bypass text property
 11806    hooks, etc. which might not be safe to run.
 11807 
 11808    This may GC (insert may run before/after change hooks),
 11809    so the buffer M must NOT point to a Lisp string.  */
 11810 
 11811 void
 11812 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
 11813 {
 11814   const unsigned char *msg = (const unsigned char *) m;
 11815 
 11816   if (!NILP (Vmemory_full))
 11817     return;
 11818 
 11819   if (!NILP (Vmessage_log_max))
 11820     {
 11821       struct buffer *oldbuf;
 11822       Lisp_Object oldpoint, oldbegv, oldzv;
 11823       int old_windows_or_buffers_changed = windows_or_buffers_changed;
 11824       ptrdiff_t point_at_end = 0;
 11825       ptrdiff_t zv_at_end = 0;
 11826       Lisp_Object old_deactivate_mark;
 11827 
 11828       old_deactivate_mark = Vdeactivate_mark;
 11829       oldbuf = current_buffer;
 11830 
 11831       /* Sanity check, in case the variable has been set to something
 11832          invalid.  */
 11833       if (! STRINGP (Vmessages_buffer_name))
 11834         Vmessages_buffer_name = build_string ("*Messages*");
 11835       /* Ensure the Messages buffer exists, and switch to it.
 11836          If we created it, set the major-mode.  */
 11837       bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
 11838       Fset_buffer (Fget_buffer_create (Vmessages_buffer_name, Qnil));
 11839       if (newbuffer
 11840           && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
 11841         call0 (intern ("messages-buffer-mode"));
 11842 
 11843       bset_undo_list (current_buffer, Qt);
 11844       bset_cache_long_scans (current_buffer, Qnil);
 11845 
 11846       oldpoint = message_dolog_marker1;
 11847       set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
 11848       oldbegv = message_dolog_marker2;
 11849       set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
 11850       oldzv = message_dolog_marker3;
 11851       set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
 11852 
 11853       if (PT == Z)
 11854         point_at_end = 1;
 11855       if (ZV == Z)
 11856         zv_at_end = 1;
 11857 
 11858       BEGV = BEG;
 11859       BEGV_BYTE = BEG_BYTE;
 11860       ZV = Z;
 11861       ZV_BYTE = Z_BYTE;
 11862       TEMP_SET_PT_BOTH (Z, Z_BYTE);
 11863 
 11864       /* Insert the string--maybe converting multibyte to single byte
 11865          or vice versa, so that all the text fits the buffer.  */
 11866       if (multibyte
 11867           && NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11868         {
 11869           /* Convert a multibyte string to single-byte
 11870              for the *Message* buffer.  */
 11871           for (ptrdiff_t i = 0; i < nbytes; )
 11872             {
 11873               int char_bytes, c = check_char_and_length (msg + i, &char_bytes);
 11874               char work = CHAR_TO_BYTE8 (c);
 11875               insert_1_both (&work, 1, 1, true, false, false);
 11876               i += char_bytes;
 11877             }
 11878         }
 11879       else if (! multibyte
 11880                && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11881         {
 11882           /* Convert a single-byte string to multibyte
 11883              for the *Message* buffer.  */
 11884           for (ptrdiff_t i = 0; i < nbytes; i++)
 11885             {
 11886               int c = make_char_multibyte (msg[i]);
 11887               unsigned char str[MAX_MULTIBYTE_LENGTH];
 11888               int char_bytes = CHAR_STRING (c, str);
 11889               insert_1_both ((char *) str, 1, char_bytes, true, false, false);
 11890             }
 11891         }
 11892       else if (nbytes)
 11893         insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
 11894                        true, false, false);
 11895 
 11896       if (nlflag)
 11897         {
 11898           ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
 11899           intmax_t dups;
 11900 
 11901           /* Since we call del_range_both passing false for PREPARE,
 11902              we aren't prepared to run modification hooks (we could
 11903              end up calling modification hooks from another buffer and
 11904              only with AFTER=t, Bug#21824).  */
 11905           specpdl_ref count = SPECPDL_INDEX ();
 11906           specbind (Qinhibit_modification_hooks, Qt);
 11907 
 11908           insert_1_both ("\n", 1, 1, true, false, false);
 11909 
 11910           scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
 11911           this_bol = PT;
 11912           this_bol_byte = PT_BYTE;
 11913 
 11914           /* See if this line duplicates the previous one.
 11915              If so, combine duplicates.  */
 11916           if (this_bol > BEG)
 11917             {
 11918               scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
 11919               prev_bol = PT;
 11920               prev_bol_byte = PT_BYTE;
 11921 
 11922               dups = message_log_check_duplicate (prev_bol_byte,
 11923                                                   this_bol_byte);
 11924               if (dups)
 11925                 {
 11926                   del_range_both (prev_bol, prev_bol_byte,
 11927                                   this_bol, this_bol_byte, false);
 11928                   if (dups > 1)
 11929                     {
 11930                       char dupstr[sizeof " [ times]"
 11931                                   + INT_STRLEN_BOUND (dups)];
 11932 
 11933                       /* If you change this format, don't forget to also
 11934                          change message_log_check_duplicate.  */
 11935                       int duplen = sprintf (dupstr, " [%"PRIdMAX" times]",
 11936                                             dups);
 11937                       TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
 11938                       insert_1_both (dupstr, duplen, duplen,
 11939                                      true, false, true);
 11940                     }
 11941                 }
 11942             }
 11943 
 11944           /* If we have more than the desired maximum number of lines
 11945              in the *Messages* buffer now, delete the oldest ones.
 11946              This is safe because we don't have undo in this buffer.  */
 11947 
 11948           if (FIXNATP (Vmessage_log_max))
 11949             {
 11950               scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
 11951                             -XFIXNAT (Vmessage_log_max) - 1, false);
 11952               del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
 11953             }
 11954 
 11955           unbind_to (count, Qnil);
 11956         }
 11957       BEGV = marker_position (oldbegv);
 11958       BEGV_BYTE = marker_byte_position (oldbegv);
 11959 
 11960       if (zv_at_end)
 11961         {
 11962           ZV = Z;
 11963           ZV_BYTE = Z_BYTE;
 11964         }
 11965       else
 11966         {
 11967           ZV = marker_position (oldzv);
 11968           ZV_BYTE = marker_byte_position (oldzv);
 11969         }
 11970 
 11971       if (point_at_end)
 11972         TEMP_SET_PT_BOTH (Z, Z_BYTE);
 11973       else
 11974         /* We can't do Fgoto_char (oldpoint) because it will run some
 11975            Lisp code.  */
 11976         TEMP_SET_PT_BOTH (marker_position (oldpoint),
 11977                           marker_byte_position (oldpoint));
 11978 
 11979       unchain_marker (XMARKER (oldpoint));
 11980       unchain_marker (XMARKER (oldbegv));
 11981       unchain_marker (XMARKER (oldzv));
 11982 
 11983       /* We called insert_1_both above with its 5th argument (PREPARE)
 11984          false, which prevents insert_1_both from calling
 11985          prepare_to_modify_buffer, which in turns prevents us from
 11986          incrementing windows_or_buffers_changed even if *Messages* is
 11987          shown in some window.  So we must manually set
 11988          windows_or_buffers_changed here to make up for that.  */
 11989       windows_or_buffers_changed = old_windows_or_buffers_changed;
 11990       bset_redisplay (current_buffer);
 11991 
 11992       set_buffer_internal (oldbuf);
 11993 
 11994       message_log_need_newline = !nlflag;
 11995       Vdeactivate_mark = old_deactivate_mark;
 11996     }
 11997 }
 11998 
 11999 
 12000 /* We are at the end of the buffer after just having inserted a newline.
 12001    (Note: We depend on the fact we won't be crossing the gap.)
 12002    Check to see if the most recent message looks a lot like the previous one.
 12003    Return 0 if different, 1 if the new one should just replace it, or a
 12004    value N > 1 if we should also append " [N times]".  */
 12005 
 12006 static intmax_t
 12007 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
 12008 {
 12009   ptrdiff_t i;
 12010   ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
 12011   bool seen_dots = false;
 12012   unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
 12013   unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
 12014 
 12015   for (i = 0; i < len; i++)
 12016     {
 12017       if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
 12018         seen_dots = true;
 12019       if (p1[i] != p2[i])
 12020         return seen_dots;
 12021     }
 12022   p1 += len;
 12023   if (*p1 == '\n')
 12024     return 2;
 12025   if (*p1++ == ' ' && *p1++ == '[')
 12026     {
 12027       char *pend;
 12028       intmax_t n = strtoimax ((char *) p1, &pend, 10);
 12029       if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
 12030         return n + 1;
 12031     }
 12032   return 0;
 12033 }
 12034 
 12035 
 12036 /* Display an echo area message M with a specified length of NBYTES
 12037    bytes.  The string may include null characters.  If M is not a
 12038    string, clear out any existing message, and let the mini-buffer
 12039    text show through.
 12040 
 12041    This function cancels echoing.  */
 12042 
 12043 void
 12044 message3 (Lisp_Object m)
 12045 {
 12046   clear_message (true, true);
 12047   cancel_echoing ();
 12048 
 12049   /* First flush out any partial line written with print.  */
 12050   message_log_maybe_newline ();
 12051   if (STRINGP (m))
 12052     {
 12053       ptrdiff_t nbytes = SBYTES (m);
 12054       bool multibyte = STRING_MULTIBYTE (m);
 12055       char *buffer;
 12056       USE_SAFE_ALLOCA;
 12057       SAFE_ALLOCA_STRING (buffer, m);
 12058       message_dolog (buffer, nbytes, true, multibyte);
 12059       SAFE_FREE ();
 12060     }
 12061   if (! inhibit_message)
 12062     message3_nolog (m);
 12063 }
 12064 
 12065 /* Log the message M to stderr.  Log an empty line if M is not a string.  */
 12066 
 12067 static void
 12068 message_to_stderr (Lisp_Object m)
 12069 {
 12070   if (noninteractive_need_newline)
 12071     {
 12072       noninteractive_need_newline = false;
 12073       errputc ('\n');
 12074     }
 12075   if (STRINGP (m))
 12076     {
 12077       Lisp_Object coding_system = Vlocale_coding_system;
 12078       Lisp_Object s;
 12079 
 12080       if (!NILP (Vcoding_system_for_write))
 12081         coding_system = Vcoding_system_for_write;
 12082       if (!NILP (coding_system))
 12083         s = code_convert_string_norecord (m, coding_system, true);
 12084       else
 12085         s = m;
 12086 
 12087       errwrite (SDATA (s), SBYTES (s));
 12088     }
 12089   if (STRINGP (m) || !cursor_in_echo_area)
 12090     errputc ('\n');
 12091 }
 12092 
 12093 /* The non-logging version of message3.
 12094    This does not cancel echoing, because it is used for echoing.
 12095    Perhaps we need to make a separate function for echoing
 12096    and make this cancel echoing.  */
 12097 
 12098 void
 12099 message3_nolog (Lisp_Object m)
 12100 {
 12101   struct frame *sf = SELECTED_FRAME ();
 12102 
 12103   if (FRAME_INITIAL_P (sf))
 12104     message_to_stderr (m);
 12105   /* Error messages get reported properly by cmd_error, so this must be just an
 12106      informative message; if the frame hasn't really been initialized yet, just
 12107      toss it.  */
 12108   else if (INTERACTIVE && sf->glyphs_initialized_p)
 12109     {
 12110       /* Get the frame containing the mini-buffer
 12111          that the selected frame is using.  */
 12112       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 12113       Lisp_Object frame = XWINDOW (mini_window)->frame;
 12114       struct frame *f = XFRAME (frame);
 12115 
 12116       if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
 12117         Fmake_frame_visible (frame);
 12118 
 12119       if (STRINGP (m) && SCHARS (m) > 0)
 12120         {
 12121           set_message (m);
 12122           if (minibuffer_auto_raise)
 12123             Fraise_frame (frame);
 12124           /* Assume we are not echoing.
 12125              (If we are, echo_now will override this.)  */
 12126           echo_message_buffer = Qnil;
 12127         }
 12128       else
 12129         clear_message (true, true);
 12130 
 12131       do_pending_window_change (false);
 12132       echo_area_display (true);
 12133       do_pending_window_change (false);
 12134       if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 12135         (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
 12136     }
 12137 }
 12138 
 12139 
 12140 /* Display a null-terminated echo area message M.  If M is 0, clear
 12141    out any existing message, and let the mini-buffer text show through.
 12142 
 12143    The buffer M must continue to exist until after the echo area gets
 12144    cleared or some other message gets displayed there.  Do not pass
 12145    text that is stored in a Lisp string.  Do not pass text in a buffer
 12146    that was alloca'd.  */
 12147 
 12148 void
 12149 message1 (const char *m)
 12150 {
 12151   message3 (m ? build_unibyte_string (m) : Qnil);
 12152 }
 12153 
 12154 
 12155 /* The non-logging counterpart of message1.  */
 12156 
 12157 void
 12158 message1_nolog (const char *m)
 12159 {
 12160   message3_nolog (m ? build_unibyte_string (m) : Qnil);
 12161 }
 12162 
 12163 /* Display a message M which contains a single %s
 12164    which gets replaced with STRING.  */
 12165 
 12166 void
 12167 message_with_string (const char *m, Lisp_Object string, bool log)
 12168 {
 12169   CHECK_STRING (string);
 12170 
 12171   bool need_message;
 12172   if (noninteractive)
 12173     need_message = !!m;
 12174   else if (!INTERACTIVE)
 12175     need_message = false;
 12176   else
 12177     {
 12178       /* The frame whose minibuffer we're going to display the message on.
 12179          It may be larger than the selected frame, so we need
 12180          to use its buffer, not the selected frame's buffer.  */
 12181       Lisp_Object mini_window;
 12182       struct frame *f, *sf = SELECTED_FRAME ();
 12183 
 12184       /* Get the frame containing the minibuffer
 12185          that the selected frame is using.  */
 12186       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12187       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12188 
 12189       /* Error messages get reported properly by cmd_error, so this must be
 12190          just an informative message; if the frame hasn't really been
 12191          initialized yet, just toss it.  */
 12192       need_message = f->glyphs_initialized_p;
 12193     }
 12194 
 12195   if (need_message)
 12196     {
 12197       AUTO_STRING (fmt, m);
 12198       Lisp_Object msg = CALLN (Fformat_message, fmt, string);
 12199 
 12200       if (noninteractive)
 12201         message_to_stderr (msg);
 12202       else
 12203         {
 12204           if (log)
 12205             message3 (msg);
 12206           else
 12207             message3_nolog (msg);
 12208 
 12209           /* Print should start at the beginning of the message
 12210              buffer next time.  */
 12211           message_buf_print = false;
 12212         }
 12213     }
 12214 }
 12215 
 12216 
 12217 /* Dump an informative message to the minibuf.  If M is 0, clear out
 12218    any existing message, and let the mini-buffer text show through.
 12219 
 12220    The message must be safe ASCII (because when Emacs is
 12221    non-interactive the message is sent straight to stderr without
 12222    encoding first) and the format must not contain ` or ' (because
 12223    this function does not account for `text-quoting-style').  If your
 12224    message and format do not fit into this category, convert your
 12225    arguments to Lisp objects and use Fmessage instead.  */
 12226 
 12227 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
 12228 vmessage (const char *m, va_list ap)
 12229 {
 12230   if (noninteractive)
 12231     {
 12232       if (m)
 12233         {
 12234           if (noninteractive_need_newline)
 12235             putc ('\n', stderr);
 12236           noninteractive_need_newline = false;
 12237           vfprintf (stderr, m, ap);
 12238           if (!cursor_in_echo_area)
 12239             putc ('\n', stderr);
 12240           fflush (stderr);
 12241         }
 12242     }
 12243   else if (INTERACTIVE)
 12244     {
 12245       /* The frame whose mini-buffer we're going to display the message
 12246          on.  It may be larger than the selected frame, so we need to
 12247          use its buffer, not the selected frame's buffer.  */
 12248       Lisp_Object mini_window;
 12249       struct frame *f, *sf = SELECTED_FRAME ();
 12250 
 12251       /* Get the frame containing the mini-buffer
 12252          that the selected frame is using.  */
 12253       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12254       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12255 
 12256       /* Error messages get reported properly by cmd_error, so this must be
 12257          just an informative message; if the frame hasn't really been
 12258          initialized yet, just toss it.  */
 12259       if (f->glyphs_initialized_p)
 12260         {
 12261           if (m)
 12262             {
 12263               ptrdiff_t len;
 12264               ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
 12265               USE_SAFE_ALLOCA;
 12266               char *message_buf = SAFE_ALLOCA (maxsize + 1);
 12267 
 12268               len = doprnt (message_buf, maxsize, m, 0, ap);
 12269 
 12270               message3 (make_string (message_buf, len));
 12271               SAFE_FREE ();
 12272             }
 12273           else
 12274             message1 (0);
 12275 
 12276           /* Print should start at the beginning of the message
 12277              buffer next time.  */
 12278           message_buf_print = false;
 12279         }
 12280     }
 12281 }
 12282 
 12283 /* See vmessage for restrictions on the text of the message.  */
 12284 void
 12285 message (const char *m, ...)
 12286 {
 12287   va_list ap;
 12288   va_start (ap, m);
 12289   vmessage (m, ap);
 12290   va_end (ap);
 12291 }
 12292 
 12293 
 12294 /* Display the current message in the current mini-buffer.  This is
 12295    only called from error handlers in process.c, and is not time
 12296    critical.  */
 12297 
 12298 void
 12299 update_echo_area (void)
 12300 {
 12301   if (!NILP (echo_area_buffer[0]))
 12302     {
 12303       Lisp_Object string;
 12304       string = Fcurrent_message ();
 12305       message3 (string);
 12306     }
 12307 }
 12308 
 12309 
 12310 /* Make sure echo area buffers in `echo_buffers' are live.
 12311    If they aren't, make new ones.  */
 12312 
 12313 static void
 12314 ensure_echo_area_buffers (void)
 12315 {
 12316   for (int i = 0; i < 2; i++)
 12317     if (!BUFFERP (echo_buffer[i])
 12318         || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
 12319       {
 12320         Lisp_Object old_buffer = echo_buffer[i];
 12321         static char const name_fmt[] = " *Echo Area %d*";
 12322         char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
 12323         AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
 12324         echo_buffer[i] = Fget_buffer_create (lname, Qnil);
 12325         bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
 12326         /* to force word wrap in echo area -
 12327            it was decided to postpone this*/
 12328         /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
 12329 
 12330         for (int j = 0; j < 2; j++)
 12331           if (EQ (old_buffer, echo_area_buffer[j]))
 12332             echo_area_buffer[j] = echo_buffer[i];
 12333       }
 12334 }
 12335 
 12336 
 12337 /* Call FN with args A1..A2 with either the current or last displayed
 12338    echo_area_buffer as current buffer.
 12339 
 12340    WHICH zero means use the current message buffer
 12341    echo_area_buffer[0].  If that is nil, choose a suitable buffer
 12342    from echo_buffer[] and clear it.
 12343 
 12344    WHICH > 0 means use echo_area_buffer[1].  If that is nil, choose a
 12345    suitable buffer from echo_buffer[] and clear it.
 12346 
 12347    If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
 12348    that the current message becomes the last displayed one, choose a
 12349    suitable buffer for echo_area_buffer[0], and clear it.
 12350 
 12351    Value is what FN returns.  */
 12352 
 12353 static bool
 12354 with_echo_area_buffer (struct window *w, int which,
 12355                        bool (*fn) (void *, Lisp_Object),
 12356                        void *a1, Lisp_Object a2)
 12357 {
 12358   Lisp_Object buffer;
 12359   bool this_one, the_other, clear_buffer_p, rc;
 12360   specpdl_ref count = SPECPDL_INDEX ();
 12361 
 12362   /* If buffers aren't live, make new ones.  */
 12363   ensure_echo_area_buffers ();
 12364 
 12365   clear_buffer_p = false;
 12366 
 12367   if (which == 0)
 12368     this_one = false, the_other = true;
 12369   else if (which > 0)
 12370     this_one = true, the_other = false;
 12371   else
 12372     {
 12373       this_one = false, the_other = true;
 12374       clear_buffer_p = true;
 12375 
 12376       /* We need a fresh one in case the current echo buffer equals
 12377          the one containing the last displayed echo area message.  */
 12378       if (!NILP (echo_area_buffer[this_one])
 12379           && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
 12380         echo_area_buffer[this_one] = Qnil;
 12381     }
 12382 
 12383   /* Choose a suitable buffer from echo_buffer[] if we don't
 12384      have one.  */
 12385   if (NILP (echo_area_buffer[this_one]))
 12386     {
 12387       echo_area_buffer[this_one]
 12388         = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
 12389            ? echo_buffer[the_other]
 12390            : echo_buffer[this_one]);
 12391       clear_buffer_p = true;
 12392     }
 12393 
 12394   buffer = echo_area_buffer[this_one];
 12395 
 12396   /* Don't get confused by reusing the buffer used for echoing
 12397      for a different purpose.  */
 12398   if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
 12399     cancel_echoing ();
 12400 
 12401   record_unwind_protect (unwind_with_echo_area_buffer,
 12402                          with_echo_area_buffer_unwind_data (w));
 12403 
 12404   /* Make the echo area buffer current.  Note that for display
 12405      purposes, it is not necessary that the displayed window's buffer
 12406      == current_buffer, except for text property lookup.  So, let's
 12407      only set that buffer temporarily here without doing a full
 12408      Fset_window_buffer.  We must also change w->pointm, though,
 12409      because otherwise an assertions in unshow_buffer fails, and Emacs
 12410      aborts.  */
 12411   set_buffer_internal_1 (XBUFFER (buffer));
 12412   if (w)
 12413     {
 12414       wset_buffer (w, buffer);
 12415       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 12416       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 12417     }
 12418 
 12419   bset_undo_list (current_buffer, Qt);
 12420   bset_read_only (current_buffer, Qnil);
 12421   specbind (Qinhibit_read_only, Qt);
 12422   specbind (Qinhibit_modification_hooks, Qt);
 12423 
 12424   if (clear_buffer_p && Z > BEG)
 12425     del_range (BEG, Z);
 12426 
 12427   eassert (BEGV >= BEG);
 12428   eassert (ZV <= Z && ZV >= BEGV);
 12429 
 12430   rc = fn (a1, a2);
 12431 
 12432   eassert (BEGV >= BEG);
 12433   eassert (ZV <= Z && ZV >= BEGV);
 12434 
 12435   unbind_to (count, Qnil);
 12436   return rc;
 12437 }
 12438 
 12439 
 12440 /* Save state that should be preserved around the call to the function
 12441    FN called in with_echo_area_buffer.  */
 12442 
 12443 static Lisp_Object
 12444 with_echo_area_buffer_unwind_data (struct window *w)
 12445 {
 12446   int i = 0;
 12447   Lisp_Object vector, tmp;
 12448 
 12449   /* Reduce consing by keeping one vector in
 12450      Vwith_echo_area_save_vector.  */
 12451   vector = Vwith_echo_area_save_vector;
 12452   Vwith_echo_area_save_vector = Qnil;
 12453 
 12454   if (NILP (vector))
 12455     vector = make_nil_vector (11);
 12456 
 12457   XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
 12458   ASET (vector, i, Vdeactivate_mark); ++i;
 12459   ASET (vector, i, make_fixnum (windows_or_buffers_changed)); ++i;
 12460 
 12461   if (w)
 12462     {
 12463       XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
 12464       ASET (vector, i, w->contents); ++i;
 12465       ASET (vector, i, make_fixnum (marker_position (w->pointm))); ++i;
 12466       ASET (vector, i, make_fixnum (marker_byte_position (w->pointm))); ++i;
 12467       ASET (vector, i, make_fixnum (marker_position (w->old_pointm))); ++i;
 12468       ASET (vector, i, make_fixnum (marker_byte_position (w->old_pointm))); ++i;
 12469       ASET (vector, i, make_fixnum (marker_position (w->start))); ++i;
 12470       ASET (vector, i, make_fixnum (marker_byte_position (w->start))); ++i;
 12471     }
 12472   else
 12473     {
 12474       int end = i + 8;
 12475       for (; i < end; ++i)
 12476         ASET (vector, i, Qnil);
 12477     }
 12478 
 12479   eassert (i == ASIZE (vector));
 12480   return vector;
 12481 }
 12482 
 12483 
 12484 /* Restore global state from VECTOR which was created by
 12485    with_echo_area_buffer_unwind_data.  */
 12486 
 12487 static void
 12488 unwind_with_echo_area_buffer (Lisp_Object vector)
 12489 {
 12490   set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
 12491   Vdeactivate_mark = AREF (vector, 1);
 12492   windows_or_buffers_changed = XFIXNAT (AREF (vector, 2));
 12493 
 12494   if (WINDOWP (AREF (vector, 3)))
 12495     {
 12496       struct window *w;
 12497       Lisp_Object buffer;
 12498 
 12499       w = XWINDOW (AREF (vector, 3));
 12500       buffer = AREF (vector, 4);
 12501 
 12502       wset_buffer (w, buffer);
 12503       set_marker_restricted_both (w->pointm, buffer,
 12504                                   XFIXNAT (AREF (vector, 5)),
 12505                                   XFIXNAT (AREF (vector, 6)));
 12506       set_marker_restricted_both (w->old_pointm, buffer,
 12507                                   XFIXNAT (AREF (vector, 7)),
 12508                                   XFIXNAT (AREF (vector, 8)));
 12509       set_marker_restricted_both (w->start, buffer,
 12510                                   XFIXNAT (AREF (vector, 9)),
 12511                                   XFIXNAT (AREF (vector, 10)));
 12512     }
 12513 
 12514   Vwith_echo_area_save_vector = vector;
 12515 }
 12516 
 12517 
 12518 /* Set up the echo area for use by print functions.  MULTIBYTE_P
 12519    means we will print multibyte.  */
 12520 
 12521 void
 12522 setup_echo_area_for_printing (bool multibyte_p)
 12523 {
 12524   /* If we can't find an echo area any more, exit.  */
 12525   if (! FRAME_LIVE_P (XFRAME (selected_frame)))
 12526     Fkill_emacs (Qnil, Qnil);
 12527 
 12528   ensure_echo_area_buffers ();
 12529 
 12530   if (!message_buf_print)
 12531     {
 12532       /* A message has been output since the last time we printed.
 12533          Choose a fresh echo area buffer.  */
 12534       if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12535         echo_area_buffer[0] = echo_buffer[1];
 12536       else
 12537         echo_area_buffer[0] = echo_buffer[0];
 12538 
 12539       /* Switch to that buffer and clear it.  */
 12540       set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12541       bset_truncate_lines (current_buffer, Qnil);
 12542 
 12543       if (Z > BEG)
 12544         {
 12545           specpdl_ref count = SPECPDL_INDEX ();
 12546           specbind (Qinhibit_read_only, Qt);
 12547           /* Note that undo recording is always disabled.  */
 12548           del_range (BEG, Z);
 12549           unbind_to (count, Qnil);
 12550         }
 12551       TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 12552 
 12553       /* Set up the buffer for the multibyteness we need.  We always
 12554          set it to be multibyte, except when
 12555          unibyte-display-via-language-environment is non-nil and the
 12556          buffer from which we are called is unibyte, because in that
 12557          case unibyte characters should not be displayed as octal
 12558          escapes.  */
 12559       if (unibyte_display_via_language_environment
 12560           && !multibyte_p
 12561           && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12562         Fset_buffer_multibyte (Qnil);
 12563       else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12564         Fset_buffer_multibyte (Qt);
 12565 
 12566       /* Raise the frame containing the echo area.  */
 12567       if (minibuffer_auto_raise)
 12568         {
 12569           struct frame *sf = SELECTED_FRAME ();
 12570           Lisp_Object mini_window;
 12571           mini_window = FRAME_MINIBUF_WINDOW (sf);
 12572           Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
 12573         }
 12574 
 12575       message_log_maybe_newline ();
 12576       message_buf_print = true;
 12577     }
 12578   else
 12579     {
 12580       if (NILP (echo_area_buffer[0]))
 12581         {
 12582           if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12583             echo_area_buffer[0] = echo_buffer[1];
 12584           else
 12585             echo_area_buffer[0] = echo_buffer[0];
 12586         }
 12587 
 12588       if (current_buffer != XBUFFER (echo_area_buffer[0]))
 12589         {
 12590           /* Someone switched buffers between print requests.  */
 12591           set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12592           bset_truncate_lines (current_buffer, Qnil);
 12593         }
 12594     }
 12595 }
 12596 
 12597 
 12598 /* Display an echo area message in window W.  Value is true if W's
 12599    height is changed.  If display_last_displayed_message_p,
 12600    display the message that was last displayed, otherwise
 12601    display the current message.  */
 12602 
 12603 static bool
 12604 display_echo_area (struct window *w)
 12605 {
 12606   bool no_message_p, window_height_changed_p;
 12607 
 12608   /* Temporarily disable garbage collections while displaying the echo
 12609      area.  This is done because a GC can print a message itself.
 12610      That message would modify the echo area buffer's contents while a
 12611      redisplay of the buffer is going on, and seriously confuse
 12612      redisplay.  */
 12613   specpdl_ref count = inhibit_garbage_collection ();
 12614 
 12615   /* If there is no message, we must call display_echo_area_1
 12616      nevertheless because it resizes the window.  But we will have to
 12617      reset the echo_area_buffer in question to nil at the end because
 12618      with_echo_area_buffer will set it to an empty buffer.  */
 12619   bool i = display_last_displayed_message_p;
 12620   /* According to the C99, C11 and C++11 standards, the integral value
 12621      of a "bool" is always 0 or 1, so this array access is safe here,
 12622      if oddly typed. */
 12623   no_message_p = NILP (echo_area_buffer[i]);
 12624 
 12625   window_height_changed_p
 12626     = with_echo_area_buffer (w, display_last_displayed_message_p,
 12627                              display_echo_area_1, w, Qnil);
 12628 
 12629   if (no_message_p)
 12630     echo_area_buffer[i] = Qnil;
 12631 
 12632   unbind_to (count, Qnil);
 12633   return window_height_changed_p;
 12634 }
 12635 
 12636 
 12637 /* Helper for display_echo_area.  Display the current buffer which
 12638    contains the current echo area message in window W, a mini-window,
 12639    a pointer to which is passed in A1.  A2 is currently not used.
 12640    Change the height of W so that all of the message is displayed.
 12641    Value is true if height of W was changed.  */
 12642 
 12643 static bool
 12644 display_echo_area_1 (void *a1, Lisp_Object a2)
 12645 {
 12646   struct window *w = a1;
 12647   Lisp_Object window;
 12648   struct text_pos start;
 12649 
 12650   /* We are about to enter redisplay without going through
 12651      redisplay_internal, so we need to forget these faces by hand
 12652      here.  */
 12653   forget_escape_and_glyphless_faces ();
 12654 
 12655   /* Do this before displaying, so that we have a large enough glyph
 12656      matrix for the display.  If we can't get enough space for the
 12657      whole text, display the last N lines.  That works by setting w->start.  */
 12658   bool window_height_changed_p = resize_mini_window (w, false);
 12659 
 12660   /* Use the starting position chosen by resize_mini_window.  */
 12661   SET_TEXT_POS_FROM_MARKER (start, w->start);
 12662 
 12663   /* Display.  */
 12664   clear_glyph_matrix (w->desired_matrix);
 12665   XSETWINDOW (window, w);
 12666   void *itdata = bidi_shelve_cache ();
 12667   try_window (window, start, 0);
 12668   bidi_unshelve_cache (itdata, false);
 12669 
 12670   return window_height_changed_p;
 12671 }
 12672 
 12673 
 12674 /* Resize the echo area window to exactly the size needed for the
 12675    currently displayed message, if there is one.  If a mini-buffer
 12676    is active, don't shrink it.  */
 12677 
 12678 void
 12679 resize_echo_area_exactly (void)
 12680 {
 12681   if (BUFFERP (echo_area_buffer[0])
 12682       && WINDOWP (echo_area_window))
 12683     {
 12684       struct window *w = XWINDOW (echo_area_window);
 12685       Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
 12686       bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
 12687                                               w, resize_exactly);
 12688       if (resized_p)
 12689         {
 12690           windows_or_buffers_changed = 42;
 12691           update_mode_lines = 30;
 12692           redisplay_internal ();
 12693         }
 12694     }
 12695 }
 12696 
 12697 
 12698 /* Callback function for with_echo_area_buffer, when used from
 12699    resize_echo_area_exactly.  A1 contains a pointer to the window to
 12700    resize, EXACTLY non-nil means resize the mini-window exactly to the
 12701    size of the text displayed.  Value is what resize_mini_window
 12702    returns.  */
 12703 
 12704 static bool
 12705 resize_mini_window_1 (void *a1, Lisp_Object exactly)
 12706 {
 12707   return resize_mini_window (a1, !NILP (exactly));
 12708 }
 12709 
 12710 
 12711 /* Resize mini-window W to fit the size of its contents.  EXACT_P
 12712    means size the window exactly to the size needed.  Otherwise, it's
 12713    only enlarged until W's buffer is empty.
 12714 
 12715    Set W->start to the right place to begin display.  If the whole
 12716    contents fit, start at the beginning.  Otherwise, start so as
 12717    to make the end of the contents appear.  This is particularly
 12718    important for y-or-n-p, but seems desirable generally.
 12719 
 12720    Value is true if the window height has been changed.  */
 12721 
 12722 bool
 12723 resize_mini_window (struct window *w, bool exact_p)
 12724 {
 12725   struct frame *f = XFRAME (w->frame);
 12726   int old_height = WINDOW_BOX_TEXT_HEIGHT (w);
 12727 
 12728   eassert (MINI_WINDOW_P (w));
 12729 
 12730   /* Don't resize windows while redisplaying a window; it would
 12731      confuse redisplay functions when the size of the window they are
 12732      displaying changes from under them.  Such a resizing can happen,
 12733      for instance, when which-func prints a long message while
 12734      we are running fontification-functions.  We're running these
 12735      functions with safe_call which binds inhibit-redisplay to t.  */
 12736   if (!NILP (Vinhibit_redisplay))
 12737     return false;
 12738 
 12739   /* By default, start display at the beginning.  */
 12740   if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12741     set_marker_both (w->start, w->contents,
 12742                      BUF_BEGV (XBUFFER (w->contents)),
 12743                      BUF_BEGV_BYTE (XBUFFER (w->contents)));
 12744 
 12745   /* Nil means don't try to resize.  */
 12746   if ((NILP (Vresize_mini_windows)
 12747        && (NILP (resize_mini_frames) || !FRAME_MINIBUF_ONLY_P (f)))
 12748       || (FRAME_X_P (f) && FRAME_OUTPUT_DATA (f) == NULL))
 12749     return false;
 12750 
 12751   if (FRAME_MINIBUF_ONLY_P (f))
 12752     {
 12753       if (!NILP (resize_mini_frames))
 12754         safe_call1 (Qwindow__resize_mini_frame, WINDOW_FRAME (w));
 12755     }
 12756   else
 12757     {
 12758       struct it it;
 12759       int unit = FRAME_LINE_HEIGHT (f);
 12760       int height, max_height;
 12761       struct text_pos start;
 12762       struct buffer *old_current_buffer = NULL;
 12763       int windows_height = FRAME_INNER_HEIGHT (f);
 12764 
 12765       if (current_buffer != XBUFFER (w->contents))
 12766         {
 12767           old_current_buffer = current_buffer;
 12768           set_buffer_internal (XBUFFER (w->contents));
 12769         }
 12770 
 12771       init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
 12772 
 12773       /* Compute the max. number of lines specified by the user.  */
 12774       if (FLOATP (Vmax_mini_window_height))
 12775         max_height = XFLOAT_DATA (Vmax_mini_window_height) * windows_height;
 12776       else if (FIXNUMP (Vmax_mini_window_height))
 12777         max_height = XFIXNUM (Vmax_mini_window_height) * unit;
 12778       else
 12779         max_height = windows_height / 4;
 12780 
 12781       /* Correct that max. height if it's bogus.  */
 12782       max_height = clip_to_bounds (unit, max_height, windows_height);
 12783 
 12784       /* Find out the height of the text in the window.  */
 12785       last_height = 0;
 12786       move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
 12787       /* If move_it_to moved to the next visible line after EOB,
 12788          account for the height of the last full line.  */
 12789       if (it.max_ascent == 0 && it.max_descent == 0)
 12790         {
 12791           height = it.current_y;
 12792           /* Don't add the last line's height if lines are truncated
 12793              and the text doesn't end in a newline.
 12794              FIXME: if the text ends in a newline from a display
 12795              property or an overlay string, they lose: the mini-window
 12796              might not show the last empty line.  */
 12797           if (!(it.line_wrap == TRUNCATE
 12798                 && it.current_x <= it.first_visible_x
 12799                 && ZV_BYTE > 1
 12800                 && FETCH_BYTE (ZV_BYTE - 1) != '\n'))
 12801             height += last_height;
 12802         }
 12803       else
 12804         height = it.current_y + it.max_ascent + it.max_descent;
 12805       height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
 12806 
 12807       /* Compute a suitable window start.  */
 12808       if (height > max_height)
 12809         {
 12810           height = (max_height / unit) * unit;
 12811           if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12812             {
 12813               init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
 12814               move_it_vertically_backward (&it, height - unit);
 12815               /* The following move is usually a no-op when the stuff
 12816                  displayed in the mini-window comes entirely from buffer
 12817                  text, but it is needed when some of it comes from overlay
 12818                  strings, especially when there's an after-string at ZV.
 12819                  This happens with some completion packages, like
 12820                  icomplete, ido-vertical, etc.  With those packages, if we
 12821                  don't force w->start to be at the beginning of a screen
 12822                  line, important parts of the stuff in the mini-window,
 12823                  such as user prompt, will be hidden from view.  */
 12824               move_it_by_lines (&it, 0);
 12825               start = it.current.pos;
 12826               /* Prevent redisplay_window from recentering, and thus from
 12827                  overriding the window-start point we computed here.  */
 12828               w->start_at_line_beg = false;
 12829               SET_MARKER_FROM_TEXT_POS (w->start, start);
 12830             }
 12831         }
 12832       else
 12833         {
 12834           SET_TEXT_POS (start, BEGV, BEGV_BYTE);
 12835           SET_MARKER_FROM_TEXT_POS (w->start, start);
 12836         }
 12837 
 12838       if (EQ (Vresize_mini_windows, Qgrow_only))
 12839         {
 12840           /* Let it grow only, until we display an empty message, in which
 12841              case the window shrinks again.  */
 12842           if (height > old_height)
 12843             grow_mini_window (w, height - old_height);
 12844           else if (height < old_height && (exact_p || BEGV == ZV))
 12845             shrink_mini_window (w);
 12846         }
 12847       else if (height != old_height)
 12848         /* Always resize to exact size needed.  */
 12849         grow_mini_window (w, height - old_height);
 12850 
 12851       if (old_current_buffer)
 12852         set_buffer_internal (old_current_buffer);
 12853     }
 12854 
 12855   return WINDOW_BOX_TEXT_HEIGHT (w) != old_height;
 12856 }
 12857 
 12858 
 12859 /* Value is the current message, a string, or nil if there is no
 12860    current message.  */
 12861 
 12862 Lisp_Object
 12863 current_message (void)
 12864 {
 12865   Lisp_Object msg;
 12866 
 12867   if (!BUFFERP (echo_area_buffer[0]))
 12868     msg = Qnil;
 12869   else
 12870     {
 12871       with_echo_area_buffer (0, 0, current_message_1, &msg, Qnil);
 12872       if (NILP (msg))
 12873         echo_area_buffer[0] = Qnil;
 12874     }
 12875 
 12876   return msg;
 12877 }
 12878 
 12879 
 12880 static bool
 12881 current_message_1 (void *a1, Lisp_Object a2)
 12882 {
 12883   Lisp_Object *msg = a1;
 12884 
 12885   if (Z > BEG)
 12886     *msg = make_buffer_string (BEG, Z, true);
 12887   else
 12888     *msg = Qnil;
 12889   return false;
 12890 }
 12891 
 12892 
 12893 /* Push the current message on Vmessage_stack for later restoration
 12894    by restore_message.  Value is true if the current message isn't
 12895    empty.  This is a relatively infrequent operation, so it's not
 12896    worth optimizing.  */
 12897 
 12898 bool
 12899 push_message (void)
 12900 {
 12901   Lisp_Object msg = current_message ();
 12902   Vmessage_stack = Fcons (msg, Vmessage_stack);
 12903   return STRINGP (msg);
 12904 }
 12905 
 12906 
 12907 /* Restore message display from the top of Vmessage_stack.  */
 12908 
 12909 void
 12910 restore_message (void)
 12911 {
 12912   eassert (CONSP (Vmessage_stack));
 12913   message3_nolog (XCAR (Vmessage_stack));
 12914 }
 12915 
 12916 
 12917 /* Handler for unwind-protect calling pop_message.  */
 12918 
 12919 void
 12920 pop_message_unwind (void)
 12921 {
 12922   /* Pop the top-most entry off Vmessage_stack.  */
 12923   eassert (CONSP (Vmessage_stack));
 12924   Vmessage_stack = XCDR (Vmessage_stack);
 12925 }
 12926 
 12927 
 12928 /* Check that Vmessage_stack is nil.  Called from emacs.c when Emacs
 12929    exits.  If the stack is not empty, we have a missing
 12930    pop_message_unwind somewhere.  */
 12931 
 12932 void
 12933 check_message_stack (void)
 12934 {
 12935   if (!NILP (Vmessage_stack))
 12936     emacs_abort ();
 12937 }
 12938 
 12939 void
 12940 clear_message_stack (void)
 12941 {
 12942   Vmessage_stack = Qnil;
 12943 }
 12944 
 12945 /* Truncate to NCHARS what will be displayed in the echo area the next
 12946    time we display it---but don't redisplay it now.  */
 12947 
 12948 void
 12949 truncate_echo_area (ptrdiff_t nchars)
 12950 {
 12951   if (nchars == 0)
 12952     echo_area_buffer[0] = Qnil;
 12953   else if (!noninteractive
 12954            && INTERACTIVE
 12955            && !NILP (echo_area_buffer[0]))
 12956     {
 12957       struct frame *sf = SELECTED_FRAME ();
 12958       /* Error messages get reported properly by cmd_error, so this must be
 12959          just an informative message; if the frame hasn't really been
 12960          initialized yet, just toss it.  */
 12961       if (sf->glyphs_initialized_p)
 12962         with_echo_area_buffer (0, 0, truncate_message_1,
 12963                                (void *) (intptr_t) nchars, Qnil);
 12964     }
 12965 }
 12966 
 12967 
 12968 /* Helper function for truncate_echo_area.  Truncate the current
 12969    message to at most NCHARS characters.  */
 12970 
 12971 static bool
 12972 truncate_message_1 (void *a1, Lisp_Object a2)
 12973 {
 12974   intptr_t nchars = (intptr_t) a1;
 12975   if (BEG + nchars < Z)
 12976     del_range (BEG + nchars, Z);
 12977   if (Z == BEG)
 12978     echo_area_buffer[0] = Qnil;
 12979   return false;
 12980 }
 12981 
 12982 extern intptr_t garbage_collection_inhibited;
 12983 
 12984 /* Set the current message to STRING.  */
 12985 
 12986 static void
 12987 set_message (Lisp_Object string)
 12988 {
 12989   Lisp_Object message = Qnil;
 12990 
 12991   eassert (STRINGP (string));
 12992 
 12993   if (FUNCTIONP (Vset_message_function)
 12994       /* FIXME: (bug#63253) We should really make the regexp engine re-entrant,
 12995          but in the mean time, let's ignore `set-message-function` when
 12996          called from `probably_quit`.  */
 12997       && !garbage_collection_inhibited)
 12998     {
 12999       specpdl_ref count = SPECPDL_INDEX ();
 13000       specbind (Qinhibit_quit, Qt);
 13001       message = safe_call1 (Vset_message_function, string);
 13002       unbind_to (count, Qnil);
 13003 
 13004       if (STRINGP (message))
 13005         {
 13006           string = message;
 13007           message = Qnil;
 13008         }
 13009     }
 13010 
 13011   if (NILP (message))
 13012     {
 13013       message_enable_multibyte = STRING_MULTIBYTE (string);
 13014 
 13015       with_echo_area_buffer (0, -1, set_message_1, 0, string);
 13016       message_buf_print = false;
 13017       help_echo_showing_p = false;
 13018     }
 13019 
 13020   if (STRINGP (Vdebug_on_message)
 13021       && STRINGP (string)
 13022       && fast_string_match (Vdebug_on_message, string) >= 0)
 13023     call_debugger (list2 (Qerror, string));
 13024 }
 13025 
 13026 
 13027 /* Helper function for set_message.  First argument is ignored and second
 13028    argument has the same meaning as for set_message.
 13029    This function is called with the echo area buffer being current.  */
 13030 
 13031 static bool
 13032 set_message_1 (void *a1, Lisp_Object string)
 13033 {
 13034   eassert (STRINGP (string));
 13035 
 13036   /* Change multibyteness of the echo buffer appropriately.  We always
 13037      set it to be multibyte, except when
 13038      unibyte-display-via-language-environment is non-nil and the
 13039      string to display is unibyte, because in that case unibyte
 13040      characters should not be displayed as octal escapes.  */
 13041   if (!message_enable_multibyte
 13042       && unibyte_display_via_language_environment
 13043       && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13044     Fset_buffer_multibyte (Qnil);
 13045   else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13046     Fset_buffer_multibyte (Qt);
 13047 
 13048   bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
 13049   if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
 13050     bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
 13051 
 13052   /* Insert new message at BEG.  */
 13053   TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 13054 
 13055   /* This function takes care of single/multibyte conversion.
 13056      We just have to ensure that the echo area buffer has the right
 13057      setting of enable_multibyte_characters.  */
 13058   insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
 13059 
 13060   return false;
 13061 }
 13062 
 13063 
 13064 /* Clear messages.  CURRENT_P means clear the current message.
 13065    LAST_DISPLAYED_P means clear the message last displayed.  */
 13066 
 13067 void
 13068 clear_message (bool current_p, bool last_displayed_p)
 13069 {
 13070   Lisp_Object preserve = Qnil;
 13071 
 13072   if (current_p)
 13073     {
 13074       if (FUNCTIONP (Vclear_message_function)
 13075           /* FIXME: (bug#63253) Same as for `set-message-function` above.  */
 13076           && !garbage_collection_inhibited)
 13077         {
 13078           specpdl_ref count = SPECPDL_INDEX ();
 13079           specbind (Qinhibit_quit, Qt);
 13080           preserve = safe_call (1, Vclear_message_function);
 13081           unbind_to (count, Qnil);
 13082         }
 13083 
 13084       if (!EQ (preserve, Qdont_clear_message))
 13085         {
 13086           echo_area_buffer[0] = Qnil;
 13087           message_cleared_p = true;
 13088         }
 13089     }
 13090 
 13091   if (last_displayed_p)
 13092     echo_area_buffer[1] = Qnil;
 13093 
 13094   message_buf_print = false;
 13095 }
 13096 
 13097 /* Clear garbaged frames.
 13098 
 13099    This function is used where the old redisplay called
 13100    redraw_garbaged_frames which in turn called redraw_frame which in
 13101    turn called clear_frame.  The call to clear_frame was a source of
 13102    flickering.  I believe a clear_frame is not necessary.  It should
 13103    suffice in the new redisplay to invalidate all current matrices,
 13104    and ensure a complete redisplay of all windows.  */
 13105 
 13106 static void
 13107 clear_garbaged_frames (void)
 13108 {
 13109   if (frame_garbaged)
 13110     {
 13111       Lisp_Object tail, frame;
 13112       struct frame *sf = SELECTED_FRAME ();
 13113 
 13114       FOR_EACH_FRAME (tail, frame)
 13115         {
 13116           struct frame *f = XFRAME (frame);
 13117 
 13118           if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
 13119             {
 13120               if (f->resized_p
 13121                   /* It makes no sense to redraw a non-selected TTY
 13122                      frame, since that will actually clear the
 13123                      selected frame, and might leave the selected
 13124                      frame with corrupted display, if it happens not
 13125                      to be marked garbaged.  */
 13126                   && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
 13127                 redraw_frame (f);
 13128               else
 13129                 clear_current_matrices (f);
 13130 
 13131 #ifdef HAVE_WINDOW_SYSTEM
 13132               if (FRAME_WINDOW_P (f)
 13133                   && FRAME_RIF (f)->clear_under_internal_border)
 13134                 FRAME_RIF (f)->clear_under_internal_border (f);
 13135 #endif
 13136               fset_redisplay (f);
 13137               f->garbaged = false;
 13138               f->resized_p = false;
 13139             }
 13140         }
 13141 
 13142       frame_garbaged = false;
 13143     }
 13144 }
 13145 
 13146 
 13147 /* Redisplay the echo area of the selected frame.  If UPDATE_FRAME_P, update
 13148    selected_frame.  */
 13149 
 13150 static void
 13151 echo_area_display (bool update_frame_p)
 13152 {
 13153   Lisp_Object mini_window;
 13154   struct window *w;
 13155   struct frame *f;
 13156   bool window_height_changed_p = false;
 13157   struct frame *sf = SELECTED_FRAME ();
 13158 
 13159   mini_window = FRAME_MINIBUF_WINDOW (sf);
 13160   if (NILP (mini_window))
 13161     return;
 13162 
 13163   w = XWINDOW (mini_window);
 13164   f = XFRAME (WINDOW_FRAME (w));
 13165 
 13166   /* Don't display if frame is invisible or not yet initialized.  */
 13167   if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
 13168     return;
 13169 
 13170 #ifdef HAVE_WINDOW_SYSTEM
 13171   /* When Emacs starts, selected_frame may be the initial terminal
 13172      frame.  If we let this through, a message would be displayed on
 13173      the terminal.  */
 13174   if (FRAME_INITIAL_P (XFRAME (selected_frame)))
 13175     return;
 13176 #endif /* HAVE_WINDOW_SYSTEM */
 13177 
 13178   /* Redraw garbaged frames.  */
 13179   clear_garbaged_frames ();
 13180 
 13181   if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
 13182     {
 13183       echo_area_window = mini_window;
 13184       window_height_changed_p = display_echo_area (w);
 13185       w->must_be_updated_p = true;
 13186 
 13187       /* Update the display, unless called from redisplay_internal.
 13188          Also don't update the screen during redisplay itself.  The
 13189          update will happen at the end of redisplay, and an update
 13190          here could cause confusion.  */
 13191       if (update_frame_p && !redisplaying_p)
 13192         {
 13193           int n = 0;
 13194 
 13195           /* If the display update has been interrupted by pending
 13196              input, update mode lines in the frame.  Due to the
 13197              pending input, it might have been that redisplay hasn't
 13198              been called, so that mode lines above the echo area are
 13199              garbaged.  This looks odd, so we prevent it here.  */
 13200           if (!display_completed)
 13201             {
 13202               n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
 13203 
 13204 #ifdef HAVE_WINDOW_SYSTEM
 13205               if (FRAME_WINDOW_P (f)
 13206                   && FRAME_RIF (f)->clear_under_internal_border)
 13207                 FRAME_RIF (f)->clear_under_internal_border (f);
 13208 #endif
 13209             }
 13210 
 13211           if (window_height_changed_p
 13212               /* Don't do this if Emacs is shutting down.  Redisplay
 13213                  needs to run hooks.  */
 13214               && !NILP (Vrun_hooks))
 13215             {
 13216               /* Must update other windows.  Likewise as in other
 13217                  cases, don't let this update be interrupted by
 13218                  pending input.  */
 13219               specpdl_ref count = SPECPDL_INDEX ();
 13220               specbind (Qredisplay_dont_pause, Qt);
 13221               fset_redisplay (f);
 13222               redisplay_internal ();
 13223               unbind_to (count, Qnil);
 13224             }
 13225           else if (FRAME_WINDOW_P (f) && n == 0)
 13226             {
 13227               /* Window configuration is the same as before.
 13228                  Can do with a display update of the echo area,
 13229                  unless we displayed some mode lines.  */
 13230               update_single_window (w);
 13231               flush_frame (f);
 13232             }
 13233           else
 13234             update_frame (f, true, true);
 13235 
 13236           /* If cursor is in the echo area, make sure that the next
 13237              redisplay displays the minibuffer, so that the cursor will
 13238              be replaced with what the minibuffer wants.  */
 13239           if (cursor_in_echo_area)
 13240             wset_redisplay (XWINDOW (mini_window));
 13241         }
 13242     }
 13243   else if (!EQ (mini_window, selected_window))
 13244     wset_redisplay (XWINDOW (mini_window));
 13245 
 13246   /* Last displayed message is now the current message.  */
 13247   echo_area_buffer[1] = echo_area_buffer[0];
 13248   /* Inform read_char that we're not echoing.  */
 13249   echo_message_buffer = Qnil;
 13250 
 13251   /* Prevent redisplay optimization in redisplay_internal by resetting
 13252      this_line_start_pos.  This is done because the mini-buffer now
 13253      displays the message instead of its buffer text.  */
 13254   if (EQ (mini_window, selected_window))
 13255     CHARPOS (this_line_start_pos) = 0;
 13256 
 13257   if (window_height_changed_p)
 13258     {
 13259       fset_redisplay (f);
 13260 
 13261       /* If window configuration was changed, frames may have been
 13262          marked garbaged.  Clear them or we will experience
 13263          surprises wrt scrolling.
 13264          FIXME: How/why/when?  */
 13265       clear_garbaged_frames ();
 13266     }
 13267 }
 13268 
 13269 /* True if W's buffer was changed but not saved.  */
 13270 
 13271 static bool
 13272 window_buffer_changed (struct window *w)
 13273 {
 13274   struct buffer *b = XBUFFER (w->contents);
 13275 
 13276   eassert (BUFFER_LIVE_P (b));
 13277 
 13278   return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
 13279 }
 13280 
 13281 /* True if W has %c or %C in its mode line and mode line should be updated.  */
 13282 
 13283 static bool
 13284 mode_line_update_needed (struct window *w)
 13285 {
 13286   return (w->column_number_displayed != -1
 13287           && !(PT == w->last_point && !window_outdated (w))
 13288           && (w->column_number_displayed != current_column ()));
 13289 }
 13290 
 13291 /* True if window start of W is frozen and may not be changed during
 13292    redisplay.  */
 13293 
 13294 static bool
 13295 window_frozen_p (struct window *w)
 13296 {
 13297   if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
 13298     {
 13299       Lisp_Object window;
 13300 
 13301       XSETWINDOW (window, w);
 13302       if (MINI_WINDOW_P (w))
 13303         return false;
 13304       else if (EQ (window, selected_window))
 13305         return false;
 13306       else if (MINI_WINDOW_P (XWINDOW (selected_window))
 13307                && EQ (window, Vminibuf_scroll_window))
 13308         /* This special window can't be frozen too.  */
 13309         return false;
 13310       else
 13311         return true;
 13312     }
 13313   return false;
 13314 }
 13315 
 13316 /***********************************************************************
 13317                      Mode Lines and Frame Titles
 13318  ***********************************************************************/
 13319 
 13320 /* A buffer for constructing non-propertized mode-line strings and
 13321    frame titles in it; allocated from the heap in init_xdisp and
 13322    resized as needed in store_mode_line_noprop_char.  */
 13323 
 13324 static char *mode_line_noprop_buf;
 13325 
 13326 /* The buffer's end, and a current output position in it.  */
 13327 
 13328 static char *mode_line_noprop_buf_end;
 13329 static char *mode_line_noprop_ptr;
 13330 
 13331 #define MODE_LINE_NOPROP_LEN(start) \
 13332   ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
 13333 
 13334 static enum {
 13335   MODE_LINE_DISPLAY = 0,
 13336   MODE_LINE_TITLE,
 13337   MODE_LINE_NOPROP,
 13338   MODE_LINE_STRING
 13339 } mode_line_target;
 13340 
 13341 /* Alist that caches the results of :propertize.
 13342    Each element is (PROPERTIZED-STRING . PROPERTY-LIST).  */
 13343 static Lisp_Object mode_line_proptrans_alist;
 13344 
 13345 /* List of strings making up the mode-line.  */
 13346 static Lisp_Object mode_line_string_list;
 13347 
 13348 /* Base face property when building propertized mode line string.  */
 13349 static Lisp_Object mode_line_string_face;
 13350 static Lisp_Object mode_line_string_face_prop;
 13351 
 13352 
 13353 /* Unwind data for mode line strings */
 13354 
 13355 static Lisp_Object Vmode_line_unwind_vector;
 13356 
 13357 static Lisp_Object
 13358 format_mode_line_unwind_data (struct frame *target_frame,
 13359                               struct buffer *obuf,
 13360                               Lisp_Object owin,
 13361                               bool save_proptrans)
 13362 {
 13363   Lisp_Object vector, tmp;
 13364 
 13365   /* Reduce consing by keeping one vector in
 13366      Vwith_echo_area_save_vector.  */
 13367   vector = Vmode_line_unwind_vector;
 13368   Vmode_line_unwind_vector = Qnil;
 13369 
 13370   if (NILP (vector))
 13371     vector = make_nil_vector (12);
 13372 
 13373   ASET (vector, 0, make_fixnum (mode_line_target));
 13374   ASET (vector, 1, make_fixnum (MODE_LINE_NOPROP_LEN (0)));
 13375   ASET (vector, 2, mode_line_string_list);
 13376   ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
 13377   ASET (vector, 4, mode_line_string_face);
 13378   ASET (vector, 5, mode_line_string_face_prop);
 13379 
 13380   if (obuf)
 13381     XSETBUFFER (tmp, obuf);
 13382   else
 13383     tmp = Qnil;
 13384   ASET (vector, 6, tmp);
 13385   ASET (vector, 7, owin);
 13386   if (target_frame)
 13387     {
 13388       Lisp_Object buffer = XWINDOW (target_frame->selected_window)->contents;
 13389       struct buffer *b = XBUFFER (buffer);
 13390       struct buffer *cb = current_buffer;
 13391 
 13392       /* Similarly to `with-selected-window', if the operation selects
 13393          a window on another frame, we must restore that frame's
 13394          selected window, and (for a tty) the top-frame.  */
 13395       ASET (vector, 8, target_frame->selected_window);
 13396       if (FRAME_TERMCAP_P (target_frame))
 13397         ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
 13398 
 13399       /* If we select a window on another frame, make sure that that
 13400          selection does not leave its buffer's point modified when
 13401          unwinding (Bug#32777).  */
 13402       ASET (vector, 10, buffer);
 13403       current_buffer = b;
 13404       ASET (vector, 11, build_marker (current_buffer, PT, PT_BYTE));
 13405       current_buffer = cb;
 13406     }
 13407 
 13408   return vector;
 13409 }
 13410 
 13411 static void
 13412 unwind_format_mode_line (Lisp_Object vector)
 13413 {
 13414   Lisp_Object old_window = AREF (vector, 7);
 13415   Lisp_Object target_frame_window = AREF (vector, 8);
 13416   Lisp_Object old_top_frame = AREF (vector, 9);
 13417 
 13418   mode_line_target = XFIXNUM (AREF (vector, 0));
 13419   mode_line_noprop_ptr = mode_line_noprop_buf + XFIXNUM (AREF (vector, 1));
 13420   mode_line_string_list = AREF (vector, 2);
 13421   if (! EQ (AREF (vector, 3), Qt))
 13422     mode_line_proptrans_alist = AREF (vector, 3);
 13423   mode_line_string_face = AREF (vector, 4);
 13424   mode_line_string_face_prop = AREF (vector, 5);
 13425 
 13426   /* Select window before buffer, since it may change the buffer.  */
 13427   if (WINDOW_LIVE_P (old_window))
 13428     {
 13429       /* If the operation that we are unwinding had selected a window
 13430          on a different frame, reset its frame-selected-window.  For a
 13431          text terminal, reset its top-frame if necessary.  */
 13432       if (WINDOW_LIVE_P (target_frame_window))
 13433         {
 13434           Lisp_Object frame
 13435             = WINDOW_FRAME (XWINDOW (target_frame_window));
 13436 
 13437           if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
 13438             Fselect_window (target_frame_window, Qt);
 13439 
 13440           if (!NILP (old_top_frame) && !EQ (old_top_frame, frame)
 13441               /* This could've been destroyed during the formatting,
 13442                  possibly because the terminal was deleted.  */
 13443               && FRAME_LIVE_P (XFRAME (old_top_frame)))
 13444             Fselect_frame (old_top_frame, Qt);
 13445         }
 13446 
 13447       Fselect_window (old_window, Qt);
 13448 
 13449       /* Restore point of target_frame_window's buffer (Bug#32777).
 13450          But do this only after old_window has been reselected to
 13451          avoid that the window point of target_frame_window moves.  */
 13452       if (WINDOW_LIVE_P (target_frame_window))
 13453         {
 13454           Lisp_Object buffer = AREF (vector, 10);
 13455 
 13456           if (BUFFER_LIVE_P (XBUFFER (buffer)))
 13457             {
 13458               struct buffer *cb = current_buffer;
 13459 
 13460               current_buffer = XBUFFER (buffer);
 13461               set_point_from_marker (AREF (vector, 11));
 13462               ASET (vector, 11, Qnil);
 13463               current_buffer = cb;
 13464             }
 13465         }
 13466     }
 13467 
 13468   if (!NILP (AREF (vector, 6)))
 13469     {
 13470       set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
 13471       ASET (vector, 6, Qnil);
 13472     }
 13473 
 13474   Vmode_line_unwind_vector = vector;
 13475 }
 13476 
 13477 
 13478 /* Store a single character C for the frame title in mode_line_noprop_buf.
 13479    Re-allocate mode_line_noprop_buf if necessary.  */
 13480 
 13481 static void
 13482 store_mode_line_noprop_char (char c)
 13483 {
 13484   /* If output position has reached the end of the allocated buffer,
 13485      increase the buffer's size.  */
 13486   if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
 13487     {
 13488       ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
 13489       ptrdiff_t size = len;
 13490       mode_line_noprop_buf =
 13491         xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
 13492       mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 13493       mode_line_noprop_ptr = mode_line_noprop_buf + len;
 13494     }
 13495 
 13496   *mode_line_noprop_ptr++ = c;
 13497 }
 13498 
 13499 
 13500 /* Store part of a frame title in mode_line_noprop_buf, beginning at
 13501    mode_line_noprop_ptr.  STRING is the string to store.  Do not copy
 13502    characters that yield more columns than PRECISION; PRECISION <= 0
 13503    means copy the whole string.  Pad with spaces until FIELD_WIDTH
 13504    number of characters have been copied; FIELD_WIDTH <= 0 means don't
 13505    pad.  Called from display_mode_element when it is used to build a
 13506    frame title.  */
 13507 
 13508 static int
 13509 store_mode_line_noprop (const char *string, int field_width, int precision)
 13510 {
 13511   const unsigned char *str = (const unsigned char *) string;
 13512   int n = 0;
 13513   ptrdiff_t dummy, nbytes;
 13514 
 13515   /* Copy at most PRECISION chars from STR.  */
 13516   nbytes = strlen (string);
 13517   n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
 13518   while (nbytes--)
 13519     store_mode_line_noprop_char (*str++);
 13520 
 13521   /* Fill up with spaces until FIELD_WIDTH reached.  */
 13522   while (field_width > 0
 13523          && n < field_width)
 13524     {
 13525       store_mode_line_noprop_char (' ');
 13526       ++n;
 13527     }
 13528 
 13529   return n;
 13530 }
 13531 
 13532 /***********************************************************************
 13533                              Frame Titles
 13534  ***********************************************************************/
 13535 
 13536 #ifdef HAVE_WINDOW_SYSTEM
 13537 
 13538 /* Set the title of FRAME, if it has changed.  The title format is
 13539    Vicon_title_format if FRAME is iconified, otherwise it is
 13540    frame_title_format.  */
 13541 
 13542 void
 13543 gui_consider_frame_title (Lisp_Object frame)
 13544 {
 13545   struct frame *f = XFRAME (frame);
 13546   Lisp_Object format_data;
 13547 
 13548   if ((FRAME_WINDOW_P (f)
 13549        || FRAME_MINIBUF_ONLY_P (f)
 13550        || f->explicit_name)
 13551       && !FRAME_TOOLTIP_P (f))
 13552     {
 13553       /* Do we have more than one visible frame on this X display?  */
 13554       Lisp_Object tail, other_frame, fmt;
 13555       ptrdiff_t title_start;
 13556       char *title;
 13557       ptrdiff_t len;
 13558       struct it it;
 13559       specpdl_ref count = SPECPDL_INDEX ();
 13560 
 13561       FOR_EACH_FRAME (tail, other_frame)
 13562         {
 13563           struct frame *tf = XFRAME (other_frame);
 13564 
 13565           if (tf != f
 13566               && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
 13567               && !FRAME_MINIBUF_ONLY_P (tf)
 13568               && !FRAME_PARENT_FRAME (tf)
 13569               && !FRAME_TOOLTIP_P (tf)
 13570               && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
 13571             break;
 13572         }
 13573 
 13574       /* Set global variable indicating that multiple frames exist.  */
 13575       multiple_frames = CONSP (tail);
 13576 
 13577       /* select-frame calls resize_mini_window, which could resize the
 13578          mini-window and by that undo the effect of this redisplay
 13579          cycle wrt minibuffer and echo-area display.  Binding
 13580          inhibit-redisplay to t makes the call to resize_mini_window a
 13581          no-op, thus avoiding the adverse side effects.  */
 13582 
 13583       /* The following was moved before the record_unwind_protect form
 13584          below to inhibit redisplay also when restoring the selected
 13585          window/frame: This avoids that resize_mini_window sizes back
 13586          the minibuffer window of a temporarily selected frame.  See
 13587          Bug#34317.  */
 13588       specbind (Qinhibit_redisplay, Qt);
 13589 
 13590       /* Switch to the buffer of selected window of the frame.  Set up
 13591          mode_line_target so that display_mode_element will output
 13592          into mode_line_noprop_buf; then display the title.  Save the
 13593          original frame and selected window, and possibly the topmost
 13594          frame of the tty (for tty frames) into a vector; it will be
 13595          restored later.  */
 13596 
 13597       format_data = format_mode_line_unwind_data (f, current_buffer,
 13598                                                   selected_window,
 13599                                                   false);
 13600       record_unwind_protect (unwind_format_mode_line, format_data);
 13601 
 13602       Fselect_window (f->selected_window, Qt);
 13603       set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->contents));
 13604       fmt = (FRAME_ICONIFIED_P (f) && !EQ (Vicon_title_format, Qt)
 13605              ? Vicon_title_format : Vframe_title_format);
 13606 
 13607       mode_line_target = MODE_LINE_TITLE;
 13608       title_start = MODE_LINE_NOPROP_LEN (0);
 13609       init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
 13610                      NULL, DEFAULT_FACE_ID);
 13611       display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
 13612       len = MODE_LINE_NOPROP_LEN (title_start);
 13613       title = mode_line_noprop_buf + title_start;
 13614       /* Make sure that any raw bytes in the title are properly
 13615          represented by their multibyte sequences.  */
 13616       ptrdiff_t nchars = 0;
 13617       len = str_as_multibyte ((unsigned char *) title,
 13618                               mode_line_noprop_buf_end - title,
 13619                               len, &nchars);
 13620       unbind_to (count, Qnil);
 13621 
 13622       /* Set the title only if it's changed.  This avoids consing in
 13623          the common case where it hasn't.  (If it turns out that we've
 13624          already wasted too much time by walking through the list with
 13625          display_mode_element, then we might need to optimize at a
 13626          higher level than this.)  */
 13627       if ((! STRINGP (f->name)
 13628            || SBYTES (f->name) != len
 13629            || memcmp (title, SDATA (f->name), len) != 0)
 13630           && FRAME_TERMINAL (f)->implicit_set_name_hook)
 13631         {
 13632           Lisp_Object title_string = make_multibyte_string (title, nchars, len);
 13633           FRAME_TERMINAL (f)->implicit_set_name_hook (f, title_string, Qnil);
 13634         }
 13635     }
 13636 }
 13637 
 13638 #endif /* not HAVE_WINDOW_SYSTEM */
 13639 
 13640 
 13641 /***********************************************************************
 13642                               Menu Bars
 13643  ***********************************************************************/
 13644 
 13645 /* True if we will not redisplay all visible windows.  */
 13646 #define REDISPLAY_SOME_P()                              \
 13647   ((windows_or_buffers_changed == 0                     \
 13648     || windows_or_buffers_changed == REDISPLAY_SOME)    \
 13649    && (update_mode_lines == 0                           \
 13650        || update_mode_lines == REDISPLAY_SOME))
 13651 
 13652 static bool
 13653 needs_no_redisplay (struct window *w)
 13654 {
 13655   struct buffer *buffer = XBUFFER (w->contents);
 13656   struct frame *f = XFRAME (w->frame);
 13657   return (REDISPLAY_SOME_P ()
 13658           && !w->redisplay
 13659           && !w->update_mode_line
 13660           && !f->face_change
 13661           && !f->redisplay
 13662           && !buffer->text->redisplay
 13663           && window_point (w) == w->last_point);
 13664 }
 13665 
 13666 /* Prepare for redisplay by updating menu-bar item lists when
 13667    appropriate.  This can call eval.  */
 13668 
 13669 static void
 13670 prepare_menu_bars (void)
 13671 {
 13672   bool all_windows = windows_or_buffers_changed || update_mode_lines;
 13673   bool some_windows = REDISPLAY_SOME_P ();
 13674 
 13675   if (FUNCTIONP (Vpre_redisplay_function))
 13676     {
 13677       Lisp_Object windows = all_windows ? Qt : Qnil;
 13678       if (all_windows && some_windows)
 13679         {
 13680           Lisp_Object ws = window_list ();
 13681           for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
 13682             {
 13683               Lisp_Object this = XCAR (ws);
 13684               struct window *w = XWINDOW (this);
 13685               /* Cf. conditions for redisplaying a window at the
 13686                  beginning of redisplay_window.  */
 13687               if (!needs_no_redisplay (w))
 13688                 windows = Fcons (this, windows);
 13689             }
 13690         }
 13691       safe__call1 (true, Vpre_redisplay_function, windows);
 13692     }
 13693 
 13694   /* Update all frame titles based on their buffer names, etc.  We do
 13695      this before the menu bars so that the buffer-menu will show the
 13696      up-to-date frame titles.  */
 13697 #ifdef HAVE_WINDOW_SYSTEM
 13698   if (all_windows)
 13699     {
 13700       Lisp_Object tail, frame;
 13701 
 13702       FOR_EACH_FRAME (tail, frame)
 13703         {
 13704           struct frame *f = XFRAME (frame);
 13705           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13706           if (some_windows
 13707               && !f->redisplay
 13708               && !w->redisplay
 13709               && !XBUFFER (w->contents)->text->redisplay)
 13710             continue;
 13711 
 13712           if (!FRAME_TOOLTIP_P (f)
 13713               && (FRAME_ICONIFIED_P (f)
 13714                   || FRAME_VISIBLE_P (f) == 1
 13715                   /* Exclude TTY frames that are obscured because they
 13716                      are not the top frame on their console.  This is
 13717                      because gui_consider_frame_title actually switches
 13718                      to the frame, which for TTY frames means it is
 13719                      marked as garbaged, and will be completely
 13720                      redrawn on the next redisplay cycle.  This causes
 13721                      TTY frames to be completely redrawn, when there
 13722                      are more than one of them, even though nothing
 13723                      should be changed on display.  */
 13724                   || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
 13725             gui_consider_frame_title (frame);
 13726         }
 13727     }
 13728 #endif /* HAVE_WINDOW_SYSTEM */
 13729 
 13730   /* Update the menu bar item lists, if appropriate.  This has to be
 13731      done before any actual redisplay or generation of display lines.  */
 13732 
 13733   if (all_windows)
 13734     {
 13735       Lisp_Object tail, frame;
 13736       specpdl_ref count = SPECPDL_INDEX ();
 13737       /* True means that update_menu_bar has run its hooks
 13738          so any further calls to update_menu_bar shouldn't do so again.  */
 13739       bool menu_bar_hooks_run = false;
 13740 
 13741       record_unwind_save_match_data ();
 13742 
 13743       FOR_EACH_FRAME (tail, frame)
 13744         {
 13745           struct frame *f = XFRAME (frame);
 13746           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13747 
 13748           /* Ignore tooltip frame.  */
 13749           if (FRAME_TOOLTIP_P (f))
 13750             continue;
 13751 
 13752           if (some_windows
 13753               && !f->redisplay
 13754               && !w->redisplay
 13755               && !XBUFFER (w->contents)->text->redisplay)
 13756             continue;
 13757 
 13758           if (!FRAME_PARENT_FRAME (f))
 13759             menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
 13760 
 13761           update_tab_bar (f, false);
 13762 #ifdef HAVE_WINDOW_SYSTEM
 13763           update_tool_bar (f, false);
 13764 #endif
 13765         }
 13766 
 13767       unbind_to (count, Qnil);
 13768     }
 13769   else
 13770     {
 13771       struct frame *sf = SELECTED_FRAME ();
 13772 
 13773       if (!FRAME_PARENT_FRAME (sf))
 13774         update_menu_bar (sf, true, false);
 13775 
 13776       update_tab_bar (sf, true);
 13777 #ifdef HAVE_WINDOW_SYSTEM
 13778       update_tool_bar (sf, true);
 13779 #endif
 13780     }
 13781 }
 13782 
 13783 
 13784 /* Update the menu bar item list for frame F.  This has to be done
 13785    before we start to fill in any display lines, because it can call
 13786    eval.
 13787 
 13788    If SAVE_MATCH_DATA, we must save and restore it here.
 13789 
 13790    If HOOKS_RUN, a previous call to update_menu_bar
 13791    already ran the menu bar hooks for this redisplay, so there
 13792    is no need to run them again.  The return value is the
 13793    updated value of this flag, to pass to the next call.  */
 13794 
 13795 static bool
 13796 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
 13797 {
 13798   Lisp_Object window;
 13799   struct window *w;
 13800 
 13801   /* If called recursively during a menu update, do nothing.  This can
 13802      happen when, for instance, an activate-menubar-hook causes a
 13803      redisplay.  */
 13804   if (inhibit_menubar_update)
 13805     return hooks_run;
 13806 
 13807   window = FRAME_SELECTED_WINDOW (f);
 13808   w = XWINDOW (window);
 13809 
 13810   if (FRAME_WINDOW_P (f)
 13811       ?
 13812 #ifdef HAVE_EXT_MENU_BAR
 13813       FRAME_EXTERNAL_MENU_BAR (f)
 13814 #else
 13815       FRAME_MENU_BAR_LINES (f) > 0
 13816 #endif
 13817       : FRAME_MENU_BAR_LINES (f) > 0)
 13818     {
 13819       /* If the user has switched buffers or windows, we need to
 13820          recompute to reflect the new bindings.  But we'll
 13821          recompute when update_mode_lines is set too; that means
 13822          that people can use force-mode-line-update to request
 13823          that the menu bar be recomputed.  The adverse effect on
 13824          the rest of the redisplay algorithm is about the same as
 13825          windows_or_buffers_changed anyway.  */
 13826       if (windows_or_buffers_changed
 13827           /* This used to test w->update_mode_line, but we believe
 13828              there is no need to recompute the menu in that case.  */
 13829           || update_mode_lines
 13830           || window_buffer_changed (w))
 13831         {
 13832           struct buffer *prev = current_buffer;
 13833           specpdl_ref count = SPECPDL_INDEX ();
 13834 
 13835           specbind (Qinhibit_menubar_update, Qt);
 13836 
 13837           set_buffer_internal_1 (XBUFFER (w->contents));
 13838           if (save_match_data)
 13839             record_unwind_save_match_data ();
 13840           if (NILP (Voverriding_local_map_menu_flag))
 13841             {
 13842               specbind (Qoverriding_terminal_local_map, Qnil);
 13843               specbind (Qoverriding_local_map, Qnil);
 13844             }
 13845 
 13846           if (!hooks_run)
 13847             {
 13848               /* Run the Lucid hook.  */
 13849               safe_run_hooks (Qactivate_menubar_hook);
 13850 
 13851               /* If it has changed current-menubar from previous value,
 13852                  really recompute the menu-bar from the value.  */
 13853               safe_run_hooks (Qmenu_bar_update_hook);
 13854 
 13855               hooks_run = true;
 13856             }
 13857 
 13858           XSETFRAME (Vmenu_updating_frame, f);
 13859           fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
 13860 
 13861           /* Redisplay the menu bar in case we changed it.  */
 13862 #ifdef HAVE_EXT_MENU_BAR
 13863           if (FRAME_WINDOW_P (f))
 13864             {
 13865 #if defined (HAVE_NS)
 13866               /* All frames on Mac OS share the same menubar.  So only
 13867                  the selected frame should be allowed to set it.  */
 13868               if (f == SELECTED_FRAME ())
 13869 #endif
 13870                 set_frame_menubar (f, false);
 13871             }
 13872           else
 13873             /* On a terminal screen, the menu bar is an ordinary screen
 13874                line, and this makes it get updated.  */
 13875             w->update_mode_line = true;
 13876 #else /* ! (HAVE_EXT_MENU_BAR) */
 13877           /* In the non-toolkit version, the menu bar is an ordinary screen
 13878              line, and this makes it get updated.  */
 13879           w->update_mode_line = true;
 13880 #endif /* HAVE_EXT_MENU_BAR */
 13881 
 13882           unbind_to (count, Qnil);
 13883           set_buffer_internal_1 (prev);
 13884         }
 13885     }
 13886 
 13887   return hooks_run;
 13888 }
 13889 
 13890 
 13891 
 13892 /***********************************************************************
 13893                                Tab-bars
 13894  ***********************************************************************/
 13895 
 13896 /* Restore WINDOW as the selected window and its frame as the selected
 13897    frame.  If WINDOW is dead but the selected frame is live, make the
 13898    latter's selected window the selected window.  If both, WINDOW and
 13899    the selected frame, are dead, assign selected frame and window from
 13900    some arbitrary live frame.  Abort if no such frame can be found.  */
 13901 static void
 13902 restore_selected_window (Lisp_Object window)
 13903 {
 13904   if (WINDOW_LIVE_P (window))
 13905     /* If WINDOW is live, make it the selected window and its frame's
 13906        selected window and set the selected frame to its frame.  */
 13907     {
 13908       selected_window = window;
 13909       selected_frame = XWINDOW (window)->frame;
 13910       FRAME_SELECTED_WINDOW (XFRAME (selected_frame)) = window;
 13911     }
 13912   else if (FRAMEP (selected_frame) && FRAME_LIVE_P (XFRAME (selected_frame)))
 13913     /* If WINDOW is dead but the selected frame is still live, make the
 13914        latter's selected window the selected one.  */
 13915     selected_window = FRAME_SELECTED_WINDOW (XFRAME (selected_frame));
 13916   else
 13917     /* If WINDOW and the selected frame are dead, choose some live,
 13918        non-child and non-tooltip frame as the new selected frame and
 13919        make its selected window the selected window.  */
 13920     {
 13921       Lisp_Object tail;
 13922       Lisp_Object frame UNINIT;
 13923 
 13924       FOR_EACH_FRAME (tail, frame)
 13925         {
 13926           struct frame *f = XFRAME (frame);
 13927 
 13928           if (!FRAME_PARENT_FRAME (f) && !FRAME_TOOLTIP_P (f))
 13929             {
 13930               selected_frame = frame;
 13931               selected_window = FRAME_SELECTED_WINDOW (f);
 13932 
 13933               return;
 13934             }
 13935         }
 13936 
 13937       /* Abort if we cannot find a live frame.  */
 13938       emacs_abort ();
 13939     }
 13940 }
 13941 
 13942 /* Restore WINDOW, if live, as its frame's selected window.  */
 13943 static void
 13944 restore_frame_selected_window (Lisp_Object window)
 13945 {
 13946   if (WINDOW_LIVE_P (window))
 13947     /* If WINDOW is live, make it its frame's selected window.  If that
 13948        frame is the selected frame, make WINDOW the selected window as
 13949        well.  */
 13950     {
 13951       Lisp_Object frame = XWINDOW (window)->frame;
 13952 
 13953       FRAME_SELECTED_WINDOW (XFRAME (frame)) = window;
 13954       if (EQ (frame, selected_frame))
 13955         selected_window = window;
 13956     }
 13957 }
 13958 
 13959 /* Update the tab-bar item list for frame F.  This has to be done
 13960    before we start to fill in any display lines.  Called from
 13961    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 13962    and restore it here.  */
 13963 
 13964 static void
 13965 update_tab_bar (struct frame *f, bool save_match_data)
 13966 {
 13967   bool do_update = false;
 13968 
 13969 #ifdef HAVE_WINDOW_SYSTEM
 13970   if (FRAME_WINDOW_P (f) && WINDOWP (f->tab_bar_window)) {
 13971     if (WINDOW_TOTAL_LINES (XWINDOW (f->tab_bar_window)) > 0)
 13972       do_update = true;
 13973   }
 13974   else
 13975 #endif
 13976   if (FRAME_TAB_BAR_LINES (f) > 0)
 13977     do_update = true;
 13978 
 13979   if (do_update)
 13980     {
 13981       Lisp_Object window;
 13982       struct window *w;
 13983 
 13984       window = FRAME_SELECTED_WINDOW (f);
 13985       w = XWINDOW (window);
 13986 
 13987       /* If the user has switched buffers or windows, we need to
 13988          recompute to reflect the new bindings.  But we'll
 13989          recompute when update_mode_lines is set too; that means
 13990          that people can use force-mode-line-update to request
 13991          that the menu bar be recomputed.  The adverse effect on
 13992          the rest of the redisplay algorithm is about the same as
 13993          windows_or_buffers_changed anyway.  */
 13994       if (windows_or_buffers_changed
 13995           || w->update_mode_line
 13996           || update_mode_lines
 13997           || window_buffer_changed (w))
 13998         {
 13999           struct buffer *prev = current_buffer;
 14000           specpdl_ref count = SPECPDL_INDEX ();
 14001           Lisp_Object new_tab_bar;
 14002           int new_n_tab_bar;
 14003 
 14004           /* Set current_buffer to the buffer of the selected
 14005              window of the frame, so that we get the right local
 14006              keymaps.  */
 14007           set_buffer_internal_1 (XBUFFER (w->contents));
 14008 
 14009           /* Save match data, if we must.  */
 14010           if (save_match_data)
 14011             record_unwind_save_match_data ();
 14012 
 14013           /* Make sure that we don't accidentally use bogus keymaps.  */
 14014           if (NILP (Voverriding_local_map_menu_flag))
 14015             {
 14016               specbind (Qoverriding_terminal_local_map, Qnil);
 14017               specbind (Qoverriding_local_map, Qnil);
 14018             }
 14019 
 14020           /* We must temporarily set the selected frame to this frame
 14021              before calling tab_bar_items, because the calculation of
 14022              the tab-bar keymap uses the selected frame (see
 14023              `tab-bar-make-keymap' in tab-bar.el).  */
 14024           eassert (EQ (selected_window,
 14025                        /* Since we only explicitly preserve selected_frame,
 14026                           check that selected_window would be redundant.  */
 14027                        XFRAME (selected_frame)->selected_window));
 14028 #ifdef HAVE_WINDOW_SYSTEM
 14029           Lisp_Object frame;
 14030           record_unwind_protect (restore_selected_window, selected_window);
 14031           XSETFRAME (frame, f);
 14032           selected_frame = frame;
 14033           selected_window = FRAME_SELECTED_WINDOW (f);
 14034 #endif
 14035 
 14036           /* Build desired tab-bar items from keymaps.  */
 14037           new_tab_bar
 14038             = tab_bar_items (Fcopy_sequence (f->tab_bar_items),
 14039                               &new_n_tab_bar);
 14040 
 14041           /* Redisplay the tab-bar if we changed it.  */
 14042           if (new_n_tab_bar != f->n_tab_bar_items
 14043               || NILP (Fequal (new_tab_bar, f->tab_bar_items)))
 14044             {
 14045               /* Redisplay that happens asynchronously due to an expose event
 14046                  may access f->tab_bar_items.  Make sure we update both
 14047                  variables within BLOCK_INPUT so no such event interrupts.  */
 14048               block_input ();
 14049               fset_tab_bar_items (f, new_tab_bar);
 14050               f->n_tab_bar_items = new_n_tab_bar;
 14051               w->update_mode_line = true;
 14052               unblock_input ();
 14053             }
 14054 
 14055           unbind_to (count, Qnil);
 14056           set_buffer_internal_1 (prev);
 14057         }
 14058     }
 14059 }
 14060 
 14061 /* Redisplay the tab bar in the frame for window W.
 14062 
 14063    The tab bar of X frames that don't have X toolkit support is
 14064    displayed in a special window W->frame->tab_bar_window.
 14065 
 14066    The tab bar of terminal frames is treated specially as far as
 14067    glyph matrices are concerned.  Tab bar lines are not part of
 14068    windows, so the update is done directly on the frame matrix rows
 14069    for the tab bar.  */
 14070 
 14071 static void
 14072 display_tab_bar (struct window *w)
 14073 {
 14074   struct frame *f = XFRAME (WINDOW_FRAME (w));
 14075   struct it it;
 14076   Lisp_Object items;
 14077   int i;
 14078 
 14079   /* Don't do all this for graphical frames.  */
 14080 #ifdef HAVE_NTGUI
 14081   if (FRAME_W32_P (f))
 14082     return;
 14083 #endif
 14084 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14085   if (FRAME_X_P (f))
 14086     return;
 14087 #endif
 14088 
 14089 #ifdef HAVE_NS
 14090   if (FRAME_NS_P (f))
 14091     return;
 14092 #endif /* HAVE_NS */
 14093 
 14094 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14095   eassert (!FRAME_WINDOW_P (f));
 14096   init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14097                  + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14098                  TAB_BAR_FACE_ID);
 14099   it.first_visible_x = 0;
 14100   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14101 #elif defined (HAVE_X_WINDOWS) /* X without toolkit.  */
 14102   if (FRAME_WINDOW_P (f))
 14103     {
 14104       /* Tab bar lines are displayed in the desired matrix of the
 14105          dummy window tab_bar_window.  */
 14106       struct window *tab_w;
 14107       tab_w = XWINDOW (f->tab_bar_window);
 14108       init_iterator (&it, tab_w, -1, -1, tab_w->desired_matrix->rows,
 14109                      TAB_BAR_FACE_ID);
 14110       it.first_visible_x = 0;
 14111       it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14112     }
 14113   else
 14114 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 14115     {
 14116       /* This is a TTY frame, i.e. character hpos/vpos are used as
 14117          pixel x/y.  */
 14118       init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14119                      + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14120                      TAB_BAR_FACE_ID);
 14121       it.first_visible_x = 0;
 14122       it.last_visible_x = FRAME_COLS (f);
 14123     }
 14124 
 14125   /* FIXME: This should be controlled by a user option.  See the
 14126      comments in redisplay_tool_bar and display_mode_line about
 14127      this.  */
 14128   it.paragraph_embedding = L2R;
 14129 
 14130   /* Clear all rows of the tab bar.  */
 14131   for (i = 0; i < FRAME_TAB_BAR_LINES (f); ++i)
 14132     {
 14133       struct glyph_row *row = it.glyph_row + i;
 14134       clear_glyph_row (row);
 14135       row->enabled_p = true;
 14136       row->full_width_p = true;
 14137       row->reversed_p = false;
 14138     }
 14139 
 14140   /* Display all items of the tab bar.  */
 14141   items = it.f->tab_bar_items;
 14142   int j;
 14143   for (i = 0, j = 0; i < it.f->n_tab_bar_items; ++i, j += TAB_BAR_ITEM_NSLOTS)
 14144     {
 14145       Lisp_Object string = AREF (items, j + TAB_BAR_ITEM_CAPTION);
 14146 
 14147       /* Stop at nil string.  */
 14148       if (NILP (string))
 14149         break;
 14150 
 14151       if (it.current_x < it.last_visible_x)
 14152         display_string (NULL, string, Qnil, 0, 0, &it,
 14153                         SCHARS (string), 0, 0, STRING_MULTIBYTE (string));
 14154     }
 14155 
 14156   /* Fill out the line with spaces.  */
 14157   if (it.current_x < it.last_visible_x)
 14158     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 14159 
 14160   /* Compute the total height of the lines.  */
 14161   compute_line_metrics (&it);
 14162 }
 14163 
 14164 #ifdef HAVE_WINDOW_SYSTEM
 14165 
 14166 /* Set F->desired_tab_bar_string to a Lisp string representing frame
 14167    F's desired tab-bar contents.  F->tab_bar_items must have
 14168    been set up previously by calling prepare_menu_bars.  */
 14169 
 14170 static void
 14171 build_desired_tab_bar_string (struct frame *f)
 14172 {
 14173   int i;
 14174   Lisp_Object caption;
 14175 
 14176   caption = Qnil;
 14177 
 14178   /* Prepare F->desired_tab_bar_string.  Make a new string.  */
 14179   fset_desired_tab_bar_string (f, build_string (""));
 14180 
 14181   /* Put a `display' property on the string for the captions to display,
 14182      put a `menu_item' property on tab-bar items with a value that
 14183      is the index of the item in F's tab-bar item vector.  */
 14184   for (i = 0; i < f->n_tab_bar_items; ++i)
 14185     {
 14186 #define PROP(IDX) \
 14187   AREF (f->tab_bar_items, i * TAB_BAR_ITEM_NSLOTS + (IDX))
 14188 
 14189       caption = Fcopy_sequence (PROP (TAB_BAR_ITEM_CAPTION));
 14190 
 14191       /* Put a `display' text property on the string for the caption to
 14192          display.  Put a `menu-item' property on the string that gives
 14193          the start of this item's properties in the tab-bar items
 14194          vector.  */
 14195       AUTO_LIST2 (props, Qmenu_item, make_fixnum (i * TAB_BAR_ITEM_NSLOTS));
 14196 
 14197       Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14198                             props, caption);
 14199 
 14200       f->desired_tab_bar_string =
 14201         concat2 (f->desired_tab_bar_string, caption);
 14202 
 14203 #undef PROP
 14204     }
 14205 }
 14206 
 14207 
 14208 /* Display one line of the tab-bar of frame IT->f.
 14209 
 14210    HEIGHT specifies the desired height of the tab-bar line.
 14211    If the actual height of the glyph row is less than HEIGHT, the
 14212    row's height is increased to HEIGHT, and the icons are centered
 14213    vertically in the new height.
 14214 
 14215    If HEIGHT is -1, we are counting needed tab-bar lines, so don't
 14216    count a final empty row in case the tab-bar width exactly matches
 14217    the window width.
 14218 */
 14219 
 14220 static void
 14221 display_tab_bar_line (struct it *it, int height)
 14222 {
 14223   struct glyph_row *row = it->glyph_row;
 14224   int max_x = it->last_visible_x;
 14225   struct glyph *last;
 14226 
 14227   /* Don't extend on a previously drawn tab bar items (Bug#16058).  */
 14228   clear_glyph_row (row);
 14229   row->enabled_p = true;
 14230   row->y = it->current_y;
 14231 
 14232   /* Note that this isn't made use of if the face hasn't a box,
 14233      so there's no need to check the face here.  */
 14234   it->start_of_box_run_p = true;
 14235 
 14236   bool enough = false;
 14237   while (it->current_x < max_x)
 14238     {
 14239       int x, n_glyphs_before, i, nglyphs;
 14240       struct it it_before;
 14241 
 14242       /* Get the next display element.  */
 14243       if (!get_next_display_element (it))
 14244         {
 14245           /* Don't count empty row if we are counting needed tab-bar lines.  */
 14246           if (height < 0 && !it->hpos)
 14247             return;
 14248           break;
 14249         }
 14250 
 14251       /* Produce glyphs.  */
 14252       n_glyphs_before = row->used[TEXT_AREA];
 14253       it_before = *it;
 14254 
 14255       PRODUCE_GLYPHS (it);
 14256 
 14257       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 14258       i = 0;
 14259       x = it_before.current_x;
 14260       while (i < nglyphs)
 14261         {
 14262           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 14263 
 14264           if (x + glyph->pixel_width > max_x)
 14265             {
 14266               /* Glyph doesn't fit on line.  Backtrack.  */
 14267               row->used[TEXT_AREA] = n_glyphs_before;
 14268               *it = it_before;
 14269               /* If this is the only glyph on this line, it will never fit on the
 14270                  tab-bar, so skip it.  But ensure there is at least one glyph,
 14271                  so we don't accidentally disable the tab-bar.  */
 14272               if (n_glyphs_before == 0
 14273                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 14274                 break;
 14275               goto out;
 14276             }
 14277 
 14278           ++it->hpos;
 14279           x += glyph->pixel_width;
 14280           ++i;
 14281         }
 14282 
 14283       enough = ITERATOR_AT_END_OF_LINE_P (it);
 14284       set_iterator_to_next (it, true);
 14285 
 14286       /* Stop at line end.  */
 14287       if (enough)
 14288         break;
 14289     }
 14290 
 14291  out:;
 14292 
 14293   row->displays_text_p = row->used[TEXT_AREA] != 0;
 14294 
 14295   /* Use default face for the border below the tab bar.
 14296 
 14297      FIXME: When auto-resize-tab-bars is grow-only, there is
 14298      no additional border below the possibly empty tab-bar lines.
 14299      So to make the extra empty lines look "normal", we have to
 14300      use the tab-bar face for the border too.  */
 14301   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14302       && !EQ (Vauto_resize_tab_bars, Qgrow_only))
 14303     it->face_id = DEFAULT_FACE_ID;
 14304 
 14305   extend_face_to_end_of_line (it);
 14306   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 14307   last->right_box_line_p = true;
 14308   if (last == row->glyphs[TEXT_AREA])
 14309     last->left_box_line_p = true;
 14310 
 14311   /* Make line the desired height and center it vertically.  */
 14312   if ((height -= it->max_ascent + it->max_descent) > 0)
 14313     {
 14314       /* Don't add more than one line height.  */
 14315       height %= FRAME_LINE_HEIGHT (it->f);
 14316       it->max_ascent += height / 2;
 14317       it->max_descent += (height + 1) / 2;
 14318     }
 14319 
 14320   compute_line_metrics (it);
 14321 
 14322   /* If line is empty, make it occupy the rest of the tab-bar.  */
 14323   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 14324     {
 14325       row->height = row->phys_height = it->last_visible_y - row->y;
 14326       row->visible_height = row->height;
 14327       row->ascent = row->phys_ascent = 0;
 14328       row->extra_line_spacing = 0;
 14329     }
 14330 
 14331   row->full_width_p = true;
 14332   row->continued_p = false;
 14333   row->truncated_on_left_p = false;
 14334   row->truncated_on_right_p = false;
 14335 
 14336   it->current_x = it->hpos = 0;
 14337   it->current_y += row->height;
 14338   ++it->vpos;
 14339   ++it->glyph_row;
 14340 }
 14341 
 14342 
 14343 /* Value is the number of pixels needed to make all tab-bar items of
 14344    frame F visible.  The actual number of glyph rows needed is
 14345    returned in *N_ROWS if non-NULL.  */
 14346 static int
 14347 tab_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 14348 {
 14349   struct window *w = XWINDOW (f->tab_bar_window);
 14350   struct it it;
 14351   /* tab_bar_height is called from redisplay_tab_bar after building
 14352      the desired matrix, so use (unused) mode-line row as temporary row to
 14353      avoid destroying the first tab-bar row.  */
 14354   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 14355 
 14356   /* Initialize an iterator for iteration over
 14357      F->desired_tab_bar_string in the tab-bar window of frame F.  */
 14358   init_iterator (&it, w, -1, -1, temp_row, TAB_BAR_FACE_ID);
 14359   temp_row->reversed_p = false;
 14360   it.first_visible_x = 0;
 14361   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14362   reseat_to_string (&it, NULL, f->desired_tab_bar_string,
 14363                     0, 0, 0, STRING_MULTIBYTE (f->desired_tab_bar_string));
 14364   it.paragraph_embedding = L2R;
 14365 
 14366   clear_glyph_row (temp_row);
 14367   while (!ITERATOR_AT_END_P (&it))
 14368     {
 14369       it.glyph_row = temp_row;
 14370       display_tab_bar_line (&it, -1);
 14371     }
 14372   clear_glyph_row (temp_row);
 14373 
 14374   /* f->n_tab_bar_rows == 0 means "unknown"; -1 means no tab-bar.  */
 14375   if (n_rows)
 14376     *n_rows = it.vpos > 0 ? it.vpos : -1;
 14377 
 14378   if (pixelwise)
 14379     return it.current_y;
 14380   else
 14381     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 14382 }
 14383 
 14384 DEFUN ("tab-bar-height", Ftab_bar_height, Stab_bar_height,
 14385        0, 2, 0,
 14386        doc: /* Return the number of lines occupied by the tab bar of FRAME.
 14387 If FRAME is nil or omitted, use the selected frame.  Optional argument
 14388 PIXELWISE non-nil means return the height of the tab bar in pixels.  */)
 14389   (Lisp_Object frame, Lisp_Object pixelwise)
 14390 {
 14391   int height = 0;
 14392 
 14393   struct frame *f = decode_any_frame (frame);
 14394 
 14395   if (WINDOWP (f->tab_bar_window)
 14396       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tab_bar_window)) > 0)
 14397     {
 14398       update_tab_bar (f, true);
 14399       if (f->n_tab_bar_items)
 14400         {
 14401           build_desired_tab_bar_string (f);
 14402           height = tab_bar_height (f, NULL, !NILP (pixelwise));
 14403         }
 14404     }
 14405 
 14406   return make_fixnum (height);
 14407 }
 14408 
 14409 
 14410 /* Display the tab-bar of frame F.  Value is true if tab-bar's
 14411    height should be changed.  */
 14412 static bool
 14413 redisplay_tab_bar (struct frame *f)
 14414 {
 14415   struct window *w;
 14416   struct it it;
 14417   struct glyph_row *row;
 14418 
 14419   f->tab_bar_redisplayed = true;
 14420 
 14421   /* If frame hasn't a tab-bar window or if it is zero-height, don't
 14422      do anything.  This means you must start with tab-bar-lines
 14423      non-zero to get the auto-sizing effect.  Or in other words, you
 14424      can turn off tab-bars by specifying tab-bar-lines zero.  */
 14425   if (!WINDOWP (f->tab_bar_window)
 14426       || (w = XWINDOW (f->tab_bar_window),
 14427           WINDOW_TOTAL_LINES (w) == 0))
 14428     {
 14429       /* Even if we do not display a tab bar initially, still pretend
 14430          that we have resized it.  This avoids that a later activation
 14431          of the tab bar resizes the frame, despite of the fact that the
 14432          setting of 'frame-inhibit-implied-resize' should inhibit it
 14433          (Bug#52986).  */
 14434       f->tab_bar_resized = true;
 14435 
 14436       return false;
 14437     }
 14438 
 14439   /* Build a string that represents the contents of the tab-bar.  */
 14440   build_desired_tab_bar_string (f);
 14441 
 14442   int new_nrows;
 14443   int new_height = tab_bar_height (f, &new_nrows, true);
 14444 
 14445   if (f->n_tab_bar_rows == 0)
 14446     {
 14447       f->n_tab_bar_rows = new_nrows;
 14448       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 14449         frame_default_tab_bar_height = new_height;
 14450     }
 14451 
 14452   /* If new_height or new_nrows indicate that we need to enlarge or
 14453      shrink the tab-bar window, we can return right away.  */
 14454   if (new_nrows > f->n_tab_bar_rows
 14455       || (EQ (Vauto_resize_tab_bars, Qgrow_only)
 14456           && !f->minimize_tab_bar_window_p
 14457           && new_height > WINDOW_PIXEL_HEIGHT (w))
 14458       || (! EQ (Vauto_resize_tab_bars, Qgrow_only)
 14459           && new_height < WINDOW_PIXEL_HEIGHT (w)))
 14460     {
 14461       if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14462         FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14463       if (new_nrows != f->n_tab_bar_rows)
 14464         f->n_tab_bar_rows = new_nrows;
 14465       clear_glyph_matrix (w->desired_matrix);
 14466       f->fonts_changed = true;
 14467       return true;
 14468     }
 14469 
 14470   /* Set up an iterator for the tab-bar window.  */
 14471   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TAB_BAR_FACE_ID);
 14472   it.first_visible_x = 0;
 14473   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14474   row = it.glyph_row;
 14475   row->reversed_p = false;
 14476   reseat_to_string (&it, NULL, f->desired_tab_bar_string, 0, 0, 0,
 14477                     STRING_MULTIBYTE (f->desired_tab_bar_string));
 14478   /* FIXME: This should be controlled by a user option.  But it
 14479      doesn't make sense to have an R2L tab bar if the menu bar cannot
 14480      be drawn also R2L, and making the menu bar R2L is tricky due
 14481      tabkit-specific code that implements it.  If an R2L tab bar is
 14482      ever supported, display_tab_bar_line should also be augmented to
 14483      call unproduce_glyphs like display_line and display_string
 14484      do.  */
 14485   it.paragraph_embedding = L2R;
 14486 
 14487   /* Display as many lines as needed to display all tab-bar items.  */
 14488 
 14489   if (f->n_tab_bar_rows > 0)
 14490     {
 14491       int border, rows, height, extra;
 14492 
 14493       if (TYPE_RANGED_FIXNUMP (int, Vtab_bar_border))
 14494         border = XFIXNUM (Vtab_bar_border);
 14495       else if (EQ (Vtab_bar_border, Qinternal_border_width))
 14496         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 14497       else if (EQ (Vtab_bar_border, Qborder_width))
 14498         border = f->border_width;
 14499       else
 14500         border = 0;
 14501       if (border < 0)
 14502         border = 0;
 14503 
 14504       rows = f->n_tab_bar_rows;
 14505       height = max (1, (it.last_visible_y - border) / rows);
 14506       extra = it.last_visible_y - border - height * rows;
 14507 
 14508       while (it.current_y < it.last_visible_y)
 14509         {
 14510           int h = 0;
 14511           if (extra > 0 && rows-- > 0)
 14512             {
 14513               h = (extra + rows - 1) / rows;
 14514               extra -= h;
 14515             }
 14516           display_tab_bar_line (&it, height + h);
 14517         }
 14518     }
 14519   else
 14520     {
 14521       while (it.current_y < it.last_visible_y)
 14522         display_tab_bar_line (&it, 0);
 14523     }
 14524 
 14525   /* It doesn't make much sense to try scrolling in the tab-bar
 14526      window, so don't do it.  */
 14527   w->desired_matrix->no_scrolling_p = true;
 14528   w->must_be_updated_p = true;
 14529 
 14530   if (!NILP (Vauto_resize_tab_bars))
 14531     {
 14532       bool change_height_p = false;
 14533 
 14534       /* If we couldn't display everything, change the tab-bar's
 14535          height if there is room for more.  */
 14536       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 14537         change_height_p = true;
 14538 
 14539       /* We subtract 1 because display_tab_bar_line advances the
 14540          glyph_row pointer before returning to its caller.  We want to
 14541          examine the last glyph row produced by
 14542          display_tab_bar_line.  */
 14543       row = it.glyph_row - 1;
 14544 
 14545       /* If there are blank lines at the end, except for a partially
 14546          visible blank line at the end that is smaller than
 14547          FRAME_LINE_HEIGHT, change the tab-bar's height.  */
 14548       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14549           && row->height >= FRAME_LINE_HEIGHT (f))
 14550         change_height_p = true;
 14551 
 14552       /* If row displays tab-bar items, but is partially visible,
 14553          change the tab-bar's height.  */
 14554       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14555           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 14556         change_height_p = true;
 14557 
 14558       /* Resize windows as needed by changing the `tab-bar-lines'
 14559          frame parameter.  */
 14560       if (change_height_p)
 14561         {
 14562           int nrows;
 14563           int new_height = tab_bar_height (f, &nrows, true);
 14564 
 14565           change_height_p = ((EQ (Vauto_resize_tab_bars, Qgrow_only)
 14566                               && !f->minimize_tab_bar_window_p)
 14567                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 14568                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 14569           f->minimize_tab_bar_window_p = false;
 14570 
 14571           if (change_height_p)
 14572             {
 14573               if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14574                 FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14575               frame_default_tab_bar_height = new_height;
 14576               clear_glyph_matrix (w->desired_matrix);
 14577               f->n_tab_bar_rows = nrows;
 14578               f->fonts_changed = true;
 14579 
 14580               return true;
 14581             }
 14582         }
 14583     }
 14584 
 14585   f->minimize_tab_bar_window_p = false;
 14586   return false;
 14587 }
 14588 
 14589 /* Get information about the tab-bar item which is displayed in GLYPH
 14590    on frame F.  Return in *PROP_IDX the index where tab-bar item
 14591    properties start in F->tab_bar_items.  Return in CLOSE_P an
 14592    indication whether the click was on the close-tab icon of the tab.
 14593    Value is false if GLYPH doesn't display a tab-bar item.  */
 14594 
 14595 static bool
 14596 tab_bar_item_info (struct frame *f, struct glyph *glyph,
 14597                    int *prop_idx, bool *close_p)
 14598 {
 14599   Lisp_Object prop;
 14600   ptrdiff_t charpos;
 14601 
 14602   /* This function can be called asynchronously, which means we must
 14603      exclude any possibility that Fget_text_property signals an
 14604      error.  */
 14605   charpos = min (SCHARS (f->current_tab_bar_string), glyph->charpos);
 14606   charpos = max (0, charpos);
 14607 
 14608   /* Get the text property `menu-item' at pos. The value of that
 14609      property is the start index of this item's properties in
 14610      F->tab_bar_items.  */
 14611   prop = Fget_text_property (make_fixnum (charpos),
 14612                              Qmenu_item, f->current_tab_bar_string);
 14613   if (! FIXNUMP (prop))
 14614     return false;
 14615   *prop_idx = XFIXNUM (prop);
 14616 
 14617   *close_p = !NILP (Fget_text_property (make_fixnum (charpos),
 14618                                         Qclose_tab,
 14619                                         f->current_tab_bar_string));
 14620 
 14621   return true;
 14622 }
 14623 
 14624 
 14625 /* Get information about the tab-bar item at position X/Y on frame F.
 14626    Return in *GLYPH a pointer to the glyph of the tab-bar item in
 14627    the current matrix of the tab-bar window of F, or NULL if not
 14628    on a tab-bar item.  Return in *PROP_IDX the index of the tab-bar
 14629    item in F->tab_bar_items.  Value is
 14630 
 14631    -1   if X/Y is not on a tab-bar item
 14632    0    if X/Y is on the same item that was highlighted before.
 14633    1    otherwise.  */
 14634 
 14635 static int
 14636 get_tab_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 14637                    int *hpos, int *vpos, int *prop_idx, bool *close_p)
 14638 {
 14639   struct window *w = XWINDOW (f->tab_bar_window);
 14640   int area;
 14641 
 14642   /* Find the glyph under X/Y.  */
 14643   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 14644   if (*glyph == NULL)
 14645     return -1;
 14646 
 14647   /* Get the start of this tab-bar item's properties in
 14648      f->tab_bar_items.  */
 14649   if (!tab_bar_item_info (f, *glyph, prop_idx, close_p))
 14650     return -1;
 14651 
 14652   return *prop_idx == f->last_tab_bar_item ? 0 : 1;
 14653 }
 14654 
 14655 
 14656 /* EXPORT:
 14657    Handle mouse button event on the tab-bar of frame F, at
 14658    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 14659    false for button release.  MODIFIERS is event modifiers for button
 14660    release.  */
 14661 
 14662 Lisp_Object
 14663 handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14664                       int modifiers)
 14665 {
 14666   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14667   struct window *w = XWINDOW (f->tab_bar_window);
 14668   int hpos, vpos, prop_idx;
 14669   bool close_p;
 14670   struct glyph *glyph;
 14671   Lisp_Object enabled_p;
 14672   int ts;
 14673 
 14674   frame_to_window_pixel_xy (w, &x, &y);
 14675   ts = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14676   if (ts == -1)
 14677     return Fcons (Qtab_bar, Qnil);
 14678 
 14679   /* If item is disabled, do nothing.  */
 14680   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14681   if (NILP (enabled_p))
 14682     return Qnil;
 14683 
 14684   if (down_p)
 14685     {
 14686       /* Show the clicked button in pressed state.  */
 14687       if (!NILP (Vmouse_highlight))
 14688         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 14689       f->last_tab_bar_item = prop_idx; /* record the pressed tab */
 14690     }
 14691   else
 14692     {
 14693       /* Show item in released state.  */
 14694       if (!NILP (Vmouse_highlight))
 14695         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 14696       f->last_tab_bar_item = -1;
 14697     }
 14698 
 14699   Lisp_Object caption =
 14700     Fcopy_sequence (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION));
 14701 
 14702   AUTO_LIST2 (props, Qmenu_item,
 14703               list3 (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_KEY),
 14704                      AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_BINDING),
 14705                      close_p ? Qt : Qnil));
 14706 
 14707   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14708                         props, caption);
 14709 
 14710   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14711 }
 14712 
 14713 
 14714 /* Possibly highlight a tab-bar item on frame F when mouse moves to
 14715    tab-bar window-relative coordinates X/Y.  Called from
 14716    note_mouse_highlight.  */
 14717 
 14718 static void
 14719 note_tab_bar_highlight (struct frame *f, int x, int y)
 14720 {
 14721   Lisp_Object window = f->tab_bar_window;
 14722   struct window *w = XWINDOW (window);
 14723   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14724   int hpos, vpos;
 14725   struct glyph *glyph;
 14726   struct glyph_row *row;
 14727   int i;
 14728   Lisp_Object enabled_p;
 14729   int prop_idx;
 14730   bool close_p;
 14731   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 14732   int rc;
 14733 
 14734   /* Function note_mouse_highlight is called with negative X/Y
 14735      values when mouse moves outside of the frame.  */
 14736   if (x <= 0 || y <= 0)
 14737     {
 14738       clear_mouse_face (hlinfo);
 14739       return;
 14740     }
 14741 
 14742   rc = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14743   if (rc < 0)
 14744     {
 14745       /* Not on tab-bar item.  */
 14746       clear_mouse_face (hlinfo);
 14747       return;
 14748     }
 14749   else if (rc == 0)
 14750     /* On same tab-bar item as before.  */
 14751     goto set_help_echo;
 14752 
 14753   clear_mouse_face (hlinfo);
 14754 
 14755   bool mouse_down_p = false;
 14756   /* Mouse is down, but on different tab-bar item?  Or alternatively,
 14757      the mouse might've been pressed somewhere we don't know about,
 14758      and then have moved onto the tab bar.  In this case,
 14759      last_tab_bar_item is -1, so we DTRT and behave like other
 14760      programs by displaying the item as sunken. */
 14761   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 14762   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 14763                   && f == dpyinfo->last_mouse_frame);
 14764 
 14765   if (mouse_down_p && f->last_tab_bar_item != prop_idx
 14766       && f->last_tab_bar_item != -1)
 14767     return;
 14768   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 14769 
 14770   /* If tab-bar item is not enabled, don't highlight it.  */
 14771   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14772   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 14773     {
 14774       /* Compute the x-position of the glyph.  In front and past the
 14775          image is a space.  We include this in the highlighted area.  */
 14776       row = MATRIX_ROW (w->current_matrix, vpos);
 14777       for (i = x = 0; i < hpos; ++i)
 14778         x += row->glyphs[TEXT_AREA][i].pixel_width;
 14779 
 14780       /* Record this as the current active region.  */
 14781       hlinfo->mouse_face_beg_col = hpos;
 14782       hlinfo->mouse_face_beg_row = vpos;
 14783       hlinfo->mouse_face_beg_x = x;
 14784       hlinfo->mouse_face_past_end = false;
 14785 
 14786       hlinfo->mouse_face_end_col = hpos + 1;
 14787       hlinfo->mouse_face_end_row = vpos;
 14788       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 14789       hlinfo->mouse_face_window = window;
 14790       hlinfo->mouse_face_face_id = TAB_BAR_FACE_ID;
 14791 
 14792       /* Display it as active.  */
 14793       show_mouse_face (hlinfo, draw);
 14794     }
 14795 
 14796  set_help_echo:
 14797 
 14798   /* Set help_echo_string to a help string to display for this tab-bar item.
 14799      XTread_socket does the rest.  */
 14800   help_echo_object = help_echo_window = Qnil;
 14801   help_echo_pos = -1;
 14802   help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_HELP);
 14803   if (NILP (help_echo_string))
 14804     help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION);
 14805 }
 14806 
 14807 #endif /* HAVE_WINDOW_SYSTEM */
 14808 
 14809 /* Find the tab-bar item at X coordinate and return its information.  */
 14810 static Lisp_Object
 14811 tty_get_tab_bar_item (struct frame *f, int x, int *prop_idx, bool *close_p)
 14812 {
 14813   ptrdiff_t clen = 0;
 14814 
 14815   for (int i = 0; i < f->n_tab_bar_items; i++)
 14816     {
 14817       Lisp_Object caption = AREF (f->tab_bar_items, (i * TAB_BAR_ITEM_NSLOTS
 14818                                                      + TAB_BAR_ITEM_CAPTION));
 14819       if (NILP (caption))
 14820         return Qnil;
 14821       clen += SCHARS (caption);
 14822       if (x < clen)
 14823         {
 14824           *prop_idx = i;
 14825           *close_p = !NILP (Fget_text_property (make_fixnum (SCHARS (caption)
 14826                                                              - (clen - x)),
 14827                                                 Qclose_tab,
 14828                                                 caption));
 14829           return caption;
 14830         }
 14831     }
 14832   return Qnil;
 14833 }
 14834 
 14835 /* Handle a mouse click at X/Y on the tab bar of TTY frame F.  If the
 14836    click was on the tab bar and was handled, populate the EVENT
 14837    structure, store it in keyboard queue, and return true; otherwise
 14838    return false.  MODIFIERS are event modifiers for generating the tab
 14839    release event.  */
 14840 Lisp_Object
 14841 tty_handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14842                           struct input_event *event)
 14843 {
 14844   /* Did they click on the tab bar?  */
 14845   if (y < FRAME_MENU_BAR_LINES (f)
 14846       || y >= FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f))
 14847     return Qnil;
 14848 
 14849   /* Find the tab-bar item where the X,Y coordinates belong.  */
 14850   int prop_idx;
 14851   bool close_p;
 14852   Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &close_p);
 14853 
 14854   if (NILP (caption))
 14855     return Qnil;
 14856 
 14857   if (NILP (AREF (f->tab_bar_items,
 14858                   prop_idx * TAB_BAR_ITEM_NSLOTS + TAB_BAR_ITEM_ENABLED_P)))
 14859     return Qnil;
 14860 
 14861   if (down_p)
 14862     f->last_tab_bar_item = prop_idx;
 14863   else
 14864     f->last_tab_bar_item = -1;
 14865 
 14866   caption = Fcopy_sequence (caption);
 14867 
 14868   AUTO_LIST2 (props, Qmenu_item,
 14869               list3 (AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14870                            + TAB_BAR_ITEM_KEY),
 14871                      AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14872                            + TAB_BAR_ITEM_BINDING),
 14873                      close_p ? Qt : Qnil));
 14874 
 14875   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14876                         props, caption);
 14877 
 14878   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14879 }
 14880 
 14881 
 14882 
 14883 /***********************************************************************
 14884                                Tool-bars
 14885  ***********************************************************************/
 14886 
 14887 #ifdef HAVE_WINDOW_SYSTEM
 14888 
 14889 /* Update the tool-bar item list for frame F.  This has to be done
 14890    before we start to fill in any display lines.  Called from
 14891    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 14892    and restore it here.  */
 14893 
 14894 static void
 14895 update_tool_bar (struct frame *f, bool save_match_data)
 14896 {
 14897 #ifdef HAVE_EXT_TOOL_BAR
 14898   bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
 14899 #else
 14900   bool do_update = (WINDOWP (f->tool_bar_window)
 14901                     && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
 14902 #endif
 14903 
 14904   if (do_update)
 14905     {
 14906       Lisp_Object window;
 14907       struct window *w;
 14908 
 14909       window = FRAME_SELECTED_WINDOW (f);
 14910       w = XWINDOW (window);
 14911 
 14912       /* If the user has switched buffers or windows, we need to
 14913          recompute to reflect the new bindings.  But we'll
 14914          recompute when update_mode_lines is set too; that means
 14915          that people can use force-mode-line-update to request
 14916          that the menu bar be recomputed.  The adverse effect on
 14917          the rest of the redisplay algorithm is about the same as
 14918          windows_or_buffers_changed anyway.  */
 14919       if (windows_or_buffers_changed
 14920           || w->update_mode_line
 14921           || update_mode_lines
 14922           || window_buffer_changed (w))
 14923         {
 14924           struct buffer *prev = current_buffer;
 14925           specpdl_ref count = SPECPDL_INDEX ();
 14926           Lisp_Object frame, new_tool_bar;
 14927           int new_n_tool_bar;
 14928 
 14929           /* Set current_buffer to the buffer of the selected
 14930              window of the frame, so that we get the right local
 14931              keymaps.  */
 14932           set_buffer_internal_1 (XBUFFER (w->contents));
 14933 
 14934           /* Save match data, if we must.  */
 14935           if (save_match_data)
 14936             record_unwind_save_match_data ();
 14937 
 14938           /* Make sure that we don't accidentally use bogus keymaps.  */
 14939           if (NILP (Voverriding_local_map_menu_flag))
 14940             {
 14941               specbind (Qoverriding_terminal_local_map, Qnil);
 14942               specbind (Qoverriding_local_map, Qnil);
 14943             }
 14944 
 14945           /* We must temporarily set the selected frame to this frame
 14946              before calling tool_bar_items, because the calculation of
 14947              the tool-bar keymap uses the selected frame (see
 14948              `tool-bar-make-keymap' in tool-bar.el).  */
 14949           eassert (EQ (selected_window,
 14950                        /* Since we only explicitly preserve selected_frame,
 14951                           check that selected_window would be redundant.  */
 14952                        XFRAME (selected_frame)->selected_window));
 14953           record_unwind_protect (restore_selected_window, selected_window);
 14954           XSETFRAME (frame, f);
 14955           selected_frame = frame;
 14956           selected_window = FRAME_SELECTED_WINDOW (f);
 14957 
 14958           /* Build desired tool-bar items from keymaps.  */
 14959           new_tool_bar
 14960             = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
 14961                               &new_n_tool_bar);
 14962 
 14963           /* Redisplay the tool-bar if we changed it.  */
 14964           if (new_n_tool_bar != f->n_tool_bar_items
 14965               || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
 14966             {
 14967               /* Redisplay that happens asynchronously due to an expose event
 14968                  may access f->tool_bar_items.  Make sure we update both
 14969                  variables within BLOCK_INPUT so no such event interrupts.  */
 14970               block_input ();
 14971               fset_tool_bar_items (f, new_tool_bar);
 14972               f->n_tool_bar_items = new_n_tool_bar;
 14973               w->update_mode_line = true;
 14974               unblock_input ();
 14975             }
 14976 
 14977           unbind_to (count, Qnil);
 14978           set_buffer_internal_1 (prev);
 14979         }
 14980     }
 14981 }
 14982 
 14983 #ifndef HAVE_EXT_TOOL_BAR
 14984 
 14985 /* Set F->desired_tool_bar_string to a Lisp string representing frame
 14986    F's desired tool-bar contents.  F->tool_bar_items must have
 14987    been set up previously by calling prepare_menu_bars.  */
 14988 
 14989 static void
 14990 build_desired_tool_bar_string (struct frame *f)
 14991 {
 14992   int i, size, size_needed;
 14993   Lisp_Object image, plist;
 14994 
 14995   image = plist = Qnil;
 14996 
 14997   /* Prepare F->desired_tool_bar_string.  If we can reuse it, do so.
 14998      Otherwise, make a new string.  */
 14999 
 15000   /* The size of the string we might be able to reuse.  */
 15001   size = (STRINGP (f->desired_tool_bar_string)
 15002           ? SCHARS (f->desired_tool_bar_string)
 15003           : 0);
 15004 
 15005   /* We need one space in the string for each image.  */
 15006   size_needed = f->n_tool_bar_items;
 15007 
 15008   /* Reuse f->desired_tool_bar_string, if possible.  */
 15009   if (size < size_needed || NILP (f->desired_tool_bar_string))
 15010     fset_desired_tool_bar_string
 15011       (f, Fmake_string (make_fixnum (size_needed), make_fixnum (' '), Qnil));
 15012   else
 15013     {
 15014       AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
 15015       Fremove_text_properties (make_fixnum (0), make_fixnum (size),
 15016                                props, f->desired_tool_bar_string);
 15017     }
 15018 
 15019   /* Put a `display' property on the string for the images to display,
 15020      put a `menu_item' property on tool-bar items with a value that
 15021      is the index of the item in F's tool-bar item vector.  */
 15022   for (i = 0; i < f->n_tool_bar_items; ++i)
 15023     {
 15024 #define PROP(IDX) \
 15025   AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
 15026 
 15027       bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
 15028       bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
 15029       int hmargin, vmargin, relief, idx, end;
 15030 
 15031       /* If image is a vector, choose the image according to the
 15032          button state.  */
 15033       image = PROP (TOOL_BAR_ITEM_IMAGES);
 15034       if (VECTORP (image))
 15035         {
 15036           if (enabled_p)
 15037             idx = (selected_p
 15038                    ? TOOL_BAR_IMAGE_ENABLED_SELECTED
 15039                    : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
 15040           else
 15041             idx = (selected_p
 15042                    ? TOOL_BAR_IMAGE_DISABLED_SELECTED
 15043                    : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
 15044 
 15045           eassert (ASIZE (image) >= idx);
 15046           image = AREF (image, idx);
 15047         }
 15048       else
 15049         idx = -1;
 15050 
 15051       /* Ignore invalid image specifications.  */
 15052       if (!valid_image_p (image))
 15053         continue;
 15054 
 15055       /* Display the tool-bar button pressed, or depressed.  */
 15056       plist = Fcopy_sequence (XCDR (image));
 15057 
 15058       /* Compute margin and relief to draw.  */
 15059       relief = (tool_bar_button_relief >= 0
 15060                 ? min (tool_bar_button_relief,
 15061                        min (INT_MAX, MOST_POSITIVE_FIXNUM))
 15062                 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
 15063       hmargin = vmargin = relief;
 15064 
 15065       if (RANGED_FIXNUMP (1, Vtool_bar_button_margin,
 15066                            INT_MAX - max (hmargin, vmargin)))
 15067         {
 15068           hmargin += XFIXNAT (Vtool_bar_button_margin);
 15069           vmargin += XFIXNAT (Vtool_bar_button_margin);
 15070         }
 15071       else if (CONSP (Vtool_bar_button_margin))
 15072         {
 15073           if (RANGED_FIXNUMP (1, XCAR (Vtool_bar_button_margin),
 15074                                INT_MAX - hmargin))
 15075             hmargin += XFIXNAT (XCAR (Vtool_bar_button_margin));
 15076 
 15077           if (RANGED_FIXNUMP (1, XCDR (Vtool_bar_button_margin),
 15078                                INT_MAX - vmargin))
 15079             vmargin += XFIXNAT (XCDR (Vtool_bar_button_margin));
 15080         }
 15081 
 15082       if (auto_raise_tool_bar_buttons_p)
 15083         {
 15084           /* Add a `:relief' property to the image spec if the item is
 15085              selected.  */
 15086           if (selected_p)
 15087             {
 15088               plist = plist_put (plist, QCrelief, make_fixnum (-relief));
 15089               hmargin -= relief;
 15090               vmargin -= relief;
 15091             }
 15092         }
 15093       else
 15094         {
 15095           /* If image is selected, display it pressed, i.e. with a
 15096              negative relief.  If it's not selected, display it with a
 15097              raised relief.  */
 15098           plist = plist_put (plist, QCrelief,
 15099                              (selected_p
 15100                               ? make_fixnum (-relief)
 15101                               : make_fixnum (relief)));
 15102           hmargin -= relief;
 15103           vmargin -= relief;
 15104         }
 15105 
 15106       /* Put a margin around the image.  */
 15107       if (hmargin || vmargin)
 15108         {
 15109           if (hmargin == vmargin)
 15110             plist = plist_put (plist, QCmargin, make_fixnum (hmargin));
 15111           else
 15112             plist = plist_put (plist, QCmargin,
 15113                                Fcons (make_fixnum (hmargin),
 15114                                       make_fixnum (vmargin)));
 15115         }
 15116 
 15117       /* If button is not enabled, and we don't have special images
 15118          for the disabled state, make the image appear disabled by
 15119          applying an appropriate algorithm to it.  */
 15120       if (!enabled_p && idx < 0)
 15121         plist = plist_put (plist, QCconversion, Qdisabled);
 15122 
 15123       /* Put a `display' text property on the string for the image to
 15124          display.  Put a `menu-item' property on the string that gives
 15125          the start of this item's properties in the tool-bar items
 15126          vector.  */
 15127       image = Fcons (Qimage, plist);
 15128       AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
 15129                   make_fixnum (i * TOOL_BAR_ITEM_NSLOTS));
 15130 
 15131       /* Let the last image hide all remaining spaces in the tool bar
 15132          string.  The string can be longer than needed when we reuse a
 15133          previous string.  */
 15134       if (i + 1 == f->n_tool_bar_items)
 15135         end = SCHARS (f->desired_tool_bar_string);
 15136       else
 15137         end = i + 1;
 15138       Fadd_text_properties (make_fixnum (i), make_fixnum (end),
 15139                             props, f->desired_tool_bar_string);
 15140 #undef PROP
 15141     }
 15142 }
 15143 
 15144 
 15145 /* Display one line of the tool-bar of frame IT->f.
 15146 
 15147    HEIGHT specifies the desired height of the tool-bar line.
 15148    If the actual height of the glyph row is less than HEIGHT, the
 15149    row's height is increased to HEIGHT, and the icons are centered
 15150    vertically in the new height.
 15151 
 15152    If HEIGHT is -1, we are counting needed tool-bar lines, so don't
 15153    count a final empty row in case the tool-bar width exactly matches
 15154    the window width.
 15155 */
 15156 
 15157 static void
 15158 display_tool_bar_line (struct it *it, int height)
 15159 {
 15160   struct glyph_row *row = it->glyph_row;
 15161   int max_x = it->last_visible_x;
 15162   struct glyph *last;
 15163 
 15164   /* Don't extend on a previously drawn tool bar items (Bug#16058).  */
 15165   clear_glyph_row (row);
 15166   row->enabled_p = true;
 15167   row->y = it->current_y;
 15168 
 15169   /* Note that this isn't made use of if the face hasn't a box,
 15170      so there's no need to check the face here.  */
 15171   it->start_of_box_run_p = true;
 15172 
 15173   while (it->current_x < max_x)
 15174     {
 15175       int x, n_glyphs_before, i, nglyphs;
 15176       struct it it_before;
 15177 
 15178       /* Get the next display element.  */
 15179       if (!get_next_display_element (it))
 15180         {
 15181           /* Don't count empty row if we are counting needed tool-bar lines.  */
 15182           if (height < 0 && !it->hpos)
 15183             return;
 15184           break;
 15185         }
 15186 
 15187       /* Produce glyphs.  */
 15188       n_glyphs_before = row->used[TEXT_AREA];
 15189       it_before = *it;
 15190 
 15191       PRODUCE_GLYPHS (it);
 15192 
 15193       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 15194       i = 0;
 15195       x = it_before.current_x;
 15196       while (i < nglyphs)
 15197         {
 15198           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 15199 
 15200           if (x + glyph->pixel_width > max_x)
 15201             {
 15202               /* Glyph doesn't fit on line.  Backtrack.  */
 15203               row->used[TEXT_AREA] = n_glyphs_before;
 15204               *it = it_before;
 15205               /* If this is the only glyph on this line, it will never fit on the
 15206                  tool-bar, so skip it.  But ensure there is at least one glyph,
 15207                  so we don't accidentally disable the tool-bar.  */
 15208               if (n_glyphs_before == 0
 15209                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 15210                 break;
 15211               goto out;
 15212             }
 15213 
 15214           ++it->hpos;
 15215           x += glyph->pixel_width;
 15216           ++i;
 15217         }
 15218 
 15219       /* Stop at line end.  */
 15220       if (ITERATOR_AT_END_OF_LINE_P (it))
 15221         break;
 15222 
 15223       set_iterator_to_next (it, true);
 15224     }
 15225 
 15226  out:;
 15227 
 15228   row->displays_text_p = row->used[TEXT_AREA] != 0;
 15229 
 15230   /* Use default face for the border below the tool bar.
 15231 
 15232      FIXME: When auto-resize-tool-bars is grow-only, there is
 15233      no additional border below the possibly empty tool-bar lines.
 15234      So to make the extra empty lines look "normal", we have to
 15235      use the tool-bar face for the border too.  */
 15236   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15237       && !EQ (Vauto_resize_tool_bars, Qgrow_only))
 15238     it->face_id = DEFAULT_FACE_ID;
 15239 
 15240   extend_face_to_end_of_line (it);
 15241   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 15242   last->right_box_line_p = true;
 15243   if (last == row->glyphs[TEXT_AREA])
 15244     last->left_box_line_p = true;
 15245 
 15246   /* Make line the desired height and center it vertically.  */
 15247   if ((height -= it->max_ascent + it->max_descent) > 0)
 15248     {
 15249       /* Don't add more than one line height.  */
 15250       height %= FRAME_LINE_HEIGHT (it->f);
 15251       it->max_ascent += height / 2;
 15252       it->max_descent += (height + 1) / 2;
 15253     }
 15254 
 15255   compute_line_metrics (it);
 15256 
 15257   /* If line is empty, make it occupy the rest of the tool-bar.  */
 15258   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 15259     {
 15260       row->height = row->phys_height = it->last_visible_y - row->y;
 15261       row->visible_height = row->height;
 15262       row->ascent = row->phys_ascent = 0;
 15263       row->extra_line_spacing = 0;
 15264     }
 15265 
 15266   row->full_width_p = true;
 15267   row->continued_p = false;
 15268   row->truncated_on_left_p = false;
 15269   row->truncated_on_right_p = false;
 15270 
 15271   it->current_x = it->hpos = 0;
 15272   it->current_y += row->height;
 15273   ++it->vpos;
 15274   ++it->glyph_row;
 15275 }
 15276 
 15277 
 15278 /* Value is the number of pixels needed to make all tool-bar items of
 15279    frame F visible.  The actual number of glyph rows needed is
 15280    returned in *N_ROWS if non-NULL.  */
 15281 static int
 15282 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 15283 {
 15284   struct window *w = XWINDOW (f->tool_bar_window);
 15285   struct it it;
 15286   /* tool_bar_height is called from redisplay_tool_bar after building
 15287      the desired matrix, so use (unused) mode-line row as temporary row to
 15288      avoid destroying the first tool-bar row.  */
 15289   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 15290 
 15291   /* Initialize an iterator for iteration over
 15292      F->desired_tool_bar_string in the tool-bar window of frame F.  */
 15293   init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
 15294   temp_row->reversed_p = false;
 15295   it.first_visible_x = 0;
 15296   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15297   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15298                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15299   it.paragraph_embedding = L2R;
 15300 
 15301   while (!ITERATOR_AT_END_P (&it))
 15302     {
 15303       clear_glyph_row (temp_row);
 15304       it.glyph_row = temp_row;
 15305       display_tool_bar_line (&it, -1);
 15306     }
 15307   clear_glyph_row (temp_row);
 15308 
 15309   /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar.  */
 15310   if (n_rows)
 15311     *n_rows = it.vpos > 0 ? it.vpos : -1;
 15312 
 15313   if (pixelwise)
 15314     return it.current_y;
 15315   else
 15316     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 15317 }
 15318 
 15319 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15320 
 15321 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
 15322        0, 2, 0,
 15323        doc: /* Return the number of lines occupied by the tool bar of FRAME.
 15324 If FRAME is nil or omitted, use the selected frame.  Optional argument
 15325 PIXELWISE non-nil means return the height of the tool bar in pixels.  */)
 15326   (Lisp_Object frame, Lisp_Object pixelwise)
 15327 {
 15328   int height = 0;
 15329 
 15330 #ifndef HAVE_EXT_TOOL_BAR
 15331   struct frame *f = decode_any_frame (frame);
 15332 
 15333   if (WINDOWP (f->tool_bar_window)
 15334       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
 15335     {
 15336       update_tool_bar (f, true);
 15337       if (f->n_tool_bar_items)
 15338         {
 15339           build_desired_tool_bar_string (f);
 15340           height = tool_bar_height (f, NULL, !NILP (pixelwise));
 15341         }
 15342     }
 15343 #endif
 15344 
 15345   return make_fixnum (height);
 15346 }
 15347 
 15348 #ifndef HAVE_EXT_TOOL_BAR
 15349 
 15350 /* Display the internal tool-bar of frame F.  Value is true if
 15351    tool-bar's height should be changed.  */
 15352 static bool
 15353 redisplay_tool_bar (struct frame *f)
 15354 {
 15355   struct window *w;
 15356   struct it it;
 15357   struct glyph_row *row;
 15358 
 15359   f->tool_bar_redisplayed = true;
 15360 
 15361   /* If frame hasn't a tool-bar window or if it is zero-height, don't
 15362      do anything.  This means you must start with tool-bar-lines
 15363      non-zero to get the auto-sizing effect.  Or in other words, you
 15364      can turn off tool-bars by specifying tool-bar-lines zero.  */
 15365   if (!WINDOWP (f->tool_bar_window)
 15366       || (w = XWINDOW (f->tool_bar_window),
 15367           WINDOW_TOTAL_LINES (w) == 0))
 15368     {
 15369       /* Even if we do not display a tool bar initially, still pretend
 15370          that we have resized it already.  This avoids that a later
 15371          activation of the tool bar resizes the frame, despite of the
 15372          fact that a setting of 'frame-inhibit-implied-resize' should
 15373          inhibit it (Bug#52986).  */
 15374       f->tool_bar_resized = true;
 15375 
 15376       return false;
 15377     }
 15378 
 15379   /* Set up an iterator for the tool-bar window.  */
 15380   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
 15381   it.first_visible_x = 0;
 15382   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15383   row = it.glyph_row;
 15384   row->reversed_p = false;
 15385 
 15386   /* Build a string that represents the contents of the tool-bar.  */
 15387   build_desired_tool_bar_string (f);
 15388   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15389                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15390   /* FIXME: This should be controlled by a user option.  But it
 15391      doesn't make sense to have an R2L tool bar if the menu bar cannot
 15392      be drawn also R2L, and making the menu bar R2L is tricky due to
 15393      toolkit-specific code that implements it.  If an R2L tool bar is
 15394      ever supported, display_tool_bar_line should also be augmented to
 15395      call unproduce_glyphs like display_line and display_string
 15396      do.  */
 15397   it.paragraph_embedding = L2R;
 15398 
 15399   if (f->n_tool_bar_rows == 0)
 15400     {
 15401       int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
 15402 
 15403       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 15404         {
 15405           if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15406             FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15407           frame_default_tool_bar_height = new_height;
 15408           /* Always do that now.  */
 15409           clear_glyph_matrix (w->desired_matrix);
 15410           f->fonts_changed = true;
 15411           return true;
 15412         }
 15413     }
 15414 
 15415   /* Display as many lines as needed to display all tool-bar items.  */
 15416 
 15417   if (f->n_tool_bar_rows > 0)
 15418     {
 15419       int border, rows, height, extra;
 15420 
 15421       if (TYPE_RANGED_FIXNUMP (int, Vtool_bar_border))
 15422         border = XFIXNUM (Vtool_bar_border);
 15423       else if (EQ (Vtool_bar_border, Qinternal_border_width))
 15424         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 15425       else if (EQ (Vtool_bar_border, Qborder_width))
 15426         border = f->border_width;
 15427       else
 15428         border = 0;
 15429       if (border < 0)
 15430         border = 0;
 15431 
 15432       rows = f->n_tool_bar_rows;
 15433       height = max (1, (it.last_visible_y - border) / rows);
 15434       extra = it.last_visible_y - border - height * rows;
 15435 
 15436       while (it.current_y < it.last_visible_y)
 15437         {
 15438           int h = 0;
 15439           if (extra > 0 && rows-- > 0)
 15440             {
 15441               h = (extra + rows - 1) / rows;
 15442               extra -= h;
 15443             }
 15444           display_tool_bar_line (&it, height + h);
 15445         }
 15446     }
 15447   else
 15448     {
 15449       while (it.current_y < it.last_visible_y)
 15450         display_tool_bar_line (&it, 0);
 15451     }
 15452 
 15453   /* It doesn't make much sense to try scrolling in the tool-bar
 15454      window, so don't do it.  */
 15455   w->desired_matrix->no_scrolling_p = true;
 15456   w->must_be_updated_p = true;
 15457 
 15458   if (!NILP (Vauto_resize_tool_bars))
 15459     {
 15460       bool change_height_p = false;
 15461 
 15462       /* If we couldn't display everything, change the tool-bar's
 15463          height if there is room for more.  */
 15464       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 15465         change_height_p = true;
 15466 
 15467       /* We subtract 1 because display_tool_bar_line advances the
 15468          glyph_row pointer before returning to its caller.  We want to
 15469          examine the last glyph row produced by
 15470          display_tool_bar_line.  */
 15471       row = it.glyph_row - 1;
 15472 
 15473       /* If there are blank lines at the end, except for a partially
 15474          visible blank line at the end that is smaller than
 15475          FRAME_LINE_HEIGHT, change the tool-bar's height.  */
 15476       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15477           && row->height >= FRAME_LINE_HEIGHT (f))
 15478         change_height_p = true;
 15479 
 15480       /* If row displays tool-bar items, but is partially visible,
 15481          change the tool-bar's height.  */
 15482       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15483           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 15484         change_height_p = true;
 15485 
 15486       /* Resize windows as needed by changing the `tool-bar-lines'
 15487          frame parameter.  */
 15488       if (change_height_p)
 15489         {
 15490           int nrows;
 15491           int new_height = tool_bar_height (f, &nrows, true);
 15492 
 15493           change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
 15494                               && !f->minimize_tool_bar_window_p)
 15495                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 15496                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 15497           f->minimize_tool_bar_window_p = false;
 15498 
 15499           if (change_height_p)
 15500             {
 15501               if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15502                 FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15503               frame_default_tool_bar_height = new_height;
 15504               clear_glyph_matrix (w->desired_matrix);
 15505               f->n_tool_bar_rows = nrows;
 15506               f->fonts_changed = true;
 15507 
 15508               return true;
 15509             }
 15510         }
 15511     }
 15512 
 15513   f->minimize_tool_bar_window_p = false;
 15514 
 15515   return false;
 15516 }
 15517 
 15518 /* Get information about the tool-bar item which is displayed in GLYPH
 15519    on frame F.  Return in *PROP_IDX the index where tool-bar item
 15520    properties start in F->tool_bar_items.  Value is false if
 15521    GLYPH doesn't display a tool-bar item.  */
 15522 
 15523 static bool
 15524 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
 15525 {
 15526   Lisp_Object prop;
 15527   ptrdiff_t charpos;
 15528 
 15529   /* This function can be called asynchronously, which means we must
 15530      exclude any possibility that Fget_text_property signals an
 15531      error.  */
 15532   charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
 15533   charpos = max (0, charpos);
 15534 
 15535   /* Get the text property `menu-item' at pos. The value of that
 15536      property is the start index of this item's properties in
 15537      F->tool_bar_items.  */
 15538   prop = Fget_text_property (make_fixnum (charpos),
 15539                              Qmenu_item, f->current_tool_bar_string);
 15540   if (! FIXNUMP (prop))
 15541     return false;
 15542   *prop_idx = XFIXNUM (prop);
 15543   return true;
 15544 }
 15545 
 15546 
 15547 /* Get information about the tool-bar item at position X/Y on frame F.
 15548    Return in *GLYPH a pointer to the glyph of the tool-bar item in
 15549    the current matrix of the tool-bar window of F, or NULL if not
 15550    on a tool-bar item.  Return in *PROP_IDX the index of the tool-bar
 15551    item in F->tool_bar_items.  Value is
 15552 
 15553    -1   if X/Y is not on a tool-bar item
 15554    0    if X/Y is on the same item that was highlighted before.
 15555    1    otherwise.  */
 15556 
 15557 static int
 15558 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 15559                    int *hpos, int *vpos, int *prop_idx)
 15560 {
 15561   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15562   struct window *w = XWINDOW (f->tool_bar_window);
 15563   int area;
 15564 
 15565   /* Find the glyph under X/Y.  */
 15566   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 15567   if (*glyph == NULL)
 15568     return -1;
 15569 
 15570   /* Get the start of this tool-bar item's properties in
 15571      f->tool_bar_items.  */
 15572   if (!tool_bar_item_info (f, *glyph, prop_idx))
 15573     return -1;
 15574 
 15575   /* Is mouse on the highlighted item?  */
 15576   if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
 15577       && *vpos >= hlinfo->mouse_face_beg_row
 15578       && *vpos <= hlinfo->mouse_face_end_row
 15579       && (*vpos > hlinfo->mouse_face_beg_row
 15580           || *hpos >= hlinfo->mouse_face_beg_col)
 15581       && (*vpos < hlinfo->mouse_face_end_row
 15582           || *hpos < hlinfo->mouse_face_end_col
 15583           || hlinfo->mouse_face_past_end))
 15584     return 0;
 15585 
 15586   return 1;
 15587 }
 15588 
 15589 
 15590 /* EXPORT:
 15591    Handle mouse button event on the tool-bar of frame F, at
 15592    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 15593    false for button release.  MODIFIERS is event modifiers for button
 15594    release.  DEVICE is the device the click came from, or Qt.  */
 15595 
 15596 void
 15597 handle_tool_bar_click_with_device (struct frame *f, int x, int y, bool down_p,
 15598                                    int modifiers, Lisp_Object device)
 15599 {
 15600   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15601   struct window *w = XWINDOW (f->tool_bar_window);
 15602   int hpos, vpos, prop_idx;
 15603   struct glyph *glyph;
 15604   Lisp_Object enabled_p;
 15605   int ts;
 15606 
 15607   /* If not on the highlighted tool-bar item, and mouse-highlight is
 15608      non-nil, return.  This is so we generate the tool-bar button
 15609      click only when the mouse button is released on the same item as
 15610      where it was pressed.  However, when mouse-highlight is disabled,
 15611      generate the click when the button is released regardless of the
 15612      highlight, since tool-bar items are not highlighted in that
 15613      case.  */
 15614   frame_to_window_pixel_xy (w, &x, &y);
 15615   ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15616   if (ts == -1
 15617       || (ts != 0 && !NILP (Vmouse_highlight)))
 15618     return;
 15619 
 15620   /* When mouse-highlight is off, generate the click for the item
 15621      where the button was pressed, disregarding where it was
 15622      released.  */
 15623   if (NILP (Vmouse_highlight) && !down_p)
 15624     prop_idx = f->last_tool_bar_item;
 15625 
 15626   /* If item is disabled, do nothing.  */
 15627   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15628   if (NILP (enabled_p))
 15629     return;
 15630 
 15631   if (down_p)
 15632     {
 15633       /* Show item in pressed state.  */
 15634       if (!NILP (Vmouse_highlight))
 15635         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 15636       f->last_tool_bar_item = prop_idx;
 15637     }
 15638   else
 15639     {
 15640       Lisp_Object key, frame;
 15641       struct input_event event;
 15642       EVENT_INIT (event);
 15643 
 15644       /* Show item in released state.  */
 15645       if (!NILP (Vmouse_highlight))
 15646         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 15647 
 15648       key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
 15649 
 15650       XSETFRAME (frame, f);
 15651       event.kind = TOOL_BAR_EVENT;
 15652       event.frame_or_window = frame;
 15653       event.arg = key;
 15654       event.modifiers = modifiers;
 15655       event.device = device;
 15656       kbd_buffer_store_event (&event);
 15657       f->last_tool_bar_item = -1;
 15658     }
 15659 }
 15660 
 15661 void
 15662 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
 15663                        int modifiers)
 15664 {
 15665   handle_tool_bar_click_with_device (f, x, y, down_p, modifiers, Qt);
 15666 }
 15667 
 15668 /* Possibly highlight a tool-bar item on frame F when mouse moves to
 15669    tool-bar window-relative coordinates X/Y.  Called from
 15670    note_mouse_highlight.  */
 15671 
 15672 static void
 15673 note_tool_bar_highlight (struct frame *f, int x, int y)
 15674 {
 15675   Lisp_Object window = f->tool_bar_window;
 15676   struct window *w = XWINDOW (window);
 15677   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 15678   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15679   int hpos, vpos;
 15680   struct glyph *glyph;
 15681   struct glyph_row *row;
 15682   int i;
 15683   Lisp_Object enabled_p;
 15684   int prop_idx;
 15685   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 15686   bool mouse_down_p;
 15687   int rc;
 15688 
 15689   /* Function note_mouse_highlight is called with negative X/Y
 15690      values when mouse moves outside of the frame.  */
 15691   if (x <= 0 || y <= 0)
 15692     {
 15693       clear_mouse_face (hlinfo);
 15694       return;
 15695     }
 15696 
 15697   rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15698   if (rc < 0)
 15699     {
 15700       /* Not on tool-bar item.  */
 15701       clear_mouse_face (hlinfo);
 15702       return;
 15703     }
 15704   else if (rc == 0)
 15705     /* On same tool-bar item as before.  */
 15706     goto set_help_echo;
 15707 
 15708   clear_mouse_face (hlinfo);
 15709 
 15710   /* Mouse is down, but on different tool-bar item?  */
 15711   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 15712                   && f == dpyinfo->last_mouse_frame);
 15713 
 15714   if (mouse_down_p && f->last_tool_bar_item != prop_idx)
 15715     return;
 15716 
 15717   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 15718 
 15719   /* If tool-bar item is not enabled, don't highlight it.  */
 15720   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15721   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 15722     {
 15723       /* Compute the x-position of the glyph.  In front and past the
 15724          image is a space.  We include this in the highlighted area.  */
 15725       row = MATRIX_ROW (w->current_matrix, vpos);
 15726       for (i = x = 0; i < hpos; ++i)
 15727         x += row->glyphs[TEXT_AREA][i].pixel_width;
 15728 
 15729       /* Record this as the current active region.  */
 15730       hlinfo->mouse_face_beg_col = hpos;
 15731       hlinfo->mouse_face_beg_row = vpos;
 15732       hlinfo->mouse_face_beg_x = x;
 15733       hlinfo->mouse_face_past_end = false;
 15734 
 15735       hlinfo->mouse_face_end_col = hpos + 1;
 15736       hlinfo->mouse_face_end_row = vpos;
 15737       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 15738       hlinfo->mouse_face_window = window;
 15739       hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
 15740 
 15741       /* Display it as active.  */
 15742       show_mouse_face (hlinfo, draw);
 15743     }
 15744 
 15745  set_help_echo:
 15746 
 15747   /* Set help_echo_string to a help string to display for this tool-bar item.
 15748      XTread_socket does the rest.  */
 15749   help_echo_object = help_echo_window = Qnil;
 15750   help_echo_pos = -1;
 15751   help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
 15752   if (NILP (help_echo_string))
 15753     help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
 15754 }
 15755 
 15756 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15757 
 15758 #endif /* HAVE_WINDOW_SYSTEM */
 15759 
 15760 
 15761 
 15762 /************************************************************************
 15763                          Horizontal scrolling
 15764  ************************************************************************/
 15765 
 15766 /* For all leaf windows in the window tree rooted at WINDOW, set their
 15767    hscroll value so that PT is (i) visible in the window, and (ii) so
 15768    that it is not within a certain margin at the window's left and
 15769    right border.  Value is true if any window's hscroll has been
 15770    changed.  */
 15771 
 15772 static bool
 15773 hscroll_window_tree (Lisp_Object window)
 15774 {
 15775   bool hscrolled_p = false;
 15776   bool hscroll_relative_p = FLOATP (Vhscroll_step);
 15777   int hscroll_step_abs = 0;
 15778   double hscroll_step_rel = 0;
 15779 
 15780   if (hscroll_relative_p)
 15781     {
 15782       hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
 15783       if (hscroll_step_rel < 0)
 15784         {
 15785           hscroll_relative_p = false;
 15786           hscroll_step_abs = 0;
 15787         }
 15788     }
 15789   else if (TYPE_RANGED_FIXNUMP (int, Vhscroll_step))
 15790     {
 15791       hscroll_step_abs = XFIXNUM (Vhscroll_step);
 15792       if (hscroll_step_abs < 0)
 15793         hscroll_step_abs = 0;
 15794     }
 15795   else
 15796     hscroll_step_abs = 0;
 15797 
 15798   while (WINDOWP (window))
 15799     {
 15800       struct window *w = XWINDOW (window);
 15801 
 15802       if (WINDOWP (w->contents))
 15803         hscrolled_p |= hscroll_window_tree (w->contents);
 15804       else if (w->cursor.vpos >= 0
 15805                /* Don't allow hscroll in mini-windows that display
 15806                   echo-area messages.  This is because desired_matrix
 15807                   of such windows was prepared while momentarily
 15808                   switched to an echo-area buffer, which is different
 15809                   from w->contents, and we simply cannot hscroll such
 15810                   windows safely.  */
 15811                && !(w == XWINDOW (echo_area_window)
 15812                     && !NILP (echo_area_buffer[0])))
 15813         {
 15814           int h_margin;
 15815           int text_area_width;
 15816           struct glyph_row *cursor_row;
 15817           struct glyph_row *bottom_row;
 15818 
 15819           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
 15820           if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
 15821             cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
 15822           else
 15823             cursor_row = bottom_row - 1;
 15824 
 15825           if (!cursor_row->enabled_p)
 15826             {
 15827               bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 15828               if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
 15829                 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 15830               else
 15831                 cursor_row = bottom_row - 1;
 15832             }
 15833           bool row_r2l_p = cursor_row->reversed_p;
 15834           bool hscl = hscrolling_current_line_p (w);
 15835           int x_offset = 0;
 15836           /* When line numbers are displayed, we need to account for
 15837              the horizontal space they consume.  */
 15838           if (!NILP (Vdisplay_line_numbers))
 15839             {
 15840               struct glyph *g;
 15841               if (!row_r2l_p)
 15842                 {
 15843                   for (g = cursor_row->glyphs[TEXT_AREA];
 15844                        g < cursor_row->glyphs[TEXT_AREA]
 15845                          + cursor_row->used[TEXT_AREA];
 15846                        g++)
 15847                     {
 15848                       if (!(NILP (g->object) && g->charpos < 0))
 15849                         break;
 15850                       x_offset += g->pixel_width;
 15851                     }
 15852                 }
 15853               else
 15854                 {
 15855                   for (g = cursor_row->glyphs[TEXT_AREA]
 15856                          + cursor_row->used[TEXT_AREA];
 15857                        g > cursor_row->glyphs[TEXT_AREA];
 15858                        g--)
 15859                     {
 15860                       if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
 15861                         break;
 15862                       x_offset += (g - 1)->pixel_width;
 15863                     }
 15864                 }
 15865             }
 15866           if (cursor_row->truncated_on_left_p)
 15867             {
 15868               /* On TTY frames, don't count the left truncation glyph.  */
 15869               struct frame *f = XFRAME (WINDOW_FRAME (w));
 15870               x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
 15871             }
 15872 
 15873           text_area_width = window_box_width (w, TEXT_AREA);
 15874 
 15875           /* Scroll when cursor is inside this scroll margin.  */
 15876           h_margin = (clip_to_bounds (0, hscroll_margin, 1000000)
 15877                       * WINDOW_FRAME_COLUMN_WIDTH (w));
 15878 
 15879           /* If the position of this window's point has explicitly
 15880              changed, no more suspend auto hscrolling.  */
 15881           if (w->suspend_auto_hscroll
 15882               && NILP (Fequal (Fwindow_point (window),
 15883                                Fwindow_old_point (window))))
 15884             {
 15885               w->suspend_auto_hscroll = false;
 15886               /* When hscrolling just the current line, and the rest
 15887                  of lines were temporarily hscrolled, but no longer
 15888                  are, force thorough redisplay of this window, to show
 15889                  the effect of disabling hscroll suspension immediately.  */
 15890               if (w->min_hscroll == 0 && w->hscroll > 0
 15891                   && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
 15892                          Qcurrent_line))
 15893                 SET_FRAME_GARBAGED (XFRAME (w->frame));
 15894             }
 15895 
 15896           /* Remember window point.  */
 15897           Fset_marker (w->old_pointm,
 15898                        ((w == XWINDOW (selected_window))
 15899                         ? make_fixnum (BUF_PT (XBUFFER (w->contents)))
 15900                         : Fmarker_position (w->pointm)),
 15901                        w->contents);
 15902 
 15903           if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
 15904               && !w->suspend_auto_hscroll
 15905               /* In some pathological cases, like restoring a window
 15906                  configuration into a frame that is much smaller than
 15907                  the one from which the configuration was saved, we
 15908                  get glyph rows whose start and end have zero buffer
 15909                  positions, which we cannot handle below.  Just skip
 15910                  such windows.  */
 15911               && (CHARPOS (cursor_row->start.pos)
 15912                   >= BUF_BEG (XBUFFER (w->contents)))
 15913               /* For left-to-right rows, hscroll when cursor is either
 15914                  (i) inside the right hscroll margin, or (ii) if it is
 15915                  inside the left margin and the window is already
 15916                  hscrolled.  */
 15917               && ((!row_r2l_p
 15918                    && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
 15919                        || (cursor_row->enabled_p
 15920                            && cursor_row->truncated_on_right_p
 15921                            && (w->cursor.x >= text_area_width - h_margin))))
 15922                   /* For right-to-left rows, the logic is similar,
 15923                      except that rules for scrolling to left and right
 15924                      are reversed.  E.g., if cursor.x <= h_margin, we
 15925                      need to hscroll "to the right" unconditionally,
 15926                      and that will scroll the screen to the left so as
 15927                      to reveal the next portion of the row.  */
 15928                   || (row_r2l_p
 15929                       && ((cursor_row->enabled_p
 15930                            /* FIXME: It is confusing to set the
 15931                               truncated_on_right_p flag when R2L rows
 15932                               are actually truncated on the left.  */
 15933                            && cursor_row->truncated_on_right_p
 15934                            && w->cursor.x <= h_margin)
 15935                           || (w->hscroll
 15936                               && (w->cursor.x >= (text_area_width - h_margin
 15937                                                   - x_offset)))))
 15938                   /* This last condition is needed when moving
 15939                      vertically from an hscrolled line to a short line
 15940                      that doesn't need to be hscrolled.  If we omit
 15941                      this condition, the line from which we move will
 15942                      remain hscrolled.  */
 15943                   || (hscl
 15944                       && w->hscroll != w->min_hscroll
 15945                       && !cursor_row->truncated_on_left_p)))
 15946             {
 15947               struct it it;
 15948               ptrdiff_t hscroll;
 15949               struct buffer *saved_current_buffer;
 15950               ptrdiff_t pt;
 15951               int wanted_x;
 15952 
 15953               /* Find point in a display of infinite width.  */
 15954               saved_current_buffer = current_buffer;
 15955               current_buffer = XBUFFER (w->contents);
 15956 
 15957               if (w == XWINDOW (selected_window))
 15958                 pt = PT;
 15959               else
 15960                 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
 15961 
 15962               /* Move iterator to pt starting at cursor_row->start in
 15963                  a line with infinite width.  */
 15964               init_to_row_start (&it, w, cursor_row);
 15965               if (hscl)
 15966                 it.first_visible_x = window_hscroll_limited (w, it.f)
 15967                                      * FRAME_COLUMN_WIDTH (it.f);
 15968               it.last_visible_x = DISP_INFINITY;
 15969 
 15970               ptrdiff_t nchars = pt - IT_CHARPOS (it);
 15971               if (current_buffer->long_line_optimizations_p
 15972                   && nchars > large_hscroll_threshold)
 15973                 {
 15974                   /* Special optimization for very long and truncated
 15975                      lines which need to be hscrolled far to the left:
 15976                      jump directly to the (approximate) first position
 15977                      that is visible, instead of slowly walking there.  */
 15978                   fast_move_it_horizontally (&it, nchars);
 15979                   it.current_x += nchars * FRAME_COLUMN_WIDTH (it.f);
 15980                 }
 15981               else
 15982                 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
 15983               /* If the line ends in an overlay string with a newline,
 15984                  we might infloop, because displaying the window will
 15985                  want to put the cursor after the overlay, i.e. at X
 15986                  coordinate of zero on the next screen line.  So we
 15987                  use the buffer position prior to the overlay string
 15988                  instead.  */
 15989               if (it.method == GET_FROM_STRING && pt > 1)
 15990                 {
 15991                   init_to_row_start (&it, w, cursor_row);
 15992                   if (hscl)
 15993                     it.first_visible_x = (window_hscroll_limited (w, it.f)
 15994                                           * FRAME_COLUMN_WIDTH (it.f));
 15995                   if (current_buffer->long_line_optimizations_p
 15996                       && nchars > large_hscroll_threshold)
 15997                     {
 15998                       fast_move_it_horizontally (&it, nchars - 1);
 15999                       it.current_x += (nchars - 1) * FRAME_COLUMN_WIDTH (it.f);
 16000                     }
 16001                   else
 16002                     move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
 16003                 }
 16004               current_buffer = saved_current_buffer;
 16005 
 16006               /* Position cursor in window.  */
 16007               if (!hscroll_relative_p && hscroll_step_abs == 0)
 16008                 hscroll = max (0, (it.current_x
 16009                                    - (ITERATOR_AT_END_OF_LINE_P (&it)
 16010                                       ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
 16011                                       : (text_area_width / 2))))
 16012                           / FRAME_COLUMN_WIDTH (it.f);
 16013               else if ((!row_r2l_p
 16014                         && w->cursor.x >= text_area_width - h_margin)
 16015                        || (row_r2l_p && w->cursor.x <= h_margin))
 16016                 {
 16017                   if (hscroll_relative_p)
 16018                     wanted_x = text_area_width * (1 - hscroll_step_rel)
 16019                                - h_margin;
 16020                   else
 16021                     wanted_x = text_area_width
 16022                                - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 16023                                - h_margin;
 16024                   hscroll
 16025                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 16026                 }
 16027               else
 16028                 {
 16029                   if (hscroll_relative_p)
 16030                     wanted_x =
 16031                       text_area_width * hscroll_step_rel + h_margin + x_offset;
 16032                   else
 16033                     wanted_x =
 16034                       hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 16035                       + h_margin + x_offset;
 16036                   hscroll
 16037                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 16038                 }
 16039               hscroll = max (hscroll, w->min_hscroll);
 16040 
 16041               /* Don't prevent redisplay optimizations if hscroll
 16042                  hasn't changed, as it will unnecessarily slow down
 16043                  redisplay.  */
 16044               if (w->hscroll != hscroll
 16045                   /* When hscrolling only the current line, we need to
 16046                      report hscroll even if its value is equal to the
 16047                      previous one, because the new line might need a
 16048                      different value.  */
 16049                   || (hscl && w->last_cursor_vpos != w->cursor.vpos))
 16050                 {
 16051                   struct buffer *b = XBUFFER (w->contents);
 16052                   b->prevent_redisplay_optimizations_p = true;
 16053                   w->hscroll = hscroll;
 16054                   hscrolled_p = true;
 16055                 }
 16056             }
 16057         }
 16058 
 16059       window = w->next;
 16060     }
 16061 
 16062   /* Value is true if hscroll of any leaf window has been changed.  */
 16063   return hscrolled_p;
 16064 }
 16065 
 16066 
 16067 /* Set hscroll so that cursor is visible and not inside horizontal
 16068    scroll margins for all windows in the tree rooted at WINDOW.  See
 16069    also hscroll_window_tree above.  Value is true if any window's
 16070    hscroll has been changed.  If it has, desired matrices on the frame
 16071    of WINDOW are cleared.  */
 16072 
 16073 static bool
 16074 hscroll_windows (Lisp_Object window)
 16075 {
 16076   bool hscrolled_p = hscroll_window_tree (window);
 16077   if (hscrolled_p)
 16078     clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
 16079   return hscrolled_p;
 16080 }
 16081 
 16082 
 16083 
 16084 /************************************************************************
 16085                                 Redisplay
 16086  ************************************************************************/
 16087 
 16088 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
 16089    This is sometimes handy to have in a debugger session.  */
 16090 
 16091 #ifdef GLYPH_DEBUG
 16092 
 16093 /* First and last unchanged row for try_window_id.  */
 16094 
 16095 static int debug_first_unchanged_at_end_vpos;
 16096 static int debug_last_unchanged_at_beg_vpos;
 16097 
 16098 /* Delta vpos and y.  */
 16099 
 16100 static int debug_dvpos, debug_dy;
 16101 
 16102 /* Delta in characters and bytes for try_window_id.  */
 16103 
 16104 static ptrdiff_t debug_delta, debug_delta_bytes;
 16105 
 16106 /* Values of window_end_pos and window_end_vpos at the end of
 16107    try_window_id.  */
 16108 
 16109 static ptrdiff_t debug_end_vpos;
 16110 
 16111 /* Append a string to W->desired_matrix->method.  FMT is a printf
 16112    format string.  If trace_redisplay_p is true also printf the
 16113    resulting string to stderr.  */
 16114 
 16115 static void debug_method_add (struct window *, char const *, ...)
 16116   ATTRIBUTE_FORMAT_PRINTF (2, 3);
 16117 
 16118 static void
 16119 debug_method_add (struct window *w, char const *fmt, ...)
 16120 {
 16121   void *ptr = w;
 16122   char *method = w->desired_matrix->method;
 16123   int len = strlen (method);
 16124   int size = sizeof w->desired_matrix->method;
 16125   int remaining = size - len - 1;
 16126   va_list ap;
 16127 
 16128   if (len && remaining)
 16129     {
 16130       method[len] = '|';
 16131       --remaining, ++len;
 16132     }
 16133 
 16134   va_start (ap, fmt);
 16135   vsnprintf (method + len, remaining + 1, fmt, ap);
 16136   va_end (ap);
 16137 
 16138   if (trace_redisplay_p)
 16139     fprintf (stderr, "%p (%s): %s\n",
 16140              ptr,
 16141              ((BUFFERP (w->contents)
 16142                && STRINGP (BVAR (XBUFFER (w->contents), name)))
 16143               ? SSDATA (BVAR (XBUFFER (w->contents), name))
 16144               : "no buffer"),
 16145              method + len);
 16146 }
 16147 
 16148 #endif /* GLYPH_DEBUG */
 16149 
 16150 
 16151 /* Value is true if all changes in window W, which displays
 16152    current_buffer, are in the text between START and END.  START is a
 16153    buffer position, END is given as a distance from Z.  Used in
 16154    redisplay_internal for display optimization.  */
 16155 
 16156 static bool
 16157 text_outside_line_unchanged_p (struct window *w,
 16158                                ptrdiff_t start, ptrdiff_t end)
 16159 {
 16160   bool unchanged_p = true;
 16161 
 16162   /* If text or overlays have changed, see where.  */
 16163   if (window_outdated (w))
 16164     {
 16165       /* Gap in the line?  */
 16166       if (GPT < start || Z - GPT < end)
 16167         unchanged_p = false;
 16168 
 16169       /* Changes start in front of the line, or end after it?  */
 16170       if (unchanged_p
 16171           && (BEG_UNCHANGED < start - 1
 16172               || END_UNCHANGED < end))
 16173         unchanged_p = false;
 16174 
 16175       /* If selective display, can't optimize if changes start at the
 16176          beginning of the line.  */
 16177       if (unchanged_p
 16178           && FIXNUMP (BVAR (current_buffer, selective_display))
 16179           && XFIXNUM (BVAR (current_buffer, selective_display)) > 0
 16180           && (BEG_UNCHANGED < start || GPT <= start))
 16181         unchanged_p = false;
 16182 
 16183       /* If there are overlays at the start or end of the line, these
 16184          may have overlay strings with newlines in them.  A change at
 16185          START, for instance, may actually concern the display of such
 16186          overlay strings as well, and they are displayed on different
 16187          lines.  So, quickly rule out this case.  (For the future, it
 16188          might be desirable to implement something more telling than
 16189          just BEG/END_UNCHANGED.)  */
 16190       if (unchanged_p)
 16191         {
 16192           if (BEG + BEG_UNCHANGED == start
 16193               && overlay_touches_p (start))
 16194             unchanged_p = false;
 16195           if (END_UNCHANGED == end
 16196               && overlay_touches_p (Z - end))
 16197             unchanged_p = false;
 16198         }
 16199 
 16200       /* Under bidi reordering, adding or deleting a character in the
 16201          beginning of a paragraph, before the first strong directional
 16202          character, can change the base direction of the paragraph (unless
 16203          the buffer specifies a fixed paragraph direction), which will
 16204          require redisplaying the whole paragraph.  It might be worthwhile
 16205          to find the paragraph limits and widen the range of redisplayed
 16206          lines to that, but for now just give up this optimization.  */
 16207       if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 16208           && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 16209         unchanged_p = false;
 16210     }
 16211 
 16212   return unchanged_p;
 16213 }
 16214 
 16215 
 16216 /* Do a frame update, taking possible shortcuts into account.  This is
 16217    the main external entry point for redisplay.
 16218 
 16219    If the last redisplay displayed an echo area message and that message
 16220    is no longer requested, we clear the echo area or bring back the
 16221    mini-buffer if that is in use.  */
 16222 
 16223 void
 16224 redisplay (void)
 16225 {
 16226   redisplay_internal ();
 16227 }
 16228 
 16229 
 16230 static Lisp_Object
 16231 overlay_arrow_string_or_property (Lisp_Object var)
 16232 {
 16233   Lisp_Object val;
 16234 
 16235   if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
 16236     return val;
 16237 
 16238   return Voverlay_arrow_string;
 16239 }
 16240 
 16241 /* Return true if there are any overlay-arrows in current_buffer.  */
 16242 static bool
 16243 overlay_arrow_in_current_buffer_p (void)
 16244 {
 16245   Lisp_Object vlist;
 16246 
 16247   for (vlist = Voverlay_arrow_variable_list;
 16248        CONSP (vlist);
 16249        vlist = XCDR (vlist))
 16250     {
 16251       Lisp_Object var = XCAR (vlist);
 16252       Lisp_Object val;
 16253 
 16254       if (!SYMBOLP (var))
 16255         continue;
 16256       val = find_symbol_value (var);
 16257       if (MARKERP (val)
 16258           && current_buffer == XMARKER (val)->buffer)
 16259         return true;
 16260     }
 16261   return false;
 16262 }
 16263 
 16264 
 16265 /* Return true if any overlay_arrows have moved or overlay-arrow-string
 16266    has changed.
 16267    If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
 16268    buffers that are affected.  */
 16269 
 16270 static bool
 16271 overlay_arrows_changed_p (bool set_redisplay)
 16272 {
 16273   Lisp_Object vlist;
 16274   bool changed = false;
 16275 
 16276   for (vlist = Voverlay_arrow_variable_list;
 16277        CONSP (vlist);
 16278        vlist = XCDR (vlist))
 16279     {
 16280       Lisp_Object var = XCAR (vlist);
 16281       Lisp_Object val, pstr;
 16282 
 16283       if (!SYMBOLP (var))
 16284         continue;
 16285       val = find_symbol_value (var);
 16286       if (!MARKERP (val))
 16287         continue;
 16288       if (! EQ (Fmarker_position (val),
 16289                 /* FIXME: Don't we have a problem, using such a global
 16290                  * "last-position" if the variable is buffer-local?  */
 16291                 Fget (var, Qlast_arrow_position))
 16292           || ! (pstr = overlay_arrow_string_or_property (var),
 16293                 EQ (pstr, Fget (var, Qlast_arrow_string))))
 16294         {
 16295           struct buffer *buf = XMARKER (val)->buffer;
 16296 
 16297           if (set_redisplay)
 16298             {
 16299               if (buf)
 16300                 bset_redisplay (buf);
 16301               changed = true;
 16302             }
 16303           else
 16304             return true;
 16305         }
 16306     }
 16307   return changed;
 16308 }
 16309 
 16310 /* Mark overlay arrows to be updated on next redisplay.  */
 16311 
 16312 static void
 16313 update_overlay_arrows (int up_to_date)
 16314 {
 16315   Lisp_Object vlist;
 16316 
 16317   for (vlist = Voverlay_arrow_variable_list;
 16318        CONSP (vlist);
 16319        vlist = XCDR (vlist))
 16320     {
 16321       Lisp_Object var = XCAR (vlist);
 16322 
 16323       if (!SYMBOLP (var))
 16324         continue;
 16325 
 16326       if (up_to_date > 0)
 16327         {
 16328           Lisp_Object val = find_symbol_value (var);
 16329           if (!MARKERP (val))
 16330             continue;
 16331           Fput (var, Qlast_arrow_position, Fmarker_position (val));
 16332           Fput (var, Qlast_arrow_string,
 16333                 overlay_arrow_string_or_property (var));
 16334         }
 16335       else if (up_to_date < 0
 16336                || !NILP (Fget (var, Qlast_arrow_position)))
 16337         {
 16338           Fput (var, Qlast_arrow_position, Qt);
 16339           Fput (var, Qlast_arrow_string, Qt);
 16340         }
 16341     }
 16342 }
 16343 
 16344 
 16345 /* Return overlay arrow string to display at row.
 16346    Return integer (bitmap number) for arrow bitmap in left fringe.
 16347    Return nil if no overlay arrow.  */
 16348 
 16349 static Lisp_Object
 16350 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
 16351 {
 16352   Lisp_Object vlist;
 16353 
 16354   for (vlist = Voverlay_arrow_variable_list;
 16355        CONSP (vlist);
 16356        vlist = XCDR (vlist))
 16357     {
 16358       Lisp_Object var = XCAR (vlist);
 16359       Lisp_Object val;
 16360 
 16361       if (!SYMBOLP (var))
 16362         continue;
 16363 
 16364       val = find_symbol_value (var);
 16365 
 16366       if (MARKERP (val)
 16367           && current_buffer == XMARKER (val)->buffer
 16368           && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
 16369         {
 16370           if (FRAME_WINDOW_P (it->f)
 16371               /* FIXME: if ROW->reversed_p is set, this should test
 16372                  the right fringe, not the left one.  */
 16373               && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
 16374             {
 16375 #ifdef HAVE_WINDOW_SYSTEM
 16376               if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
 16377                 {
 16378                   int fringe_bitmap = lookup_fringe_bitmap (val);
 16379                   if (fringe_bitmap != 0)
 16380                     return make_fixnum (fringe_bitmap);
 16381                 }
 16382 #endif
 16383               return make_fixnum (-1); /* Use default arrow bitmap.  */
 16384             }
 16385           return overlay_arrow_string_or_property (var);
 16386         }
 16387     }
 16388 
 16389   return Qnil;
 16390 }
 16391 
 16392 /* Return true if point moved out of or into a composition.  Otherwise
 16393    return false.  PREV_BUF and PREV_PT are the last point buffer and
 16394    position.  BUF and PT are the current point buffer and position.  */
 16395 
 16396 static bool
 16397 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
 16398                             struct buffer *buf, ptrdiff_t pt)
 16399 {
 16400   ptrdiff_t start, end;
 16401   Lisp_Object prop;
 16402   Lisp_Object buffer;
 16403 
 16404   XSETBUFFER (buffer, buf);
 16405   /* Check a composition at the last point if point moved within the
 16406      same buffer.  */
 16407   if (prev_buf == buf)
 16408     {
 16409       if (prev_pt == pt)
 16410         /* Point didn't move.  */
 16411         return false;
 16412 
 16413       if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
 16414           && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
 16415           && composition_valid_p (start, end, prop)
 16416           && start < prev_pt && end > prev_pt)
 16417         /* The last point was within the composition.  Return true iff
 16418             point moved out of the composition.  */
 16419         return (pt <= start || pt >= end);
 16420     }
 16421 
 16422   /* Check a composition at the current point.  */
 16423   return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
 16424           && find_composition (pt, -1, &start, &end, &prop, buffer)
 16425           && composition_valid_p (start, end, prop)
 16426           && start < pt && end > pt);
 16427 }
 16428 
 16429 /* Reconsider the clip changes of buffer which is displayed in W.  */
 16430 
 16431 static void
 16432 reconsider_clip_changes (struct window *w)
 16433 {
 16434   struct buffer *b = XBUFFER (w->contents);
 16435 
 16436   if (b->clip_changed
 16437       && w->window_end_valid
 16438       && w->current_matrix->buffer == b
 16439       && w->current_matrix->zv == BUF_ZV (b)
 16440       && w->current_matrix->begv == BUF_BEGV (b))
 16441     b->clip_changed = false;
 16442 
 16443   /* If display wasn't paused, and W is not a tool bar window, see if
 16444      point has been moved into or out of a composition.  In that case,
 16445      set b->clip_changed to force updating the screen.  If
 16446      b->clip_changed has already been set, skip this check.  */
 16447   if (!b->clip_changed && w->window_end_valid)
 16448     {
 16449       ptrdiff_t pt = (w == XWINDOW (selected_window)
 16450                       ? PT : marker_position (w->pointm));
 16451 
 16452       if ((w->current_matrix->buffer != b || pt != w->last_point)
 16453           && check_point_in_composition (w->current_matrix->buffer,
 16454                                          w->last_point, b, pt))
 16455         b->clip_changed = true;
 16456     }
 16457 }
 16458 
 16459 static void
 16460 propagate_buffer_redisplay (void)
 16461 { /* Resetting b->text->redisplay is problematic!
 16462      We can't just reset it in the case that some window that displays
 16463      it has not been redisplayed; and such a window can stay
 16464      unredisplayed for a long time if it's currently invisible.
 16465      But we do want to reset it at the end of redisplay otherwise
 16466      its displayed windows will keep being redisplayed over and over
 16467      again.
 16468      So we copy all b->text->redisplay flags up to their windows here,
 16469      such that mark_window_display_accurate can safely reset
 16470      b->text->redisplay.  */
 16471   Lisp_Object ws = window_list ();
 16472   for (; CONSP (ws); ws = XCDR (ws))
 16473     {
 16474       struct window *thisw = XWINDOW (XCAR (ws));
 16475       struct buffer *thisb = XBUFFER (thisw->contents);
 16476       if (thisb->text->redisplay)
 16477         thisw->redisplay = true;
 16478     }
 16479 }
 16480 
 16481 #define STOP_POLLING                                    \
 16482 do { if (! polling_stopped_here) stop_polling ();       \
 16483        polling_stopped_here = true; } while (false)
 16484 
 16485 #define RESUME_POLLING                                  \
 16486 do { if (polling_stopped_here) start_polling ();        \
 16487        polling_stopped_here = false; } while (false)
 16488 
 16489 /* Perhaps in the future avoid recentering windows if it
 16490    is not necessary; currently that causes some problems.  */
 16491 
 16492 static void
 16493 redisplay_internal (void)
 16494 {
 16495   struct window *w = XWINDOW (selected_window);
 16496   struct window *sw;
 16497   struct frame *fr;
 16498   bool pending;
 16499   bool must_finish = false, match_p;
 16500   struct text_pos tlbufpos, tlendpos;
 16501   int number_of_visible_frames;
 16502   struct frame *sf;
 16503   bool polling_stopped_here = false;
 16504   Lisp_Object tail, frame;
 16505 
 16506   /* Set a limit to the number of retries we perform due to horizontal
 16507      scrolling, this avoids getting stuck in an uninterruptible
 16508      infinite loop (Bug #24633).  */
 16509   enum { MAX_HSCROLL_RETRIES = 16 };
 16510   int hscroll_retries = 0;
 16511 
 16512   /* Limit the number of retries for when frame(s) become garbaged as
 16513      result of redisplaying them.  Some packages set various redisplay
 16514      hooks, such as window-scroll-functions, to run Lisp that always
 16515      calls APIs which cause the frame's garbaged flag to become set,
 16516      so we loop indefinitely.  */
 16517   enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
 16518   int garbaged_frame_retries = 0;
 16519 
 16520   /* True means redisplay has to consider all windows on all
 16521      frames.  False, only selected_window is considered.  */
 16522   bool consider_all_windows_p;
 16523 
 16524   /* True means redisplay has to redisplay the miniwindow.  */
 16525   bool update_miniwindow_p = false;
 16526 
 16527   redisplay_trace ("redisplay_internal %d\n", redisplaying_p);
 16528 
 16529   /* No redisplay if running in batch mode or frame is not yet fully
 16530      initialized, or redisplay is explicitly turned off by setting
 16531      Vinhibit_redisplay.  */
 16532   if ((FRAME_INITIAL_P (SELECTED_FRAME ())
 16533        && redisplay_skip_initial_frame)
 16534       || !NILP (Vinhibit_redisplay))
 16535     return;
 16536 
 16537   /* Don't examine these until after testing Vinhibit_redisplay.
 16538      When Emacs is shutting down, perhaps because its connection to
 16539      X has dropped, we should not look at them at all.  */
 16540   fr = XFRAME (w->frame);
 16541   sf = SELECTED_FRAME ();
 16542 
 16543   if (!fr->glyphs_initialized_p)
 16544     return;
 16545 
 16546 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
 16547   if (popup_activated ())
 16548     return;
 16549 #endif
 16550 
 16551 #if defined (HAVE_HAIKU)
 16552   if (popup_activated_p)
 16553     return;
 16554 #endif
 16555 
 16556   /* I don't think this happens but let's be paranoid.  */
 16557   if (redisplaying_p)
 16558     return;
 16559 
 16560   /* Record a function that clears redisplaying_p
 16561      when we leave this function.  */
 16562   specpdl_ref count = SPECPDL_INDEX ();
 16563   record_unwind_protect_void (unwind_redisplay);
 16564   redisplaying_p = true;
 16565   block_buffer_flips ();
 16566   specbind (Qinhibit_free_realized_faces, Qnil);
 16567 
 16568   /* Record this function, so it appears on the profiler's backtraces.  */
 16569   record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
 16570 
 16571   FOR_EACH_FRAME (tail, frame)
 16572     XFRAME (frame)->already_hscrolled_p = false;
 16573 
 16574   reset_outermost_restrictions ();
 16575 
 16576  retry:
 16577   /* Remember the currently selected window.  */
 16578   sw = w;
 16579 
 16580   pending = false;
 16581   forget_escape_and_glyphless_faces ();
 16582 
 16583   inhibit_free_realized_faces = false;
 16584 
 16585   /* If face_change, init_iterator will free all realized faces, which
 16586      includes the faces referenced from current matrices.  So, we
 16587      can't reuse current matrices in this case.  */
 16588   if (face_change)
 16589     windows_or_buffers_changed = 47;
 16590 
 16591   if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
 16592       && FRAME_TTY (sf)->previous_frame != sf)
 16593     {
 16594       /* Since frames on a single ASCII terminal share the same
 16595          display area, displaying a different frame means redisplay
 16596          the whole thing.  */
 16597       SET_FRAME_GARBAGED (sf);
 16598 #ifndef DOS_NT
 16599       set_tty_color_mode (FRAME_TTY (sf), sf);
 16600 #endif
 16601       FRAME_TTY (sf)->previous_frame = sf;
 16602     }
 16603 
 16604   /* Set the visible flags for all frames.  Do this before checking for
 16605      resized or garbaged frames; they want to know if their frames are
 16606      visible.  See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
 16607   number_of_visible_frames = 0;
 16608 
 16609   FOR_EACH_FRAME (tail, frame)
 16610     {
 16611       struct frame *f = XFRAME (frame);
 16612 
 16613       if (FRAME_VISIBLE_P (f))
 16614         {
 16615           ++number_of_visible_frames;
 16616           /* Adjust matrices for visible frames only.  */
 16617           if (f->fonts_changed)
 16618             {
 16619               adjust_frame_glyphs (f);
 16620               /* Disable all redisplay optimizations for this frame.
 16621                  This is because adjust_frame_glyphs resets the
 16622                  enabled_p flag for all glyph rows of all windows, so
 16623                  many optimizations will fail anyway, and some might
 16624                  fail to test that flag and do bogus things as
 16625                  result.  */
 16626               SET_FRAME_GARBAGED (f);
 16627               f->fonts_changed = false;
 16628             }
 16629           /* If cursor type has been changed on the frame
 16630              other than selected, consider all frames.  */
 16631           if (f != sf && f->cursor_type_changed)
 16632             fset_redisplay (f);
 16633         }
 16634       clear_desired_matrices (f);
 16635     }
 16636 
 16637   /* Notice any pending interrupt request to change frame size.  */
 16638   do_pending_window_change (true);
 16639 
 16640   /* Clear frames marked as garbaged.  */
 16641   clear_garbaged_frames ();
 16642 
 16643   /* Build menubar and tool-bar items.  */
 16644   if (NILP (Vmemory_full))
 16645     prepare_menu_bars ();
 16646 
 16647   /* do_pending_window_change could change the selected_window due to
 16648      frame resizing which makes the selected window too small.
 16649      prepare_menu_bars may call lisp hooks and hence also change the
 16650      selected_window.  */
 16651   if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
 16652     sw = w;
 16653 
 16654   reconsider_clip_changes (w);
 16655 
 16656   /* In most cases selected window displays current buffer.  */
 16657   match_p = XBUFFER (w->contents) == current_buffer;
 16658   if (match_p)
 16659     {
 16660       /* Detect case that we need to write or remove a star in the mode line.  */
 16661       if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
 16662         w->update_mode_line = true;
 16663 
 16664       if (mode_line_update_needed (w))
 16665         w->update_mode_line = true;
 16666 
 16667       /* If reconsider_clip_changes above decided that the narrowing
 16668          in the current buffer changed, make sure all other windows
 16669          showing that buffer will be redisplayed.  */
 16670       if (current_buffer->clip_changed)
 16671         bset_update_mode_line (current_buffer);
 16672     }
 16673 
 16674   /* Normally the message* functions will have already displayed and
 16675      updated the echo area, but the frame may have been trashed, or
 16676      the update may have been preempted, so display the echo area
 16677      again here.  Checking message_cleared_p captures the case that
 16678      the echo area should be cleared.  */
 16679   if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
 16680       || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
 16681       || (message_cleared_p
 16682           && minibuf_level == 0
 16683           /* If the mini-window is currently selected, this means the
 16684              echo-area doesn't show through.  */
 16685           && !MINI_WINDOW_P (XWINDOW (selected_window))))
 16686     {
 16687       echo_area_display (false);
 16688 
 16689       if (message_cleared_p)
 16690         update_miniwindow_p = true;
 16691 
 16692       must_finish = true;
 16693 
 16694       /* If we don't display the current message, don't clear the
 16695          message_cleared_p flag, because, if we did, we wouldn't clear
 16696          the echo area in the next redisplay which doesn't preserve
 16697          the echo area.  */
 16698       if (!display_last_displayed_message_p)
 16699         message_cleared_p = false;
 16700     }
 16701   else if (EQ (selected_window, minibuf_window)
 16702            && (current_buffer->clip_changed || window_outdated (w))
 16703            && resize_mini_window (w, false))
 16704     {
 16705       /* Resized active mini-window to fit the size of what it is
 16706          showing if its contents might have changed.  */
 16707       must_finish = true;
 16708 
 16709       /* If window configuration was changed, frames may have been
 16710          marked garbaged.  Clear them or we will experience
 16711          surprises wrt scrolling.  */
 16712       clear_garbaged_frames ();
 16713     }
 16714 
 16715   if (!NILP (Vrun_hooks))
 16716     run_window_change_functions ();
 16717 
 16718   if (windows_or_buffers_changed && !update_mode_lines)
 16719     /* Code that sets windows_or_buffers_changed doesn't distinguish whether
 16720        only the windows's contents needs to be refreshed, or whether the
 16721        mode-lines also need a refresh.  */
 16722     update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
 16723                          ? REDISPLAY_SOME : 32);
 16724 
 16725   /* If specs for an arrow have changed, do thorough redisplay
 16726      to ensure we remove any arrow that should no longer exist.  */
 16727   /* Apparently, this is the only case where we update other windows,
 16728      without updating other mode-lines.  */
 16729   overlay_arrows_changed_p (true);
 16730 
 16731   consider_all_windows_p = (update_mode_lines
 16732                             || windows_or_buffers_changed);
 16733 
 16734 #define AINC(a,i)                                                       \
 16735   {                                                                     \
 16736     Lisp_Object entry = Fgethash (make_fixnum (i), a, make_fixnum (0)); \
 16737     if (FIXNUMP (entry))                                                \
 16738       Fputhash (make_fixnum (i), make_fixnum (1 + XFIXNUM (entry)), a); \
 16739   }
 16740 
 16741   AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
 16742   AINC (Vredisplay__mode_lines_cause, update_mode_lines);
 16743 
 16744   /* Optimize the case that only the line containing the cursor in the
 16745      selected window has changed.  Variables starting with this_ are
 16746      set in display_line and record information about the line
 16747      containing the cursor.  */
 16748   tlbufpos = this_line_start_pos;
 16749   tlendpos = this_line_end_pos;
 16750   if (!consider_all_windows_p
 16751       && CHARPOS (tlbufpos) > 0
 16752       && !w->update_mode_line
 16753       && !current_buffer->clip_changed
 16754       && !current_buffer->prevent_redisplay_optimizations_p
 16755       && FRAME_VISIBLE_P (XFRAME (w->frame))
 16756       && !FRAME_OBSCURED_P (XFRAME (w->frame))
 16757       && !XFRAME (w->frame)->cursor_type_changed
 16758       && !XFRAME (w->frame)->face_change
 16759       /* Make sure recorded data applies to current buffer, etc.  */
 16760       && this_line_buffer == current_buffer
 16761       && match_p
 16762       && !w->force_start
 16763       && !w->optional_new_start
 16764       /* Point must be on the line that we have info recorded about.  */
 16765       && PT >= CHARPOS (tlbufpos)
 16766       && PT <= Z - CHARPOS (tlendpos)
 16767       /* FIXME: The following condition is only needed when
 16768          significant parts of the buffer are hidden (e.g., under
 16769          hs-minor-mode), but there doesn't seem to be a simple way of
 16770          detecting that, so we always disable the one-line redisplay
 16771          optimizations whenever display-line-numbers-mode is turned on
 16772          in the buffer.  */
 16773       && (NILP (Vdisplay_line_numbers)
 16774           || EQ (Vdisplay_line_numbers, Qvisual))
 16775       /* All text outside that line, including its final newline,
 16776          must be unchanged.  */
 16777       && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
 16778                                         CHARPOS (tlendpos)))
 16779     {
 16780       if (CHARPOS (tlbufpos) > BEGV
 16781           && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
 16782           && (CHARPOS (tlbufpos) == ZV
 16783               || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
 16784         /* Former continuation line has disappeared by becoming empty.  */
 16785         goto cancel;
 16786       else if (window_outdated (w) || MINI_WINDOW_P (w))
 16787         {
 16788           /* We have to handle the case of continuation around a
 16789              wide-column character (see the comment in indent.c around
 16790              line 1340).
 16791 
 16792              For instance, in the following case:
 16793 
 16794              --------  Insert  --------
 16795              K_A_N_\\   `a'    K_A_N_a\         `X_' are wide-column chars.
 16796              J_I_       ==>    J_I_             `^^' are cursors.
 16797              ^^                ^^
 16798              --------          --------
 16799 
 16800              As we have to redraw the line above, we cannot use this
 16801              optimization.  */
 16802 
 16803           struct it it;
 16804           int line_height_before = this_line_pixel_height;
 16805 
 16806           /* Note that start_display will handle the case that the
 16807              line starting at tlbufpos is a continuation line.  */
 16808           start_display (&it, w, tlbufpos);
 16809 
 16810           /* Implementation note: It this still necessary?  */
 16811           if (it.current_x != this_line_start_x)
 16812             goto cancel;
 16813 
 16814           /* Give up on this optimization if the line starts with a
 16815              string with display property that draws on the fringes,
 16816              as that might interfere with line-prefix display.  */
 16817           if (it.sp > 1
 16818               && it.method == GET_FROM_IMAGE && it.image_id == -1)
 16819             goto cancel;
 16820           redisplay_trace ("trying display optimization 1\n");
 16821           w->cursor.vpos = -1;
 16822           overlay_arrow_seen = false;
 16823           it.vpos = this_line_vpos;
 16824           it.current_y = this_line_y;
 16825           it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
 16826           display_line (&it, -1);
 16827 
 16828           /* If line contains point, is not continued,
 16829              and ends at same distance from eob as before, we win.  */
 16830           if (w->cursor.vpos >= 0
 16831               /* Line is not continued, otherwise this_line_start_pos
 16832                  would have been set to 0 in display_line.  */
 16833               && CHARPOS (this_line_start_pos)
 16834               /* Line ends as before.  */
 16835               && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
 16836               /* Line has same height as before.  Otherwise other lines
 16837                  would have to be shifted up or down.  */
 16838               && this_line_pixel_height == line_height_before
 16839               /* Cannot use this optimization if hscrolling current
 16840                  line and this line is the current one, because
 16841                  display_line above is not informed about the
 16842                  current-line's vpos, and cannot DTRT in that case.  */
 16843               && !hscrolling_current_line_p (w))
 16844             {
 16845               /* If this is not the window's last line, we must adjust
 16846                  the charstarts of the lines below.  */
 16847               if (it.current_y < it.last_visible_y)
 16848                 {
 16849                   struct glyph_row *row
 16850                     = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
 16851                   ptrdiff_t delta, delta_bytes;
 16852 
 16853                   /* We used to distinguish between two cases here,
 16854                      conditioned by Z - CHARPOS (tlendpos) == ZV, for
 16855                      when the line ends in a newline or the end of the
 16856                      buffer's accessible portion.  But both cases did
 16857                      the same, so they were collapsed.  */
 16858                   delta = (Z
 16859                            - CHARPOS (tlendpos)
 16860                            - MATRIX_ROW_START_CHARPOS (row));
 16861                   delta_bytes = (Z_BYTE
 16862                                  - BYTEPOS (tlendpos)
 16863                                  - MATRIX_ROW_START_BYTEPOS (row));
 16864 
 16865                   increment_matrix_positions (w->current_matrix,
 16866                                               this_line_vpos + 1,
 16867                                               w->current_matrix->nrows,
 16868                                               delta, delta_bytes);
 16869                 }
 16870 
 16871               /* If this row displays text now but previously didn't,
 16872                  or vice versa, w->window_end_vpos may have to be
 16873                  adjusted.  */
 16874               if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
 16875                 {
 16876                   if (w->window_end_vpos < this_line_vpos)
 16877                     w->window_end_vpos = this_line_vpos;
 16878                 }
 16879               else if (w->window_end_vpos == this_line_vpos
 16880                        && this_line_vpos > 0)
 16881                 w->window_end_vpos = this_line_vpos - 1;
 16882               w->window_end_valid = false;
 16883 
 16884               /* Update hint: No need to try to scroll in update_window.  */
 16885               w->desired_matrix->no_scrolling_p = true;
 16886 
 16887 #ifdef GLYPH_DEBUG
 16888               *w->desired_matrix->method = 0;
 16889               debug_method_add (w, "optimization 1");
 16890 #endif
 16891 #ifdef HAVE_WINDOW_SYSTEM
 16892               update_window_fringes (w, false);
 16893 #endif
 16894               goto update;
 16895             }
 16896           else
 16897             goto cancel;
 16898         }
 16899       else if (/* Cursor position hasn't changed.  */
 16900                PT == w->last_point
 16901                /* Make sure the cursor was last displayed
 16902                   in this window.  Otherwise we have to reposition it.  */
 16903 
 16904                /* PXW: Must be converted to pixels, probably.  */
 16905                && 0 <= w->cursor.vpos
 16906                && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
 16907         {
 16908           if (!must_finish)
 16909             {
 16910               do_pending_window_change (true);
 16911               /* If selected_window changed, redisplay again.  */
 16912               if (WINDOWP (selected_window)
 16913                   && (w = XWINDOW (selected_window)) != sw)
 16914                 goto retry;
 16915 
 16916                 /* We used to always goto end_of_redisplay here, but this
 16917                  isn't enough if we have a blinking cursor.  */
 16918               if (w->cursor_off_p == w->last_cursor_off_p)
 16919                 goto end_of_redisplay;
 16920             }
 16921           goto update;
 16922         }
 16923       /* If highlighting the region, or if the cursor is in the echo area,
 16924          then we can't just move the cursor.  */
 16925       else if (NILP (Vshow_trailing_whitespace)
 16926                && !cursor_in_echo_area
 16927                && !composition_break_at_point)
 16928         {
 16929           struct it it;
 16930           struct glyph_row *row;
 16931 
 16932           /* Skip from tlbufpos to PT and see where it is.  Note that
 16933              PT may be in invisible text.  If so, we will end at the
 16934              next visible position.  */
 16935           init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
 16936                          NULL, DEFAULT_FACE_ID);
 16937           it.current_x = this_line_start_x;
 16938           it.current_y = this_line_y;
 16939           it.vpos = this_line_vpos;
 16940 
 16941           if (current_buffer->long_line_optimizations_p
 16942               && it.line_wrap == TRUNCATE
 16943               && PT - CHARPOS (tlbufpos) > large_hscroll_threshold)
 16944             {
 16945               /* When lines are very long and truncated, jumping to
 16946                  the next visible line is much faster than slowly
 16947                  iterating there.  */
 16948               reseat_at_next_visible_line_start (&it, false);
 16949               if (IT_CHARPOS (it) <= PT) /* point moved off this line */
 16950                 it.vpos = this_line_vpos + 1;
 16951             }
 16952           else
 16953             {
 16954               /* The call to move_it_to stops in front of PT, but
 16955                  moves over before-strings.  */
 16956               move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
 16957             }
 16958 
 16959           if (it.vpos == this_line_vpos
 16960               && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
 16961                   row->enabled_p))
 16962             {
 16963               eassert (this_line_vpos == it.vpos);
 16964               eassert (this_line_y == it.current_y);
 16965               set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 16966               if (cursor_row_fully_visible_p (w, false, true, false))
 16967                 {
 16968 #ifdef GLYPH_DEBUG
 16969                   *w->desired_matrix->method = 0;
 16970                   debug_method_add (w, "optimization 3");
 16971 #endif
 16972                   goto update;
 16973                 }
 16974               else
 16975                 goto cancel;
 16976             }
 16977           else
 16978             goto cancel;
 16979         }
 16980 
 16981     cancel:
 16982       /* Text changed drastically or point moved off of line.  */
 16983       SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
 16984     }
 16985 
 16986   CHARPOS (this_line_start_pos) = 0;
 16987   ++clear_face_cache_count;
 16988 #ifdef HAVE_WINDOW_SYSTEM
 16989   ++clear_image_cache_count;
 16990 #endif
 16991 
 16992   /* Build desired matrices, and update the display.  If
 16993      consider_all_windows_p, do it for all windows on all frames that
 16994      require redisplay, as specified by their 'redisplay' flag.
 16995      Otherwise do it for selected_window, only.  */
 16996 
 16997   if (consider_all_windows_p)
 16998     {
 16999       FOR_EACH_FRAME (tail, frame)
 17000         XFRAME (frame)->updated_p = false;
 17001 
 17002       propagate_buffer_redisplay ();
 17003 
 17004       FOR_EACH_FRAME (tail, frame)
 17005         {
 17006           struct frame *f = XFRAME (frame);
 17007 
 17008           /* We don't have to do anything for unselected terminal
 17009              frames.  */
 17010           if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
 17011               && !EQ (FRAME_TTY (f)->top_frame, frame))
 17012             continue;
 17013 
 17014         retry_frame:
 17015           if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
 17016             {
 17017               bool gcscrollbars
 17018                 /* Only GC scrollbars when we redisplay the whole frame.  */
 17019                 = f->redisplay || !REDISPLAY_SOME_P ();
 17020               bool f_redisplay_flag = f->redisplay;
 17021 
 17022               /* The X error handler may have deleted that frame
 17023                  before we went back to retry_frame.  This must come
 17024                  before any accesses to f->terminal.  */
 17025               if (!FRAME_LIVE_P (f))
 17026                 continue;
 17027 
 17028               /* Mark all the scroll bars to be removed; we'll redeem
 17029                  the ones we want when we redisplay their windows.  */
 17030               if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
 17031                 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
 17032 
 17033               if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
 17034                 {
 17035                   /* Don't allow freeing images and faces for this
 17036                      frame as long as the frame's update wasn't
 17037                      completed.  This prevents crashes when some Lisp
 17038                      that runs from the various hooks or font-lock
 17039                      decides to clear the frame's image cache and face
 17040                      cache, when the images and faces in those caches
 17041                      are referenced by the desired matrix.  */
 17042                   f->inhibit_clear_image_cache = true;
 17043                   redisplay_windows (FRAME_ROOT_WINDOW (f));
 17044                 }
 17045               /* Remember that the invisible frames need to be redisplayed next
 17046                  time they're visible.  */
 17047               else if (!REDISPLAY_SOME_P ())
 17048                 f->redisplay = true;
 17049 
 17050               /* The X error handler may have deleted that frame.  */
 17051               if (!FRAME_LIVE_P (f))
 17052                 continue;
 17053 
 17054               /* Any scroll bars which redisplay_windows should have
 17055                  nuked should now go away.  */
 17056               if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
 17057                 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
 17058 
 17059               if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
 17060                 {
 17061                   /* If fonts changed on visible frame, display again.  */
 17062                   if (f->fonts_changed)
 17063                     {
 17064                       adjust_frame_glyphs (f);
 17065                       /* Disable all redisplay optimizations for this
 17066                          frame.  For the reasons, see the comment near
 17067                          the previous call to adjust_frame_glyphs above.  */
 17068                       SET_FRAME_GARBAGED (f);
 17069                       f->fonts_changed = false;
 17070                       goto retry_frame;
 17071                     }
 17072 
 17073                   /* See if we have to hscroll.  */
 17074                   if (!f->already_hscrolled_p)
 17075                     {
 17076                       f->already_hscrolled_p = true;
 17077                       if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17078                           && hscroll_windows (f->root_window))
 17079                         {
 17080                           hscroll_retries++;
 17081                           goto retry_frame;
 17082                         }
 17083                     }
 17084 
 17085                   /* If the frame's redisplay flag was not set before
 17086                      we went about redisplaying its windows, but it is
 17087                      set now, that means we employed some redisplay
 17088                      optimizations inside redisplay_windows, and
 17089                      bypassed producing some screen lines.  But if
 17090                      f->redisplay is now set, it might mean the old
 17091                      faces are no longer valid (e.g., if redisplaying
 17092                      some window called some Lisp which defined a new
 17093                      face or redefined an existing face), so trying to
 17094                      use them in update_frame will segfault.
 17095                      Therefore, we must redisplay this frame.  */
 17096                   if (!f_redisplay_flag && f->redisplay)
 17097                     goto retry_frame;
 17098                   /* In some case (e.g., window resize), we notice
 17099                      only during window updating that the window
 17100                      content changed unpredictably (e.g., a GTK
 17101                      scrollbar moved, or some Lisp hook that winds up
 17102                      calling adjust_frame_glyphs) and that our
 17103                      previous estimation of the frame content was
 17104                      garbage.  We have to start over.  These cases
 17105                      should be rare, so going all the way back to the
 17106                      top of redisplay should be good enough.  */
 17107                   if (FRAME_GARBAGED_P (f)
 17108                       && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
 17109                     goto retry;
 17110 
 17111 #ifdef HAVE_WINDOW_SYSTEM
 17112                   if (FRAME_WINDOW_P (f)
 17113                       && FRAME_RIF (f)->clear_under_internal_border)
 17114                     FRAME_RIF (f)->clear_under_internal_border (f);
 17115 #endif
 17116                   /* Prevent various kinds of signals during display
 17117                      update.  stdio is not robust about handling
 17118                      signals, which can cause an apparent I/O error.  */
 17119                   if (interrupt_input)
 17120                     unrequest_sigio ();
 17121                   STOP_POLLING;
 17122 
 17123                   pending |= update_frame (f, false, false);
 17124                   /* On some platforms (at least MS-Windows), the
 17125                      scroll_run_hook called from scrolling_window
 17126                      called from update_frame could set the frame's
 17127                      garbaged flag, in which case we need to redisplay
 17128                      the frame.  Don't do that on TTY frames, since we
 17129                      need to keep the garbaged flag in that case when
 17130                      the frame has been resized.  */
 17131                   if (FRAME_GARBAGED_P (f))
 17132                     {
 17133                       fset_redisplay (f);
 17134                       f->garbaged = false;
 17135                       goto retry_frame;
 17136                     }
 17137                   f->cursor_type_changed = false;
 17138                   f->updated_p = true;
 17139                   f->inhibit_clear_image_cache = false;
 17140                 }
 17141             }
 17142         }
 17143 
 17144       eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
 17145 
 17146       if (!pending)
 17147         {
 17148           /* Do the mark_window_display_accurate after all windows have
 17149              been redisplayed because this call resets flags in buffers
 17150              which are needed for proper redisplay.  */
 17151           FOR_EACH_FRAME (tail, frame)
 17152             {
 17153               struct frame *f = XFRAME (frame);
 17154               if (f->updated_p)
 17155                 {
 17156                   f->redisplay = false;
 17157                   f->garbaged = false;
 17158                   mark_window_display_accurate (f->root_window, true);
 17159                   if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 17160                     FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
 17161                 }
 17162             }
 17163         }
 17164     }
 17165   else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
 17166     {
 17167       sf->inhibit_clear_image_cache = true;
 17168       displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
 17169       /* Use list_of_error, not Qerror, so that
 17170          we catch only errors and don't run the debugger.  */
 17171       internal_condition_case_1 (redisplay_window_1, selected_window,
 17172                                  list_of_error,
 17173                                  redisplay_window_error);
 17174       if (update_miniwindow_p)
 17175         {
 17176           Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17177 
 17178           displayed_buffer = XBUFFER (XWINDOW (mini_window)->contents);
 17179           internal_condition_case_1 (redisplay_window_1, mini_window,
 17180                                      list_of_error,
 17181                                      redisplay_window_error);
 17182         }
 17183 
 17184       /* Compare desired and current matrices, perform output.  */
 17185 
 17186     update:
 17187       /* If fonts changed, display again.  Likewise if redisplay_window_1
 17188          above caused some change (e.g., a change in faces) that requires
 17189          considering the entire frame again.  */
 17190       if (sf->fonts_changed || sf->redisplay)
 17191         {
 17192           if (sf->redisplay)
 17193             {
 17194               /* Set this to force a more thorough redisplay.
 17195                  Otherwise, we might immediately loop back to the
 17196                  above "else-if" clause (since all the conditions that
 17197                  led here might still be true), and we will then
 17198                  infloop, because the selected-frame's redisplay flag
 17199                  is not (and cannot be) reset.  */
 17200               windows_or_buffers_changed = 50;
 17201             }
 17202           goto retry;
 17203         }
 17204 
 17205       /* Prevent freeing of realized faces, since desired matrices are
 17206          pending that reference the faces we computed and cached.  */
 17207       inhibit_free_realized_faces = true;
 17208 
 17209       /* Prevent various kinds of signals during display update.
 17210          stdio is not robust about handling signals,
 17211          which can cause an apparent I/O error.  */
 17212       if (interrupt_input)
 17213         unrequest_sigio ();
 17214       STOP_POLLING;
 17215 
 17216       if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
 17217         {
 17218           if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17219               && hscroll_windows (selected_window))
 17220             {
 17221               hscroll_retries++;
 17222               goto retry;
 17223             }
 17224 
 17225           XWINDOW (selected_window)->must_be_updated_p = true;
 17226           pending = update_frame (sf, false, false);
 17227           sf->cursor_type_changed = false;
 17228           sf->inhibit_clear_image_cache = false;
 17229         }
 17230 
 17231       /* We may have called echo_area_display at the top of this
 17232          function.  If the echo area is on another frame, that may
 17233          have put text on a frame other than the selected one, so the
 17234          above call to update_frame would not have caught it.  Catch
 17235          it here.  */
 17236       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17237       struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 17238 
 17239       if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
 17240         {
 17241           XWINDOW (mini_window)->must_be_updated_p = true;
 17242           pending |= update_frame (mini_frame, false, false);
 17243           mini_frame->cursor_type_changed = false;
 17244           if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
 17245               && hscroll_windows (mini_window))
 17246             {
 17247               hscroll_retries++;
 17248               goto retry;
 17249             }
 17250         }
 17251     }
 17252 
 17253   /* If display was paused because of pending input, make sure we do a
 17254      thorough update the next time.  */
 17255   if (pending)
 17256     {
 17257       /* Prevent the optimization at the beginning of
 17258          redisplay_internal that tries a single-line update of the
 17259          line containing the cursor in the selected window.  */
 17260       CHARPOS (this_line_start_pos) = 0;
 17261 
 17262       /* Let the overlay arrow be updated the next time.  */
 17263       update_overlay_arrows (0);
 17264 
 17265       /* If we pause after scrolling, some rows in the current
 17266          matrices of some windows are not valid.  */
 17267       if (!WINDOW_FULL_WIDTH_P (w)
 17268           && !FRAME_WINDOW_P (XFRAME (w->frame)))
 17269         update_mode_lines = 36;
 17270     }
 17271   else
 17272     {
 17273       if (!consider_all_windows_p)
 17274         {
 17275           /* This has already been done above if
 17276              consider_all_windows_p is set.  */
 17277           if (XBUFFER (w->contents)->text->redisplay
 17278               && buffer_window_count (XBUFFER (w->contents)) > 1)
 17279             /* This can happen if b->text->redisplay was set during
 17280                jit-lock.  */
 17281             propagate_buffer_redisplay ();
 17282           mark_window_display_accurate_1 (w, true);
 17283 
 17284           /* Say overlay arrows are up to date.  */
 17285           update_overlay_arrows (1);
 17286 
 17287           if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
 17288             FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
 17289         }
 17290 
 17291       update_mode_lines = 0;
 17292       windows_or_buffers_changed = 0;
 17293     }
 17294 
 17295   /* Start SIGIO interrupts coming again.  Having them off during the
 17296      code above makes it less likely one will discard output, but not
 17297      impossible, since there might be stuff in the system buffer here.
 17298      But it is much hairier to try to do anything about that.  */
 17299   if (interrupt_input)
 17300     request_sigio ();
 17301   RESUME_POLLING;
 17302 
 17303   /* If a frame has become visible which was not before, redisplay
 17304      again, so that we display it.  Expose events for such a frame
 17305      (which it gets when becoming visible) don't call the parts of
 17306      redisplay constructing glyphs, so simply exposing a frame won't
 17307      display anything in this case.  So, we have to display these
 17308      frames here explicitly.  */
 17309   if (!pending)
 17310     {
 17311       int new_count = 0;
 17312 
 17313       FOR_EACH_FRAME (tail, frame)
 17314         {
 17315           if (XFRAME (frame)->visible)
 17316             new_count++;
 17317         }
 17318 
 17319       if (new_count != number_of_visible_frames)
 17320         windows_or_buffers_changed = 52;
 17321     }
 17322 
 17323   /* Change frame size now if a change is pending.  */
 17324   do_pending_window_change (true);
 17325 
 17326   /* If we just did a pending size change, or have additional
 17327      visible frames, or selected_window changed, redisplay again.  */
 17328   if ((windows_or_buffers_changed && !pending)
 17329       || (WINDOWP (selected_window)
 17330           && (w = XWINDOW (selected_window)) != sw))
 17331     goto retry;
 17332 
 17333   /* Clear the face and image caches.
 17334 
 17335      We used to do this only if consider_all_windows_p.  But the cache
 17336      needs to be cleared if a timer creates images in the current
 17337      buffer (e.g. the test case in Bug#6230).  */
 17338 
 17339   if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
 17340     {
 17341       clear_face_cache (false);
 17342       clear_face_cache_count = 0;
 17343     }
 17344 
 17345 #ifdef HAVE_WINDOW_SYSTEM
 17346   if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
 17347     {
 17348       clear_image_caches (Qnil);
 17349       clear_image_cache_count = 0;
 17350     }
 17351 #endif /* HAVE_WINDOW_SYSTEM */
 17352 
 17353  end_of_redisplay:
 17354 #ifdef HAVE_NS
 17355   ns_set_doc_edited ();
 17356 #endif
 17357   if (interrupt_input && interrupts_deferred)
 17358     request_sigio ();
 17359 
 17360   /* We're done with this redisplay cycle, so reset the tick count in
 17361      preparation for the next redisplay cycle.  */
 17362   if (max_redisplay_ticks > 0)
 17363     update_redisplay_ticks (0, NULL);
 17364 
 17365   unbind_to (count, Qnil);
 17366   RESUME_POLLING;
 17367 }
 17368 
 17369 static void
 17370 unwind_redisplay_preserve_echo_area (void)
 17371 {
 17372   unblock_buffer_flips ();
 17373 }
 17374 
 17375 /* Redisplay, but leave alone any recent echo area message unless
 17376    another message has been requested in its place.
 17377 
 17378    This is useful in situations where you need to redisplay but no
 17379    user action has occurred, making it inappropriate for the message
 17380    area to be cleared.  See tracking_off and
 17381    wait_reading_process_output for examples of these situations.
 17382 
 17383    FROM_WHERE is an integer saying from where this function was
 17384    called.  This is useful for debugging.  */
 17385 
 17386 void
 17387 redisplay_preserve_echo_area (int from_where)
 17388 {
 17389   redisplay_trace ("redisplay_preserve_echo_area (%d)\n", from_where);
 17390 
 17391   block_input ();
 17392   specpdl_ref count = SPECPDL_INDEX ();
 17393   record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
 17394   block_buffer_flips ();
 17395   unblock_input ();
 17396 
 17397   if (!NILP (echo_area_buffer[1]))
 17398     {
 17399       /* We have a previously displayed message, but no current
 17400          message.  Redisplay the previous message.  */
 17401       display_last_displayed_message_p = true;
 17402       redisplay_internal ();
 17403       display_last_displayed_message_p = false;
 17404     }
 17405   else
 17406     redisplay_internal ();
 17407 
 17408   flush_frame (SELECTED_FRAME ());
 17409   unbind_to (count, Qnil);
 17410 }
 17411 
 17412 
 17413 /* Function registered with record_unwind_protect in redisplay_internal.  */
 17414 
 17415 static void
 17416 unwind_redisplay (void)
 17417 {
 17418   redisplaying_p = false;
 17419   unblock_buffer_flips ();
 17420 }
 17421 
 17422 /* Function registered with record_unwind_protect before calling
 17423    start_display outside of redisplay_internal.  */
 17424 void
 17425 unwind_display_working_on_window (void)
 17426 {
 17427   display_working_on_window_p = false;
 17428 }
 17429 
 17430 /* Mark the display of leaf window W as accurate or inaccurate.
 17431    If ACCURATE_P, mark display of W as accurate.
 17432    If !ACCURATE_P, arrange for W to be redisplayed the next
 17433    time redisplay_internal is called.  */
 17434 
 17435 static void
 17436 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
 17437 {
 17438   struct buffer *b = XBUFFER (w->contents);
 17439 
 17440   w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
 17441   w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
 17442   w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
 17443 
 17444   if (accurate_p)
 17445     {
 17446       b->clip_changed = false;
 17447       b->prevent_redisplay_optimizations_p = false;
 17448       eassert (buffer_window_count (b) > 0);
 17449       /* Resetting b->text->redisplay is problematic!
 17450          In order to make it safer to do it here, redisplay_internal must
 17451          have copied all b->text->redisplay to their respective windows.  */
 17452       b->text->redisplay = false;
 17453 
 17454       BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
 17455       BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
 17456       BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
 17457       BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
 17458 
 17459       w->current_matrix->buffer = b;
 17460       w->current_matrix->begv = BUF_BEGV (b);
 17461       w->current_matrix->zv = BUF_ZV (b);
 17462       w->current_matrix->header_line_p = window_wants_header_line (w);
 17463       w->current_matrix->tab_line_p = window_wants_tab_line (w);
 17464 
 17465       w->last_cursor_vpos = w->cursor.vpos;
 17466       w->last_cursor_off_p = w->cursor_off_p;
 17467 
 17468       if (w == XWINDOW (selected_window))
 17469         w->last_point = BUF_PT (b);
 17470       else
 17471         w->last_point = marker_position (w->pointm);
 17472 
 17473       w->window_end_valid = true;
 17474       w->update_mode_line = false;
 17475       w->preserve_vscroll_p = false;
 17476     }
 17477 
 17478   w->redisplay = !accurate_p;
 17479 }
 17480 
 17481 
 17482 /* Mark the display of windows in the window tree rooted at WINDOW as
 17483    accurate or inaccurate.  If ACCURATE_P, mark display of
 17484    windows as accurate.  If !ACCURATE_P, arrange for windows to
 17485    be redisplayed the next time redisplay_internal is called.  */
 17486 
 17487 void
 17488 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
 17489 {
 17490   struct window *w;
 17491 
 17492   for (; !NILP (window); window = w->next)
 17493     {
 17494       w = XWINDOW (window);
 17495       if (WINDOWP (w->contents))
 17496         mark_window_display_accurate (w->contents, accurate_p);
 17497       else
 17498         mark_window_display_accurate_1 (w, accurate_p);
 17499     }
 17500 
 17501   if (accurate_p)
 17502     update_overlay_arrows (1);
 17503   else
 17504     /* Force a thorough redisplay the next time by setting
 17505        last_arrow_position and last_arrow_string to t, which is
 17506        unequal to any useful value of Voverlay_arrow_...  */
 17507     update_overlay_arrows (-1);
 17508 }
 17509 
 17510 
 17511 /* Return value in display table DP (Lisp_Char_Table *) for character
 17512    C.  Since a display table doesn't have any parent, we don't have to
 17513    follow parent.  Do not call this function directly but use the
 17514    macro DISP_CHAR_VECTOR.  */
 17515 
 17516 Lisp_Object
 17517 disp_char_vector (struct Lisp_Char_Table *dp, int c)
 17518 {
 17519   Lisp_Object val;
 17520 
 17521   if (ASCII_CHAR_P (c))
 17522     {
 17523       val = dp->ascii;
 17524       if (SUB_CHAR_TABLE_P (val))
 17525         val = XSUB_CHAR_TABLE (val)->contents[c];
 17526     }
 17527   else
 17528     {
 17529       Lisp_Object table;
 17530 
 17531       XSETCHAR_TABLE (table, dp);
 17532       val = char_table_ref (table, c);
 17533     }
 17534   if (NILP (val))
 17535     val = dp->defalt;
 17536   return val;
 17537 }
 17538 
 17539 static int buffer_flip_blocked_depth;
 17540 
 17541 static void
 17542 block_buffer_flips (void)
 17543 {
 17544   eassert (buffer_flip_blocked_depth >= 0);
 17545   buffer_flip_blocked_depth++;
 17546 }
 17547 
 17548 static void
 17549 unblock_buffer_flips (void)
 17550 {
 17551   eassert (buffer_flip_blocked_depth > 0);
 17552   if (--buffer_flip_blocked_depth == 0)
 17553     {
 17554       Lisp_Object tail, frame;
 17555       block_input ();
 17556       FOR_EACH_FRAME (tail, frame)
 17557         {
 17558           struct frame *f = XFRAME (frame);
 17559           if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
 17560             (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
 17561         }
 17562       unblock_input ();
 17563     }
 17564 }
 17565 
 17566 bool
 17567 buffer_flipping_blocked_p (void)
 17568 {
 17569   return buffer_flip_blocked_depth > 0;
 17570 }
 17571 
 17572 
 17573 /***********************************************************************
 17574                            Window Redisplay
 17575  ***********************************************************************/
 17576 
 17577 /* Redisplay all leaf windows in the window tree rooted at WINDOW.  */
 17578 
 17579 static void
 17580 redisplay_windows (Lisp_Object window)
 17581 {
 17582   while (!NILP (window))
 17583     {
 17584       struct window *w = XWINDOW (window);
 17585 
 17586       if (WINDOWP (w->contents))
 17587         redisplay_windows (w->contents);
 17588       else if (BUFFERP (w->contents))
 17589         {
 17590           displayed_buffer = XBUFFER (w->contents);
 17591           /* Use list_of_error, not Qerror, so that
 17592              we catch only errors and don't run the debugger.  */
 17593           internal_condition_case_1 (redisplay_window_0, window,
 17594                                      list_of_error,
 17595                                      redisplay_window_error);
 17596         }
 17597 
 17598       window = w->next;
 17599     }
 17600 }
 17601 
 17602 static Lisp_Object
 17603 redisplay_window_error (Lisp_Object error_data)
 17604 {
 17605   displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
 17606 
 17607   /* When in redisplay, the error is captured and not shown.  Arrange
 17608      for it to be shown later.  */
 17609   if (max_redisplay_ticks > 0
 17610       && CONSP (error_data)
 17611       && EQ (XCAR (error_data), Qerror)
 17612       && CONSP (XCDR (error_data))
 17613       && STRINGP (XCAR (XCDR (error_data))))
 17614     Vdelayed_warnings_list = Fcons (list2 (XCAR (error_data),
 17615                                            XCAR (XCDR (error_data))),
 17616                                     Vdelayed_warnings_list);
 17617   return Qnil;
 17618 }
 17619 
 17620 static Lisp_Object
 17621 redisplay_window_0 (Lisp_Object window)
 17622 {
 17623   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17624     redisplay_window (window, false);
 17625   return Qnil;
 17626 }
 17627 
 17628 static Lisp_Object
 17629 redisplay_window_1 (Lisp_Object window)
 17630 {
 17631   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17632     redisplay_window (window, true);
 17633   return Qnil;
 17634 }
 17635 
 17636 
 17637 /***********************************************************************
 17638                       Aborting runaway redisplay
 17639  ***********************************************************************/
 17640 
 17641 /* Update the redisplay-tick count for window W, and signal an error
 17642    if the tick count is above some threshold, indicating that
 17643    redisplay of the window takes "too long".
 17644 
 17645    TICKS is the amount of ticks to add to the W's current count; zero
 17646    means to initialize the tick count to zero.
 17647 
 17648    W can be NULL if TICKS is zero: that means unconditionally
 17649    re-initialize the current tick count to zero.
 17650 
 17651    W can also be NULL if the caller doesn't know which window is being
 17652    processed by the display code.  In that case, if TICKS is non-zero,
 17653    we assume it's the last window that shows the current buffer.  */
 17654 void
 17655 update_redisplay_ticks (int ticks, struct window *w)
 17656 {
 17657   /* This keeps track of the window on which redisplay is working.  */
 17658   static struct window *cwindow;
 17659   static EMACS_INT window_ticks;
 17660 
 17661   /* We only initialize the count if this is a different window or
 17662      NULL.  Otherwise, this is a call from init_iterator for the same
 17663      window we tracked before, and we should keep the count.  */
 17664   if (!ticks && w != cwindow)
 17665     {
 17666       cwindow = w;
 17667       window_ticks = 0;
 17668     }
 17669   /* Some callers can be run in contexts unrelated to display code, so
 17670      don't abort them and don't update the tick count in those cases.  */
 17671   if ((!w && !redisplaying_p && !display_working_on_window_p)
 17672       /* We never disable redisplay of a mini-window, since that is
 17673          absolutely essential for communicating with Emacs.  */
 17674       || (w && MINI_WINDOW_P (w)))
 17675     return;
 17676 
 17677   if (ticks > 0)
 17678     window_ticks += ticks;
 17679   if (max_redisplay_ticks > 0 && window_ticks > max_redisplay_ticks)
 17680     {
 17681       /* In addition to a buffer, this could be a window (for non-leaf
 17682          windows, not expected here) or nil (for pseudo-windows like
 17683          the one used for the native tool bar).  */
 17684       Lisp_Object contents = w ? w->contents : Qnil;
 17685       char *bufname =
 17686         NILP (contents)
 17687         ? SSDATA (BVAR (current_buffer, name))
 17688         : (BUFFERP (contents)
 17689            ? SSDATA (BVAR (XBUFFER (contents), name))
 17690            : (char *) "<unknown>");
 17691 
 17692       windows_or_buffers_changed = 177;
 17693       /* scrolling_window depends too much on the glyph matrices being
 17694          correct, and we cannot guarantee that if we abort the
 17695          redisplay of this window.  */
 17696       if (w && w->desired_matrix)
 17697         w->desired_matrix->no_scrolling_p = true;
 17698       error ("Window showing buffer %s takes too long to redisplay", bufname);
 17699     }
 17700 }
 17701 
 17702 
 17703 
 17704 /* Set cursor position of W.  PT is assumed to be displayed in ROW.
 17705    DELTA and DELTA_BYTES are the numbers of characters and bytes by
 17706    which positions recorded in ROW differ from current buffer
 17707    positions.
 17708 
 17709    Return true iff cursor is on this row.  */
 17710 
 17711 static bool
 17712 set_cursor_from_row (struct window *w, struct glyph_row *row,
 17713                      struct glyph_matrix *matrix,
 17714                      ptrdiff_t delta, ptrdiff_t delta_bytes,
 17715                      int dy, int dvpos)
 17716 {
 17717   struct glyph *glyph = row->glyphs[TEXT_AREA];
 17718   struct glyph *end = glyph + row->used[TEXT_AREA];
 17719   struct glyph *cursor = NULL;
 17720   /* The last known character position in row.  */
 17721   ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
 17722   int x = row->x;
 17723   ptrdiff_t pt_old = PT - delta;
 17724   ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
 17725   ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 17726   struct glyph *glyph_before = glyph - 1, *glyph_after = end;
 17727   /* A glyph beyond the edge of TEXT_AREA which we should never
 17728      touch.  */
 17729   struct glyph *glyphs_end = end;
 17730   /* True means we've found a match for cursor position, but that
 17731      glyph has the avoid_cursor_p flag set.  */
 17732   bool match_with_avoid_cursor = false;
 17733   /* True means we've seen at least one glyph that came from a
 17734      display string.  */
 17735   bool string_seen = false;
 17736   /* Largest and smallest buffer positions seen so far during scan of
 17737      glyph row.  */
 17738   ptrdiff_t bpos_max = pos_before;
 17739   ptrdiff_t bpos_min = pos_after;
 17740   /* Last buffer position covered by an overlay string with an integer
 17741      `cursor' property.  */
 17742   ptrdiff_t bpos_covered = 0;
 17743   /* True means the display string on which to display the cursor
 17744      comes from a text property, not from an overlay.  */
 17745   bool string_from_text_prop = false;
 17746 
 17747   /* Don't even try doing anything if called for a mode-line or
 17748      header-line or tab-line row, since the rest of the code isn't
 17749      prepared to deal with such calamities.  */
 17750   eassert (!row->mode_line_p);
 17751   if (row->mode_line_p)
 17752     return false;
 17753 
 17754   /* Skip over glyphs not having an object at the start and the end of
 17755      the row.  These are special glyphs like truncation marks on
 17756      terminal frames.  */
 17757   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 17758     {
 17759       if (!row->reversed_p)
 17760         {
 17761           while (glyph < end
 17762                  && NILP (glyph->object)
 17763                  && glyph->charpos < 0)
 17764             {
 17765               x += glyph->pixel_width;
 17766               ++glyph;
 17767             }
 17768           while (end > glyph
 17769                  && NILP ((end - 1)->object)
 17770                  /* CHARPOS is zero for blanks and stretch glyphs
 17771                     inserted by extend_face_to_end_of_line.  */
 17772                  && (end - 1)->charpos <= 0)
 17773             --end;
 17774           glyph_before = glyph - 1;
 17775           glyph_after = end;
 17776         }
 17777       else
 17778         {
 17779           struct glyph *g;
 17780 
 17781           /* If the glyph row is reversed, we need to process it from back
 17782              to front, so swap the edge pointers.  */
 17783           glyphs_end = end = glyph - 1;
 17784           glyph += row->used[TEXT_AREA] - 1;
 17785 
 17786           while (glyph > end + 1
 17787                  && NILP (glyph->object)
 17788                  && glyph->charpos < 0)
 17789             --glyph;
 17790           if (NILP (glyph->object) && glyph->charpos < 0)
 17791             --glyph;
 17792           /* By default, in reversed rows we put the cursor on the
 17793              rightmost (first in the reading order) glyph.  */
 17794           for (x = 0, g = end + 1; g < glyph; g++)
 17795             x += g->pixel_width;
 17796           while (end < glyph
 17797                  && NILP ((end + 1)->object)
 17798                  && (end + 1)->charpos <= 0)
 17799             ++end;
 17800           glyph_before = glyph + 1;
 17801           glyph_after = end;
 17802         }
 17803     }
 17804   else if (row->reversed_p)
 17805     {
 17806       /* In R2L rows that don't display text, put the cursor on the
 17807          rightmost glyph.  Case in point: an empty last line that is
 17808          part of an R2L paragraph.  */
 17809       cursor = end - 1;
 17810       /* Avoid placing the cursor on the last glyph of the row, where
 17811          on terminal frames we hold the vertical border between
 17812          adjacent windows.  */
 17813       if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
 17814           && !WINDOW_RIGHTMOST_P (w)
 17815           && cursor == row->glyphs[LAST_AREA] - 1)
 17816         cursor--;
 17817       x = -1;   /* will be computed below, at label compute_x */
 17818     }
 17819 
 17820   /* Step 1: Try to find the glyph whose character position
 17821      corresponds to point.  If that's not possible, find 2 glyphs
 17822      whose character positions are the closest to point, one before
 17823      point, the other after it.  */
 17824   if (!row->reversed_p)
 17825     while (/* not marched to end of glyph row */
 17826            glyph < end
 17827            /* glyph was not inserted by redisplay for internal purposes */
 17828            && !NILP (glyph->object))
 17829       {
 17830         if (BUFFERP (glyph->object))
 17831           {
 17832             ptrdiff_t dpos = glyph->charpos - pt_old;
 17833 
 17834             if (glyph->charpos > bpos_max)
 17835               bpos_max = glyph->charpos;
 17836             if (glyph->charpos < bpos_min)
 17837               bpos_min = glyph->charpos;
 17838             if (!glyph->avoid_cursor_p)
 17839               {
 17840                 /* If we hit point, we've found the glyph on which to
 17841                    display the cursor.  */
 17842                 if (dpos == 0)
 17843                   {
 17844                     match_with_avoid_cursor = false;
 17845                     break;
 17846                   }
 17847                 /* See if we've found a better approximation to
 17848                    POS_BEFORE or to POS_AFTER.  */
 17849                 if (0 > dpos && dpos > pos_before - pt_old)
 17850                   {
 17851                     pos_before = glyph->charpos;
 17852                     glyph_before = glyph;
 17853                   }
 17854                 else if (0 < dpos && dpos < pos_after - pt_old)
 17855                   {
 17856                     pos_after = glyph->charpos;
 17857                     glyph_after = glyph;
 17858                   }
 17859               }
 17860             else if (dpos == 0)
 17861               match_with_avoid_cursor = true;
 17862           }
 17863         else if (STRINGP (glyph->object))
 17864           {
 17865             Lisp_Object chprop;
 17866             ptrdiff_t glyph_pos = glyph->charpos;
 17867 
 17868             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 17869                                          glyph->object);
 17870             if (!NILP (chprop))
 17871               {
 17872                 /* If the string came from a `display' text property,
 17873                    look up the buffer position of that property and
 17874                    use that position to update bpos_max, as if we
 17875                    actually saw such a position in one of the row's
 17876                    glyphs.  This helps with supporting integer values
 17877                    of `cursor' property on the display string in
 17878                    situations where most or all of the row's buffer
 17879                    text is completely covered by display properties,
 17880                    so that no glyph with valid buffer positions is
 17881                    ever seen in the row.  */
 17882                 ptrdiff_t prop_pos =
 17883                   string_buffer_position_lim (glyph->object, pos_before,
 17884                                               pos_after, false);
 17885 
 17886                 if (prop_pos >= pos_before)
 17887                   bpos_max = prop_pos;
 17888               }
 17889             if (FIXNUMP (chprop))
 17890               {
 17891                 bpos_covered = bpos_max + XFIXNUM (chprop);
 17892                 /* If the `cursor' property covers buffer positions up
 17893                    to and including point, we should display cursor on
 17894                    this glyph.  Note that, if a `cursor' property on one
 17895                    of the string's characters has an integer value, we
 17896                    will break out of the loop below _before_ we get to
 17897                    the position match above.  IOW, integer values of
 17898                    the `cursor' property override the "exact match for
 17899                    point" strategy of positioning the cursor.  */
 17900                 /* Implementation note: bpos_max == pt_old when, e.g.,
 17901                    we are in an empty line, where bpos_max is set to
 17902                    MATRIX_ROW_START_CHARPOS, see above.  */
 17903                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 17904                   {
 17905                     cursor = glyph;
 17906                     break;
 17907                   }
 17908               }
 17909 
 17910             string_seen = true;
 17911           }
 17912         x += glyph->pixel_width;
 17913         ++glyph;
 17914       }
 17915   else if (glyph > end) /* row is reversed */
 17916     while (!NILP (glyph->object))
 17917       {
 17918         if (BUFFERP (glyph->object))
 17919           {
 17920             ptrdiff_t dpos = glyph->charpos - pt_old;
 17921 
 17922             if (glyph->charpos > bpos_max)
 17923               bpos_max = glyph->charpos;
 17924             if (glyph->charpos < bpos_min)
 17925               bpos_min = glyph->charpos;
 17926             if (!glyph->avoid_cursor_p)
 17927               {
 17928                 if (dpos == 0)
 17929                   {
 17930                     match_with_avoid_cursor = false;
 17931                     break;
 17932                   }
 17933                 if (0 > dpos && dpos > pos_before - pt_old)
 17934                   {
 17935                     pos_before = glyph->charpos;
 17936                     glyph_before = glyph;
 17937                   }
 17938                 else if (0 < dpos && dpos < pos_after - pt_old)
 17939                   {
 17940                     pos_after = glyph->charpos;
 17941                     glyph_after = glyph;
 17942                   }
 17943               }
 17944             else if (dpos == 0)
 17945               match_with_avoid_cursor = true;
 17946           }
 17947         else if (STRINGP (glyph->object))
 17948           {
 17949             Lisp_Object chprop;
 17950             ptrdiff_t glyph_pos = glyph->charpos;
 17951 
 17952             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 17953                                          glyph->object);
 17954             if (!NILP (chprop))
 17955               {
 17956                 ptrdiff_t prop_pos =
 17957                   string_buffer_position_lim (glyph->object, pos_before,
 17958                                               pos_after, false);
 17959 
 17960                 if (prop_pos >= pos_before)
 17961                   bpos_max = prop_pos;
 17962               }
 17963             if (FIXNUMP (chprop))
 17964               {
 17965                 bpos_covered = bpos_max + XFIXNUM (chprop);
 17966                 /* If the `cursor' property covers buffer positions up
 17967                    to and including point, we should display cursor on
 17968                    this glyph.  */
 17969                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 17970                   {
 17971                     cursor = glyph;
 17972                     break;
 17973                   }
 17974               }
 17975             string_seen = true;
 17976           }
 17977         --glyph;
 17978         if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
 17979           {
 17980             x--;                /* can't use any pixel_width */
 17981             break;
 17982           }
 17983         x -= glyph->pixel_width;
 17984     }
 17985 
 17986   /* Step 2: If we didn't find an exact match for point, we need to
 17987      look for a proper place to put the cursor among glyphs between
 17988      GLYPH_BEFORE and GLYPH_AFTER.  */
 17989   if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 17990         && BUFFERP (glyph->object) && glyph->charpos == pt_old)
 17991       && !(bpos_max <= pt_old && pt_old <= bpos_covered))
 17992     {
 17993       /* An empty line has a single glyph whose OBJECT is nil and
 17994          whose CHARPOS is the position of a newline on that line.
 17995          Note that on a TTY, there are more glyphs after that, which
 17996          were produced by extend_face_to_end_of_line, but their
 17997          CHARPOS is zero or negative.  */
 17998       bool empty_line_p =
 17999         ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 18000          && NILP (glyph->object) && glyph->charpos > 0
 18001          /* On a TTY, continued and truncated rows also have a glyph at
 18002             their end whose OBJECT is nil and whose CHARPOS is
 18003             positive (the continuation and truncation glyphs), but such
 18004             rows are obviously not "empty".  */
 18005          && !(row->continued_p || row->truncated_on_right_p));
 18006 
 18007       if (row->ends_in_ellipsis_p && pos_after == last_pos)
 18008         {
 18009           ptrdiff_t ellipsis_pos;
 18010 
 18011           /* Scan back over the ellipsis glyphs.  */
 18012           if (!row->reversed_p)
 18013             {
 18014               ellipsis_pos = (glyph - 1)->charpos;
 18015               while (glyph > row->glyphs[TEXT_AREA]
 18016                      && (glyph - 1)->charpos == ellipsis_pos)
 18017                 glyph--, x -= glyph->pixel_width;
 18018               /* That loop always goes one position too far, including
 18019                  the glyph before the ellipsis.  So scan forward over
 18020                  that one.  */
 18021               x += glyph->pixel_width;
 18022               glyph++;
 18023             }
 18024           else  /* row is reversed */
 18025             {
 18026               ellipsis_pos = (glyph + 1)->charpos;
 18027               while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18028                      && (glyph + 1)->charpos == ellipsis_pos)
 18029                 glyph++, x += glyph->pixel_width;
 18030               x -= glyph->pixel_width;
 18031               glyph--;
 18032             }
 18033         }
 18034       else if (match_with_avoid_cursor)
 18035         {
 18036           cursor = glyph_after;
 18037           x = -1;
 18038         }
 18039       else if (string_seen)
 18040         {
 18041           int incr = row->reversed_p ? -1 : +1;
 18042 
 18043           /* Need to find the glyph that came out of a string which is
 18044              present at point.  That glyph is somewhere between
 18045              GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
 18046              positioned between POS_BEFORE and POS_AFTER in the
 18047              buffer.  */
 18048           struct glyph *start, *stop;
 18049           ptrdiff_t pos = pos_before;
 18050 
 18051           x = -1;
 18052 
 18053           /* If the row ends in a newline from a display string,
 18054              reordering could have moved the glyphs belonging to the
 18055              string out of the [GLYPH_BEFORE..GLYPH_AFTER] range.  So
 18056              in this case we extend the search to the last glyph in
 18057              the row that was not inserted by redisplay.  */
 18058           if (row->ends_in_newline_from_string_p)
 18059             {
 18060               glyph_after = end;
 18061               pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 18062             }
 18063 
 18064           /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
 18065              correspond to POS_BEFORE and POS_AFTER, respectively.  We
 18066              need START and STOP in the order that corresponds to the
 18067              row's direction as given by its reversed_p flag.  If the
 18068              directionality of characters between POS_BEFORE and
 18069              POS_AFTER is the opposite of the row's base direction,
 18070              these characters will have been reordered for display,
 18071              and we need to reverse START and STOP.  */
 18072           if (!row->reversed_p)
 18073             {
 18074               start = min (glyph_before, glyph_after);
 18075               stop = max (glyph_before, glyph_after);
 18076             }
 18077           else
 18078             {
 18079               start = max (glyph_before, glyph_after);
 18080               stop = min (glyph_before, glyph_after);
 18081             }
 18082           for (glyph = start + incr;
 18083                row->reversed_p ? glyph > stop : glyph < stop; )
 18084             {
 18085 
 18086               /* Any glyphs that come from the buffer are here because
 18087                  of bidi reordering.  Skip them, and only pay
 18088                  attention to glyphs that came from some string.  */
 18089               if (STRINGP (glyph->object))
 18090                 {
 18091                   Lisp_Object str;
 18092                   ptrdiff_t tem;
 18093                   /* If the display property covers the newline, we
 18094                      need to search for it one position farther.  */
 18095                   ptrdiff_t lim = pos_after
 18096                     + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
 18097 
 18098                   string_from_text_prop = false;
 18099                   str = glyph->object;
 18100                   tem = string_buffer_position_lim (str, pos, lim, false);
 18101                   if (tem == 0  /* from overlay */
 18102                       || pos <= tem)
 18103                     {
 18104                       /* If the string from which this glyph came is
 18105                          found in the buffer at point, or at position
 18106                          that is closer to point than pos_after, then
 18107                          we've found the glyph we've been looking for.
 18108                          If it comes from an overlay (tem == 0), and
 18109                          it has the `cursor' property on one of its
 18110                          glyphs, record that glyph as a candidate for
 18111                          displaying the cursor.  (As in the
 18112                          unidirectional version, we will display the
 18113                          cursor on the last candidate we find.)  */
 18114                       if (tem == 0
 18115                           || tem == pt_old
 18116                           || (tem - pt_old > 0 && tem < pos_after))
 18117                         {
 18118                           /* The glyphs from this string could have
 18119                              been reordered.  Find the one with the
 18120                              smallest string position.  Or there could
 18121                              be a character in the string with the
 18122                              `cursor' property, which means display
 18123                              cursor on that character's glyph.  */
 18124                           ptrdiff_t strpos = glyph->charpos;
 18125 
 18126                           if (tem)
 18127                             {
 18128                               cursor = glyph;
 18129                               string_from_text_prop = true;
 18130                             }
 18131                           for ( ;
 18132                                (row->reversed_p ? glyph > stop : glyph < stop)
 18133                                  && EQ (glyph->object, str);
 18134                                glyph += incr)
 18135                             {
 18136                               Lisp_Object cprop;
 18137                               ptrdiff_t gpos = glyph->charpos;
 18138 
 18139                               cprop = Fget_char_property (make_fixnum (gpos),
 18140                                                           Qcursor,
 18141                                                           glyph->object);
 18142                               if (!NILP (cprop))
 18143                                 {
 18144                                   cursor = glyph;
 18145                                   break;
 18146                                 }
 18147                               if (tem && glyph->charpos < strpos)
 18148                                 {
 18149                                   strpos = glyph->charpos;
 18150                                   cursor = glyph;
 18151                                 }
 18152                             }
 18153 
 18154                           if (tem == pt_old
 18155                               || (tem - pt_old > 0 && tem < pos_after))
 18156                             goto compute_x;
 18157                         }
 18158                       if (tem)
 18159                         pos = tem + 1; /* don't find previous instances */
 18160                     }
 18161                   /* This string is not what we want; skip all of the
 18162                      glyphs that came from it.  */
 18163                   while ((row->reversed_p ? glyph > stop : glyph < stop)
 18164                          && EQ (glyph->object, str))
 18165                     glyph += incr;
 18166                 }
 18167               else
 18168                 glyph += incr;
 18169             }
 18170 
 18171           /* If we reached the end of the line, and END was from a string,
 18172              the cursor is not on this line.  */
 18173           if (cursor == NULL
 18174               && (row->reversed_p ? glyph <= end : glyph >= end)
 18175               && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
 18176               && STRINGP (end->object)
 18177               && row->continued_p)
 18178             return false;
 18179         }
 18180       /* A truncated row may not include PT among its character positions.
 18181          Setting the cursor inside the scroll margin will trigger
 18182          recalculation of hscroll in hscroll_window_tree.  But if a
 18183          display string covers point, defer to the string-handling
 18184          code below to figure this out.  */
 18185       else if (row->truncated_on_left_p && pt_old < bpos_min)
 18186         {
 18187           cursor = glyph_before;
 18188           x = -1;
 18189         }
 18190       else if ((row->truncated_on_right_p && pt_old > bpos_max)
 18191                /* Zero-width characters produce no glyphs.  */
 18192                || (!empty_line_p
 18193                    && (row->reversed_p
 18194                        ? glyph_after > glyphs_end
 18195                        : glyph_after < glyphs_end)))
 18196         {
 18197           cursor = glyph_after;
 18198           x = -1;
 18199         }
 18200     }
 18201 
 18202  compute_x:
 18203   if (cursor != NULL)
 18204     glyph = cursor;
 18205   else if (glyph == glyphs_end
 18206            && pos_before == pos_after
 18207            && STRINGP ((row->reversed_p
 18208                         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18209                         : row->glyphs[TEXT_AREA])->object))
 18210     {
 18211       /* If all the glyphs of this row came from strings, put the
 18212          cursor on the first glyph of the row.  This avoids having the
 18213          cursor outside of the text area in this very rare and hard
 18214          use case.  */
 18215       glyph =
 18216         row->reversed_p
 18217         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18218         : row->glyphs[TEXT_AREA];
 18219     }
 18220   if (x < 0)
 18221     {
 18222       struct glyph *g;
 18223 
 18224       /* Need to compute x that corresponds to GLYPH.  */
 18225       for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
 18226         {
 18227           if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
 18228             emacs_abort ();
 18229           x += g->pixel_width;
 18230         }
 18231     }
 18232 
 18233   /* ROW could be part of a continued line, which, under bidi
 18234      reordering, might have other rows whose start and end charpos
 18235      occlude point.  Only set w->cursor if we found a better
 18236      approximation to the cursor position than we have from previously
 18237      examined candidate rows belonging to the same continued line.  */
 18238   if (/* We already have a candidate row.  */
 18239       w->cursor.vpos >= 0
 18240       /* That candidate is not the row we are processing.  */
 18241       && MATRIX_ROW (matrix, w->cursor.vpos) != row
 18242       /* Make sure cursor.vpos specifies a row whose start and end
 18243          charpos occlude point, and it is valid candidate for being a
 18244          cursor-row.  This is because some callers of this function
 18245          leave cursor.vpos at the row where the cursor was displayed
 18246          during the last redisplay cycle.  */
 18247       && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
 18248       && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18249       && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
 18250     {
 18251       struct glyph *g1
 18252         = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
 18253 
 18254       /* Don't consider glyphs that are outside TEXT_AREA.  */
 18255       if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
 18256         return false;
 18257       /* Keep the candidate whose buffer position is the closest to
 18258          point or has the `cursor' property.  */
 18259       if (/* Previous candidate is a glyph in TEXT_AREA of that row.  */
 18260           w->cursor.hpos >= 0
 18261           && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
 18262           && ((BUFFERP (g1->object)
 18263                && (g1->charpos == pt_old /* An exact match always wins.  */
 18264                    || (BUFFERP (glyph->object)
 18265                        && eabs (g1->charpos - pt_old)
 18266                        < eabs (glyph->charpos - pt_old))))
 18267               /* Previous candidate is a glyph from a string that has
 18268                  a non-nil `cursor' property.  */
 18269               || (STRINGP (g1->object)
 18270                   && (!NILP (Fget_char_property (make_fixnum (g1->charpos),
 18271                                                 Qcursor, g1->object))
 18272                       /* Previous candidate is from the same display
 18273                          string as this one, and the display string
 18274                          came from a text property.  */
 18275                       || (EQ (g1->object, glyph->object)
 18276                           && string_from_text_prop)
 18277                       /* this candidate is from newline and its
 18278                          position is not an exact match */
 18279                       || (NILP (glyph->object)
 18280                           && glyph->charpos != pt_old)))))
 18281         return false;
 18282       /* If this candidate gives an exact match, use that.  */
 18283       if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
 18284             /* If this candidate is a glyph created for the
 18285                terminating newline of a line, and point is on that
 18286                newline, it wins because it's an exact match.  */
 18287             || (!row->continued_p
 18288                 && NILP (glyph->object)
 18289                 && glyph->charpos == 0
 18290                 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
 18291           /* Otherwise, keep the candidate that comes from a row
 18292              spanning less buffer positions.  This may win when one or
 18293              both candidate positions are on glyphs that came from
 18294              display strings, for which we cannot compare buffer
 18295              positions.  */
 18296           && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18297              - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18298              < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
 18299         return false;
 18300     }
 18301   w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
 18302   w->cursor.x = x;
 18303   w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
 18304   w->cursor.y = row->y + dy;
 18305 
 18306   if (w == XWINDOW (selected_window))
 18307     {
 18308       if (!row->continued_p
 18309           && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 18310           && row->x == 0)
 18311         {
 18312           this_line_buffer = XBUFFER (w->contents);
 18313 
 18314           CHARPOS (this_line_start_pos)
 18315             = MATRIX_ROW_START_CHARPOS (row) + delta;
 18316           BYTEPOS (this_line_start_pos)
 18317             = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
 18318 
 18319           CHARPOS (this_line_end_pos)
 18320             = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
 18321           BYTEPOS (this_line_end_pos)
 18322             = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
 18323 
 18324           this_line_y = w->cursor.y;
 18325           this_line_pixel_height = row->height;
 18326           this_line_vpos = w->cursor.vpos;
 18327           this_line_start_x = row->x;
 18328         }
 18329       else
 18330         CHARPOS (this_line_start_pos) = 0;
 18331     }
 18332 
 18333   return true;
 18334 }
 18335 
 18336 
 18337 /* Run window scroll functions, if any, for WINDOW with new window
 18338    start STARTP.  Sets the window start of WINDOW to that position.
 18339 
 18340    We assume that the window's buffer is really current.  */
 18341 
 18342 static struct text_pos
 18343 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
 18344 {
 18345   struct window *w = XWINDOW (window);
 18346   SET_MARKER_FROM_TEXT_POS (w->start, startp);
 18347 
 18348   eassert (current_buffer == XBUFFER (w->contents));
 18349 
 18350   if (!NILP (Vwindow_scroll_functions))
 18351     {
 18352       specpdl_ref count = SPECPDL_INDEX ();
 18353       specbind (Qinhibit_quit, Qt);
 18354       safe_run_hooks_2
 18355         (Qwindow_scroll_functions, window, make_fixnum (CHARPOS (startp)));
 18356       unbind_to (count, Qnil);
 18357       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18358       /* In case the hook functions switch buffers.  */
 18359       set_buffer_internal (XBUFFER (w->contents));
 18360     }
 18361 
 18362   return startp;
 18363 }
 18364 
 18365 
 18366 /* Make sure the line containing the cursor is fully visible.
 18367    A value of true means there is nothing to be done.
 18368    (Either the line is fully visible, or it cannot be made so,
 18369    or we cannot tell.)
 18370 
 18371    If FORCE_P, return false even if partial visible cursor row
 18372    is higher than window.
 18373 
 18374    If CURRENT_MATRIX_P, use the information from the
 18375    window's current glyph matrix; otherwise use the desired glyph
 18376    matrix.
 18377 
 18378    If JUST_TEST_USER_PREFERENCE_P, just test what the value of
 18379    make-cursor-row-fully-visible requires, don't test the actual
 18380    cursor position.  The assumption is that in that case the caller
 18381    performs the necessary testing of the cursor position.
 18382 
 18383    A value of false means the caller should do scrolling
 18384    as if point had gone off the screen.  */
 18385 
 18386 static bool
 18387 cursor_row_fully_visible_p (struct window *w, bool force_p,
 18388                             bool current_matrix_p,
 18389                             bool just_test_user_preference_p)
 18390 {
 18391   struct glyph_matrix *matrix;
 18392   struct glyph_row *row;
 18393   int window_height;
 18394   Lisp_Object mclfv_p =
 18395     buffer_local_value (Qmake_cursor_line_fully_visible, w->contents);
 18396 
 18397   /* If no local binding, use the global value.  */
 18398   if (BASE_EQ (mclfv_p, Qunbound))
 18399     mclfv_p = Vmake_cursor_line_fully_visible;
 18400   /* Follow mode sets the variable to a Lisp function in buffers that
 18401      are under Follow mode.  */
 18402   if (FUNCTIONP (mclfv_p))
 18403     {
 18404       Lisp_Object window;
 18405       XSETWINDOW (window, w);
 18406       /* Implementation note: if the function we call here signals an
 18407          error, we will NOT scroll when the cursor is partially-visible.  */
 18408       Lisp_Object val = safe_call1 (mclfv_p, window);
 18409       if (NILP (val))
 18410         return true;
 18411       else if (just_test_user_preference_p)
 18412         return false;
 18413     }
 18414   else if (NILP (mclfv_p))
 18415     return true;
 18416   else if (just_test_user_preference_p)
 18417     return false;
 18418 
 18419   /* It's not always possible to find the cursor, e.g, when a window
 18420      is full of overlay strings.  Don't do anything in that case.  */
 18421   if (w->cursor.vpos < 0)
 18422     return true;
 18423 
 18424   matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
 18425   row = MATRIX_ROW (matrix, w->cursor.vpos);
 18426 
 18427   /* If the cursor row is not partially visible, there's nothing to do.  */
 18428   if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
 18429     return true;
 18430 
 18431   /* If the row the cursor is in is taller than the window's height,
 18432      it's not clear what to do, so do nothing.  */
 18433   window_height = window_box_height (w);
 18434   if (row->height >= window_height)
 18435     {
 18436       if (!force_p || MINI_WINDOW_P (w)
 18437           || w->vscroll || w->cursor.vpos == 0)
 18438         return true;
 18439     }
 18440   return false;
 18441 }
 18442 
 18443 
 18444 /* Try scrolling PT into view in window WINDOW.  JUST_THIS_ONE_P
 18445    means only WINDOW is redisplayed in redisplay_internal.
 18446    TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
 18447    in redisplay_window to bring a partially visible line into view in
 18448    the case that only the cursor has moved.
 18449 
 18450    LAST_LINE_MISFIT should be true if we're scrolling because the
 18451    last screen line's vertical height extends past the end of the screen.
 18452 
 18453    Value is
 18454 
 18455    1    if scrolling succeeded
 18456 
 18457    0    if scrolling didn't find point.
 18458 
 18459    -1   if new fonts have been loaded so that we must interrupt
 18460    redisplay, adjust glyph matrices, and try again.  */
 18461 
 18462 enum
 18463 {
 18464   SCROLLING_SUCCESS = 1,
 18465   SCROLLING_FAILED = 0,
 18466   SCROLLING_NEED_LARGER_MATRICES = -1
 18467 };
 18468 
 18469 /* If scroll-conservatively is more than this, never recenter.
 18470 
 18471    If you change this, don't forget to update the doc string of
 18472    `scroll-conservatively' and the Emacs manual.  */
 18473 #define SCROLL_LIMIT 100
 18474 
 18475 static int
 18476 try_scrolling (Lisp_Object window, bool just_this_one_p,
 18477                intmax_t arg_scroll_conservatively, intmax_t scroll_step,
 18478                bool temp_scroll_step, bool last_line_misfit)
 18479 {
 18480   struct window *w = XWINDOW (window);
 18481   struct text_pos pos, startp;
 18482   struct it it;
 18483   int this_scroll_margin, scroll_max, rc, height;
 18484   int dy = 0, amount_to_scroll = 0;
 18485   bool scroll_down_p = false;
 18486   int extra_scroll_margin_lines = last_line_misfit;
 18487   Lisp_Object aggressive;
 18488   /* We will never try scrolling more than this number of lines.  */
 18489   int scroll_limit = SCROLL_LIMIT;
 18490   int frame_line_height = default_line_pixel_height (w);
 18491 
 18492 #ifdef GLYPH_DEBUG
 18493   debug_method_add (w, "try_scrolling");
 18494 #endif
 18495 
 18496   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18497 
 18498   this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 18499 
 18500   /* Force arg_scroll_conservatively to have a reasonable value, to
 18501      avoid scrolling too far away with slow move_it_* functions.  Note
 18502      that the user can supply scroll-conservatively equal to
 18503      `most-positive-fixnum', which can be larger than INT_MAX.  */
 18504   if (arg_scroll_conservatively > scroll_limit)
 18505     {
 18506       arg_scroll_conservatively = scroll_limit + 1;
 18507       scroll_max = scroll_limit * frame_line_height;
 18508     }
 18509   else if (0 < scroll_step || 0 < arg_scroll_conservatively || temp_scroll_step)
 18510     /* Compute how much we should try to scroll maximally to bring
 18511        point into view.  */
 18512     {
 18513       intmax_t scroll_lines_max
 18514         = max (scroll_step, max (arg_scroll_conservatively, temp_scroll_step));
 18515       int scroll_lines = clip_to_bounds (0, scroll_lines_max, 1000000);
 18516       scroll_max = scroll_lines * frame_line_height;
 18517     }
 18518   else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
 18519            || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
 18520     /* We're trying to scroll because of aggressive scrolling but no
 18521        scroll_step is set.  Choose an arbitrary one.  */
 18522     scroll_max = 10 * frame_line_height;
 18523   else
 18524     scroll_max = 0;
 18525 
 18526  too_near_end:
 18527 
 18528   /* Decide whether to scroll down.  */
 18529   if (PT > CHARPOS (startp))
 18530     {
 18531       int scroll_margin_y;
 18532 
 18533       /* Compute the pixel ypos of the scroll margin, then move IT to
 18534          either that ypos or PT, whichever comes first.  */
 18535       start_display (&it, w, startp);
 18536       scroll_margin_y = it.last_visible_y - partial_line_height (&it)
 18537         - this_scroll_margin
 18538         - frame_line_height * extra_scroll_margin_lines;
 18539       move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
 18540                   (MOVE_TO_POS | MOVE_TO_Y));
 18541 
 18542       if (PT > CHARPOS (it.current.pos))
 18543         {
 18544           int y0 = line_bottom_y (&it);
 18545           /* Compute how many pixels below window bottom to stop searching
 18546              for PT.  This avoids costly search for PT that is far away if
 18547              the user limited scrolling by a small number of lines, but
 18548              always finds PT if scroll_conservatively is set to a large
 18549              number, such as most-positive-fixnum.  */
 18550           int slack = max (scroll_max, 10 * frame_line_height);
 18551           int y_to_move = it.last_visible_y + slack;
 18552 
 18553           /* Compute the distance from the scroll margin to PT or to
 18554              the scroll limit, whichever comes first.  This should
 18555              include the height of the cursor line, to make that line
 18556              fully visible.  */
 18557           move_it_to (&it, PT, -1, y_to_move,
 18558                       -1, MOVE_TO_POS | MOVE_TO_Y);
 18559           dy = line_bottom_y (&it) - y0;
 18560 
 18561           if (dy > scroll_max)
 18562             return SCROLLING_FAILED;
 18563 
 18564           if (dy > 0)
 18565             scroll_down_p = true;
 18566         }
 18567       else if (PT == IT_CHARPOS (it)
 18568                && IT_CHARPOS (it) < ZV
 18569                && it.method == GET_FROM_STRING
 18570                && arg_scroll_conservatively > scroll_limit
 18571                && it.current_x == 0)
 18572         {
 18573           enum move_it_result skip;
 18574           int y1 = it.current_y;
 18575           int vpos;
 18576 
 18577           /* A before-string that includes newlines and is displayed
 18578              on the last visible screen line could fail us under
 18579              scroll-conservatively > 100, because we will be unable to
 18580              position the cursor on that last visible line.  Try to
 18581              recover by finding the first screen line that has some
 18582              glyphs coming from the buffer text.  */
 18583           do {
 18584             skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 18585             if (skip != MOVE_NEWLINE_OR_CR
 18586                 || IT_CHARPOS (it) != PT
 18587                 || it.method == GET_FROM_BUFFER)
 18588               break;
 18589             vpos = it.vpos;
 18590             move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
 18591           } while (it.vpos > vpos);
 18592 
 18593           dy = it.current_y - y1;
 18594 
 18595           if (dy > scroll_max)
 18596             return SCROLLING_FAILED;
 18597 
 18598           if (dy > 0)
 18599             scroll_down_p = true;
 18600         }
 18601     }
 18602 
 18603   if (scroll_down_p)
 18604     {
 18605       /* Point is in or below the bottom scroll margin, so move the
 18606          window start down.  If scrolling conservatively, move it just
 18607          enough down to make point visible.  If scroll_step is set,
 18608          move it down by scroll_step.  */
 18609       if (arg_scroll_conservatively)
 18610         amount_to_scroll
 18611           = min (max (dy, frame_line_height),
 18612                  frame_line_height * arg_scroll_conservatively);
 18613       else if (scroll_step || temp_scroll_step)
 18614         amount_to_scroll = scroll_max;
 18615       else
 18616         {
 18617           aggressive = BVAR (current_buffer, scroll_up_aggressively);
 18618           height = WINDOW_BOX_TEXT_HEIGHT (w);
 18619           if (NUMBERP (aggressive))
 18620             {
 18621               double float_amount = XFLOATINT (aggressive) * height;
 18622               int aggressive_scroll = float_amount;
 18623               if (aggressive_scroll == 0 && float_amount > 0)
 18624                 aggressive_scroll = 1;
 18625               /* Don't let point enter the scroll margin near top of
 18626                  the window.  This could happen if the value of
 18627                  scroll_up_aggressively is too large and there are
 18628                  non-zero margins, because scroll_up_aggressively
 18629                  means put point that fraction of window height
 18630                  _from_the_bottom_margin_.  */
 18631               if (aggressive_scroll + 2 * this_scroll_margin > height)
 18632                 aggressive_scroll = height - 2 * this_scroll_margin;
 18633               amount_to_scroll = dy + aggressive_scroll;
 18634             }
 18635         }
 18636 
 18637       if (amount_to_scroll <= 0)
 18638         return SCROLLING_FAILED;
 18639 
 18640       start_display (&it, w, startp);
 18641       if (arg_scroll_conservatively <= scroll_limit)
 18642         move_it_vertically (&it, amount_to_scroll);
 18643       else
 18644         {
 18645           /* Extra precision for users who set scroll-conservatively
 18646              to a large number: make sure the amount we scroll
 18647              the window start is never less than amount_to_scroll,
 18648              which was computed as distance from window bottom to
 18649              point.  This matters when lines at window top and lines
 18650              below window bottom have different height.  */
 18651           struct it it1;
 18652           void *it1data = NULL;
 18653           /* We use a temporary it1 because line_bottom_y can modify
 18654              its argument, if it moves one line down; see there.  */
 18655           int start_y;
 18656 
 18657           SAVE_IT (it1, it, it1data);
 18658           start_y = line_bottom_y (&it1);
 18659           do {
 18660             RESTORE_IT (&it, &it, it1data);
 18661             move_it_by_lines (&it, 1);
 18662             SAVE_IT (it1, it, it1data);
 18663           } while (IT_CHARPOS (it) < ZV
 18664                    && line_bottom_y (&it1) - start_y < amount_to_scroll);
 18665           bidi_unshelve_cache (it1data, true);
 18666         }
 18667 
 18668       /* If STARTP is unchanged, move it down another screen line.  */
 18669       if (IT_CHARPOS (it) == CHARPOS (startp))
 18670         move_it_by_lines (&it, 1);
 18671       startp = it.current.pos;
 18672     }
 18673   else
 18674     {
 18675       struct text_pos scroll_margin_pos = startp;
 18676       int y_offset = 0;
 18677 
 18678       /* See if point is inside the scroll margin at the top of the
 18679          window.  */
 18680       if (this_scroll_margin)
 18681         {
 18682           int y_start;
 18683 
 18684           start_display (&it, w, startp);
 18685           y_start = it.current_y;
 18686           move_it_vertically (&it, this_scroll_margin);
 18687           scroll_margin_pos = it.current.pos;
 18688           /* If we didn't move enough before hitting ZV, request
 18689              additional amount of scroll, to move point out of the
 18690              scroll margin.  */
 18691           if (IT_CHARPOS (it) == ZV
 18692               && it.current_y - y_start < this_scroll_margin)
 18693             y_offset = this_scroll_margin - (it.current_y - y_start);
 18694         }
 18695 
 18696       if (PT < CHARPOS (scroll_margin_pos))
 18697         {
 18698           /* Point is in the scroll margin at the top of the window or
 18699              above what is displayed in the window.  */
 18700           int y0, y_to_move;
 18701 
 18702           /* Compute the vertical distance from PT to the scroll
 18703              margin position.  Move as far as scroll_max allows, or
 18704              one screenful, or 10 screen lines, whichever is largest.
 18705              Give up if distance is greater than scroll_max or if we
 18706              didn't reach the scroll margin position.  */
 18707           SET_TEXT_POS (pos, PT, PT_BYTE);
 18708           start_display (&it, w, pos);
 18709           y0 = it.current_y;
 18710           y_to_move = max (it.last_visible_y,
 18711                            max (scroll_max, 10 * frame_line_height));
 18712           move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
 18713                       y_to_move, -1,
 18714                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18715           dy = it.current_y - y0;
 18716           if (dy > scroll_max
 18717               || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
 18718             return SCROLLING_FAILED;
 18719 
 18720           /* Additional scroll for when ZV was too close to point.  */
 18721           dy += y_offset;
 18722 
 18723           /* Compute new window start.  */
 18724           start_display (&it, w, startp);
 18725 
 18726           if (arg_scroll_conservatively)
 18727             amount_to_scroll
 18728               = min (max (dy, frame_line_height),
 18729                      frame_line_height * arg_scroll_conservatively);
 18730           else if (scroll_step || temp_scroll_step)
 18731             amount_to_scroll = scroll_max;
 18732           else
 18733             {
 18734               aggressive = BVAR (current_buffer, scroll_down_aggressively);
 18735               height = WINDOW_BOX_TEXT_HEIGHT (w);
 18736               if (NUMBERP (aggressive))
 18737                 {
 18738                   double float_amount = XFLOATINT (aggressive) * height;
 18739                   int aggressive_scroll = float_amount;
 18740                   if (aggressive_scroll == 0 && float_amount > 0)
 18741                     aggressive_scroll = 1;
 18742                   /* Don't let point enter the scroll margin near
 18743                      bottom of the window, if the value of
 18744                      scroll_down_aggressively happens to be too
 18745                      large.  */
 18746                   if (aggressive_scroll + 2 * this_scroll_margin > height)
 18747                     aggressive_scroll = height - 2 * this_scroll_margin;
 18748                   amount_to_scroll = dy + aggressive_scroll;
 18749                 }
 18750             }
 18751 
 18752           if (amount_to_scroll <= 0)
 18753             return SCROLLING_FAILED;
 18754 
 18755           move_it_vertically_backward (&it, amount_to_scroll);
 18756           startp = it.current.pos;
 18757         }
 18758     }
 18759 
 18760   /* Run window scroll functions.  */
 18761   startp = run_window_scroll_functions (window, startp);
 18762 
 18763   /* Display the window.  Give up if new fonts are loaded, or if point
 18764      doesn't appear.  */
 18765   if (!try_window (window, startp, 0))
 18766     rc = SCROLLING_NEED_LARGER_MATRICES;
 18767   else if (w->cursor.vpos < 0)
 18768     {
 18769       clear_glyph_matrix (w->desired_matrix);
 18770       rc = SCROLLING_FAILED;
 18771     }
 18772   else
 18773     {
 18774       /* Maybe forget recorded base line for line number display.  */
 18775       if (!just_this_one_p
 18776           || current_buffer->clip_changed
 18777           || BEG_UNCHANGED < CHARPOS (startp))
 18778         w->base_line_number = 0;
 18779 
 18780       /* If cursor ends up on a partially visible line,
 18781          treat that as being off the bottom of the screen.  */
 18782       if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
 18783                                         false, false)
 18784           /* It's possible that the cursor is on the first line of the
 18785              buffer, which is partially obscured due to a vscroll
 18786              (Bug#7537).  In that case, avoid looping forever. */
 18787           && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
 18788         {
 18789           clear_glyph_matrix (w->desired_matrix);
 18790           ++extra_scroll_margin_lines;
 18791           goto too_near_end;
 18792         }
 18793       rc = SCROLLING_SUCCESS;
 18794     }
 18795 
 18796   return rc;
 18797 }
 18798 
 18799 
 18800 /* Compute a suitable window start for window W if display of W starts
 18801    on a continuation line.  Value is true if a new window start
 18802    was computed.
 18803 
 18804    The new window start will be computed, based on W's width, starting
 18805    from the start of the continued line.  It is the start of the
 18806    screen line with the minimum distance from the old start W->start,
 18807    which is still before point (otherwise point will definitely not
 18808    be visible in the window).  */
 18809 
 18810 static bool
 18811 compute_window_start_on_continuation_line (struct window *w)
 18812 {
 18813   struct text_pos pos, start_pos, pos_before_pt;
 18814   bool window_start_changed_p = false;
 18815 
 18816   SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 18817 
 18818   /* If window start is on a continuation line...  Window start may be
 18819      < BEGV in case there's invisible text at the start of the
 18820      buffer (M-x rmail, for example).  */
 18821   if (CHARPOS (start_pos) > BEGV
 18822       && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
 18823     {
 18824       struct it it;
 18825       struct glyph_row *row;
 18826 
 18827       /* Handle the case that the window start is out of range.  */
 18828       if (CHARPOS (start_pos) < BEGV)
 18829         SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
 18830       else if (CHARPOS (start_pos) > ZV)
 18831         SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
 18832 
 18833       /* Find the start of the continued line.  This should be fast
 18834          because find_newline is fast (newline cache).  */
 18835       row = w->desired_matrix->rows + window_wants_tab_line (w)
 18836                                     + window_wants_header_line (w);
 18837       init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
 18838                      row, DEFAULT_FACE_ID);
 18839       reseat_at_previous_visible_line_start (&it);
 18840 
 18841       /* Give up (by not using the code in the block below) and say it
 18842          takes too much time to compute a new window start, if the
 18843          line start is "too far" away from the window start.  Also,
 18844          give up if the line start is after point, as in that case
 18845          point will not be visible with any window start we
 18846          compute.  */
 18847       if (IT_CHARPOS (it) <= PT
 18848           && (CHARPOS (start_pos) - IT_CHARPOS (it)
 18849               /* PXW: Do we need upper bounds here?  */
 18850               < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
 18851         {
 18852           int min_distance, distance;
 18853 
 18854           /* Move forward by display lines to find the new window
 18855              start.  If window width was enlarged, the new start can
 18856              be expected to be > the old start.  If window width was
 18857              decreased, the new window start will be < the old start.
 18858              So, we're looking for the display line start with the
 18859              minimum distance from the old window start.  */
 18860           pos_before_pt = pos = it.current.pos;
 18861           min_distance = DISP_INFINITY;
 18862           while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
 18863                  distance < min_distance)
 18864             {
 18865               min_distance = distance;
 18866               if (CHARPOS (pos) <= PT)
 18867                 pos_before_pt = pos;
 18868               pos = it.current.pos;
 18869               if (it.line_wrap == WORD_WRAP)
 18870                 {
 18871                   /* Under WORD_WRAP, move_it_by_lines is likely to
 18872                      overshoot and stop not at the first, but the
 18873                      second character from the left margin.  So in
 18874                      that case, we need a more tight control on the X
 18875                      coordinate of the iterator than move_it_by_lines
 18876                      promises in its contract.  The method is to first
 18877                      go to the last (rightmost) visible character of a
 18878                      line, then move to the leftmost character on the
 18879                      next line in a separate call.  */
 18880                   move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
 18881                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18882                   move_it_to (&it, ZV, 0,
 18883                               it.current_y + it.max_ascent + it.max_descent, -1,
 18884                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18885                 }
 18886               else
 18887                 move_it_by_lines (&it, 1);
 18888             }
 18889 
 18890           /* It makes very little sense to make the new window start
 18891              after point, as point won't be visible.  If that's what
 18892              the loop above finds, fall back on the candidate before
 18893              or at point that is closest to the old window start.  */
 18894           if (CHARPOS (pos) > PT)
 18895             pos = pos_before_pt;
 18896 
 18897           /* Set the window start there.  */
 18898           SET_MARKER_FROM_TEXT_POS (w->start, pos);
 18899           window_start_changed_p = true;
 18900         }
 18901     }
 18902 
 18903   return window_start_changed_p;
 18904 }
 18905 
 18906 
 18907 /* Try cursor movement in case text has not changed in window WINDOW,
 18908    with window start STARTP.  Value is
 18909 
 18910    CURSOR_MOVEMENT_SUCCESS if successful
 18911 
 18912    CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
 18913 
 18914    CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
 18915    display.  *SCROLL_STEP is set to true, under certain circumstances, if
 18916    we want to scroll as if scroll-step were set to 1.  See the code.
 18917 
 18918    CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
 18919    which case we have to abort this redisplay, and adjust matrices
 18920    first.  */
 18921 
 18922 enum
 18923 {
 18924   CURSOR_MOVEMENT_SUCCESS,
 18925   CURSOR_MOVEMENT_CANNOT_BE_USED,
 18926   CURSOR_MOVEMENT_MUST_SCROLL,
 18927   CURSOR_MOVEMENT_NEED_LARGER_MATRICES
 18928 };
 18929 
 18930 static int
 18931 try_cursor_movement (Lisp_Object window, struct text_pos startp,
 18932                      bool *scroll_step)
 18933 {
 18934   struct window *w = XWINDOW (window);
 18935   struct frame *f = XFRAME (w->frame);
 18936   int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
 18937 
 18938 #ifdef GLYPH_DEBUG
 18939   if (inhibit_try_cursor_movement)
 18940     return rc;
 18941 #endif
 18942 
 18943   /* Previously, there was a check for Lisp integer in the
 18944      if-statement below. Now, this field is converted to
 18945      ptrdiff_t, thus zero means invalid position in a buffer.  */
 18946   eassert (w->last_point > 0);
 18947   /* Likewise there was a check whether window_end_vpos is nil or larger
 18948      than the window.  Now window_end_vpos is int and so never nil, but
 18949      let's leave eassert to check whether it fits in the window.  */
 18950   eassert (!w->window_end_valid
 18951            || w->window_end_vpos < w->current_matrix->nrows);
 18952 
 18953   /* Handle case where text has not changed, only point, and it has
 18954      not moved off the frame.  */
 18955   if (/* Point may be in this window.  */
 18956       PT >= CHARPOS (startp)
 18957       /* Selective display hasn't changed.  */
 18958       && !current_buffer->clip_changed
 18959       /* Function force-mode-line-update is used to force a thorough
 18960          redisplay.  It sets either windows_or_buffers_changed or
 18961          update_mode_lines.  So don't take a shortcut here for these
 18962          cases.  */
 18963       && !update_mode_lines
 18964       && !windows_or_buffers_changed
 18965       && !f->cursor_type_changed
 18966       && NILP (Vshow_trailing_whitespace)
 18967       /* When display-line-numbers is in relative mode, moving point
 18968          requires to redraw the entire window.  */
 18969       && !EQ (Vdisplay_line_numbers, Qrelative)
 18970       && !EQ (Vdisplay_line_numbers, Qvisual)
 18971       /* When the current line number should be displayed in a
 18972          distinct face, moving point cannot be handled in optimized
 18973          way as below.  */
 18974       && !(!NILP (Vdisplay_line_numbers)
 18975            && NILP (Finternal_lisp_face_equal_p (Qline_number,
 18976                                                  Qline_number_current_line,
 18977                                                  w->frame)))
 18978       /* This code is not used for mini-buffer for the sake of the case
 18979          of redisplaying to replace an echo area message; since in
 18980          that case the mini-buffer contents per se are usually
 18981          unchanged.  This code is of no real use in the mini-buffer
 18982          since the handling of this_line_start_pos, etc., in redisplay
 18983          handles the same cases.  */
 18984       && !EQ (window, minibuf_window)
 18985       /* When overlay arrow is shown in current buffer, point movement
 18986          is no longer "simple", as it typically causes the overlay
 18987          arrow to move as well.  */
 18988       && !overlay_arrow_in_current_buffer_p ())
 18989     {
 18990       int this_scroll_margin, top_scroll_margin;
 18991       struct glyph_row *row = NULL;
 18992 
 18993 #ifdef GLYPH_DEBUG
 18994       debug_method_add (w, "cursor movement");
 18995 #endif
 18996 
 18997       this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 18998 
 18999       top_scroll_margin = this_scroll_margin;
 19000       if (window_wants_tab_line (w))
 19001         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 19002       if (window_wants_header_line (w))
 19003         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 19004 
 19005       /* Start with the row the cursor was displayed during the last
 19006          not paused redisplay.  Give up if that row is not valid.  */
 19007       if (w->last_cursor_vpos < 0
 19008           || w->last_cursor_vpos >= w->current_matrix->nrows)
 19009         rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19010       else
 19011         {
 19012           row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
 19013           /* Skip the tab-line and header-line rows, if any.  */
 19014           if (row->tab_line_p)
 19015             ++row;
 19016           if (row->mode_line_p)
 19017             ++row;
 19018           if (!row->enabled_p)
 19019             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19020         }
 19021 
 19022       if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
 19023         {
 19024           bool scroll_p = false, must_scroll = false;
 19025           int last_y = window_text_bottom_y (w) - this_scroll_margin;
 19026 
 19027           if (PT > w->last_point)
 19028             {
 19029               /* Point has moved forward.  */
 19030               while (MATRIX_ROW_END_CHARPOS (row) < PT
 19031                      && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19032                 {
 19033                   eassert (row->enabled_p);
 19034                   ++row;
 19035                 }
 19036 
 19037               /* If the end position of a row equals the start
 19038                  position of the next row, and PT is at that position,
 19039                  we would rather display cursor in the next line.  */
 19040               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19041                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19042                      && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
 19043                      && MATRIX_ROW_START_CHARPOS (row+1) >= PT
 19044                      && !cursor_row_p (row))
 19045                 ++row;
 19046 
 19047               /* If within the scroll margin, scroll.  Note that
 19048                  MATRIX_ROW_BOTTOM_Y gives the pixel position at which
 19049                  the next line would be drawn, and that
 19050                  this_scroll_margin can be zero.  */
 19051               if (MATRIX_ROW_BOTTOM_Y (row) > last_y
 19052                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19053                   /* Line is completely visible last line in window
 19054                      and PT is to be set in the next line.  */
 19055                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19056                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19057                       && !row->ends_at_zv_p
 19058                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19059                 scroll_p = true;
 19060             }
 19061           else if (PT < w->last_point)
 19062             {
 19063               /* Cursor has to be moved backward.  Note that PT >=
 19064                  CHARPOS (startp) because of the outer if-statement.  */
 19065               struct glyph_row *row0 = row;
 19066 
 19067               while (!row->mode_line_p
 19068                      && (MATRIX_ROW_START_CHARPOS (row) > PT
 19069                          || (MATRIX_ROW_START_CHARPOS (row) == PT
 19070                              && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
 19071                                  || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
 19072                                      row > w->current_matrix->rows
 19073                                      && (row-1)->ends_in_newline_from_string_p))))
 19074                      && (row->y > top_scroll_margin
 19075                          || CHARPOS (startp) == BEGV))
 19076                 {
 19077                   eassert (row->enabled_p);
 19078                   --row;
 19079                 }
 19080 
 19081               /* With bidi-reordered rows we can have buffer positions
 19082                  _decrease_ when going down by rows.  If we haven't
 19083                  found our row in the loop above, give it another try
 19084                  now going in the other direction from the original row.  */
 19085               if (!(MATRIX_ROW_START_CHARPOS (row) <= PT
 19086                     && PT <= MATRIX_ROW_END_CHARPOS (row))
 19087                   && row0->continued_p)
 19088                 {
 19089                   row = row0;
 19090                   while (MATRIX_ROW_START_CHARPOS (row) > PT
 19091                          && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19092                     {
 19093                       eassert (row->enabled_p);
 19094                       ++row;
 19095                     }
 19096                 }
 19097 
 19098               /* Consider the following case: Window starts at BEGV,
 19099                  there is invisible, intangible text at BEGV, so that
 19100                  display starts at some point START > BEGV.  It can
 19101                  happen that we are called with PT somewhere between
 19102                  BEGV and START.  Try to handle that case.  */
 19103               if (row < w->current_matrix->rows
 19104                   || row->mode_line_p)
 19105                 {
 19106                   row = w->current_matrix->rows;
 19107                   /* Skip the tab-line and header-line rows, if any.  */
 19108                   if (row->tab_line_p)
 19109                     ++row;
 19110                   if (row->mode_line_p)
 19111                     ++row;
 19112                 }
 19113 
 19114               /* Due to newlines in overlay strings, we may have to
 19115                  skip forward over overlay strings.  */
 19116               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19117                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19118                      && !cursor_row_p (row))
 19119                 ++row;
 19120 
 19121               /* If within the scroll margin, either the top one or
 19122                  the bottom one, scroll.  */
 19123               if ((row->y < top_scroll_margin
 19124                    && CHARPOS (startp) != BEGV)
 19125                   || MATRIX_ROW_BOTTOM_Y (row) > last_y
 19126                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19127                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19128                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19129                       && !row->ends_at_zv_p
 19130                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19131                 scroll_p = true;
 19132             }
 19133           else
 19134             {
 19135               /* Cursor did not move.  So don't scroll even if cursor line
 19136                  is partially visible, as it was so before.  */
 19137                  rc = CURSOR_MOVEMENT_SUCCESS;
 19138             }
 19139 
 19140           if ((PT < MATRIX_ROW_START_CHARPOS (row)
 19141                && (row == MATRIX_FIRST_TEXT_ROW (w->current_matrix)
 19142                    /* Don't give up if point is inside invisible text
 19143                       at the beginning of its glyph row.  */
 19144                    || (MATRIX_ROW_END_CHARPOS (row-1)
 19145                        == MATRIX_ROW_START_CHARPOS (row))))
 19146               || PT > MATRIX_ROW_END_CHARPOS (row))
 19147             {
 19148               /* if PT is not in the glyph row, give up.  */
 19149               rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19150               must_scroll = true;
 19151             }
 19152           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19153                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19154             {
 19155               struct glyph_row *row1;
 19156 
 19157               /* If rows are bidi-reordered and point moved, back up
 19158                  until we find a row that does not belong to a
 19159                  continuation line.  This is because we must consider
 19160                  all rows of a continued line as candidates for the
 19161                  new cursor positioning, since row start and end
 19162                  positions change non-linearly with vertical position
 19163                  in such rows.  */
 19164               /* FIXME: Revisit this when glyph ``spilling'' in
 19165                  continuation lines' rows is implemented for
 19166                  bidi-reordered rows.  */
 19167               for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 19168                    MATRIX_ROW_CONTINUATION_LINE_P (row);
 19169                    --row)
 19170                 {
 19171                   /* If we hit the beginning of the displayed portion
 19172                      without finding the first row of a continued
 19173                      line, give up.  */
 19174                   if (row <= row1)
 19175                     {
 19176                       rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19177                       break;
 19178                     }
 19179                   eassert (row->enabled_p);
 19180                 }
 19181             }
 19182           if (must_scroll)
 19183             ;
 19184           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19185               && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
 19186               /* Make sure this isn't a header line nor a tab-line by
 19187                  any chance, since then MATRIX_ROW_PARTIALLY_VISIBLE_P
 19188                  might yield true.  */
 19189               && !row->mode_line_p
 19190               && !cursor_row_fully_visible_p (w, true, true, true))
 19191             {
 19192               if (PT == MATRIX_ROW_END_CHARPOS (row)
 19193                   && !row->ends_at_zv_p
 19194                   && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 19195                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19196               else if (row->height > window_box_height (w))
 19197                 {
 19198                   /* If we end up in a partially visible line, let's
 19199                      make it fully visible, except when it's taller
 19200                      than the window, in which case we can't do much
 19201                      about it.  */
 19202                   *scroll_step = true;
 19203                   rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19204                 }
 19205               else
 19206                 {
 19207                   set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 19208                   if (!cursor_row_fully_visible_p (w, false, true, false))
 19209                     rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19210                   else
 19211                     rc = CURSOR_MOVEMENT_SUCCESS;
 19212                 }
 19213             }
 19214           else if (scroll_p)
 19215             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19216           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19217                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19218             {
 19219               /* With bidi-reordered rows, there could be more than
 19220                  one candidate row whose start and end positions
 19221                  occlude point.  We need to let set_cursor_from_row
 19222                  find the best candidate.  */
 19223               /* FIXME: Revisit this when glyph ``spilling'' in
 19224                  continuation lines' rows is implemented for
 19225                  bidi-reordered rows.  */
 19226               bool rv = false;
 19227               bool pt_invis = false;
 19228               Lisp_Object val = get_char_property_and_overlay (make_fixnum (PT),
 19229                                                                Qinvisible,
 19230                                                                Qnil, NULL);
 19231 
 19232               if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 19233                 pt_invis = true;
 19234 
 19235               do
 19236                 {
 19237                   bool at_zv_p = false, exact_match_p = false;
 19238 
 19239                   /* If point is in invisible text, we cannot assume
 19240                      it must be after row's start position, since the
 19241                      row could have invisible text at its beginning
 19242                      where point is located.  */
 19243                   if ((pt_invis || MATRIX_ROW_START_CHARPOS (row) <= PT)
 19244                       && PT <= MATRIX_ROW_END_CHARPOS (row)
 19245                       && cursor_row_p (row))
 19246                     rv |= set_cursor_from_row (w, row, w->current_matrix,
 19247                                                0, 0, 0, 0);
 19248                   /* As soon as we've found the exact match for point,
 19249                      or the first suitable row whose ends_at_zv_p flag
 19250                      is set, we are done.  */
 19251                   if (rv)
 19252                     {
 19253                       at_zv_p = MATRIX_ROW (w->current_matrix,
 19254                                             w->cursor.vpos)->ends_at_zv_p;
 19255                       if (!at_zv_p
 19256                           && w->cursor.hpos >= 0
 19257                           && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
 19258                                                                w->cursor.vpos))
 19259                         {
 19260                           struct glyph_row *candidate =
 19261                             MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 19262                           struct glyph *g =
 19263                             candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
 19264                           ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
 19265 
 19266                           exact_match_p =
 19267                             (BUFFERP (g->object) && g->charpos == PT)
 19268                             || (NILP (g->object)
 19269                                 && (g->charpos == PT
 19270                                     || (g->charpos == 0 && endpos - 1 == PT)));
 19271                           /* Perhaps the point position is inside
 19272                              invisible text?  In that case, we trust
 19273                              'set_cursor_from_row' to do its job and
 19274                              find the best position for the cursor.  */
 19275                           if (!exact_match_p && pt_invis)
 19276                             exact_match_p = true;
 19277                         }
 19278                       if (at_zv_p || exact_match_p)
 19279                         {
 19280                           rc = CURSOR_MOVEMENT_SUCCESS;
 19281                           break;
 19282                         }
 19283                     }
 19284                   if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
 19285                     break;
 19286                   ++row;
 19287                 }
 19288               while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
 19289                        || row->continued_p)
 19290                       && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
 19291                      || (MATRIX_ROW_START_CHARPOS (row) == PT
 19292                          && MATRIX_ROW_BOTTOM_Y (row) < last_y));
 19293               /* If we didn't find any candidate rows, or exited the
 19294                  loop before all the candidates were examined, signal
 19295                  to the caller that this method failed.  */
 19296               if (rc != CURSOR_MOVEMENT_SUCCESS
 19297                   && !(rv
 19298                        && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 19299                        && !row->continued_p))
 19300                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19301               else if (rv)
 19302                 rc = CURSOR_MOVEMENT_SUCCESS;
 19303             }
 19304           else
 19305             {
 19306               do
 19307                 {
 19308                   if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
 19309                     {
 19310                       rc = CURSOR_MOVEMENT_SUCCESS;
 19311                       break;
 19312                     }
 19313                   ++row;
 19314                 }
 19315               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19316                      && MATRIX_ROW_START_CHARPOS (row) == PT
 19317                      && cursor_row_p (row));
 19318             }
 19319         }
 19320     }
 19321 
 19322   return rc;
 19323 }
 19324 
 19325 
 19326 void
 19327 set_vertical_scroll_bar (struct window *w)
 19328 {
 19329   ptrdiff_t start, end, whole;
 19330 
 19331   /* Calculate the start and end positions for the current window.
 19332      At some point, it would be nice to choose between scrollbars
 19333      which reflect the whole buffer size, with special markers
 19334      indicating narrowing, and scrollbars which reflect only the
 19335      visible region.
 19336 
 19337      Note that mini-buffers sometimes aren't displaying any text.  */
 19338   if (!MINI_WINDOW_P (w)
 19339       || (w == XWINDOW (minibuf_window)
 19340           && NILP (echo_area_buffer[0])))
 19341     {
 19342       struct buffer *buf = XBUFFER (w->contents);
 19343 
 19344       whole = BUF_ZV (buf) - BUF_BEGV (buf);
 19345       start = marker_position (w->start) - BUF_BEGV (buf);
 19346       end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
 19347 
 19348       /* If w->window_end_pos cannot be trusted, recompute it "the
 19349          hard way".  But don't bother to be too accurate when
 19350          long-line shortcuts are in effect.  */
 19351       if (!w->window_end_valid && !buf->long_line_optimizations_p)
 19352         {
 19353           struct it it;
 19354           struct text_pos start_pos;
 19355           struct buffer *obuf = current_buffer;
 19356           /* When we display the scroll bar of a mini-window,
 19357              current_buffer is not guaranteed to be the mini-window's
 19358              buffer, see the beginning of redisplay_window.  */
 19359           set_buffer_internal_1 (XBUFFER (w->contents));
 19360           SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 19361           start_display (&it, w, start_pos);
 19362           move_it_to (&it, -1, it.last_visible_x, window_box_height (w), -1,
 19363                       MOVE_TO_X | MOVE_TO_Y);
 19364           end -= (BUF_Z (buf) - IT_CHARPOS (it)) - w->window_end_pos;
 19365           set_buffer_internal_1 (obuf);
 19366         }
 19367 
 19368       if (end < start)
 19369         end = start;
 19370       if (whole < (end - start))
 19371         whole = end - start;
 19372     }
 19373   else
 19374     start = end = whole = 0;
 19375 
 19376   /* Indicate what this scroll bar ought to be displaying now.  */
 19377   if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19378     (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19379       (w, end - start, whole, start);
 19380 }
 19381 
 19382 
 19383 void
 19384 set_horizontal_scroll_bar (struct window *w)
 19385 {
 19386   int start, end, whole, portion;
 19387 
 19388   if (!MINI_WINDOW_P (w) || EQ (w->horizontal_scroll_bar_type, Qbottom))
 19389     {
 19390       struct buffer *b = XBUFFER (w->contents);
 19391       struct buffer *old_buffer = NULL;
 19392       struct it it;
 19393       struct text_pos startp;
 19394 
 19395       if (b != current_buffer)
 19396         {
 19397           old_buffer = current_buffer;
 19398           set_buffer_internal (b);
 19399         }
 19400 
 19401       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19402       start_display (&it, w, startp);
 19403       it.last_visible_x = INT_MAX;
 19404       whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
 19405                           MOVE_TO_X | MOVE_TO_Y);
 19406       /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
 19407                           window_box_height (w), -1,
 19408                           MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
 19409 
 19410       start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
 19411       end = start + window_box_width (w, TEXT_AREA);
 19412       portion = end - start;
 19413       /* After enlarging a horizontally scrolled window such that it
 19414          gets at least as wide as the text it contains, make sure that
 19415          the thumb doesn't fill the entire scroll bar so we can still
 19416          drag it back to see the entire text.  */
 19417       whole = max (whole, end);
 19418 
 19419       if (it.bidi_p)
 19420         {
 19421           Lisp_Object pdir;
 19422 
 19423           pdir = Fcurrent_bidi_paragraph_direction (Qnil);
 19424           if (EQ (pdir, Qright_to_left))
 19425             {
 19426               start = whole - end;
 19427               end = start + portion;
 19428             }
 19429         }
 19430 
 19431       if (old_buffer)
 19432         set_buffer_internal (old_buffer);
 19433     }
 19434   else
 19435     start = end = whole = portion = 0;
 19436 
 19437   w->hscroll_whole = whole;
 19438 
 19439   /* Indicate what this scroll bar ought to be displaying now.  */
 19440   if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19441     (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19442       (w, portion, whole, start);
 19443 }
 19444 
 19445 /* Subroutine of redisplay_window, to determine whether a window-start
 19446    point STARTP of WINDOW should be rejected.  */
 19447 static bool
 19448 window_start_acceptable_p (Lisp_Object window, ptrdiff_t startp)
 19449 {
 19450   if (!make_window_start_visible)
 19451     return true;
 19452 
 19453   struct window *w = XWINDOW (window);
 19454   struct frame *f = XFRAME (w->frame);
 19455   Lisp_Object startpos = make_fixnum (startp);
 19456   Lisp_Object invprop, disp_spec;
 19457   struct text_pos ignored;
 19458 
 19459   /* Is STARTP in invisible text?  */
 19460   if ((invprop = Fget_char_property (startpos, Qinvisible, window)),
 19461       TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 19462     return false;
 19463 
 19464   /* Is STARTP covered by a replacing 'display' property?  */
 19465   if (!NILP (disp_spec = Fget_char_property (startpos, Qdisplay, window))
 19466       && handle_display_spec (NULL, disp_spec, Qnil, Qnil, &ignored, startp,
 19467                               FRAME_WINDOW_P (f)) > 0)
 19468     return false;
 19469 
 19470   return true;
 19471 }
 19472 
 19473 DEFUN ("long-line-optimizations-p", Flong_line_optimizations_p, Slong_line_optimizations_p,
 19474        0, 0, 0,
 19475        doc: /* Return non-nil if long-line optimizations are in effect in current buffer.
 19476 See `long-line-threshold' and `large-hscroll-threshold' for what these
 19477 optimizations mean and when they are in effect.  */)
 19478   (void)
 19479 {
 19480   return current_buffer->long_line_optimizations_p ? Qt : Qnil;
 19481 }
 19482 
 19483 /* Redisplay leaf window WINDOW.  JUST_THIS_ONE_P means only
 19484    selected_window is redisplayed.
 19485 
 19486    We can return without actually redisplaying the window if fonts has been
 19487    changed on window's frame.  In that case, redisplay_internal will retry.
 19488 
 19489    As one of the important parts of redisplaying a window, we need to
 19490    decide whether the previous window-start position (stored in the
 19491    window's w->start marker position) is still valid, and if it isn't,
 19492    recompute it.  Some details about that:
 19493 
 19494     . The previous window-start could be in a continuation line, in
 19495       which case we need to recompute it when the window width
 19496       changes.  See compute_window_start_on_continuation_line and its
 19497       call below.
 19498 
 19499     . The text that changed since last redisplay could include the
 19500       previous window-start position.  In that case, we try to salvage
 19501       what we can from the current glyph matrix by calling
 19502       try_scrolling, which see.
 19503 
 19504     . Some Emacs command could force us to use a specific window-start
 19505       position by setting the window's force_start flag, or gently
 19506       propose doing that by setting the window's optional_new_start
 19507       flag.  In these cases, we try using the specified start point if
 19508       that succeeds (i.e. the window desired matrix is successfully
 19509       recomputed, and point location is within the window).  In case
 19510       of optional_new_start, we first check if the specified start
 19511       position is feasible, i.e. if it will allow point to be
 19512       displayed in the window.  If using the specified start point
 19513       fails, e.g., if new fonts are needed to be loaded, we abort the
 19514       redisplay cycle and leave it up to the next cycle to figure out
 19515       things.
 19516 
 19517     . Note that the window's force_start flag is sometimes set by
 19518       redisplay itself, when it decides that the previous window start
 19519       point is fine and should be kept.  Search for "goto force_start"
 19520       below to see the details.  Like the values of window-start
 19521       specified outside of redisplay, these internally-deduced values
 19522       are tested for feasibility, and ignored if found to be
 19523       unfeasible.
 19524 
 19525     . Note that the function try_window, used to completely redisplay
 19526       a window, accepts the window's start point as its argument.
 19527       This is used several times in the redisplay code to control
 19528       where the window start will be, according to user options such
 19529       as scroll-conservatively, and also to ensure the screen line
 19530       showing point will be fully (as opposed to partially) visible on
 19531       display.  */
 19532 
 19533 static void
 19534 redisplay_window (Lisp_Object window, bool just_this_one_p)
 19535 {
 19536   struct window *w = XWINDOW (window);
 19537   struct frame *f = XFRAME (w->frame);
 19538   struct buffer *buffer = XBUFFER (w->contents);
 19539   struct buffer *old = current_buffer;
 19540   struct text_pos lpoint, opoint, startp;
 19541   bool update_mode_line;
 19542   int tem;
 19543   struct it it;
 19544   /* Record it now because it's overwritten.  */
 19545   bool current_matrix_up_to_date_p = false;
 19546   bool used_current_matrix_p = false;
 19547   /* This is less strict than current_matrix_up_to_date_p.
 19548      It indicates that the buffer contents and narrowing are unchanged.  */
 19549   bool buffer_unchanged_p = false;
 19550   bool temp_scroll_step = false;
 19551   specpdl_ref count = SPECPDL_INDEX ();
 19552   int rc;
 19553   int centering_position = -1;
 19554   bool last_line_misfit = false;
 19555   ptrdiff_t beg_unchanged, end_unchanged;
 19556   int frame_line_height, margin;
 19557   bool use_desired_matrix;
 19558   void *itdata = NULL;
 19559 
 19560   SET_TEXT_POS (lpoint, PT, PT_BYTE);
 19561   opoint = lpoint;
 19562 
 19563 #ifdef GLYPH_DEBUG
 19564   *w->desired_matrix->method = 0;
 19565 #endif
 19566 
 19567   if (!just_this_one_p && needs_no_redisplay (w))
 19568     return;
 19569 
 19570   /* Make sure that both W's markers are valid.  */
 19571   eassert (XMARKER (w->start)->buffer == buffer);
 19572   eassert (XMARKER (w->pointm)->buffer == buffer);
 19573 
 19574   reconsider_clip_changes (w);
 19575   frame_line_height = default_line_pixel_height (w);
 19576   margin = window_scroll_margin (w, MARGIN_IN_LINES);
 19577 
 19578 
 19579   /* Has the mode line to be updated?  */
 19580   update_mode_line = (w->update_mode_line
 19581                       || update_mode_lines
 19582                       || buffer->clip_changed
 19583                       || buffer->prevent_redisplay_optimizations_p);
 19584 
 19585   if (!just_this_one_p)
 19586     /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
 19587        cleverly elsewhere.  */
 19588     w->must_be_updated_p = true;
 19589 
 19590   if (MINI_WINDOW_P (w))
 19591     {
 19592       if (w == XWINDOW (echo_area_window)
 19593           && !NILP (echo_area_buffer[0]))
 19594         {
 19595           if (update_mode_line)
 19596             /* We may have to update a tty frame's menu bar or a
 19597                tool-bar.  Example `M-x C-h C-h C-g'.  */
 19598             goto finish_menu_bars;
 19599           else
 19600             /* We've already displayed the echo area glyphs in this window.  */
 19601             goto finish_scroll_bars;
 19602         }
 19603       else if ((w != XWINDOW (minibuf_window)
 19604                 || minibuf_level == 0)
 19605                /* When buffer is nonempty, redisplay window normally.  */
 19606                && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
 19607                /* Quail displays non-mini buffers in minibuffer window.
 19608                   In that case, redisplay the window normally.  */
 19609                && !NILP (Fmemq (w->contents, Vminibuffer_list)))
 19610         {
 19611           /* W is a mini-buffer window, but it's not active, so clear
 19612              it.  */
 19613           int yb = window_text_bottom_y (w);
 19614           struct glyph_row *row;
 19615           int y;
 19616 
 19617           for (y = 0, row = w->desired_matrix->rows;
 19618                y < yb;
 19619                y += row->height, ++row)
 19620             blank_row (w, row, y);
 19621           goto finish_scroll_bars;
 19622         }
 19623       else if (minibuf_level >= 1)
 19624         {
 19625           /* We could have a message produced by set-minibuffer-message
 19626              displayed in the mini-window as an overlay, so resize the
 19627              mini-window if needed.  */
 19628           resize_mini_window (w, false);
 19629         }
 19630 
 19631       clear_glyph_matrix (w->desired_matrix);
 19632     }
 19633 
 19634   /* Otherwise set up data on this window; select its buffer and point
 19635      value.  */
 19636   /* Really select the buffer, for the sake of buffer-local
 19637      variables.  */
 19638   set_buffer_internal_1 (XBUFFER (w->contents));
 19639 
 19640   current_matrix_up_to_date_p
 19641     = (w->window_end_valid
 19642        && !current_buffer->clip_changed
 19643        && !current_buffer->prevent_redisplay_optimizations_p
 19644        && !window_outdated (w)
 19645        && !composition_break_at_point
 19646        && !hscrolling_current_line_p (w));
 19647 
 19648   beg_unchanged = BEG_UNCHANGED;
 19649   end_unchanged = END_UNCHANGED;
 19650 
 19651   SET_TEXT_POS (opoint, PT, PT_BYTE);
 19652 
 19653   specbind (Qinhibit_point_motion_hooks, Qt);
 19654 
 19655   buffer_unchanged_p
 19656     = (w->window_end_valid
 19657        && !current_buffer->clip_changed
 19658        && !window_outdated (w));
 19659 
 19660   /* When windows_or_buffers_changed is non-zero, we can't rely
 19661      on the window end being valid, so set it to zero there.  */
 19662   if (windows_or_buffers_changed)
 19663     {
 19664       /* If window starts on a continuation line, maybe adjust the
 19665          window start in case the window's width changed.  */
 19666       if (XMARKER (w->start)->buffer == current_buffer)
 19667         compute_window_start_on_continuation_line (w);
 19668 
 19669       w->window_end_valid = false;
 19670       /* If so, we also can't rely on current matrix
 19671          and should not fool try_cursor_movement below.  */
 19672       current_matrix_up_to_date_p = false;
 19673     }
 19674 
 19675   /* Some sanity checks.  */
 19676   CHECK_WINDOW_END (w);
 19677   if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
 19678     emacs_abort ();
 19679   if (BYTEPOS (opoint) < CHARPOS (opoint))
 19680     emacs_abort ();
 19681 
 19682   if (mode_line_update_needed (w))
 19683     update_mode_line = true;
 19684 
 19685   /* Point refers normally to the selected window.  For any other
 19686      window, set up appropriate value.  */
 19687   if (!EQ (window, selected_window))
 19688     {
 19689       ptrdiff_t new_pt = marker_position (w->pointm);
 19690       ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
 19691 
 19692       if (new_pt < BEGV)
 19693         {
 19694           new_pt = BEGV;
 19695           new_pt_byte = BEGV_BYTE;
 19696           set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
 19697         }
 19698       else if (new_pt > (ZV - 1))
 19699         {
 19700           new_pt = ZV;
 19701           new_pt_byte = ZV_BYTE;
 19702           set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
 19703         }
 19704 
 19705       /* We don't use SET_PT so that the point-motion hooks don't run.  */
 19706       TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
 19707     }
 19708 
 19709   /* If any of the character widths specified in the display table
 19710      have changed, invalidate the width run cache.  It's true that
 19711      this may be a bit late to catch such changes, but the rest of
 19712      redisplay goes (non-fatally) haywire when the display table is
 19713      changed, so why should we worry about doing any better?  */
 19714   if (current_buffer->width_run_cache
 19715       || (current_buffer->base_buffer
 19716           && current_buffer->base_buffer->width_run_cache))
 19717     {
 19718       struct Lisp_Char_Table *disptab = buffer_display_table ();
 19719 
 19720       if (! disptab_matches_widthtab
 19721           (disptab, XVECTOR (BVAR (current_buffer, width_table))))
 19722         {
 19723           struct buffer *buf = current_buffer;
 19724 
 19725           if (buf->base_buffer)
 19726             buf = buf->base_buffer;
 19727           invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
 19728           recompute_width_table (current_buffer, disptab);
 19729         }
 19730     }
 19731 
 19732   /* Check whether the buffer to be displayed contains long lines.  */
 19733   if (!NILP (Vlong_line_threshold)
 19734       && !current_buffer->long_line_optimizations_p
 19735       && (CHARS_MODIFF - UNCHANGED_MODIFIED > 8
 19736           || current_buffer->clip_changed))
 19737     {
 19738       ptrdiff_t cur, next, found, max = 0, threshold;
 19739       threshold = XFIXNUM (Vlong_line_threshold);
 19740       for (cur = BEGV; cur < ZV; cur = next)
 19741         {
 19742           next = find_newline1 (cur, CHAR_TO_BYTE (cur), 0, -1, 1,
 19743                                 &found, NULL, true);
 19744           if (next - cur > max) max = next - cur;
 19745           if (!found || max > threshold) break;
 19746         }
 19747       if (max > threshold)
 19748         current_buffer->long_line_optimizations_p = true;
 19749     }
 19750 
 19751   /* If window-start is screwed up, choose a new one.  */
 19752   if (XMARKER (w->start)->buffer != current_buffer)
 19753     goto recenter;
 19754 
 19755   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19756 
 19757   /* If someone specified a new starting point but did not insist,
 19758      check whether it can be used.  */
 19759   if ((w->optional_new_start || window_frozen_p (w))
 19760       && CHARPOS (startp) >= BEGV
 19761       && CHARPOS (startp) <= ZV)
 19762     {
 19763       ptrdiff_t it_charpos;
 19764 
 19765       w->optional_new_start = false;
 19766       if (!w->force_start)
 19767         {
 19768           start_display (&it, w, startp);
 19769           move_it_to (&it, PT, 0, it.last_visible_y, -1,
 19770                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19771           /* Record IT's position now, since line_bottom_y might
 19772              change that.  */
 19773           it_charpos = IT_CHARPOS (it);
 19774           /* Make sure we set the force_start flag only if the cursor
 19775              row will be fully visible.  Otherwise, the code under
 19776              force_start label below will try to move point back into
 19777              view, which is not what the code which sets
 19778              optional_new_start wants.  */
 19779           if (it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
 19780             {
 19781               if (it_charpos == PT)
 19782                 w->force_start = true;
 19783               /* IT may overshoot PT if text at PT is invisible.  */
 19784               else if (it_charpos > PT && CHARPOS (startp) <= PT)
 19785                 w->force_start = true;
 19786 #ifdef GLYPH_DEBUG
 19787               if (w->force_start)
 19788                 {
 19789                   if (window_frozen_p (w))
 19790                     debug_method_add (w, "set force_start from frozen window start");
 19791                   else
 19792                     debug_method_add (w, "set force_start from optional_new_start");
 19793                 }
 19794 #endif
 19795             }
 19796         }
 19797     }
 19798 
 19799  force_start:
 19800 
 19801   /* Handle case where place to start displaying has been specified,
 19802      unless the specified location is outside the accessible range.  */
 19803   if (w->force_start)
 19804     {
 19805       /* We set this later on if we have to adjust point.  */
 19806       int new_vpos = -1;
 19807 
 19808       w->force_start = false;
 19809 
 19810       /* The vscroll should be preserved in this case, since
 19811          `pixel-scroll-precision-mode' must continue working normally
 19812          when a mini-window is resized.  (bug#55312) */
 19813       if (!w->preserve_vscroll_p || !window_frozen_p (w))
 19814         w->vscroll = 0;
 19815 
 19816       w->preserve_vscroll_p = false;
 19817       w->window_end_valid = false;
 19818 
 19819       /* Forget any recorded base line for line number display.  */
 19820       if (!buffer_unchanged_p)
 19821         w->base_line_number = 0;
 19822 
 19823       /* Redisplay the mode line.  Select the buffer properly for that.
 19824          Also, run the hook window-scroll-functions
 19825          because we have scrolled.  */
 19826       /* Note, we do this after clearing force_start because
 19827          if there's an error, it is better to forget about force_start
 19828          than to get into an infinite loop calling the hook functions
 19829          and having them get more errors.  */
 19830       if (!update_mode_line
 19831           || ! NILP (Vwindow_scroll_functions))
 19832         {
 19833           update_mode_line = true;
 19834           w->update_mode_line = true;
 19835           startp = run_window_scroll_functions (window, startp);
 19836         }
 19837 
 19838       if (CHARPOS (startp) < BEGV)
 19839         SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
 19840       else if (CHARPOS (startp) > ZV)
 19841         SET_TEXT_POS (startp, ZV, ZV_BYTE);
 19842 
 19843       /* Reject the specified start location if it is invisible, and
 19844          the buffer wants it always visible.  */
 19845       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 19846         goto ignore_start;
 19847 
 19848       /* Redisplay, then check if cursor has been set during the
 19849          redisplay.  Give up if new fonts were loaded.  */
 19850       /* We used to issue a CHECK_MARGINS argument to try_window here,
 19851          but this causes scrolling to fail when point begins inside
 19852          the scroll margin (bug#148) -- cyd  */
 19853       clear_glyph_matrix (w->desired_matrix);
 19854       if (!try_window (window, startp, 0))
 19855         {
 19856           w->force_start = true;
 19857           clear_glyph_matrix (w->desired_matrix);
 19858           goto need_larger_matrices;
 19859         }
 19860 
 19861       if (w->cursor.vpos < 0)
 19862         {
 19863           /* If point does not appear, try to move point so it does
 19864              appear.  The desired matrix has been built above, so we
 19865              can use it here.  First see if point is in invisible
 19866              text, and if so, move it to the first visible buffer
 19867              position past that.  */
 19868           struct glyph_row *r = NULL;
 19869           Lisp_Object invprop =
 19870             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 19871                                            Qnil, NULL);
 19872 
 19873           if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 19874             {
 19875               ptrdiff_t alt_pt;
 19876               Lisp_Object invprop_end =
 19877                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 19878                                                    Qnil, Qnil);
 19879 
 19880               if (FIXNATP (invprop_end))
 19881                 alt_pt = XFIXNAT (invprop_end);
 19882               else
 19883                 alt_pt = ZV;
 19884               r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
 19885                                       NULL, 0);
 19886             }
 19887           if (r)
 19888             new_vpos = MATRIX_ROW_BOTTOM_Y (r);
 19889           else  /* Give up and just move to the middle of the window.  */
 19890             new_vpos = window_box_height (w) / 2;
 19891         }
 19892 
 19893       if (!cursor_row_fully_visible_p (w, false, false, false))
 19894         {
 19895           /* Point does appear, but on a line partly visible at end of window.
 19896              Move it back to a fully-visible line.  */
 19897           new_vpos = window_box_height (w);
 19898           /* But if window_box_height suggests a Y coordinate that is
 19899              not less than we already have, that line will clearly not
 19900              be fully visible, so give up and scroll the display.
 19901              This can happen when the default face uses a font whose
 19902              dimensions are different from the frame's default
 19903              font.  */
 19904           if (new_vpos >= w->cursor.y)
 19905             {
 19906               w->cursor.vpos = -1;
 19907               clear_glyph_matrix (w->desired_matrix);
 19908               goto try_to_scroll;
 19909             }
 19910         }
 19911       else if (w->cursor.vpos >= 0)
 19912         {
 19913           /* Some people insist on not letting point enter the scroll
 19914              margin, even though this part handles windows that didn't
 19915              scroll at all.  */
 19916           int pixel_margin = margin * frame_line_height;
 19917           bool tab_line = window_wants_tab_line (w);
 19918           bool header_line = window_wants_header_line (w);
 19919 
 19920           /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
 19921              below, which finds the row to move point to, advances by
 19922              the Y coordinate of the _next_ row, see the definition of
 19923              MATRIX_ROW_BOTTOM_Y.  */
 19924           if (w->cursor.vpos < margin + tab_line + header_line)
 19925             {
 19926               w->cursor.vpos = -1;
 19927               clear_glyph_matrix (w->desired_matrix);
 19928               goto try_to_scroll;
 19929             }
 19930           else
 19931             {
 19932               int window_height = window_box_height (w);
 19933 
 19934               if (tab_line)
 19935                 window_height += CURRENT_TAB_LINE_HEIGHT (w);
 19936               if (header_line)
 19937                 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
 19938               if (w->cursor.y >= window_height - pixel_margin)
 19939                 {
 19940                   w->cursor.vpos = -1;
 19941                   clear_glyph_matrix (w->desired_matrix);
 19942                   goto try_to_scroll;
 19943                 }
 19944             }
 19945         }
 19946 
 19947       /* If we need to move point for either of the above reasons,
 19948          now actually do it.  */
 19949       if (new_vpos >= 0)
 19950         {
 19951           struct glyph_row *row;
 19952 
 19953           row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
 19954           while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos
 19955                  && !row->ends_at_zv_p)
 19956             ++row;
 19957 
 19958           TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
 19959                             MATRIX_ROW_START_BYTEPOS (row));
 19960 
 19961           if (w != XWINDOW (selected_window))
 19962             set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
 19963           else if (current_buffer == old)
 19964             SET_TEXT_POS (lpoint, PT, PT_BYTE);
 19965 
 19966           set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
 19967 
 19968           /* Re-run pre-redisplay-function so it can update the region
 19969              according to the new position of point.  */
 19970           /* Other than the cursor, w's redisplay is done so we can set its
 19971              redisplay to false.  Also the buffer's redisplay can be set to
 19972              false, since propagate_buffer_redisplay should have already
 19973              propagated its info to `w' anyway.  */
 19974           w->redisplay = false;
 19975           XBUFFER (w->contents)->text->redisplay = false;
 19976           safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
 19977 
 19978           if (w->redisplay || XBUFFER (w->contents)->text->redisplay
 19979               || ((EQ (Vdisplay_line_numbers, Qrelative)
 19980                    || EQ (Vdisplay_line_numbers, Qvisual))
 19981                   && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
 19982             {
 19983               /* Either pre-redisplay-function made changes (e.g. move
 19984                  the region), or we moved point in a window that is
 19985                  under display-line-numbers = relative mode.  We need
 19986                  another round of redisplay.  */
 19987               clear_glyph_matrix (w->desired_matrix);
 19988               if (!try_window (window, startp, 0))
 19989                 goto need_larger_matrices;
 19990             }
 19991         }
 19992       if (w->cursor.vpos < 0
 19993           || !cursor_row_fully_visible_p (w, false, false, false))
 19994         {
 19995           clear_glyph_matrix (w->desired_matrix);
 19996           goto try_to_scroll;
 19997         }
 19998 
 19999 #ifdef GLYPH_DEBUG
 20000       debug_method_add (w, "forced window start");
 20001 #endif
 20002       goto done;
 20003     }
 20004 
 20005  ignore_start:
 20006 
 20007   /* Handle case where text has not changed, only point, and it has
 20008      not moved off the frame, and we are not retrying after hscroll.
 20009      (current_matrix_up_to_date_p is true when retrying.)  */
 20010   if (current_matrix_up_to_date_p
 20011       && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
 20012           rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
 20013     {
 20014       switch (rc)
 20015         {
 20016         case CURSOR_MOVEMENT_SUCCESS:
 20017           used_current_matrix_p = true;
 20018           goto done;
 20019 
 20020         case CURSOR_MOVEMENT_MUST_SCROLL:
 20021           goto try_to_scroll;
 20022 
 20023         default:
 20024           emacs_abort ();
 20025         }
 20026     }
 20027   /* If current starting point was originally the beginning of a line
 20028      but no longer is, or if the starting point is invisible but the
 20029      buffer wants it always visible, find a new starting point.  */
 20030   else if (w->start_at_line_beg
 20031            && ((CHARPOS (startp) > BEGV
 20032                 && FETCH_BYTE (BYTEPOS (startp) - 1) != '\n')
 20033                || (CHARPOS (startp) >= BEGV
 20034                    && CHARPOS (startp) <= ZV
 20035                    && !window_start_acceptable_p (window, CHARPOS (startp)))))
 20036     {
 20037 #ifdef GLYPH_DEBUG
 20038       debug_method_add (w, "recenter 1");
 20039 #endif
 20040       goto recenter;
 20041     }
 20042 
 20043   /* Try scrolling with try_window_id.  Value is > 0 if update has
 20044      been done, it is -1 if we know that the same window start will
 20045      not work.  It is 0 if unsuccessful for some other reason.  */
 20046   else if ((tem = try_window_id (w)) != 0)
 20047     {
 20048 #ifdef GLYPH_DEBUG
 20049       debug_method_add (w, "try_window_id %d", tem);
 20050 #endif
 20051 
 20052       if (f->fonts_changed)
 20053         goto need_larger_matrices;
 20054       if (tem > 0)
 20055         goto done;
 20056 
 20057       /* Otherwise try_window_id has returned -1 which means that we
 20058          don't want the alternative below this comment to execute.  */
 20059     }
 20060   else if (CHARPOS (startp) >= BEGV
 20061            && CHARPOS (startp) <= ZV
 20062            && PT >= CHARPOS (startp)
 20063            && (CHARPOS (startp) < ZV
 20064                /* Avoid starting at end of buffer.  */
 20065                || CHARPOS (startp) == BEGV
 20066                || !window_outdated (w)))
 20067     {
 20068       int d1, d2, d5, d6;
 20069       int rtop, rbot;
 20070 
 20071       /* If first window line is a continuation line, and window start
 20072          is inside the modified region, but the first change is before
 20073          current window start, we must select a new window start.
 20074 
 20075          However, if this is the result of a down-mouse event (e.g. by
 20076          extending the mouse-drag-overlay), we don't want to select a
 20077          new window start, since that would change the position under
 20078          the mouse, resulting in an unwanted mouse-movement rather
 20079          than a simple mouse-click.  */
 20080       if (!w->start_at_line_beg
 20081           && NILP (track_mouse)
 20082           && CHARPOS (startp) > BEGV
 20083           && CHARPOS (startp) > BEG + beg_unchanged
 20084           && CHARPOS (startp) <= Z - end_unchanged
 20085           /* Even if w->start_at_line_beg is nil, a new window may
 20086              start at a line_beg, since that's how set_buffer_window
 20087              sets it.  So, we need to check the return value of
 20088              compute_window_start_on_continuation_line.  (See also
 20089              bug#197).  */
 20090           && XMARKER (w->start)->buffer == current_buffer
 20091           && compute_window_start_on_continuation_line (w)
 20092           /* It doesn't make sense to force the window start like we
 20093              do at label force_start if it is already known that point
 20094              will not be fully visible in the resulting window, because
 20095              doing so will move point from its correct position
 20096              instead of scrolling the window to bring point into view.
 20097              See bug#9324.  */
 20098           && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
 20099           /* A very tall row could need more than the window height,
 20100              in which case we accept that it is partially visible.  */
 20101           && (rtop != 0) == (rbot != 0))
 20102         {
 20103           w->force_start = true;
 20104           SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20105 #ifdef GLYPH_DEBUG
 20106           debug_method_add (w, "recomputed window start in continuation line");
 20107 #endif
 20108           goto force_start;
 20109         }
 20110 
 20111       /* Don't use the same window-start if it is invisible or covered
 20112          by a replacing 'display' property and the buffer requested
 20113          the window-start to be always visible.  */
 20114       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 20115         {
 20116 #ifdef GLYPH_DEBUG
 20117           debug_method_add (w, "recenter 2");
 20118 #endif
 20119           goto recenter;
 20120         }
 20121 
 20122 #ifdef GLYPH_DEBUG
 20123       debug_method_add (w, "same window start");
 20124 #endif
 20125 
 20126       /* Try to redisplay starting at same place as before.
 20127          If point has not moved off frame, accept the results.  */
 20128       if (!current_matrix_up_to_date_p
 20129           /* Don't use try_window_reusing_current_matrix in this case
 20130              because a window scroll function can have changed the
 20131              buffer.  */
 20132           || !NILP (Vwindow_scroll_functions)
 20133           || MINI_WINDOW_P (w)
 20134           || !(used_current_matrix_p
 20135                = try_window_reusing_current_matrix (w)))
 20136         {
 20137           IF_DEBUG (debug_method_add (w, "1"));
 20138           clear_glyph_matrix (w->desired_matrix);
 20139           if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
 20140             /* -1 means we need to scroll.
 20141                0 means we need new matrices, but fonts_changed
 20142                is set in that case, so we will detect it below.  */
 20143             goto try_to_scroll;
 20144         }
 20145 
 20146       if (f->fonts_changed)
 20147         goto need_larger_matrices;
 20148 
 20149       if (w->cursor.vpos >= 0)
 20150         {
 20151           if (!just_this_one_p
 20152               || current_buffer->clip_changed
 20153               || BEG_UNCHANGED < CHARPOS (startp))
 20154             /* Forget any recorded base line for line number display.  */
 20155             w->base_line_number = 0;
 20156 
 20157           if (!cursor_row_fully_visible_p (w, true, false, false))
 20158             {
 20159               clear_glyph_matrix (w->desired_matrix);
 20160               last_line_misfit = true;
 20161             }
 20162             /* Drop through and scroll.  */
 20163           else
 20164             goto done;
 20165         }
 20166       else
 20167         clear_glyph_matrix (w->desired_matrix);
 20168     }
 20169 
 20170  try_to_scroll:
 20171 
 20172   /* Redisplay the mode line.  Select the buffer properly for that.  */
 20173   if (!update_mode_line)
 20174     {
 20175       update_mode_line = true;
 20176       w->update_mode_line = true;
 20177     }
 20178 
 20179   /* Try to scroll by specified few lines.  */
 20180   if ((0 < scroll_conservatively
 20181        /* FIXME: the option is supposed to affect minibuffers, but we
 20182           test MINI_WINDOW_P, which can also catch uses of
 20183           mini-windows for displaying the echo area.  Do we need to
 20184           distinguish these two use cases?  */
 20185        || (scroll_minibuffer_conservatively && MINI_WINDOW_P (w))
 20186        || 0 < emacs_scroll_step
 20187        || temp_scroll_step
 20188        || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
 20189        || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
 20190       && CHARPOS (startp) >= BEGV
 20191       && CHARPOS (startp) <= ZV)
 20192     {
 20193       /* The function returns -1 if new fonts were loaded, 1 if
 20194          successful, 0 if not successful.  */
 20195       int ss = try_scrolling (window, just_this_one_p,
 20196                               ((scroll_minibuffer_conservatively
 20197                                 && MINI_WINDOW_P (w))
 20198                                ? SCROLL_LIMIT + 1
 20199                                : scroll_conservatively),
 20200                               emacs_scroll_step,
 20201                               temp_scroll_step, last_line_misfit);
 20202       switch (ss)
 20203         {
 20204         case SCROLLING_SUCCESS:
 20205           goto done;
 20206 
 20207         case SCROLLING_NEED_LARGER_MATRICES:
 20208           goto need_larger_matrices;
 20209 
 20210         case SCROLLING_FAILED:
 20211           break;
 20212 
 20213         default:
 20214           emacs_abort ();
 20215         }
 20216     }
 20217 
 20218   /* Finally, just choose a place to start which positions point
 20219      according to user preferences.  */
 20220 
 20221  recenter:
 20222 
 20223 #ifdef GLYPH_DEBUG
 20224   debug_method_add (w, "recenter");
 20225 #endif
 20226 
 20227   /* Forget any previously recorded base line for line number display.  */
 20228   if (!buffer_unchanged_p)
 20229     w->base_line_number = 0;
 20230 
 20231   /* Determine the window start relative to point.  */
 20232   init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20233   it.current_y = it.last_visible_y;
 20234   if (centering_position < 0)
 20235     {
 20236       ptrdiff_t margin_pos = CHARPOS (startp);
 20237       Lisp_Object aggressive;
 20238       bool scrolling_up;
 20239 
 20240       /* If there is a scroll margin at the top of the window, find
 20241          its character position.  */
 20242       if (margin
 20243           /* Cannot call start_display if startp is not in the
 20244              accessible region of the buffer.  This can happen when we
 20245              have just switched to a different buffer and/or changed
 20246              its restriction.  In that case, startp is initialized to
 20247              the character position 1 (BEGV) because we did not yet
 20248              have chance to display the buffer even once.  */
 20249           && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
 20250         {
 20251           struct it it1;
 20252           void *it1data = NULL;
 20253 
 20254           SAVE_IT (it1, it, it1data);
 20255           start_display (&it1, w, startp);
 20256           move_it_vertically (&it1, margin * frame_line_height);
 20257           margin_pos = IT_CHARPOS (it1);
 20258           RESTORE_IT (&it, &it, it1data);
 20259         }
 20260       scrolling_up = PT > margin_pos;
 20261       aggressive =
 20262         scrolling_up
 20263         ? BVAR (current_buffer, scroll_up_aggressively)
 20264         : BVAR (current_buffer, scroll_down_aggressively);
 20265 
 20266       if (!MINI_WINDOW_P (w)
 20267           && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
 20268         {
 20269           int pt_offset = 0;
 20270 
 20271           /* Setting scroll-conservatively overrides
 20272              scroll-*-aggressively.  */
 20273           if (!scroll_conservatively && NUMBERP (aggressive))
 20274             {
 20275               double float_amount = XFLOATINT (aggressive);
 20276 
 20277               pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
 20278               if (pt_offset == 0 && float_amount > 0)
 20279                 pt_offset = 1;
 20280               if (pt_offset && margin > 0)
 20281                 margin -= 1;
 20282             }
 20283           /* Compute how much to move the window start backward from
 20284              point so that point will be displayed where the user
 20285              wants it.  */
 20286           if (scrolling_up)
 20287             {
 20288               centering_position = it.last_visible_y;
 20289               if (pt_offset)
 20290                 centering_position -= pt_offset;
 20291               centering_position -=
 20292                 (frame_line_height * (1 + margin + last_line_misfit)
 20293                  + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 20294               /* Don't let point enter the scroll margin near top of
 20295                  the window.  */
 20296               if (centering_position < margin * frame_line_height)
 20297                 centering_position = margin * frame_line_height;
 20298             }
 20299           else
 20300             centering_position = margin * frame_line_height + pt_offset;
 20301         }
 20302       else
 20303         /* Set the window start half the height of the window backward
 20304            from point.  */
 20305         centering_position = window_box_height (w) / 2;
 20306     }
 20307   if (current_buffer->long_line_optimizations_p
 20308       && it.line_wrap == TRUNCATE)
 20309     {
 20310       /* For very long and truncated lines, go back using a simplified
 20311          method, which ignored any inaccuracies due to line-height
 20312          differences, display properties/overlays, etc.  */
 20313       int nlines = centering_position / frame_line_height;
 20314 
 20315       while (nlines-- && IT_CHARPOS (it) > BEGV)
 20316         back_to_previous_visible_line_start (&it);
 20317       reseat_1 (&it, it.current.pos, true);
 20318     }
 20319   else
 20320     move_it_vertically_backward (&it, centering_position);
 20321 
 20322   eassert (IT_CHARPOS (it) >= BEGV);
 20323 
 20324   /* The function move_it_vertically_backward may move over more
 20325      than the specified y-distance.  If it->w is small, e.g. a
 20326      mini-buffer window, we may end up in front of the window's
 20327      display area.  Start displaying at the start of the line
 20328      containing PT in this case.  */
 20329   if (it.current_y <= 0)
 20330     {
 20331       init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20332       move_it_vertically_backward (&it, 0);
 20333       it.current_y = 0;
 20334     }
 20335 
 20336   it.current_x = it.hpos = 0;
 20337 
 20338   /* Set the window start position here explicitly, to avoid an
 20339      infinite loop in case the functions in window-scroll-functions
 20340      get errors.  */
 20341   set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
 20342 
 20343   /* Run scroll hooks.  */
 20344   startp = run_window_scroll_functions (window, it.current.pos);
 20345 
 20346   /* We invoke try_window and try_window_reusing_current_matrix below,
 20347      and they manipulate the bidi cache.  Save and restore the cache
 20348      state of our iterator, so we could continue using it after that.  */
 20349   itdata = bidi_shelve_cache ();
 20350 
 20351   /* Redisplay the window.  */
 20352   use_desired_matrix = false;
 20353   if (!current_matrix_up_to_date_p
 20354       || windows_or_buffers_changed
 20355       || f->cursor_type_changed
 20356       /* Don't use try_window_reusing_current_matrix in this case
 20357          because it can have changed the buffer.  */
 20358       || !NILP (Vwindow_scroll_functions)
 20359       || !just_this_one_p
 20360       || MINI_WINDOW_P (w)
 20361       || !(used_current_matrix_p
 20362            = try_window_reusing_current_matrix (w)))
 20363     use_desired_matrix = (try_window (window, startp, 0) == 1);
 20364 
 20365   bidi_unshelve_cache (itdata, false);
 20366 
 20367   /* If new fonts have been loaded (due to fontsets), give up.  We
 20368      have to start a new redisplay since we need to re-adjust glyph
 20369      matrices.  */
 20370   if (f->fonts_changed)
 20371     goto need_larger_matrices;
 20372 
 20373   /* If cursor did not appear assume that the middle of the window is
 20374      in the first line of the window.  Do it again with the next line.
 20375      (Imagine a window of height 100, displaying two lines of height
 20376      60.  Moving back 50 from it->last_visible_y will end in the first
 20377      line.)  */
 20378   if (w->cursor.vpos < 0)
 20379     {
 20380       if (w->window_end_valid && PT >= Z - w->window_end_pos)
 20381         {
 20382           clear_glyph_matrix (w->desired_matrix);
 20383           move_it_by_lines (&it, 1);
 20384           try_window (window, it.current.pos, 0);
 20385         }
 20386       else if (PT < IT_CHARPOS (it))
 20387         {
 20388           clear_glyph_matrix (w->desired_matrix);
 20389           move_it_by_lines (&it, -1);
 20390           try_window (window, it.current.pos, 0);
 20391         }
 20392       else if (scroll_conservatively > SCROLL_LIMIT
 20393                && (it.method == GET_FROM_STRING
 20394                    || overlay_touches_p (IT_CHARPOS (it)))
 20395                && IT_CHARPOS (it) < ZV)
 20396         {
 20397           /* If the window starts with a before-string that spans more
 20398              than one screen line, using that position to display the
 20399              window might fail to bring point into the view, because
 20400              start_display will always start by displaying the string,
 20401              whereas the code above determines where to set w->start
 20402              by the buffer position of the place where it takes screen
 20403              coordinates.  Try to recover by finding the next screen
 20404              line that displays buffer text.  */
 20405           ptrdiff_t pos0 = IT_CHARPOS (it);
 20406 
 20407           clear_glyph_matrix (w->desired_matrix);
 20408           do {
 20409             move_it_by_lines (&it, 1);
 20410           } while (IT_CHARPOS (it) == pos0);
 20411           try_window (window, it.current.pos, 0);
 20412         }
 20413       else
 20414         {
 20415           /* Not much we can do about it.  */
 20416         }
 20417     }
 20418 
 20419   /* Consider the following case: Window starts at BEGV, there is
 20420      invisible, intangible text at BEGV, so that display starts at
 20421      some point START > BEGV.  It can happen that we are called with
 20422      PT somewhere between BEGV and START.  Try to handle that case,
 20423      and similar ones.  */
 20424   if (w->cursor.vpos < 0)
 20425     {
 20426       /* Prefer the desired matrix to the current matrix, if possible,
 20427          in the fallback calculations below.  This is because using
 20428          the current matrix might completely goof, e.g. if its first
 20429          row is after point.  */
 20430       struct glyph_matrix *matrix =
 20431         use_desired_matrix ? w->desired_matrix : w->current_matrix;
 20432       /* First, try locating the proper glyph row for PT.  */
 20433       struct glyph_row *row =
 20434         row_containing_pos (w, PT, matrix->rows, NULL, 0);
 20435 
 20436       /* Sometimes point is at the beginning of invisible text that is
 20437          before the 1st character displayed in the row.  In that case,
 20438          row_containing_pos fails to find the row, because no glyphs
 20439          with appropriate buffer positions are present in the row.
 20440          Therefore, we next try to find the row which shows the 1st
 20441          position after the invisible text.  */
 20442       if (!row)
 20443         {
 20444           Lisp_Object val =
 20445             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 20446                                            Qnil, NULL);
 20447 
 20448           if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 20449             {
 20450               ptrdiff_t alt_pos;
 20451               Lisp_Object invis_end =
 20452                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 20453                                                    Qnil, Qnil);
 20454 
 20455               if (FIXNATP (invis_end))
 20456                 alt_pos = XFIXNAT (invis_end);
 20457               else
 20458                 alt_pos = ZV;
 20459               row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
 20460             }
 20461         }
 20462       /* Finally, fall back on the first row of the window after the
 20463          tab-line and header line (if any).  This is slightly better
 20464          than not displaying the cursor at all.  */
 20465       if (!row)
 20466         {
 20467           row = matrix->rows;
 20468           /* Skip the tab-line and header-line rows, if any.  */
 20469           if (row->tab_line_p)
 20470             ++row;
 20471           if (row->mode_line_p)
 20472             ++row;
 20473         }
 20474       set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
 20475     }
 20476 
 20477   if (!cursor_row_fully_visible_p (w, false, false, false))
 20478     {
 20479       /* If vscroll is enabled, disable it and try again.  */
 20480       if (w->vscroll)
 20481         {
 20482           w->vscroll = 0;
 20483           clear_glyph_matrix (w->desired_matrix);
 20484           goto recenter;
 20485         }
 20486 
 20487       /* Users who set scroll-conservatively to a large number want
 20488          point just above/below the scroll margin.  If we ended up
 20489          with point's row partially visible, move the window start to
 20490          make that row fully visible and out of the margin.  */
 20491       if (scroll_conservatively > SCROLL_LIMIT)
 20492         {
 20493           int window_total_lines
 20494             = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
 20495           bool move_down = w->cursor.vpos >= window_total_lines / 2;
 20496 
 20497           move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
 20498           clear_glyph_matrix (w->desired_matrix);
 20499           if (1 == try_window (window, it.current.pos,
 20500                                TRY_WINDOW_CHECK_MARGINS))
 20501             goto done;
 20502         }
 20503 
 20504       /* If centering point failed to make the whole line visible,
 20505          put point at the top instead.  That has to make the whole line
 20506          visible, if it can be done.  */
 20507       if (centering_position == 0)
 20508         goto done;
 20509 
 20510       clear_glyph_matrix (w->desired_matrix);
 20511       centering_position = 0;
 20512       goto recenter;
 20513     }
 20514 
 20515  done:
 20516 
 20517   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20518   w->start_at_line_beg = (CHARPOS (startp) == BEGV
 20519                           || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
 20520 
 20521   /* Display the mode line, header line, and tab-line, if we must.  */
 20522   if ((update_mode_line
 20523        /* If window not full width, must redo its mode line
 20524           if (a) the window to its side is being redone and
 20525           (b) we do a frame-based redisplay.  This is a consequence
 20526           of how inverted lines are drawn in frame-based redisplay.  */
 20527        || (!just_this_one_p
 20528            && !FRAME_WINDOW_P (f)
 20529            && !WINDOW_FULL_WIDTH_P (w))
 20530        /* Line number to display.  */
 20531        || w->base_line_pos > 0
 20532        /* Column number is displayed and different from the one displayed.  */
 20533        || (w->column_number_displayed != -1
 20534            && (w->column_number_displayed != current_column ())))
 20535       /* This means that the window has a mode line.  */
 20536       && (window_wants_mode_line (w)
 20537           || window_wants_header_line (w)
 20538           || window_wants_tab_line (w)))
 20539     {
 20540       specpdl_ref count1 = SPECPDL_INDEX ();
 20541 
 20542       specbind (Qinhibit_quit, Qt);
 20543       display_mode_lines (w);
 20544       unbind_to (count1, Qnil);
 20545 
 20546       /* If mode line height has changed, arrange for a thorough
 20547          immediate redisplay using the correct mode line height.  */
 20548       if (window_wants_mode_line (w)
 20549           && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
 20550         {
 20551           f->fonts_changed = true;
 20552           w->mode_line_height = -1;
 20553           MATRIX_MODE_LINE_ROW (w->current_matrix)->height
 20554             = DESIRED_MODE_LINE_HEIGHT (w);
 20555         }
 20556 
 20557       /* If tab line height has changed, arrange for a thorough
 20558          immediate redisplay using the correct tab line height.  */
 20559       if (window_wants_tab_line (w)
 20560           && CURRENT_TAB_LINE_HEIGHT (w) != DESIRED_TAB_LINE_HEIGHT (w))
 20561         {
 20562           f->fonts_changed = true;
 20563           w->tab_line_height = -1;
 20564           MATRIX_TAB_LINE_ROW (w->current_matrix)->height
 20565             = DESIRED_TAB_LINE_HEIGHT (w);
 20566         }
 20567 
 20568       /* If header line height has changed, arrange for a thorough
 20569          immediate redisplay using the correct header line height.  */
 20570       if (window_wants_header_line (w)
 20571           && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
 20572         {
 20573           f->fonts_changed = true;
 20574           w->header_line_height = -1;
 20575           MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
 20576             = DESIRED_HEADER_LINE_HEIGHT (w);
 20577         }
 20578 
 20579       if (f->fonts_changed)
 20580         goto need_larger_matrices;
 20581     }
 20582 
 20583   if (!line_number_displayed && w->base_line_pos != -1)
 20584     {
 20585       w->base_line_pos = 0;
 20586       w->base_line_number = 0;
 20587     }
 20588 
 20589  finish_menu_bars:
 20590 
 20591   /* When we reach a frame's selected window, redo the frame's menu
 20592      bar, tool bar, tab-bar, and the frame's title.  */
 20593   if (update_mode_line
 20594       && EQ (FRAME_SELECTED_WINDOW (f), window))
 20595     {
 20596       bool redisplay_menu_p;
 20597 
 20598       if (FRAME_WINDOW_P (f))
 20599         {
 20600 #ifdef HAVE_EXT_MENU_BAR
 20601           redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
 20602 #else
 20603           redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20604 #endif
 20605         }
 20606       else
 20607         redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20608 
 20609       if (redisplay_menu_p)
 20610         display_menu_bar (w);
 20611 
 20612 #ifdef HAVE_WINDOW_SYSTEM
 20613       if (FRAME_WINDOW_P (f))
 20614         {
 20615           if (WINDOWP (f->tab_bar_window)
 20616               && (FRAME_TAB_BAR_LINES (f) > 0
 20617                   || !NILP (Vauto_resize_tab_bars))
 20618               && redisplay_tab_bar (f))
 20619             ignore_mouse_drag_p = true;
 20620 
 20621 #ifdef HAVE_EXT_TOOL_BAR
 20622           if (FRAME_EXTERNAL_TOOL_BAR (f))
 20623             update_frame_tool_bar (f);
 20624 #else
 20625           if (WINDOWP (f->tool_bar_window)
 20626               && (FRAME_TOOL_BAR_LINES (f) > 0
 20627                   || !NILP (Vauto_resize_tool_bars))
 20628               && redisplay_tool_bar (f))
 20629             ignore_mouse_drag_p = true;
 20630 #endif
 20631         }
 20632       else
 20633         {
 20634           if ((FRAME_TAB_BAR_LINES (f) > 0))
 20635             display_tab_bar (w);
 20636         }
 20637 
 20638       gui_consider_frame_title (w->frame);
 20639 #else
 20640       if ((FRAME_TAB_BAR_LINES (f) > 0))
 20641         display_tab_bar (w);
 20642 #endif
 20643     }
 20644 
 20645 #ifdef HAVE_WINDOW_SYSTEM
 20646   if (FRAME_WINDOW_P (f)
 20647       && update_window_fringes (w, (just_this_one_p
 20648                                     || (!used_current_matrix_p && !overlay_arrow_seen)
 20649                                     || w->pseudo_window_p)))
 20650     {
 20651       update_begin (f);
 20652       block_input ();
 20653       if (draw_window_fringes (w, true))
 20654         {
 20655           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 20656             gui_draw_right_divider (w);
 20657           else
 20658             gui_draw_vertical_border (w);
 20659         }
 20660       unblock_input ();
 20661       update_end (f);
 20662     }
 20663 
 20664   if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 20665     gui_draw_bottom_divider (w);
 20666 #endif /* HAVE_WINDOW_SYSTEM */
 20667 
 20668   /* We go to this label, with fonts_changed set, if it is
 20669      necessary to try again using larger glyph matrices.
 20670      We have to redeem the scroll bar even in this case,
 20671      because the loop in redisplay_internal expects that.  */
 20672  need_larger_matrices:
 20673   ;
 20674  finish_scroll_bars:
 20675 
 20676    if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20677     {
 20678       if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
 20679         /* Set the thumb's position and size.  */
 20680         set_vertical_scroll_bar (w);
 20681 
 20682       if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20683         /* Set the thumb's position and size.  */
 20684         set_horizontal_scroll_bar (w);
 20685 
 20686       /* Note that we actually used the scroll bar attached to this
 20687          window, so it shouldn't be deleted at the end of redisplay.  */
 20688       if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
 20689         (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
 20690     }
 20691 
 20692   /* Restore current_buffer and value of point in it.  The window
 20693      update may have changed the buffer, so first make sure `opoint'
 20694      is still valid (Bug#6177).  */
 20695   if (CHARPOS (opoint) < BEGV)
 20696     TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
 20697   else if (CHARPOS (opoint) > ZV)
 20698     TEMP_SET_PT_BOTH (Z, Z_BYTE);
 20699   else
 20700     TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
 20701 
 20702   set_buffer_internal_1 (old);
 20703   /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
 20704      shorter.  This can be caused by log truncation in *Messages*.  */
 20705   if (CHARPOS (lpoint) <= ZV)
 20706     TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 20707 
 20708   unbind_to (count, Qnil);
 20709 }
 20710 
 20711 
 20712 /* Build the complete desired matrix of WINDOW with a window start
 20713    buffer position POS.
 20714 
 20715    Value is 1 if successful.  It is zero if fonts were loaded during
 20716    redisplay or the dimensions of the desired matrix were found
 20717    insufficient, which makes re-adjusting glyph matrices necessary.
 20718    Value is -1 if point would appear in the scroll margins.  (We check
 20719    the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is unset in
 20720    FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is set in
 20721    FLAGS.)
 20722 
 20723    Note that 'x-show-tip' invokes this function in a special way, and
 20724    in that case the return value of zero doesn't necessarily mean the
 20725    glyph matrices need to be re-adjusted, if the entire text of the
 20726    tooltip was processed and has its glyphs in the matrix's glyph
 20727    rows, i.e. if the dimensions of the matrix were found insufficient
 20728    while producing empty glyph rows beyond ZV.  */
 20729 
 20730 int
 20731 try_window (Lisp_Object window, struct text_pos pos, int flags)
 20732 {
 20733   struct window *w = XWINDOW (window);
 20734   struct it it;
 20735   struct glyph_row *last_text_row = NULL;
 20736   struct frame *f = XFRAME (w->frame);
 20737   int cursor_vpos = w->cursor.vpos;
 20738 
 20739   /* Make POS the new window start.  */
 20740   set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
 20741 
 20742   /* Mark cursor position as unknown.  No overlay arrow seen.  */
 20743   w->cursor.vpos = -1;
 20744   overlay_arrow_seen = false;
 20745 
 20746   /* Initialize iterator and info to start at POS.  */
 20747   start_display (&it, w, pos);
 20748   it.glyph_row->reversed_p = false;
 20749 
 20750   /* Display all lines of W.  */
 20751   while (it.current_y < it.last_visible_y)
 20752     {
 20753       int last_row_scale = it.w->nrows_scale_factor;
 20754       int last_col_scale = it.w->ncols_scale_factor;
 20755       if (display_line (&it, cursor_vpos))
 20756         last_text_row = it.glyph_row - 1;
 20757       if (f->fonts_changed
 20758           && !((flags & TRY_WINDOW_IGNORE_FONTS_CHANGE)
 20759                /* If the matrix dimensions are insufficient, we _must_
 20760                   fail and let dispnew.c reallocate the matrix.  */
 20761                && last_row_scale == it.w->nrows_scale_factor
 20762                && last_col_scale == it.w->ncols_scale_factor))
 20763         return 0;
 20764     }
 20765 
 20766   /* Save the character position of 'it' before we call
 20767      'start_display' again.  */
 20768   ptrdiff_t it_charpos = IT_CHARPOS (it);
 20769 
 20770   /* Don't let the cursor end in the scroll margins.  However, when
 20771      the window is vscrolled, we leave it to vscroll to handle the
 20772      margins, see window_scroll_pixel_based.  */
 20773   if ((flags & TRY_WINDOW_CHECK_MARGINS)
 20774       && w->vscroll == 0
 20775       && !MINI_WINDOW_P (w))
 20776     {
 20777       int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 20778       int bot_scroll_margin = top_scroll_margin;
 20779       if (window_wants_header_line (w))
 20780         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 20781       if (window_wants_tab_line (w))
 20782         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 20783       start_display (&it, w, pos);
 20784 
 20785       if ((w->cursor.y >= 0
 20786            && w->cursor.y < top_scroll_margin
 20787            && CHARPOS (pos) > BEGV)
 20788           /* rms: considering make_cursor_line_fully_visible_p here
 20789              seems to give wrong results.  We don't want to recenter
 20790              when the last line is partly visible, we want to allow
 20791              that case to be handled in the usual way.  */
 20792           || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
 20793                             - bot_scroll_margin - 1))
 20794         {
 20795           w->cursor.vpos = -1;
 20796           clear_glyph_matrix (w->desired_matrix);
 20797           return -1;
 20798         }
 20799     }
 20800 
 20801   /* If bottom moved off end of frame, change mode line percentage.  */
 20802   if (w->window_end_pos <= 0 && Z != it_charpos)
 20803     w->update_mode_line = true;
 20804 
 20805   /* Set window_end_pos to the offset of the last character displayed
 20806      on the window from the end of current_buffer.  Set
 20807      window_end_vpos to its row number.  */
 20808   if (last_text_row)
 20809     {
 20810       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
 20811       adjust_window_ends (w, last_text_row, false);
 20812       eassert
 20813         (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
 20814                                                  w->window_end_vpos)));
 20815     }
 20816   else
 20817     {
 20818       w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 20819       w->window_end_pos = Z - ZV;
 20820       w->window_end_vpos = 0;
 20821     }
 20822 
 20823   /* But that is not valid info until redisplay finishes.  */
 20824   w->window_end_valid = false;
 20825   return 1;
 20826 }
 20827 
 20828 
 20829 
 20830 /************************************************************************
 20831     Window redisplay reusing current matrix when buffer has not changed
 20832  ************************************************************************/
 20833 
 20834 /* Try redisplay of window W showing an unchanged buffer with a
 20835    different window start than the last time it was displayed by
 20836    reusing its current matrix.  Value is true if successful.
 20837    W->start is the new window start.  */
 20838 
 20839 static bool
 20840 try_window_reusing_current_matrix (struct window *w)
 20841 {
 20842   struct frame *f = XFRAME (w->frame);
 20843   struct glyph_row *bottom_row;
 20844   struct it it;
 20845   struct run run;
 20846   struct text_pos start, new_start;
 20847   int nrows_scrolled, i;
 20848   struct glyph_row *last_text_row;
 20849   struct glyph_row *last_reused_text_row;
 20850   struct glyph_row *start_row;
 20851   int start_vpos, min_y, max_y;
 20852 
 20853 #ifdef GLYPH_DEBUG
 20854   if (inhibit_try_window_reusing)
 20855     return false;
 20856 #endif
 20857 
 20858   if (/* This function doesn't handle terminal frames.  */
 20859       !FRAME_WINDOW_P (f)
 20860       /* Don't try to reuse the display if windows have been split
 20861          or such.  */
 20862       || windows_or_buffers_changed
 20863       || f->cursor_type_changed
 20864       /* This function cannot handle buffers where the overlay arrow
 20865          is shown on the fringes, because if the arrow position
 20866          changes, we cannot just reuse the current matrix.  */
 20867       || overlay_arrow_in_current_buffer_p ())
 20868     return false;
 20869 
 20870   /* Can't do this if showing trailing whitespace.  */
 20871   if (!NILP (Vshow_trailing_whitespace))
 20872     return false;
 20873 
 20874   /* If top-line visibility has changed, give up.  */
 20875   if (window_wants_tab_line (w)
 20876       != MATRIX_TAB_LINE_ROW (w->current_matrix)->mode_line_p)
 20877     return false;
 20878 
 20879   /* If top-line visibility has changed, give up.  */
 20880   if (window_wants_header_line (w)
 20881       != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
 20882     return false;
 20883 
 20884   /* Give up if old or new display is scrolled vertically.  We could
 20885      make this function handle this, but right now it doesn't.  */
 20886   start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 20887   if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
 20888     return false;
 20889 
 20890   /* Clear the desired matrix for the display below.  */
 20891   clear_glyph_matrix (w->desired_matrix);
 20892 
 20893   /* Give up if line numbers are being displayed, because reusing the
 20894      current matrix might use the wrong width for line-number
 20895      display.  */
 20896   if (!NILP (Vdisplay_line_numbers))
 20897     return false;
 20898 
 20899   /* Can't scroll the display of w32 GUI frames when position of point
 20900      is indicated by the system caret, because scrolling the display
 20901      will then "copy" the pixels used by the caret.  */
 20902 #ifdef HAVE_NTGUI
 20903   if (w32_use_visible_system_caret)
 20904     return false;
 20905 #endif
 20906 
 20907   /* The variable new_start now holds the new window start.  The old
 20908      start `start' can be determined from the current matrix.  */
 20909   SET_TEXT_POS_FROM_MARKER (new_start, w->start);
 20910   start = start_row->minpos;
 20911   start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 20912 
 20913   if (CHARPOS (new_start) <= CHARPOS (start))
 20914     {
 20915       /* Don't use this method if the display starts with an ellipsis
 20916          displayed for invisible text.  It's not easy to handle that case
 20917          below, and it's certainly not worth the effort since this is
 20918          not a frequent case.  */
 20919       if (in_ellipses_for_invisible_text_p (&start_row->start, w))
 20920         return false;
 20921 
 20922       IF_DEBUG (debug_method_add (w, "twu1"));
 20923 
 20924       /* Display up to a row that can be reused.  The variable
 20925          last_text_row is set to the last row displayed that displays
 20926          text.  Note that it.vpos == 0 if or if not there is a
 20927          header-line; it's not the same as the MATRIX_ROW_VPOS!  */
 20928       start_display (&it, w, new_start);
 20929       w->cursor.vpos = -1;
 20930       last_text_row = last_reused_text_row = NULL;
 20931 
 20932       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 20933         {
 20934           /* If we have reached into the characters in the START row,
 20935              that means the line boundaries have changed.  So we
 20936              can't start copying with the row START.  Maybe it will
 20937              work to start copying with the following row.  */
 20938           while (IT_CHARPOS (it) > CHARPOS (start))
 20939             {
 20940               /* Advance to the next row as the "start".  */
 20941               start_row++;
 20942               start = start_row->minpos;
 20943               /* If there are no more rows to try, or just one, give up.  */
 20944               if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
 20945                   || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
 20946                   || CHARPOS (start) == ZV)
 20947                 {
 20948                   clear_glyph_matrix (w->desired_matrix);
 20949                   return false;
 20950                 }
 20951 
 20952               start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 20953             }
 20954           /* If we have reached alignment, we can copy the rest of the
 20955              rows.  */
 20956           if (IT_CHARPOS (it) == CHARPOS (start)
 20957               /* Don't accept "alignment" inside a display vector,
 20958                  since start_row could have started in the middle of
 20959                  that same display vector (thus their character
 20960                  positions match), and we have no way of telling if
 20961                  that is the case.  */
 20962               && it.current.dpvec_index < 0)
 20963             break;
 20964 
 20965           it.glyph_row->reversed_p = false;
 20966           if (display_line (&it, -1))
 20967             last_text_row = it.glyph_row - 1;
 20968 
 20969         }
 20970 
 20971       /* A value of current_y < last_visible_y means that we stopped
 20972          at the previous window start, which in turn means that we
 20973          have at least one reusable row.  */
 20974       if (it.current_y < it.last_visible_y)
 20975         {
 20976           struct glyph_row *row;
 20977 
 20978           /* IT.vpos always starts from 0; it counts text lines.  */
 20979           nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
 20980 
 20981           /* Find PT if not already found in the lines displayed.  */
 20982           if (w->cursor.vpos < 0)
 20983             {
 20984               int dy = it.current_y - start_row->y;
 20985 
 20986               row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 20987               row = row_containing_pos (w, PT, row, NULL, dy);
 20988               if (row)
 20989                 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
 20990                                      dy, nrows_scrolled);
 20991               else
 20992                 {
 20993                   clear_glyph_matrix (w->desired_matrix);
 20994                   return false;
 20995                 }
 20996             }
 20997 
 20998           /* Scroll the display.  Do it before the current matrix is
 20999              changed.  The problem here is that update has not yet
 21000              run, i.e. part of the current matrix is not up to date.
 21001              scroll_run_hook will clear the cursor, and use the
 21002              current matrix to get the height of the row the cursor is
 21003              in.  */
 21004           run.current_y = start_row->y;
 21005           run.desired_y = it.current_y;
 21006           run.height = it.last_visible_y - it.current_y;
 21007 
 21008           if (run.height > 0 && run.current_y != run.desired_y)
 21009             {
 21010 #ifdef HAVE_WINDOW_SYSTEM
 21011               update_begin (f);
 21012               gui_update_window_begin (w);
 21013               FRAME_RIF (f)->clear_window_mouse_face (w);
 21014               FRAME_RIF (f)->scroll_run_hook (w, &run);
 21015               gui_update_window_end (w, false, false);
 21016               update_end (f);
 21017 #endif
 21018             }
 21019 
 21020           /* Shift current matrix down by nrows_scrolled lines.  */
 21021           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21022           rotate_matrix (w->current_matrix,
 21023                          start_vpos,
 21024                          MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21025                          nrows_scrolled);
 21026 
 21027           /* Disable lines that must be updated.  */
 21028           for (i = 0; i < nrows_scrolled; ++i)
 21029             (start_row + i)->enabled_p = false;
 21030 
 21031           /* Re-compute Y positions.  */
 21032           min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21033           max_y = it.last_visible_y;
 21034           for (row = start_row + nrows_scrolled;
 21035                row < bottom_row;
 21036                ++row)
 21037             {
 21038               row->y = it.current_y;
 21039               row->visible_height = row->height;
 21040 
 21041               if (row->y < min_y)
 21042                 row->visible_height -= min_y - row->y;
 21043               if (row->y + row->height > max_y)
 21044                 row->visible_height -= row->y + row->height - max_y;
 21045               if (row->fringe_bitmap_periodic_p)
 21046                 row->redraw_fringe_bitmaps_p = true;
 21047 
 21048               it.current_y += row->height;
 21049 
 21050               if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21051                 last_reused_text_row = row;
 21052               if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
 21053                 break;
 21054             }
 21055 
 21056           /* Disable lines in the current matrix which are now
 21057              below the window.  */
 21058           for (++row; row < bottom_row; ++row)
 21059             row->enabled_p = row->mode_line_p = row->tab_line_p = false;
 21060         }
 21061 
 21062       /* Update window_end_pos etc.; last_reused_text_row is the last
 21063          reused row from the current matrix containing text, if any.
 21064          The value of last_text_row is the last displayed line
 21065          containing text.  */
 21066       if (last_reused_text_row)
 21067         adjust_window_ends (w, last_reused_text_row, true);
 21068       else if (last_text_row)
 21069         adjust_window_ends (w, last_text_row, false);
 21070       else
 21071         {
 21072           /* This window must be completely empty.  */
 21073           w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 21074           w->window_end_pos = Z - ZV;
 21075           w->window_end_vpos = 0;
 21076         }
 21077       w->window_end_valid = false;
 21078 
 21079       /* Update hint: don't try scrolling again in update_window.  */
 21080       w->desired_matrix->no_scrolling_p = true;
 21081 
 21082 #ifdef GLYPH_DEBUG
 21083       debug_method_add (w, "try_window_reusing_current_matrix 1");
 21084 #endif
 21085       return true;
 21086     }
 21087   else if (CHARPOS (new_start) > CHARPOS (start))
 21088     {
 21089       struct glyph_row *pt_row, *row;
 21090       struct glyph_row *first_reusable_row;
 21091       struct glyph_row *first_row_to_display;
 21092       int dy;
 21093       int yb = window_text_bottom_y (w);
 21094 
 21095       /* Find the row starting at new_start, if there is one.  Don't
 21096          reuse a partially visible line at the end.  */
 21097       first_reusable_row = start_row;
 21098       while (first_reusable_row->enabled_p
 21099              && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
 21100              && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21101                  < CHARPOS (new_start)))
 21102         ++first_reusable_row;
 21103 
 21104       /* Give up if there is no row to reuse.  */
 21105       if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
 21106           || !first_reusable_row->enabled_p
 21107           || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21108               != CHARPOS (new_start)))
 21109         return false;
 21110 
 21111       /* We can reuse fully visible rows beginning with
 21112          first_reusable_row to the end of the window.  Set
 21113          first_row_to_display to the first row that cannot be reused.
 21114          Set pt_row to the row containing point, if there is any.  */
 21115       pt_row = NULL;
 21116       for (first_row_to_display = first_reusable_row;
 21117            MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
 21118            ++first_row_to_display)
 21119         {
 21120           if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
 21121               && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21122                   || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21123                       && first_row_to_display->ends_at_zv_p
 21124                       && pt_row == NULL)))
 21125             pt_row = first_row_to_display;
 21126         }
 21127 
 21128       if (first_row_to_display->y >= yb)
 21129         return false;
 21130 
 21131       /* Start displaying at the start of first_row_to_display.  */
 21132       init_to_row_start (&it, w, first_row_to_display);
 21133 
 21134       nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
 21135                         - start_vpos);
 21136       it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
 21137                  - nrows_scrolled);
 21138       it.current_y = (first_row_to_display->y - first_reusable_row->y
 21139                       + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 21140 
 21141       /* Display lines beginning with first_row_to_display in the
 21142          desired matrix.  Set last_text_row to the last row displayed
 21143          that displays text.  */
 21144       it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
 21145       if (pt_row == NULL)
 21146         w->cursor.vpos = -1;
 21147       last_text_row = NULL;
 21148       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 21149         if (display_line (&it, w->cursor.vpos))
 21150           last_text_row = it.glyph_row - 1;
 21151 
 21152       /* If point is in a reused row, adjust y and vpos of the cursor
 21153          position.  */
 21154       if (pt_row)
 21155         {
 21156           w->cursor.vpos -= nrows_scrolled;
 21157           w->cursor.y -= first_reusable_row->y - start_row->y;
 21158         }
 21159 
 21160       /* Give up if point isn't in a row displayed or reused.  (This
 21161          also handles the case where w->cursor.vpos < nrows_scrolled
 21162          after the calls to display_line, which can happen with scroll
 21163          margins.  See bug#1295.)  */
 21164       if (w->cursor.vpos < 0)
 21165         {
 21166           clear_glyph_matrix (w->desired_matrix);
 21167           return false;
 21168         }
 21169 
 21170       /* Scroll the display.  */
 21171       run.current_y = first_reusable_row->y;
 21172       run.desired_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21173       run.height = it.last_visible_y - run.current_y;
 21174       dy = run.current_y - run.desired_y;
 21175 
 21176       if (run.height)
 21177         {
 21178 #ifdef HAVE_WINDOW_SYSTEM
 21179           update_begin (f);
 21180           gui_update_window_begin (w);
 21181           FRAME_RIF (f)->clear_window_mouse_face (w);
 21182           FRAME_RIF (f)->scroll_run_hook (w, &run);
 21183           gui_update_window_end (w, false, false);
 21184           update_end (f);
 21185 #endif
 21186         }
 21187 
 21188       /* Adjust Y positions of reused rows.  */
 21189       bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21190       min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21191       max_y = it.last_visible_y;
 21192       for (row = first_reusable_row; row < first_row_to_display; ++row)
 21193         {
 21194           row->y -= dy;
 21195           row->visible_height = row->height;
 21196           if (row->y < min_y)
 21197             row->visible_height -= min_y - row->y;
 21198           if (row->y + row->height > max_y)
 21199             row->visible_height -= row->y + row->height - max_y;
 21200           if (row->fringe_bitmap_periodic_p)
 21201             row->redraw_fringe_bitmaps_p = true;
 21202         }
 21203 
 21204       /* Scroll the current matrix.  */
 21205       eassert (nrows_scrolled > 0);
 21206       rotate_matrix (w->current_matrix,
 21207                      start_vpos,
 21208                      MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21209                      -nrows_scrolled);
 21210 
 21211       /* Disable rows not reused.  */
 21212       for (row -= nrows_scrolled; row < bottom_row; ++row)
 21213         row->enabled_p = false;
 21214 
 21215       /* Point may have moved to a different line, so we cannot assume that
 21216          the previous cursor position is valid; locate the correct row.  */
 21217       if (pt_row)
 21218         {
 21219           for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 21220                row < bottom_row
 21221                  && PT >= MATRIX_ROW_END_CHARPOS (row)
 21222                  && !row->ends_at_zv_p;
 21223                row++)
 21224             {
 21225               w->cursor.vpos++;
 21226               w->cursor.y = row->y;
 21227             }
 21228           if (row < bottom_row)
 21229             {
 21230               /* Can't simply scan the row for point with
 21231                  bidi-reordered glyph rows.  Let set_cursor_from_row
 21232                  figure out where to put the cursor, and if it fails,
 21233                  give up.  */
 21234               if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 21235                 {
 21236                   if (!set_cursor_from_row (w, row, w->current_matrix,
 21237                                             0, 0, 0, 0))
 21238                     {
 21239                       clear_glyph_matrix (w->desired_matrix);
 21240                       return false;
 21241                     }
 21242                 }
 21243               else
 21244                 {
 21245                   struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
 21246                   struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21247 
 21248                   for (; glyph < end
 21249                          && (!BUFFERP (glyph->object)
 21250                              || glyph->charpos < PT);
 21251                        glyph++)
 21252                     {
 21253                       w->cursor.hpos++;
 21254                       w->cursor.x += glyph->pixel_width;
 21255                     }
 21256                 }
 21257             }
 21258         }
 21259 
 21260       /* Adjust window end.  A null value of last_text_row means that
 21261          the window end is in reused rows which in turn means that
 21262          only its vpos can have changed.  */
 21263       if (last_text_row)
 21264         adjust_window_ends (w, last_text_row, false);
 21265       else
 21266         w->window_end_vpos -= nrows_scrolled;
 21267 
 21268       w->window_end_valid = false;
 21269       w->desired_matrix->no_scrolling_p = true;
 21270 
 21271 #ifdef GLYPH_DEBUG
 21272       debug_method_add (w, "try_window_reusing_current_matrix 2");
 21273 #endif
 21274       return true;
 21275     }
 21276 
 21277   return false;
 21278 }
 21279 
 21280 
 21281 
 21282 /************************************************************************
 21283    Window redisplay reusing current matrix when buffer has changed
 21284  ************************************************************************/
 21285 
 21286 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
 21287 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
 21288                                                           ptrdiff_t *, ptrdiff_t *);
 21289 static struct glyph_row *
 21290 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
 21291                                struct glyph_row *);
 21292 
 21293 
 21294 /* Return the last row in MATRIX displaying text.  If row START is
 21295    non-null, start searching with that row.  IT gives the dimensions
 21296    of the display.  Value is null if matrix is empty; otherwise it is
 21297    a pointer to the row found.  */
 21298 
 21299 static struct glyph_row *
 21300 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
 21301                                struct glyph_row *start)
 21302 {
 21303   struct glyph_row *row, *row_found;
 21304 
 21305   /* Set row_found to the last row in IT->w's current matrix
 21306      displaying text.  The loop looks funny but think of partially
 21307      visible lines.  */
 21308   row_found = NULL;
 21309   row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
 21310   while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21311     {
 21312       eassert (row->enabled_p);
 21313       row_found = row;
 21314       if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
 21315         break;
 21316       ++row;
 21317     }
 21318 
 21319   return row_found;
 21320 }
 21321 
 21322 
 21323 /* Return the last row in the current matrix of W that is not affected
 21324    by changes at the start of current_buffer that occurred since W's
 21325    current matrix was built.  Value is null if no such row exists.
 21326 
 21327    BEG_UNCHANGED is the number of characters unchanged at the start of
 21328    current_buffer.  BEG + BEG_UNCHANGED is the buffer position of the
 21329    first changed character in current_buffer.  Characters at positions <
 21330    BEG + BEG_UNCHANGED are at the same buffer positions as they were
 21331    when the current matrix was built.  */
 21332 
 21333 static struct glyph_row *
 21334 find_last_unchanged_at_beg_row (struct window *w)
 21335 {
 21336   ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
 21337   struct glyph_row *row;
 21338   struct glyph_row *row_found = NULL;
 21339   int yb = window_text_bottom_y (w);
 21340 
 21341   /* Find the last row displaying unchanged text.  */
 21342   for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21343        MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21344          && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
 21345        ++row)
 21346     {
 21347       if (/* If row ends before first_changed_pos, it is unchanged,
 21348              except in some case.  */
 21349           MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
 21350           /* When row ends in ZV and we write at ZV it is not
 21351              unchanged.  */
 21352           && !row->ends_at_zv_p
 21353           /* When first_changed_pos is the end of a continued line,
 21354              row is not unchanged because it may be no longer
 21355              continued.  */
 21356           && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
 21357                && (row->continued_p
 21358                    || row->exact_window_width_line_p))
 21359           /* If ROW->end is beyond ZV, then ROW->end is outdated and
 21360              needs to be recomputed, so don't consider this row as
 21361              unchanged.  This happens when the last line was
 21362              bidi-reordered and was killed immediately before this
 21363              redisplay cycle.  In that case, ROW->end stores the
 21364              buffer position of the first visual-order character of
 21365              the killed text, which is now beyond ZV.  */
 21366           && CHARPOS (row->end.pos) <= ZV)
 21367         row_found = row;
 21368 
 21369       /* Stop if last visible row.  */
 21370       if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
 21371         break;
 21372     }
 21373 
 21374   return row_found;
 21375 }
 21376 
 21377 
 21378 /* Find the first glyph row in the current matrix of W that is not
 21379    affected by changes at the end of current_buffer since the
 21380    time W's current matrix was built.
 21381 
 21382    Return in *DELTA the number of chars by which buffer positions in
 21383    unchanged text at the end of current_buffer must be adjusted.
 21384 
 21385    Return in *DELTA_BYTES the corresponding number of bytes.
 21386 
 21387    Value is null if no such row exists, i.e. all rows are affected by
 21388    changes.  */
 21389 
 21390 static struct glyph_row *
 21391 find_first_unchanged_at_end_row (struct window *w,
 21392                                  ptrdiff_t *delta, ptrdiff_t *delta_bytes)
 21393 {
 21394   struct glyph_row *row;
 21395   struct glyph_row *row_found = NULL;
 21396 
 21397   *delta = *delta_bytes = 0;
 21398 
 21399   /* Display must not have been paused, otherwise the current matrix
 21400      is not up to date.  */
 21401   eassert (w->window_end_valid);
 21402 
 21403   /* A value of window_end_pos >= END_UNCHANGED means that the window
 21404      end is in the range of changed text.  If so, there is no
 21405      unchanged row at the end of W's current matrix.  */
 21406   if (w->window_end_pos >= END_UNCHANGED)
 21407     return NULL;
 21408 
 21409   /* Set row to the last row in W's current matrix displaying text.  */
 21410   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21411 
 21412   /* If matrix is entirely empty, no unchanged row exists.  */
 21413   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21414     {
 21415       /* The value of row is the last glyph row in the matrix having a
 21416          meaningful buffer position in it.  The end position of row
 21417          corresponds to window_end_pos.  This allows us to translate
 21418          buffer positions in the current matrix to current buffer
 21419          positions for characters not in changed text.  */
 21420       ptrdiff_t Z_old =
 21421         MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21422       ptrdiff_t Z_BYTE_old =
 21423         MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21424       ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
 21425       struct glyph_row *first_text_row
 21426         = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21427 
 21428       *delta = Z - Z_old;
 21429       *delta_bytes = Z_BYTE - Z_BYTE_old;
 21430 
 21431       /* Set last_unchanged_pos to the buffer position of the last
 21432          character in the buffer that has not been changed.  Z is the
 21433          index + 1 of the last character in current_buffer, i.e. by
 21434          subtracting END_UNCHANGED we get the index of the last
 21435          unchanged character, and we have to add BEG to get its buffer
 21436          position.  */
 21437       last_unchanged_pos = Z - END_UNCHANGED + BEG;
 21438       last_unchanged_pos_old = last_unchanged_pos - *delta;
 21439 
 21440       /* Search backward from ROW for a row displaying a line that
 21441          starts at a minimum position >= last_unchanged_pos_old.  */
 21442       for (; row > first_text_row; --row)
 21443         {
 21444           /* This used to abort, but it can happen.
 21445              It is ok to just stop the search instead here.  KFS.  */
 21446           if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21447             break;
 21448 
 21449           if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
 21450             row_found = row;
 21451         }
 21452     }
 21453 
 21454   eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
 21455 
 21456   return row_found;
 21457 }
 21458 
 21459 
 21460 /* Make sure that glyph rows in the current matrix of window W
 21461    reference the same glyph memory as corresponding rows in the
 21462    frame's frame matrix.  This function is called after scrolling W's
 21463    current matrix on a terminal frame in try_window_id and
 21464    try_window_reusing_current_matrix.  */
 21465 
 21466 static void
 21467 sync_frame_with_window_matrix_rows (struct window *w)
 21468 {
 21469   struct frame *f = XFRAME (w->frame);
 21470   struct glyph_row *window_row, *window_row_end, *frame_row;
 21471 
 21472   /* Preconditions: W must be a leaf window and full-width.  Its frame
 21473      must have a frame matrix.  */
 21474   eassert (BUFFERP (w->contents));
 21475   eassert (WINDOW_FULL_WIDTH_P (w));
 21476   eassert (!FRAME_WINDOW_P (f));
 21477 
 21478   /* If W is a full-width window, glyph pointers in W's current matrix
 21479      have, by definition, to be the same as glyph pointers in the
 21480      corresponding frame matrix.  Note that frame matrices have no
 21481      marginal areas (see build_frame_matrix).  */
 21482   window_row = w->current_matrix->rows;
 21483   window_row_end = window_row + w->current_matrix->nrows;
 21484   frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
 21485   while (window_row < window_row_end)
 21486     {
 21487       struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
 21488       struct glyph *end = window_row->glyphs[LAST_AREA];
 21489 
 21490       frame_row->glyphs[LEFT_MARGIN_AREA] = start;
 21491       frame_row->glyphs[TEXT_AREA] = start;
 21492       frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
 21493       frame_row->glyphs[LAST_AREA] = end;
 21494 
 21495       /* Disable frame rows whose corresponding window rows have
 21496          been disabled in try_window_id.  */
 21497       if (!window_row->enabled_p)
 21498         frame_row->enabled_p = false;
 21499 
 21500       ++window_row, ++frame_row;
 21501     }
 21502 }
 21503 
 21504 
 21505 /* Find the glyph row in window W containing CHARPOS.  Consider all
 21506    rows between START and END (not inclusive).  END null means search
 21507    all rows to the end of the display area of W.  Value is the row
 21508    containing CHARPOS or null.  */
 21509 
 21510 struct glyph_row *
 21511 row_containing_pos (struct window *w, ptrdiff_t charpos,
 21512                     struct glyph_row *start, struct glyph_row *end, int dy)
 21513 {
 21514   struct glyph_row *row = start;
 21515   struct glyph_row *best_row = NULL;
 21516   ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
 21517   int last_y;
 21518 
 21519   /* If we happen to start on a header-line or a tab-line, skip that.  */
 21520   if (row->tab_line_p)
 21521     ++row;
 21522   if (row->mode_line_p)
 21523     ++row;
 21524 
 21525   if ((end && row >= end) || !row->enabled_p)
 21526     return NULL;
 21527 
 21528   last_y = window_text_bottom_y (w) - dy;
 21529 
 21530   while (true)
 21531     {
 21532       /* Give up if we have gone too far.  */
 21533       if ((end && row >= end) || !row->enabled_p)
 21534         return NULL;
 21535       /* This formerly returned if they were equal.
 21536          I think that both quantities are of a "last plus one" type;
 21537          if so, when they are equal, the row is within the screen. -- rms.  */
 21538       if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
 21539         return NULL;
 21540 
 21541       /* If it is in this row, return this row.  */
 21542       if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
 21543              || (MATRIX_ROW_END_CHARPOS (row) == charpos
 21544                  /* The end position of a row equals the start
 21545                     position of the next row.  If CHARPOS is there, we
 21546                     would rather consider it displayed in the next
 21547                     line, except when this line ends in ZV.  */
 21548                  && !row_for_charpos_p (row, charpos)))
 21549           && charpos >= MATRIX_ROW_START_CHARPOS (row))
 21550         {
 21551           struct glyph *g;
 21552 
 21553           if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21554               || (!best_row && !row->continued_p))
 21555             return row;
 21556           /* In bidi-reordered rows, there could be several rows whose
 21557              edges surround CHARPOS, all of these rows belonging to
 21558              the same continued line.  We need to find the row which
 21559              fits CHARPOS the best.  */
 21560           for (g = row->glyphs[TEXT_AREA];
 21561                g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21562                g++)
 21563             {
 21564               if (!STRINGP (g->object))
 21565                 {
 21566                   if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
 21567                     {
 21568                       mindif = eabs (g->charpos - charpos);
 21569                       best_row = row;
 21570                       /* Exact match always wins.  */
 21571                       if (mindif == 0)
 21572                         return best_row;
 21573                     }
 21574                 }
 21575             }
 21576         }
 21577       else if (best_row && !row->continued_p)
 21578         return best_row;
 21579       ++row;
 21580     }
 21581 }
 21582 
 21583 
 21584 /* Try to redisplay window W by reusing its existing display.  W's
 21585    current matrix must be up to date when this function is called,
 21586    i.e., window_end_valid must be true.
 21587 
 21588    Value is
 21589 
 21590    >= 1 if successful, i.e. display has been updated
 21591          specifically:
 21592          1 means the changes were in front of a newline that precedes
 21593            the window start, and the whole current matrix was reused
 21594          2 means the changes were after the last position displayed
 21595            in the window, and the whole current matrix was reused
 21596          3 means portions of the current matrix were reused, while
 21597            some of the screen lines were redrawn
 21598    -1   if redisplay with same window start is known not to succeed
 21599    0    if otherwise unsuccessful
 21600 
 21601    The following steps are performed:
 21602 
 21603    1. Find the last row in the current matrix of W that is not
 21604    affected by changes at the start of current_buffer.  If no such row
 21605    is found, give up.
 21606 
 21607    2. Find the first row in W's current matrix that is not affected by
 21608    changes at the end of current_buffer.  Maybe there is no such row.
 21609 
 21610    3. Display lines beginning with the row + 1 found in step 1 to the
 21611    row found in step 2 or, if step 2 didn't find a row, to the end of
 21612    the window.
 21613 
 21614    4. If cursor is not known to appear on the window, give up.
 21615 
 21616    5. If display stopped at the row found in step 2, scroll the
 21617    display and current matrix as needed.
 21618 
 21619    6. Maybe display some lines at the end of W, if we must.  This can
 21620    happen under various circumstances, like a partially visible line
 21621    becoming fully visible, or because newly displayed lines are displayed
 21622    in smaller font sizes.
 21623 
 21624    7. Update W's window end information.  */
 21625 
 21626 static int
 21627 try_window_id (struct window *w)
 21628 {
 21629   struct frame *f = XFRAME (w->frame);
 21630   struct glyph_matrix *current_matrix = w->current_matrix;
 21631   struct glyph_matrix *desired_matrix = w->desired_matrix;
 21632   struct glyph_row *last_unchanged_at_beg_row;
 21633   struct glyph_row *first_unchanged_at_end_row;
 21634   struct glyph_row *row;
 21635   struct glyph_row *bottom_row;
 21636   int bottom_vpos;
 21637   struct it it;
 21638   ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
 21639   int dvpos, dy;
 21640   struct text_pos start_pos;
 21641   struct run run;
 21642   int first_unchanged_at_end_vpos = 0;
 21643   struct glyph_row *last_text_row, *last_text_row_at_end;
 21644   struct text_pos start;
 21645   ptrdiff_t first_changed_charpos, last_changed_charpos;
 21646 
 21647 #ifdef GLYPH_DEBUG
 21648   if (inhibit_try_window_id)
 21649     return 0;
 21650 #endif
 21651 
 21652   /* This is handy for debugging.  */
 21653 #if false
 21654 #define GIVE_UP(X)                                              \
 21655   do {                                                          \
 21656     redisplay_trace ("try_window_id give up %d\n", X);          \
 21657     return 0;                                                   \
 21658   } while (false)
 21659 #else
 21660 #define GIVE_UP(X) return 0
 21661 #endif
 21662 
 21663   SET_TEXT_POS_FROM_MARKER (start, w->start);
 21664 
 21665   /* Don't use this for mini-windows because these can show
 21666      messages and mini-buffers, and we don't handle that here.  */
 21667   if (MINI_WINDOW_P (w))
 21668     GIVE_UP (1);
 21669 
 21670   /* This flag is used to prevent redisplay optimizations.  */
 21671   if (windows_or_buffers_changed || f->cursor_type_changed)
 21672     GIVE_UP (2);
 21673 
 21674   /* This function's optimizations cannot be used if overlays have
 21675      changed in the buffer displayed by the window, so give up if they
 21676      have.  */
 21677   if (w->last_overlay_modified != OVERLAY_MODIFF)
 21678     GIVE_UP (200);
 21679 
 21680   /* Verify that narrowing has not changed.
 21681      Also verify that we were not told to prevent redisplay optimizations.
 21682      It would be nice to further
 21683      reduce the number of cases where this prevents try_window_id.  */
 21684   if (current_buffer->clip_changed
 21685       || current_buffer->prevent_redisplay_optimizations_p)
 21686     GIVE_UP (3);
 21687 
 21688   /* Window must either use window-based redisplay or be full width.  */
 21689   if (!FRAME_WINDOW_P (f)
 21690       && (!FRAME_LINE_INS_DEL_OK (f)
 21691           || !WINDOW_FULL_WIDTH_P (w)))
 21692     GIVE_UP (4);
 21693 
 21694   /* Give up if point is known NOT to appear in W.  */
 21695   if (PT < CHARPOS (start))
 21696     GIVE_UP (5);
 21697 
 21698   /* Another way to prevent redisplay optimizations.  */
 21699   if (w->last_modified == 0)
 21700     GIVE_UP (6);
 21701 
 21702   /* Verify that window is not hscrolled.  */
 21703   if (w->hscroll != 0)
 21704     GIVE_UP (7);
 21705 
 21706   /* Verify that display wasn't paused.  */
 21707   if (!w->window_end_valid)
 21708     GIVE_UP (8);
 21709 
 21710   /* Likewise if highlighting trailing whitespace.  */
 21711   if (!NILP (Vshow_trailing_whitespace))
 21712     GIVE_UP (11);
 21713 
 21714   /* Can't use this if overlay arrow position and/or string have
 21715      changed.  */
 21716   if (overlay_arrows_changed_p (false))
 21717     GIVE_UP (12);
 21718 
 21719   /* When word-wrap is on, adding a space to the first word of a
 21720      wrapped line can change the wrap position, altering the line
 21721      above it.  It might be worthwhile to handle this more
 21722      intelligently, but for now just redisplay from scratch.  */
 21723   if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
 21724     GIVE_UP (21);
 21725 
 21726   /* Under bidi reordering, adding or deleting a character in the
 21727      beginning of a paragraph, before the first strong directional
 21728      character, can change the base direction of the paragraph (unless
 21729      the buffer specifies a fixed paragraph direction), which will
 21730      require redisplaying the whole paragraph.  It might be worthwhile
 21731      to find the paragraph limits and widen the range of redisplayed
 21732      lines to that, but for now just give up this optimization and
 21733      redisplay from scratch.  */
 21734   if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21735       && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 21736     GIVE_UP (22);
 21737 
 21738   /* Give up if the buffer has line-spacing set, as Lisp-level changes
 21739      to that variable require thorough redisplay.  */
 21740   if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
 21741     GIVE_UP (23);
 21742 
 21743   /* Give up if display-line-numbers is in relative mode, or when the
 21744      current line's number needs to be displayed in a distinct face.  */
 21745   if (EQ (Vdisplay_line_numbers, Qrelative)
 21746       || EQ (Vdisplay_line_numbers, Qvisual)
 21747       || (!NILP (Vdisplay_line_numbers)
 21748           && NILP (Finternal_lisp_face_equal_p (Qline_number,
 21749                                                 Qline_number_current_line,
 21750                                                 w->frame))))
 21751     GIVE_UP (24);
 21752 
 21753   /* composition-break-at-point is incompatible with the optimizations
 21754      in this function, because we need to recompose characters when
 21755      point moves off their positions.  */
 21756   if (composition_break_at_point)
 21757     GIVE_UP (27);
 21758 
 21759   /* Make sure beg_unchanged and end_unchanged are up to date.  Do it
 21760      only if buffer has really changed.  The reason is that the gap is
 21761      initially at Z for freshly visited files.  The code below would
 21762      set end_unchanged to 0 in that case.  */
 21763   if (MODIFF > SAVE_MODIFF
 21764       /* This seems to happen sometimes after saving a buffer.  */
 21765       || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
 21766     {
 21767       if (GPT - BEG < BEG_UNCHANGED)
 21768         BEG_UNCHANGED = GPT - BEG;
 21769       if (Z - GPT < END_UNCHANGED)
 21770         END_UNCHANGED = Z - GPT;
 21771     }
 21772 
 21773   /* The position of the first and last character that has been changed.  */
 21774   first_changed_charpos = BEG + BEG_UNCHANGED;
 21775   last_changed_charpos  = Z - END_UNCHANGED;
 21776 
 21777   /* If window starts after a line end, and the last change is in
 21778      front of that newline, then changes don't affect the display.
 21779      This case happens with stealth-fontification.  Note that although
 21780      the display is unchanged, glyph positions in the matrix have to
 21781      be adjusted, of course.  */
 21782   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21783   if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21784       && ((last_changed_charpos < CHARPOS (start)
 21785            && CHARPOS (start) == BEGV)
 21786           || (last_changed_charpos < CHARPOS (start) - 1
 21787               && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
 21788     {
 21789       ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
 21790       struct glyph_row *r0;
 21791 
 21792       /* Compute how many chars/bytes have been added to or removed
 21793          from the buffer.  */
 21794       Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21795       Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21796       Z_delta = Z - Z_old;
 21797       Z_delta_bytes = Z_BYTE - Z_BYTE_old;
 21798 
 21799       /* Give up if PT is not in the window.  Note that it already has
 21800          been checked at the start of try_window_id that PT is not in
 21801          front of the window start.  */
 21802       if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
 21803         GIVE_UP (13);
 21804 
 21805       /* If window start is unchanged, we can reuse the whole matrix
 21806          as is, after adjusting glyph positions.  No need to compute
 21807          the window end again, since its offset from Z hasn't changed.  */
 21808       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21809       if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
 21810           && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
 21811           /* PT must not be in a partially visible line.  */
 21812           && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
 21813                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 21814         {
 21815           /* Adjust positions in the glyph matrix.  */
 21816           if (Z_delta || Z_delta_bytes)
 21817             {
 21818               struct glyph_row *r1
 21819                 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 21820               increment_matrix_positions (w->current_matrix,
 21821                                           MATRIX_ROW_VPOS (r0, current_matrix),
 21822                                           MATRIX_ROW_VPOS (r1, current_matrix),
 21823                                           Z_delta, Z_delta_bytes);
 21824             }
 21825 
 21826           /* Set the cursor.  */
 21827           row = row_containing_pos (w, PT, r0, NULL, 0);
 21828           if (row)
 21829             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 21830           return 1;
 21831         }
 21832     }
 21833 
 21834   /* Handle the case that changes are all below what is displayed in
 21835      the window, and that PT is in the window.  This shortcut cannot
 21836      be taken if ZV is visible in the window, and text has been added
 21837      there that is visible in the window.  */
 21838   if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
 21839       /* ZV is not visible in the window, or there are no
 21840          changes at ZV, actually.  */
 21841       && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
 21842           || first_changed_charpos == last_changed_charpos))
 21843     {
 21844       struct glyph_row *r0;
 21845 
 21846       /* Give up if PT is not in the window.  Note that it already has
 21847          been checked at the start of try_window_id that PT is not in
 21848          front of the window start.  */
 21849       if (PT >= MATRIX_ROW_END_CHARPOS (row))
 21850         GIVE_UP (14);
 21851 
 21852       /* If window start is unchanged, we can reuse the whole matrix
 21853          as is, without changing glyph positions since no text has
 21854          been added/removed in front of the window end.  */
 21855       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21856       if (TEXT_POS_EQUAL_P (start, r0->minpos)
 21857           /* PT must not be in a partially visible line.  */
 21858           && !(PT >= MATRIX_ROW_START_CHARPOS (row)
 21859                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 21860         {
 21861           /* We have to compute the window end anew since text
 21862              could have been added/removed after it.  */
 21863           w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 21864           w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 21865 
 21866           /* Set the cursor.  */
 21867           row = row_containing_pos (w, PT, r0, NULL, 0);
 21868           if (row)
 21869             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 21870           return 2;
 21871         }
 21872     }
 21873 
 21874   /* Give up if window start is in the changed area.
 21875 
 21876      The condition used to read
 21877 
 21878      (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
 21879 
 21880      but why that was tested escapes me at the moment.  */
 21881   if (CHARPOS (start) >= first_changed_charpos
 21882       && CHARPOS (start) <= last_changed_charpos)
 21883     GIVE_UP (15);
 21884 
 21885   /* Check that window start agrees with the start of the first glyph
 21886      row in its current matrix.  Check this after we know the window
 21887      start is not in changed text, otherwise positions would not be
 21888      comparable.  */
 21889   row = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21890   if (!TEXT_POS_EQUAL_P (start, row->minpos))
 21891     GIVE_UP (16);
 21892 
 21893   /* Give up if the window ends in strings.  Overlay strings
 21894      at the end are difficult to handle, so don't try.  */
 21895   row = MATRIX_ROW (current_matrix, w->window_end_vpos);
 21896   if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
 21897     GIVE_UP (20);
 21898 
 21899   /* Can't let scroll_run_hook below run on w32 GUI frames when
 21900      position of point is indicated by the system caret, because
 21901      scrolling the display will then "copy" the pixels used by the
 21902      caret.  */
 21903 #ifdef HAVE_NTGUI
 21904   if (FRAME_W32_P (f) && w32_use_visible_system_caret)
 21905     GIVE_UP (25);
 21906 #endif
 21907 
 21908   /* Compute the position at which we have to start displaying new
 21909      lines.  Some of the lines at the top of the window might be
 21910      reusable because they are not displaying changed text.  Find the
 21911      last row in W's current matrix not affected by changes at the
 21912      start of current_buffer.  Value is null if changes start in the
 21913      first line of window.  */
 21914   last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
 21915   if (last_unchanged_at_beg_row)
 21916     {
 21917       /* Avoid starting to display in the middle of a character, a TAB
 21918          for instance.  This is easier than to set up the iterator
 21919          exactly, and it's not a frequent case, so the additional
 21920          effort wouldn't really pay off.  */
 21921       while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
 21922               || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
 21923              && last_unchanged_at_beg_row > w->current_matrix->rows)
 21924         --last_unchanged_at_beg_row;
 21925 
 21926       if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
 21927         GIVE_UP (17);
 21928 
 21929       if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
 21930         GIVE_UP (18);
 21931       /* Give up if the row starts with a display property that draws
 21932          on the fringes, since that could prevent correct display of
 21933          line-prefix and wrap-prefix.  */
 21934       if (it.sp > 1
 21935           && it.method == GET_FROM_IMAGE && it.image_id == -1)
 21936         GIVE_UP (26);
 21937       start_pos = it.current.pos;
 21938 
 21939       /* Start displaying new lines in the desired matrix at the same
 21940          vpos we would use in the current matrix, i.e. below
 21941          last_unchanged_at_beg_row.  */
 21942       it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
 21943                                      current_matrix);
 21944       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 21945       it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
 21946 
 21947       eassert (it.hpos == 0 && it.current_x == 0);
 21948     }
 21949   else
 21950     {
 21951       /* There are no reusable lines at the start of the window.
 21952          Start displaying in the first text line.  */
 21953       start_display (&it, w, start);
 21954       it.vpos = it.first_vpos;
 21955       start_pos = it.current.pos;
 21956     }
 21957 
 21958   /* Find the first row that is not affected by changes at the end of
 21959      the buffer.  Value will be null if there is no unchanged row, in
 21960      which case we must redisplay to the end of the window.  delta
 21961      will be set to the value by which buffer positions beginning with
 21962      first_unchanged_at_end_row have to be adjusted due to text
 21963      changes.  */
 21964   first_unchanged_at_end_row
 21965     = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
 21966   IF_DEBUG (debug_delta = delta);
 21967   IF_DEBUG (debug_delta_bytes = delta_bytes);
 21968 
 21969   /* Set stop_pos to the buffer position up to which we will have to
 21970      display new lines.  If first_unchanged_at_end_row != NULL, this
 21971      is the buffer position of the start of the line displayed in that
 21972      row.  For first_unchanged_at_end_row == NULL, use 0 to indicate
 21973      that we don't stop at a buffer position.  */
 21974   stop_pos = 0;
 21975   if (first_unchanged_at_end_row)
 21976     {
 21977       eassert (last_unchanged_at_beg_row == NULL
 21978                || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
 21979 
 21980       /* If this is a continuation line, move forward to the next one
 21981          that isn't.  Changes in lines above affect this line.
 21982          Caution: this may move first_unchanged_at_end_row to a row
 21983          not displaying text.  */
 21984       while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
 21985              && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 21986              && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 21987                  < it.last_visible_y))
 21988         ++first_unchanged_at_end_row;
 21989 
 21990       if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 21991           || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 21992               >= it.last_visible_y))
 21993         first_unchanged_at_end_row = NULL;
 21994       else
 21995         {
 21996           stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
 21997                       + delta);
 21998           first_unchanged_at_end_vpos
 21999             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
 22000           eassert (stop_pos >= Z - END_UNCHANGED);
 22001         }
 22002     }
 22003   else if (last_unchanged_at_beg_row == NULL)
 22004     GIVE_UP (19);
 22005 
 22006 
 22007 #ifdef GLYPH_DEBUG
 22008 
 22009   /* Either there is no unchanged row at the end, or the one we have
 22010      now displays text.  This is a necessary condition for the window
 22011      end pos calculation at the end of this function.  */
 22012   eassert (first_unchanged_at_end_row == NULL
 22013            || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22014 
 22015   debug_last_unchanged_at_beg_vpos
 22016     = (last_unchanged_at_beg_row
 22017        ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
 22018        : -1);
 22019   debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
 22020 
 22021 #endif /* GLYPH_DEBUG */
 22022 
 22023 
 22024   /* Display new lines.  Set last_text_row to the last new line
 22025      displayed which has text on it, i.e. might end up as being the
 22026      line where the window_end_vpos is.  */
 22027   w->cursor.vpos = -1;
 22028   last_text_row = NULL;
 22029   overlay_arrow_seen = false;
 22030   if (it.current_y < it.last_visible_y
 22031       && !f->fonts_changed
 22032       && (first_unchanged_at_end_row == NULL
 22033           || IT_CHARPOS (it) < stop_pos))
 22034     it.glyph_row->reversed_p = false;
 22035   while (it.current_y < it.last_visible_y
 22036          && !f->fonts_changed
 22037          && (first_unchanged_at_end_row == NULL
 22038              || IT_CHARPOS (it) < stop_pos))
 22039     {
 22040       if (display_line (&it, -1))
 22041         last_text_row = it.glyph_row - 1;
 22042     }
 22043 
 22044   if (f->fonts_changed)
 22045     return -1;
 22046 
 22047   /* The redisplay iterations in display_line above could have
 22048      triggered font-lock, which could have done something that
 22049      invalidates IT->w window's end-point information, on which we
 22050      rely below.  E.g., one package, which will remain unnamed, used
 22051      to install a font-lock-fontify-region-function that called
 22052      bury-buffer, whose side effect is to switch the buffer displayed
 22053      by IT->w, and that predictably resets IT->w's window_end_valid
 22054      flag, which we already tested at the entry to this function.
 22055      Amply punish such packages/modes by giving up on this
 22056      optimization in those cases.  */
 22057   if (!w->window_end_valid)
 22058     {
 22059       clear_glyph_matrix (w->desired_matrix);
 22060       return -1;
 22061     }
 22062 
 22063   /* Compute differences in buffer positions, y-positions etc.  for
 22064      lines reused at the bottom of the window.  Compute what we can
 22065      scroll.  */
 22066   if (first_unchanged_at_end_row
 22067       /* No lines reused because we displayed everything up to the
 22068          bottom of the window.  */
 22069       && it.current_y < it.last_visible_y)
 22070     {
 22071       dvpos = (it.vpos
 22072                - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
 22073                                   current_matrix));
 22074       dy = it.current_y - first_unchanged_at_end_row->y;
 22075       run.current_y = first_unchanged_at_end_row->y;
 22076       run.desired_y = run.current_y + dy;
 22077       run.height = it.last_visible_y - max (run.current_y, run.desired_y);
 22078     }
 22079   else
 22080     {
 22081       delta = delta_bytes = dvpos = dy
 22082         = run.current_y = run.desired_y = run.height = 0;
 22083       first_unchanged_at_end_row = NULL;
 22084     }
 22085   IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
 22086 
 22087 
 22088   /* Find the cursor if not already found.  We have to decide whether
 22089      PT will appear on this window (it sometimes doesn't, but this is
 22090      not a very frequent case.)  This decision has to be made before
 22091      the current matrix is altered.  A value of cursor.vpos < 0 means
 22092      that PT is either in one of the lines beginning at
 22093      first_unchanged_at_end_row or below the window.  Don't care for
 22094      lines that might be displayed later at the window end; as
 22095      mentioned, this is not a frequent case.  */
 22096   if (w->cursor.vpos < 0)
 22097     {
 22098       /* Cursor in unchanged rows at the top?  */
 22099       if (PT < CHARPOS (start_pos)
 22100           && last_unchanged_at_beg_row)
 22101         {
 22102           row = row_containing_pos (w, PT,
 22103                                     MATRIX_FIRST_TEXT_ROW (w->current_matrix),
 22104                                     last_unchanged_at_beg_row + 1, 0);
 22105           if (row)
 22106             set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 22107         }
 22108 
 22109       /* Start from first_unchanged_at_end_row looking for PT.  */
 22110       else if (first_unchanged_at_end_row)
 22111         {
 22112           row = row_containing_pos (w, PT - delta,
 22113                                     first_unchanged_at_end_row, NULL, 0);
 22114           if (row)
 22115             set_cursor_from_row (w, row, w->current_matrix, delta,
 22116                                  delta_bytes, dy, dvpos);
 22117         }
 22118 
 22119       /* Give up if cursor was not found.  */
 22120       if (w->cursor.vpos < 0)
 22121         {
 22122           clear_glyph_matrix (w->desired_matrix);
 22123           return -1;
 22124         }
 22125     }
 22126 
 22127   /* Don't let the cursor end in the scroll margins.  */
 22128   {
 22129     int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 22130     int bot_scroll_margin = top_scroll_margin;
 22131     int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
 22132 
 22133     if (window_wants_header_line (w))
 22134       top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 22135     if (window_wants_tab_line (w))
 22136       top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 22137 
 22138     if ((w->cursor.y < top_scroll_margin
 22139          && CHARPOS (start) > BEGV)
 22140         /* Old redisplay didn't take scroll margin into account at the bottom,
 22141            but then global-hl-line-mode doesn't scroll.  KFS 2004-06-14 */
 22142         || (w->cursor.y
 22143             + (cursor_row_fully_visible_p (w, false, true, true)
 22144                ? 1
 22145                : cursor_height + bot_scroll_margin)) > it.last_visible_y)
 22146       {
 22147         w->cursor.vpos = -1;
 22148         clear_glyph_matrix (w->desired_matrix);
 22149         return -1;
 22150       }
 22151   }
 22152 
 22153   /* Scroll the display.  Do it before changing the current matrix so
 22154      that xterm.c doesn't get confused about where the cursor glyph is
 22155      found.  */
 22156   if (dy && run.height)
 22157     {
 22158       update_begin (f);
 22159 
 22160       if (FRAME_WINDOW_P (f))
 22161         {
 22162 #ifdef HAVE_WINDOW_SYSTEM
 22163           gui_update_window_begin (w);
 22164           FRAME_RIF (f)->clear_window_mouse_face (w);
 22165           FRAME_RIF (f)->scroll_run_hook (w, &run);
 22166           gui_update_window_end (w, false, false);
 22167 #endif
 22168         }
 22169       else
 22170         {
 22171           /* Terminal frame.  In this case, dvpos gives the number of
 22172              lines to scroll by; dvpos < 0 means scroll up.  */
 22173           int from_vpos
 22174             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
 22175           int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
 22176           int end = (WINDOW_TOP_EDGE_LINE (w)
 22177                      + window_wants_tab_line (w)
 22178                      + window_wants_header_line (w)
 22179                      + window_internal_height (w));
 22180 
 22181           gui_clear_window_mouse_face (w);
 22182 
 22183           /* Perform the operation on the screen.  */
 22184           if (dvpos > 0)
 22185             {
 22186               /* Scroll last_unchanged_at_beg_row to the end of the
 22187                  window down dvpos lines.  */
 22188               set_terminal_window (f, end);
 22189 
 22190               /* On dumb terminals delete dvpos lines at the end
 22191                  before inserting dvpos empty lines.  */
 22192               if (!FRAME_SCROLL_REGION_OK (f))
 22193                 ins_del_lines (f, end - dvpos, -dvpos);
 22194 
 22195               /* Insert dvpos empty lines in front of
 22196                  last_unchanged_at_beg_row.  */
 22197               ins_del_lines (f, from, dvpos);
 22198             }
 22199           else if (dvpos < 0)
 22200             {
 22201               /* Scroll up last_unchanged_at_beg_vpos to the end of
 22202                  the window to last_unchanged_at_beg_vpos - |dvpos|.  */
 22203               set_terminal_window (f, end);
 22204 
 22205               /* Delete dvpos lines in front of
 22206                  last_unchanged_at_beg_vpos.  ins_del_lines will set
 22207                  the cursor to the given vpos and emit |dvpos| delete
 22208                  line sequences.  */
 22209               ins_del_lines (f, from + dvpos, dvpos);
 22210 
 22211               /* On a dumb terminal insert dvpos empty lines at the
 22212                  end.  */
 22213               if (!FRAME_SCROLL_REGION_OK (f))
 22214                 ins_del_lines (f, end + dvpos, -dvpos);
 22215             }
 22216 
 22217           set_terminal_window (f, 0);
 22218         }
 22219 
 22220       update_end (f);
 22221     }
 22222 
 22223   /* Shift reused rows of the current matrix to the right position.
 22224      BOTTOM_ROW is the last + 1 row in the current matrix reserved for
 22225      text.  */
 22226   bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 22227   bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
 22228   if (dvpos < 0)
 22229     {
 22230       rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
 22231                      bottom_vpos, dvpos);
 22232       clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
 22233                                bottom_vpos);
 22234     }
 22235   else if (dvpos > 0)
 22236     {
 22237       rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
 22238                      bottom_vpos, dvpos);
 22239       clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
 22240                                first_unchanged_at_end_vpos + dvpos);
 22241     }
 22242 
 22243   /* For frame-based redisplay, make sure that current frame and window
 22244      matrix are in sync with respect to glyph memory.  */
 22245   if (!FRAME_WINDOW_P (f))
 22246     sync_frame_with_window_matrix_rows (w);
 22247 
 22248   /* Adjust buffer positions in reused rows.  */
 22249   if (delta || delta_bytes)
 22250     increment_matrix_positions (current_matrix,
 22251                                 first_unchanged_at_end_vpos + dvpos,
 22252                                 bottom_vpos, delta, delta_bytes);
 22253 
 22254   /* Adjust Y positions.  */
 22255   if (dy)
 22256     shift_glyph_matrix (w, current_matrix,
 22257                         first_unchanged_at_end_vpos + dvpos,
 22258                         bottom_vpos, dy);
 22259 
 22260   if (first_unchanged_at_end_row)
 22261     {
 22262       first_unchanged_at_end_row += dvpos;
 22263       if (first_unchanged_at_end_row->y >= it.last_visible_y
 22264           || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
 22265         first_unchanged_at_end_row = NULL;
 22266     }
 22267 
 22268   /* If scrolling up, there may be some lines to display at the end of
 22269      the window.  */
 22270   last_text_row_at_end = NULL;
 22271   if (dy < 0)
 22272     {
 22273       /* Scrolling up can leave for example a partially visible line
 22274          at the end of the window to be redisplayed.  */
 22275       /* Set last_row to the glyph row in the current matrix where the
 22276          window end line is found.  It has been moved up or down in
 22277          the matrix by dvpos.  */
 22278       int last_vpos = w->window_end_vpos + dvpos;
 22279       struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
 22280 
 22281       /* If last_row is the window end line, it should display text.  */
 22282       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
 22283 
 22284       /* If window end line was partially visible before, begin
 22285          displaying at that line.  Otherwise begin displaying with the
 22286          line following it.  */
 22287       if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
 22288         {
 22289           init_to_row_start (&it, w, last_row);
 22290           it.vpos = last_vpos;
 22291           it.current_y = last_row->y;
 22292         }
 22293       else
 22294         {
 22295           init_to_row_end (&it, w, last_row);
 22296           it.vpos = 1 + last_vpos;
 22297           it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
 22298           ++last_row;
 22299         }
 22300 
 22301       /* We may start in a continuation line.  If so, we have to
 22302          get the right continuation_lines_width and current_x.  */
 22303       it.continuation_lines_width = last_row->continuation_lines_width;
 22304       it.hpos = it.current_x = 0;
 22305 
 22306       /* Display the rest of the lines at the window end.  */
 22307       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 22308       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 22309         {
 22310           /* Is it always sure that the display agrees with lines in
 22311              the current matrix?  I don't think so, so we mark rows
 22312              displayed invalid in the current matrix by setting their
 22313              enabled_p flag to false.  */
 22314           SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
 22315           if (display_line (&it, w->cursor.vpos))
 22316             last_text_row_at_end = it.glyph_row - 1;
 22317         }
 22318     }
 22319 
 22320   /* Update window_end_pos and window_end_vpos.  */
 22321   if (first_unchanged_at_end_row && !last_text_row_at_end)
 22322     {
 22323       /* Window end line if one of the preserved rows from the current
 22324          matrix.  Set row to the last row displaying text in current
 22325          matrix starting at first_unchanged_at_end_row, after
 22326          scrolling.  */
 22327       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22328       row = find_last_row_displaying_text (w->current_matrix, &it,
 22329                                            first_unchanged_at_end_row);
 22330       eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
 22331       adjust_window_ends (w, row, true);
 22332       eassert (w->window_end_bytepos >= 0);
 22333       IF_DEBUG (debug_method_add (w, "A"));
 22334     }
 22335   else if (last_text_row_at_end)
 22336     {
 22337       adjust_window_ends (w, last_text_row_at_end, false);
 22338       eassert (w->window_end_bytepos >= 0);
 22339       IF_DEBUG (debug_method_add (w, "B"));
 22340     }
 22341   else if (last_text_row)
 22342     {
 22343       /* We have displayed either to the end of the window or at the
 22344          end of the window, i.e. the last row with text is to be found
 22345          in the desired matrix.  */
 22346       adjust_window_ends (w, last_text_row, false);
 22347       eassert (w->window_end_bytepos >= 0);
 22348     }
 22349   else if (first_unchanged_at_end_row == NULL
 22350            && last_text_row == NULL
 22351            && last_text_row_at_end == NULL)
 22352     {
 22353       /* Displayed to end of window, but no line containing text was
 22354          displayed.  Lines were deleted at the end of the window.  */
 22355       int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
 22356       int vpos = w->window_end_vpos;
 22357       struct glyph_row *current_row = current_matrix->rows + vpos;
 22358       struct glyph_row *desired_row = desired_matrix->rows + vpos;
 22359 
 22360       for (row = NULL; !row; --vpos, --current_row, --desired_row)
 22361         {
 22362           eassert (first_vpos <= vpos);
 22363           if (desired_row->enabled_p)
 22364             {
 22365               if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
 22366                 row = desired_row;
 22367             }
 22368           else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
 22369             row  = current_row;
 22370         }
 22371 
 22372       w->window_end_vpos = vpos + 1;
 22373       w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 22374       w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 22375       eassert (w->window_end_bytepos >= 0);
 22376       IF_DEBUG (debug_method_add (w, "C"));
 22377     }
 22378   else
 22379     emacs_abort ();
 22380 
 22381   IF_DEBUG ((debug_end_pos = w->window_end_pos,
 22382              debug_end_vpos = w->window_end_vpos));
 22383 
 22384   /* Record that display has not been completed.  */
 22385   w->window_end_valid = false;
 22386   w->desired_matrix->no_scrolling_p = true;
 22387   return 3;
 22388 
 22389 #undef GIVE_UP
 22390 }
 22391 
 22392 
 22393 
 22394 /***********************************************************************
 22395                         More debugging support
 22396  ***********************************************************************/
 22397 
 22398 #ifdef GLYPH_DEBUG
 22399 
 22400 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
 22401 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
 22402 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
 22403 
 22404 
 22405 /* Dump the contents of glyph matrix MATRIX on stderr.
 22406 
 22407    GLYPHS 0 means don't show glyph contents.
 22408    GLYPHS 1 means show glyphs in short form
 22409    GLYPHS > 1 means show glyphs in long form.  */
 22410 
 22411 void
 22412 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
 22413 {
 22414   int i;
 22415   for (i = 0; i < matrix->nrows; ++i)
 22416     dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
 22417 }
 22418 
 22419 
 22420 /* Dump contents of glyph GLYPH to stderr.  ROW and AREA are
 22421    the glyph row and area where the glyph comes from.  */
 22422 
 22423 void
 22424 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
 22425 {
 22426   if (glyph->type == CHAR_GLYPH
 22427       || glyph->type == GLYPHLESS_GLYPH)
 22428     {
 22429       fprintf (stderr,
 22430                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22431                glyph - row->glyphs[TEXT_AREA],
 22432                (glyph->type == CHAR_GLYPH
 22433                 ? 'C'
 22434                 : 'G'),
 22435                glyph->charpos,
 22436                (BUFFERP (glyph->object)
 22437                 ? 'B'
 22438                 : (STRINGP (glyph->object)
 22439                    ? 'S'
 22440                    : (NILP (glyph->object)
 22441                       ? '0'
 22442                       : '-'))),
 22443                glyph->pixel_width,
 22444                glyph->u.ch,
 22445                (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
 22446                 ? (int) glyph->u.ch
 22447                 : '.'),
 22448                glyph->face_id,
 22449                glyph->left_box_line_p,
 22450                glyph->right_box_line_p);
 22451     }
 22452   else if (glyph->type == STRETCH_GLYPH)
 22453     {
 22454       fprintf (stderr,
 22455                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22456                glyph - row->glyphs[TEXT_AREA],
 22457                'S',
 22458                glyph->charpos,
 22459                (BUFFERP (glyph->object)
 22460                 ? 'B'
 22461                 : (STRINGP (glyph->object)
 22462                    ? 'S'
 22463                    : (NILP (glyph->object)
 22464                       ? '0'
 22465                       : '-'))),
 22466                glyph->pixel_width,
 22467                0u,
 22468                ' ',
 22469                glyph->face_id,
 22470                glyph->left_box_line_p,
 22471                glyph->right_box_line_p);
 22472     }
 22473   else if (glyph->type == IMAGE_GLYPH)
 22474     {
 22475       fprintf (stderr,
 22476                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22477                glyph - row->glyphs[TEXT_AREA],
 22478                'I',
 22479                glyph->charpos,
 22480                (BUFFERP (glyph->object)
 22481                 ? 'B'
 22482                 : (STRINGP (glyph->object)
 22483                    ? 'S'
 22484                    : (NILP (glyph->object)
 22485                       ? '0'
 22486                       : '-'))),
 22487                glyph->pixel_width,
 22488                (unsigned int) glyph->u.img_id,
 22489                '.',
 22490                glyph->face_id,
 22491                glyph->left_box_line_p,
 22492                glyph->right_box_line_p);
 22493     }
 22494   else if (glyph->type == COMPOSITE_GLYPH)
 22495     {
 22496       fprintf (stderr,
 22497                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x",
 22498                glyph - row->glyphs[TEXT_AREA],
 22499                '+',
 22500                glyph->charpos,
 22501                (BUFFERP (glyph->object)
 22502                 ? 'B'
 22503                 : (STRINGP (glyph->object)
 22504                    ? 'S'
 22505                    : (NILP (glyph->object)
 22506                       ? '0'
 22507                       : '-'))),
 22508                glyph->pixel_width,
 22509                (unsigned int) glyph->u.cmp.id);
 22510       if (glyph->u.cmp.automatic)
 22511         fprintf (stderr,
 22512                  "[%d-%d]",
 22513                  glyph->slice.cmp.from, glyph->slice.cmp.to);
 22514       fprintf (stderr, " . %4d %1.1d%1.1d\n",
 22515                glyph->face_id,
 22516                glyph->left_box_line_p,
 22517                glyph->right_box_line_p);
 22518     }
 22519   else if (glyph->type == XWIDGET_GLYPH)
 22520     {
 22521 #ifndef HAVE_XWIDGETS
 22522       eassume (false);
 22523 #else
 22524       fprintf (stderr,
 22525                "  %5td %4c %6td %c %3d %7p %c %4d %1.1d%1.1d\n",
 22526                glyph - row->glyphs[TEXT_AREA],
 22527                'X',
 22528                glyph->charpos,
 22529                (BUFFERP (glyph->object)
 22530                 ? 'B'
 22531                 : (STRINGP (glyph->object)
 22532                    ? 'S'
 22533                    : '-')),
 22534                glyph->pixel_width,
 22535                glyph->u.xwidget,
 22536                '.',
 22537                glyph->face_id,
 22538                glyph->left_box_line_p,
 22539                glyph->right_box_line_p);
 22540 #endif
 22541     }
 22542 }
 22543 
 22544 
 22545 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
 22546    GLYPHS 0 means don't show glyph contents.
 22547    GLYPHS 1 means show glyphs in short form
 22548    GLYPHS > 1 means show glyphs in long form.  */
 22549 
 22550 void
 22551 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
 22552 {
 22553   if (glyphs != 1)
 22554     {
 22555       fputs (("Row     Start       End Used oE><\\CTZFesm     X    Y    W    H    V    A    P\n"
 22556               "==============================================================================\n"),
 22557              stderr);
 22558       fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
 22559 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d  %4d %4d %4d %4d %4d %4d %4d\n",
 22560                vpos,
 22561                MATRIX_ROW_START_CHARPOS (row),
 22562                MATRIX_ROW_END_CHARPOS (row),
 22563                row->used[TEXT_AREA],
 22564                row->contains_overlapping_glyphs_p,
 22565                row->enabled_p,
 22566                row->truncated_on_left_p,
 22567                row->truncated_on_right_p,
 22568                row->continued_p,
 22569                MATRIX_ROW_CONTINUATION_LINE_P (row),
 22570                MATRIX_ROW_DISPLAYS_TEXT_P (row),
 22571                row->ends_at_zv_p,
 22572                row->fill_line_p,
 22573                row->ends_in_middle_of_char_p,
 22574                row->starts_in_middle_of_char_p,
 22575                row->mouse_face_p,
 22576                row->x,
 22577                row->y,
 22578                row->pixel_width,
 22579                row->height,
 22580                row->visible_height,
 22581                row->ascent,
 22582                row->phys_ascent);
 22583       /* The next 3 lines should align to "Start" in the header.  */
 22584       fprintf (stderr, "    %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
 22585                row->end.overlay_string_index,
 22586                row->continuation_lines_width);
 22587       fprintf (stderr, "    %9"pD"d %9"pD"d\n",
 22588                CHARPOS (row->start.string_pos),
 22589                CHARPOS (row->end.string_pos));
 22590       fprintf (stderr, "    %9d %9d\n", row->start.dpvec_index,
 22591                row->end.dpvec_index);
 22592     }
 22593 
 22594   if (glyphs > 1)
 22595     {
 22596       int area;
 22597 
 22598       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22599         {
 22600           struct glyph *glyph = row->glyphs[area];
 22601           struct glyph *glyph_end = glyph + row->used[area];
 22602 
 22603           /* Glyph for a line end in text.  */
 22604           if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
 22605             ++glyph_end;
 22606 
 22607           if (glyph < glyph_end)
 22608             fputs (" Glyph#  Type       Pos   O   W     Code      C Face LR\n",
 22609                    stderr);
 22610 
 22611           for (; glyph < glyph_end; ++glyph)
 22612             dump_glyph (row, glyph, area);
 22613         }
 22614     }
 22615   else if (glyphs == 1)
 22616     {
 22617       int area;
 22618       char s[SHRT_MAX + 4];
 22619 
 22620       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22621         {
 22622           int i;
 22623 
 22624           for (i = 0; i < row->used[area]; ++i)
 22625             {
 22626               struct glyph *glyph = row->glyphs[area] + i;
 22627               if (i == row->used[area] - 1
 22628                   && area == TEXT_AREA
 22629                   && NILP (glyph->object)
 22630                   && glyph->type == CHAR_GLYPH
 22631                   && glyph->u.ch == ' ')
 22632                 {
 22633                   strcpy (&s[i], "[\\n]");
 22634                   i += 4;
 22635                 }
 22636               else if (glyph->type == CHAR_GLYPH
 22637                        && glyph->u.ch < 0x80
 22638                        && glyph->u.ch >= ' ')
 22639                 s[i] = glyph->u.ch;
 22640               else
 22641                 s[i] = '.';
 22642             }
 22643 
 22644           s[i] = '\0';
 22645           fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
 22646         }
 22647     }
 22648 }
 22649 
 22650 
 22651 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
 22652        Sdump_glyph_matrix, 0, 1, "p",
 22653        doc: /* Dump the current matrix of the selected window to stderr.
 22654 Shows contents of glyph row structures.  With non-nil
 22655 parameter GLYPHS, dump glyphs as well.  If GLYPHS is 1 show
 22656 glyphs in short form, otherwise show glyphs in long form.
 22657 
 22658 Interactively, no argument means show glyphs in short form;
 22659 with numeric argument, its value is passed as the GLYPHS flag.  */)
 22660   (Lisp_Object glyphs)
 22661 {
 22662   struct window *w = XWINDOW (selected_window);
 22663   struct buffer *buffer = XBUFFER (w->contents);
 22664 
 22665   fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
 22666            BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
 22667   fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
 22668            w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
 22669   fputs ("=============================================\n", stderr);
 22670   dump_glyph_matrix (w->current_matrix,
 22671                      TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 0);
 22672   return Qnil;
 22673 }
 22674 
 22675 
 22676 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
 22677        Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
 22678 Only text-mode frames have frame glyph matrices.  */)
 22679   (void)
 22680 {
 22681   struct frame *f = XFRAME (selected_frame);
 22682 
 22683   if (f->current_matrix)
 22684     dump_glyph_matrix (f->current_matrix, 1);
 22685   else
 22686     fputs ("*** This frame doesn't have a frame glyph matrix ***\n", stderr);
 22687   return Qnil;
 22688 }
 22689 
 22690 
 22691 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
 22692        doc: /* Dump glyph row ROW to stderr.
 22693 Interactively, ROW is the prefix numeric argument and defaults to
 22694 the row which displays point.
 22695 Optional argument GLYPHS 0 means don't dump glyphs.
 22696 GLYPHS 1 means dump glyphs in short form.
 22697 GLYPHS > 1 or omitted means dump glyphs in long form.  */)
 22698   (Lisp_Object row, Lisp_Object glyphs)
 22699 {
 22700   struct glyph_matrix *matrix;
 22701   EMACS_INT vpos;
 22702 
 22703   if (NILP (row))
 22704     {
 22705       int d1, d2, d3, d4, d5, ypos;
 22706       bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
 22707                                       &d1, &d2, &d3, &d4, &d5, &ypos);
 22708       if (visible_p)
 22709         vpos = ypos;
 22710       else
 22711         vpos = 0;
 22712     }
 22713   else
 22714     {
 22715       CHECK_FIXNUM (row);
 22716       vpos = XFIXNUM (row);
 22717     }
 22718   matrix = XWINDOW (selected_window)->current_matrix;
 22719   if (vpos >= 0 && vpos < matrix->nrows)
 22720     dump_glyph_row (MATRIX_ROW (matrix, vpos),
 22721                     vpos,
 22722                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22723   return Qnil;
 22724 }
 22725 
 22726 
 22727 DEFUN ("dump-tab-bar-row", Fdump_tab_bar_row, Sdump_tab_bar_row, 1, 2, "P",
 22728        doc: /* Dump glyph row ROW of the tab-bar of the current frame to stderr.
 22729 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22730 GLYPHS 0 means don't dump glyphs.
 22731 GLYPHS 1 means dump glyphs in short form.
 22732 GLYPHS > 1 or omitted means dump glyphs in long form.
 22733 
 22734 If there's no tab-bar, or if the tab-bar is not drawn by Emacs,
 22735 do nothing.  */)
 22736   (Lisp_Object row, Lisp_Object glyphs)
 22737 {
 22738 #if defined (HAVE_WINDOW_SYSTEM)
 22739   struct frame *sf = SELECTED_FRAME ();
 22740   struct glyph_matrix *m = WINDOWP (sf->tab_bar_window)
 22741     ? XWINDOW (sf->tab_bar_window)->current_matrix
 22742     : sf->current_matrix;
 22743   EMACS_INT vpos;
 22744 
 22745   if (NILP (row))
 22746     vpos = WINDOWP (sf->tab_bar_window) ? 0 :
 22747       FRAME_MENU_BAR_LINES (sf) > 0 ? 1 : 0;
 22748   else
 22749     {
 22750       CHECK_FIXNUM (row);
 22751       vpos = XFIXNUM (row);
 22752     }
 22753   if (vpos >= 0 && vpos < m->nrows)
 22754     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22755                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22756 #endif
 22757   return Qnil;
 22758 }
 22759 
 22760 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
 22761        doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
 22762 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22763 GLYPHS 0 means don't dump glyphs.
 22764 GLYPHS 1 means dump glyphs in short form.
 22765 GLYPHS > 1 or omitted means dump glyphs in long form.
 22766 
 22767 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
 22768 do nothing.  */)
 22769   (Lisp_Object row, Lisp_Object glyphs)
 22770 {
 22771 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 22772   struct frame *sf = SELECTED_FRAME ();
 22773   struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
 22774   EMACS_INT vpos;
 22775 
 22776   if (NILP (row))
 22777     vpos = 0;
 22778   else
 22779     {
 22780       CHECK_FIXNUM (row);
 22781       vpos = XFIXNUM (row);
 22782     }
 22783   if (vpos >= 0 && vpos < m->nrows)
 22784     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22785                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22786 #endif
 22787   return Qnil;
 22788 }
 22789 
 22790 
 22791 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
 22792        doc: /* Toggle tracing of redisplay.
 22793 With ARG, turn tracing on if and only if ARG is positive.  */)
 22794   (Lisp_Object arg)
 22795 {
 22796   if (NILP (arg))
 22797     trace_redisplay_p = !trace_redisplay_p;
 22798   else
 22799     {
 22800       arg = Fprefix_numeric_value (arg);
 22801       trace_redisplay_p = XFIXNUM (arg) > 0;
 22802     }
 22803 
 22804   return Qnil;
 22805 }
 22806 
 22807 
 22808 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
 22809        doc: /* Like `format', but print result to stderr.
 22810 usage: (trace-to-stderr STRING &rest OBJECTS)  */)
 22811   (ptrdiff_t nargs, Lisp_Object *args)
 22812 {
 22813   Lisp_Object s = Fformat (nargs, args);
 22814   fwrite (SDATA (s), 1, SBYTES (s), stderr);
 22815   return Qnil;
 22816 }
 22817 
 22818 #endif /* GLYPH_DEBUG */
 22819 
 22820 
 22821 
 22822 /***********************************************************************
 22823                      Building Desired Matrix Rows
 22824  ***********************************************************************/
 22825 
 22826 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
 22827    Used for non-window-redisplay windows, and for windows without left
 22828    fringe.  */
 22829 
 22830 static struct glyph_row *
 22831 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
 22832 {
 22833   struct frame *f = XFRAME (WINDOW_FRAME (w));
 22834   struct buffer *buffer = XBUFFER (w->contents);
 22835   struct buffer *old = current_buffer;
 22836   const unsigned char *arrow_string = SDATA (overlay_arrow_string);
 22837   ptrdiff_t arrow_len = SBYTES (overlay_arrow_string), char_num = 0;
 22838   const unsigned char *arrow_end = arrow_string + arrow_len;
 22839   const unsigned char *p;
 22840   struct it it;
 22841   bool multibyte_p;
 22842   int n_glyphs_before;
 22843 
 22844   set_buffer_temp (buffer);
 22845   init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
 22846   scratch_glyph_row.reversed_p = false;
 22847   it.glyph_row->used[TEXT_AREA] = 0;
 22848   SET_TEXT_POS (it.position, 0, 0);
 22849 
 22850   multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
 22851   p = arrow_string;
 22852   while (p < arrow_end)
 22853     {
 22854       Lisp_Object face, ilisp;
 22855 
 22856       /* Get the next character.  */
 22857       if (multibyte_p)
 22858         it.c = it.char_to_display = check_char_and_length (p, &it.len);
 22859       else
 22860         {
 22861           it.c = it.char_to_display = *p, it.len = 1;
 22862           if (! ASCII_CHAR_P (it.c))
 22863             it.char_to_display = BYTE8_TO_CHAR (it.c);
 22864         }
 22865       p += it.len;
 22866 
 22867       /* Get its face.  */
 22868       ilisp = make_fixnum (char_num++);
 22869       face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
 22870       it.face_id = compute_char_face (f, it.char_to_display, face);
 22871 
 22872       /* Compute its width, get its glyphs.  */
 22873       n_glyphs_before = it.glyph_row->used[TEXT_AREA];
 22874       SET_TEXT_POS (it.position, -1, -1);
 22875       PRODUCE_GLYPHS (&it);
 22876 
 22877       /* If this character doesn't fit any more in the line, we have
 22878          to remove some glyphs.  */
 22879       if (it.current_x > it.last_visible_x)
 22880         {
 22881           it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
 22882           break;
 22883         }
 22884     }
 22885 
 22886   set_buffer_temp (old);
 22887   return it.glyph_row;
 22888 }
 22889 
 22890 
 22891 /* Insert truncation glyphs at the start of IT->glyph_row.  Which
 22892    glyphs to insert is determined by produce_special_glyphs.  */
 22893 
 22894 static void
 22895 insert_left_trunc_glyphs (struct it *it)
 22896 {
 22897   struct it truncate_it;
 22898   struct glyph *from, *end, *to, *toend;
 22899 
 22900   eassert (!FRAME_WINDOW_P (it->f)
 22901            || (!it->glyph_row->reversed_p
 22902                && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 22903            || (it->glyph_row->reversed_p
 22904                && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
 22905 
 22906   /* Get the truncation glyphs.  */
 22907   truncate_it = *it;
 22908   truncate_it.current_x = 0;
 22909   truncate_it.face_id = DEFAULT_FACE_ID;
 22910   truncate_it.glyph_row = &scratch_glyph_row;
 22911   truncate_it.area = TEXT_AREA;
 22912   truncate_it.glyph_row->used[TEXT_AREA] = 0;
 22913   CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
 22914   truncate_it.object = Qnil;
 22915   produce_special_glyphs (&truncate_it, IT_TRUNCATION);
 22916 
 22917   /* Overwrite glyphs from IT with truncation glyphs.  */
 22918   if (!it->glyph_row->reversed_p)
 22919     {
 22920       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 22921 
 22922       from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 22923       end = from + tused;
 22924       to = it->glyph_row->glyphs[TEXT_AREA];
 22925       toend = to + it->glyph_row->used[TEXT_AREA];
 22926       if (FRAME_WINDOW_P (it->f))
 22927         {
 22928           /* On GUI frames, when variable-size fonts are displayed,
 22929              the truncation glyphs may need more pixels than the row's
 22930              glyphs they overwrite.  We overwrite more glyphs to free
 22931              enough screen real estate, and enlarge the stretch glyph
 22932              on the right (see display_line), if there is one, to
 22933              preserve the screen position of the truncation glyphs on
 22934              the right.  */
 22935           int w = 0;
 22936           struct glyph *g = to;
 22937           short used;
 22938 
 22939           /* The first glyph could be partially visible, in which case
 22940              it->glyph_row->x will be negative.  But we want the left
 22941              truncation glyphs to be aligned at the left margin of the
 22942              window, so we override the x coordinate at which the row
 22943              will begin.  */
 22944           it->glyph_row->x = 0;
 22945           while (g < toend && w < it->truncation_pixel_width)
 22946             {
 22947               w += g->pixel_width;
 22948               ++g;
 22949             }
 22950           if (g - to - tused > 0)
 22951             {
 22952               memmove (to + tused, g, (toend - g) * sizeof(*g));
 22953               it->glyph_row->used[TEXT_AREA] -= g - to - tused;
 22954             }
 22955           used = it->glyph_row->used[TEXT_AREA];
 22956           if (it->glyph_row->truncated_on_right_p
 22957               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
 22958               && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
 22959               == STRETCH_GLYPH)
 22960             {
 22961               int extra = w - it->truncation_pixel_width;
 22962 
 22963               it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
 22964             }
 22965         }
 22966 
 22967       while (from < end)
 22968         *to++ = *from++;
 22969 
 22970       /* There may be padding glyphs left over.  Overwrite them too.  */
 22971       if (!FRAME_WINDOW_P (it->f))
 22972         {
 22973           while (to < toend && CHAR_GLYPH_PADDING_P (*to))
 22974             {
 22975               from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 22976               while (from < end)
 22977                 *to++ = *from++;
 22978             }
 22979         }
 22980 
 22981       if (to > toend)
 22982         it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
 22983     }
 22984   else
 22985     {
 22986       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 22987 
 22988       /* In R2L rows, overwrite the last (rightmost) glyphs, and do
 22989          that back to front.  */
 22990       end = truncate_it.glyph_row->glyphs[TEXT_AREA];
 22991       from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 22992       toend = it->glyph_row->glyphs[TEXT_AREA];
 22993       to = toend + it->glyph_row->used[TEXT_AREA] - 1;
 22994       if (FRAME_WINDOW_P (it->f))
 22995         {
 22996           int w = 0;
 22997           struct glyph *g = to;
 22998 
 22999           while (g >= toend && w < it->truncation_pixel_width)
 23000             {
 23001               w += g->pixel_width;
 23002               --g;
 23003             }
 23004           if (to - g - tused > 0)
 23005             to = g + tused;
 23006           if (it->glyph_row->truncated_on_right_p
 23007               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
 23008               && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
 23009             {
 23010               int extra = w - it->truncation_pixel_width;
 23011 
 23012               it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
 23013             }
 23014         }
 23015 
 23016       while (from >= end && to >= toend)
 23017         *to-- = *from--;
 23018       if (!FRAME_WINDOW_P (it->f))
 23019         {
 23020           while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
 23021             {
 23022               from =
 23023                 truncate_it.glyph_row->glyphs[TEXT_AREA]
 23024                 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 23025               while (from >= end && to >= toend)
 23026                 *to-- = *from--;
 23027             }
 23028         }
 23029       if (from >= end)
 23030         {
 23031           /* Need to free some room before prepending additional
 23032              glyphs.  */
 23033           int move_by = from - end + 1;
 23034           struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
 23035           struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
 23036 
 23037           for ( ; g >= g0; g--)
 23038             g[move_by] = *g;
 23039           while (from >= end)
 23040             *to-- = *from--;
 23041           it->glyph_row->used[TEXT_AREA] += move_by;
 23042         }
 23043     }
 23044 }
 23045 
 23046 /* Compute the hash code for ROW.  */
 23047 unsigned
 23048 row_hash (struct glyph_row *row)
 23049 {
 23050   int area, k;
 23051   unsigned hashval = 0;
 23052 
 23053   for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 23054     for (k = 0; k < row->used[area]; ++k)
 23055       hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
 23056                   + row->glyphs[area][k].u.val
 23057                   + row->glyphs[area][k].face_id
 23058                   + row->glyphs[area][k].padding_p
 23059                   + (row->glyphs[area][k].type << 2));
 23060 
 23061   return hashval;
 23062 }
 23063 
 23064 /* Compute the pixel height and width of IT->glyph_row.
 23065 
 23066    Most of the time, ascent and height of a display line will be equal
 23067    to the max_ascent and max_height values of the display iterator
 23068    structure.  This is not the case if
 23069 
 23070    1. We hit ZV without displaying anything.  In this case, max_ascent
 23071    and max_height will be zero.
 23072 
 23073    2. We have some glyphs that don't contribute to the line height.
 23074    (The glyph row flag contributes_to_line_height_p is for future
 23075    pixmap extensions).
 23076 
 23077    The first case is easily covered by using default values because in
 23078    these cases, the line height does not really matter, except that it
 23079    must not be zero.  */
 23080 
 23081 static void
 23082 compute_line_metrics (struct it *it)
 23083 {
 23084   struct glyph_row *row = it->glyph_row;
 23085 
 23086   if (FRAME_WINDOW_P (it->f))
 23087     {
 23088       int i, min_y, max_y;
 23089 
 23090       /* The line may consist of one space only, that was added to
 23091          place the cursor on it.  If so, the row's height hasn't been
 23092          computed yet.  */
 23093       if (row->height == 0)
 23094         {
 23095           if (it->max_ascent + it->max_descent == 0)
 23096             it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
 23097           row->ascent = it->max_ascent;
 23098           row->height = it->max_ascent + it->max_descent;
 23099           row->phys_ascent = it->max_phys_ascent;
 23100           row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 23101           row->extra_line_spacing = it->max_extra_line_spacing;
 23102         }
 23103 
 23104       /* Compute the width of this line.  */
 23105       row->pixel_width = row->x;
 23106       for (i = 0; i < row->used[TEXT_AREA]; ++i)
 23107         row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
 23108 
 23109       eassert (row->pixel_width >= 0);
 23110       eassert (row->ascent >= 0 && row->height > 0);
 23111 
 23112       row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
 23113                             || MATRIX_ROW_OVERLAPS_PRED_P (row));
 23114 
 23115       /* If first line's physical ascent is larger than its logical
 23116          ascent, use the physical ascent, and make the row taller.
 23117          This makes accented characters fully visible.  */
 23118       if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
 23119           && row->phys_ascent > row->ascent)
 23120         {
 23121           row->height += row->phys_ascent - row->ascent;
 23122           row->ascent = row->phys_ascent;
 23123         }
 23124 
 23125       /* Compute how much of the line is visible.  */
 23126       row->visible_height = row->height;
 23127 
 23128       min_y = WINDOW_TAB_LINE_HEIGHT (it->w) + WINDOW_HEADER_LINE_HEIGHT (it->w);
 23129       max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
 23130 
 23131       if (row->y < min_y)
 23132         row->visible_height -= min_y - row->y;
 23133       if (row->y + row->height > max_y)
 23134         row->visible_height -= row->y + row->height - max_y;
 23135     }
 23136   else
 23137     {
 23138       row->pixel_width = row->used[TEXT_AREA];
 23139       if (row->continued_p)
 23140         row->pixel_width -= it->continuation_pixel_width;
 23141       else if (row->truncated_on_right_p)
 23142         row->pixel_width -= it->truncation_pixel_width;
 23143       row->ascent = row->phys_ascent = 0;
 23144       row->height = row->phys_height = row->visible_height = 1;
 23145       row->extra_line_spacing = 0;
 23146     }
 23147 
 23148   /* Compute a hash code for this row.  */
 23149   row->hash = row_hash (row);
 23150 
 23151   it->max_ascent = it->max_descent = 0;
 23152   it->max_phys_ascent = it->max_phys_descent = 0;
 23153 }
 23154 
 23155 
 23156 static void
 23157 clear_position (struct it *it)
 23158 {
 23159   it->position.charpos = 0;
 23160   it->position.bytepos = 0;
 23161 }
 23162 
 23163 /* Append one space to the glyph row of iterator IT if doing a
 23164    window-based redisplay.  The space has the same face as
 23165    IT->face_id.  Value is true if a space was added.
 23166 
 23167    This function is called to make sure that there is always one glyph
 23168    at the end of a glyph row that the cursor can be set on under
 23169    window-systems.  (If there weren't such a glyph we would not know
 23170    how wide and tall a box cursor should be displayed).
 23171 
 23172    At the same time this space let's a nicely handle clearing to the
 23173    end of the line if the row ends in italic text.  */
 23174 
 23175 static bool
 23176 append_space_for_newline (struct it *it, bool default_face_p)
 23177 {
 23178   int n = it->glyph_row->used[TEXT_AREA];
 23179 
 23180   if (it->glyph_row->glyphs[TEXT_AREA] + n
 23181       < it->glyph_row->glyphs[1 + TEXT_AREA])
 23182     {
 23183       /* Save some values that must not be changed.
 23184          Must save IT->c and IT->len because otherwise
 23185          ITERATOR_AT_END_P wouldn't work anymore after
 23186          append_space_for_newline has been called.  */
 23187       enum display_element_type saved_what = it->what;
 23188       int saved_c = it->c, saved_len = it->len;
 23189       int saved_char_to_display = it->char_to_display;
 23190       int saved_x = it->current_x;
 23191       const int saved_face_id = it->face_id;
 23192       bool saved_box_end = it->end_of_box_run_p;
 23193       struct text_pos saved_pos = it->position;
 23194       Lisp_Object saved_object = it->object;
 23195       struct face *face;
 23196 
 23197       it->what = IT_CHARACTER;
 23198       clear_position (it);
 23199       it->object = Qnil;
 23200       it->len = 1;
 23201 
 23202       int char_width = 1;
 23203 
 23204       if (default_face_p
 23205 #ifdef HAVE_WINDOW_SYSTEM
 23206           || FRAME_WINDOW_P (it->f)
 23207 #endif
 23208           )
 23209         {
 23210           const int local_default_face_id =
 23211             lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
 23212 
 23213 #ifdef HAVE_WINDOW_SYSTEM
 23214           if (FRAME_WINDOW_P (it->f))
 23215             {
 23216               struct face *default_face
 23217                 = FACE_FROM_ID (it->f, local_default_face_id);
 23218               struct font *font = (default_face->font
 23219                                    ? default_face->font
 23220                                    : FRAME_FONT (it->f));
 23221               char_width = (font->average_width
 23222                             ? font->average_width
 23223                             : font->space_width);
 23224             }
 23225 #endif
 23226           if (default_face_p)
 23227             it->face_id = local_default_face_id;
 23228         }
 23229       /* Corner case for when display-fill-column-indicator-mode
 23230          is active and the extra character should be added in the
 23231          same place than the line.  */
 23232 
 23233       const int indicator_column =
 23234         fill_column_indicator_column (it, char_width);
 23235       int saved_end_of_box_run = it->end_of_box_run_p;
 23236       bool should_keep_end_of_box_run = false;
 23237 
 23238       if (it->current_x == indicator_column)
 23239         {
 23240           it->c = it->char_to_display
 23241             = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23242           it->face_id
 23243             = merge_faces (it->w, Qfill_column_indicator,
 23244                            0, saved_face_id);
 23245           face = FACE_FROM_ID (it->f, it->face_id);
 23246         }
 23247       else
 23248         {
 23249           it->c = it->char_to_display = ' ';
 23250           /* If the default face was remapped, be sure to use the
 23251              remapped face for the appended newline.  */
 23252 
 23253           face = FACE_FROM_ID (it->f, it->face_id);
 23254           it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
 23255           /* In R2L rows, we will prepend a stretch glyph that will
 23256              have the end_of_box_run_p flag set for it, so there's no
 23257              need for the appended newline glyph to have that flag
 23258              set.  */
 23259           if (!(it->glyph_row->reversed_p
 23260                 /* But if the appended newline glyph goes all the way to
 23261                    the end of the row, there will be no stretch glyph,
 23262                    so leave the box flag set.  */
 23263                 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x))
 23264             should_keep_end_of_box_run = true;
 23265         }
 23266       PRODUCE_GLYPHS (it);
 23267       /* Restore the end_of_box_run_p flag which was reset by
 23268          PRODUCE_GLYPHS.  */
 23269       if (should_keep_end_of_box_run)
 23270         it->end_of_box_run_p = saved_end_of_box_run;
 23271 #ifdef HAVE_WINDOW_SYSTEM
 23272       if (FRAME_WINDOW_P (it->f))
 23273         {
 23274           /* Make sure this space glyph has the right ascent and
 23275              descent values, or else cursor at end of line will look
 23276              funny, and height of empty lines will be incorrect.  */
 23277           struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
 23278           struct font *font = face->font ? face->font : FRAME_FONT (it->f);
 23279           if (n == 0)
 23280             {
 23281               Lisp_Object height, total_height;
 23282               int extra_line_spacing = it->extra_line_spacing;
 23283               int boff = font->baseline_offset;
 23284 
 23285               if (font->vertical_centering)
 23286                 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 23287 
 23288               it->object = saved_object; /* get_it_property needs this */
 23289               normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 23290               /* Must do a subset of line height processing from
 23291                  gui_produce_glyph for newline characters.  */
 23292               height = get_it_property (it, Qline_height);
 23293               if (CONSP (height)
 23294                   && CONSP (XCDR (height))
 23295                   && NILP (XCDR (XCDR (height))))
 23296                 {
 23297                   total_height = XCAR (XCDR (height));
 23298                   height = XCAR (height);
 23299                 }
 23300               else
 23301                 total_height = Qnil;
 23302               height = calc_line_height_property (it, height, font, boff, true);
 23303 
 23304               if (it->override_ascent >= 0)
 23305                 {
 23306                   it->ascent = it->override_ascent;
 23307                   it->descent = it->override_descent;
 23308                   boff = it->override_boff;
 23309                 }
 23310               if (EQ (height, Qt))
 23311                 extra_line_spacing = 0;
 23312               else
 23313                 {
 23314                   Lisp_Object spacing;
 23315 
 23316                   it->phys_ascent = it->ascent;
 23317                   it->phys_descent = it->descent;
 23318                   if (!NILP (height)
 23319                       && XFIXNUM (height) > it->ascent + it->descent)
 23320                     it->ascent = XFIXNUM (height) - it->descent;
 23321 
 23322                   if (!NILP (total_height))
 23323                     spacing = calc_line_height_property (it, total_height, font,
 23324                                                          boff, false);
 23325                   else
 23326                     {
 23327                       spacing = get_it_property (it, Qline_spacing);
 23328                       spacing = calc_line_height_property (it, spacing, font,
 23329                                                            boff, false);
 23330                     }
 23331                   if (FIXNUMP (spacing))
 23332                     {
 23333                       extra_line_spacing = XFIXNUM (spacing);
 23334                       if (!NILP (total_height))
 23335                         extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 23336                     }
 23337                 }
 23338               if (extra_line_spacing > 0)
 23339                 {
 23340                   it->descent += extra_line_spacing;
 23341                   if (extra_line_spacing > it->max_extra_line_spacing)
 23342                     it->max_extra_line_spacing = extra_line_spacing;
 23343                 }
 23344               it->max_ascent = it->ascent;
 23345               it->max_descent = it->descent;
 23346               /* Make sure compute_line_metrics recomputes the row height.  */
 23347               it->glyph_row->height = 0;
 23348             }
 23349 
 23350           g->ascent = it->max_ascent;
 23351           g->descent = it->max_descent;
 23352         }
 23353 #endif /* HAVE_WINDOW_SYSTEM  */
 23354       it->override_ascent = -1;
 23355       it->constrain_row_ascent_descent_p = false;
 23356       it->current_x = saved_x;
 23357       it->object = saved_object;
 23358       it->position = saved_pos;
 23359       it->what = saved_what;
 23360       it->face_id = saved_face_id;
 23361       it->len = saved_len;
 23362       it->c = saved_c;
 23363       it->char_to_display = saved_char_to_display;
 23364       it->end_of_box_run_p = saved_box_end;
 23365       return true;
 23366     }
 23367 
 23368   return false;
 23369 }
 23370 
 23371 
 23372 /* Extend the face of the last glyph in the text area of IT->glyph_row
 23373    to the end of the display line.  Called from display_line.  If the
 23374    glyph row is empty, add a space glyph to it so that we know the
 23375    face to draw.  Set the glyph row flag fill_line_p.  If the glyph
 23376    row is R2L, prepend a stretch glyph to cover the empty space to the
 23377    left of the leftmost glyph.  */
 23378 
 23379 static void
 23380 extend_face_to_end_of_line (struct it *it)
 23381 {
 23382   struct frame *f = it->f;
 23383 
 23384   /* If line is already filled, do nothing.  Non window-system frames
 23385      get a grace of one more ``pixel'' because their characters are
 23386      1-``pixel'' wide, so they hit the equality too early.  This grace
 23387      is needed only for R2L rows that are not continued, to produce
 23388      one extra blank where we could display the cursor.  */
 23389   if ((it->current_x >= it->last_visible_x
 23390        + (!FRAME_WINDOW_P (f)
 23391           && it->glyph_row->reversed_p
 23392           && !it->glyph_row->continued_p))
 23393       /* If the window has display margins, we will need to extend
 23394          their face even if the text area is filled.  */
 23395       && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23396            || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
 23397     return;
 23398 
 23399   specpdl_ref count = SPECPDL_INDEX ();
 23400 
 23401   /* Don't allow the user to quit out of face-merging code, in case
 23402      this is called when redisplaying a non-selected window, with
 23403      point temporarily moved to window-point.  */
 23404   specbind (Qinhibit_quit, Qt);
 23405   /* The default face, possibly remapped. */
 23406   struct face *default_face =
 23407     FACE_FROM_ID_OR_NULL (f, lookup_basic_face (it->w, f, DEFAULT_FACE_ID));
 23408   if (!default_face)
 23409     return;
 23410 
 23411   const int extend_face_id =
 23412     (it->face_id == default_face->id || it->s != NULL)
 23413     ? it->face_id
 23414     : (it->glyph_row->ends_at_zv_p
 23415        ? default_face->id
 23416        : face_at_pos (it, LFACE_EXTEND_INDEX));
 23417   unbind_to (count, Qnil);
 23418 
 23419   /* Face extension extends the background and box of IT->extend_face_id
 23420      to the end of the line.  If the background equals the background
 23421      of the frame, we don't have to do anything.  */
 23422   struct face *face = FACE_FROM_ID (f, (it->face_before_selective_p
 23423                                         ? it->saved_face_id
 23424                                         : extend_face_id));
 23425 
 23426   if (FRAME_WINDOW_P (f)
 23427       && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
 23428       && face->box == FACE_NO_BOX
 23429       && face->underline == FACE_NO_UNDERLINE
 23430       && !face->overline_p
 23431       && !face->strike_through_p
 23432       && face->background == FRAME_BACKGROUND_PIXEL (f)
 23433 #ifdef HAVE_WINDOW_SYSTEM
 23434       && !face->stipple
 23435 #endif
 23436       && !it->glyph_row->reversed_p
 23437       && !display_fill_column_indicator)
 23438     return;
 23439 
 23440   /* Set the glyph row flag indicating that the face of the last glyph
 23441      in the text area has to be drawn to the end of the text area.  */
 23442   it->glyph_row->fill_line_p = true;
 23443 
 23444   const int orig_face_id = it->face_id;
 23445   /* If current character of IT is not ASCII, make sure we have the
 23446      ASCII face.  This will be automatically undone the next time
 23447      get_next_display_element returns a multibyte character.  Note
 23448      that the character will always be single byte in unibyte
 23449      text.  */
 23450   if (!ASCII_CHAR_P (it->c))
 23451     it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
 23452 
 23453 
 23454 #ifdef HAVE_WINDOW_SYSTEM
 23455   if (FRAME_WINDOW_P (f))
 23456     {
 23457       /* If the row is empty, add a space with the current face of IT,
 23458          so that we know which face to draw.  */
 23459       if (it->glyph_row->used[TEXT_AREA] == 0)
 23460         {
 23461           it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
 23462           it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
 23463           it->glyph_row->used[TEXT_AREA] = 1;
 23464         }
 23465       /* Mode line and the header line don't have margins, and
 23466          likewise the frame's tool-bar window, if there is any.  */
 23467       if (!(it->glyph_row->mode_line_p
 23468             || (WINDOWP (f->tab_bar_window)
 23469                 && it->w == XWINDOW (f->tab_bar_window))
 23470 #ifndef HAVE_EXT_TOOL_BAR
 23471             || (WINDOWP (f->tool_bar_window)
 23472                 && it->w == XWINDOW (f->tool_bar_window))
 23473 #endif
 23474             ))
 23475         {
 23476           if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23477               && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
 23478             {
 23479               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
 23480               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
 23481                 default_face->id;
 23482               it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
 23483             }
 23484           if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23485               && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
 23486             {
 23487               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
 23488               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
 23489                 default_face->id;
 23490               it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
 23491             }
 23492 
 23493           struct font *font = (default_face->font
 23494                                ? default_face->font
 23495                                : FRAME_FONT (f));
 23496 
 23497           const int char_width = (font->average_width
 23498                                   ? font->average_width
 23499                                   : font->space_width);
 23500 
 23501           const int indicator_column =
 23502             fill_column_indicator_column (it, char_width);
 23503 
 23504           const char saved_char = it->char_to_display;
 23505           const struct text_pos saved_pos = it->position;
 23506           const bool saved_avoid_cursor = it->avoid_cursor_p;
 23507           const bool saved_box_start = it->start_of_box_run_p;
 23508           Lisp_Object save_object = it->object;
 23509           const int saved_face_id = it->face_id;
 23510 
 23511           it->face_id = extend_face_id;
 23512           it->avoid_cursor_p = true;
 23513           it->object = Qnil;
 23514 
 23515           const int stretch_height = it->ascent + it->descent;
 23516           const int stretch_ascent =
 23517             (stretch_height * FONT_BASE (font)) / FONT_HEIGHT (font);
 23518 
 23519           if (indicator_column >= 0
 23520               && indicator_column > it->current_x
 23521               && indicator_column < it->last_visible_x)
 23522             {
 23523 
 23524               /* Here we subtract char_width because we want the
 23525                  column indicator in the column INDICATOR_COLUMN,
 23526                  not after it.  */
 23527               const int stretch_width =
 23528                 indicator_column - it->current_x - char_width;
 23529 
 23530               clear_position (it);
 23531 
 23532               /* Only generate a stretch glyph if there is distance
 23533                  between current_x and the indicator position.  */
 23534               if (stretch_width > 0)
 23535                 {
 23536                   append_stretch_glyph (it, Qnil, stretch_width,
 23537                                         stretch_height, stretch_ascent);
 23538                 }
 23539 
 23540               /* Generate the glyph indicator only if
 23541                  append_space_for_newline didn't already.  */
 23542               if (it->current_x < indicator_column)
 23543                 {
 23544                   const int save_face_id = it->face_id;
 23545                   const int save_ascent = it->ascent;
 23546                   const int save_descent = it->descent;
 23547                   it->char_to_display
 23548                     = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23549                   it->face_id
 23550                     = merge_faces (it->w, Qfill_column_indicator,
 23551                                    0, extend_face_id);
 23552                   PRODUCE_GLYPHS (it);
 23553                   it->face_id = save_face_id;
 23554                   it->ascent = save_ascent;
 23555                   it->descent = save_descent;
 23556                 }
 23557             }
 23558 
 23559           /* Fill space until window edge with the merged face.  Do that
 23560              only for L2R rows, as R2L rows are handled specially below.  */
 23561           if (!it->glyph_row->reversed_p)
 23562             {
 23563               const int stretch_width = it->last_visible_x - it->current_x;
 23564 
 23565               if (stretch_width > 0)
 23566                 {
 23567                   clear_position (it);
 23568                   append_stretch_glyph (it, Qnil, stretch_width,
 23569                                         stretch_height, stretch_ascent);
 23570                 }
 23571             }
 23572 
 23573           it->char_to_display = saved_char;
 23574           it->position = saved_pos;
 23575           it->avoid_cursor_p = saved_avoid_cursor;
 23576           it->start_of_box_run_p = saved_box_start;
 23577           it->object = save_object;
 23578           it->face_id = saved_face_id;
 23579         }
 23580       if (it->glyph_row->reversed_p)
 23581         {
 23582           /* Prepend a stretch glyph to the row, such that the
 23583              rightmost glyph will be drawn flushed all the way to the
 23584              right margin of the window.  The stretch glyph that will
 23585              occupy the empty space, if any, to the left of the
 23586              glyph.  */
 23587           struct font *font = face->font ? face->font : FRAME_FONT (f);
 23588           struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
 23589           struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
 23590           struct glyph *g;
 23591           int row_width, stretch_ascent, stretch_width;
 23592           struct text_pos saved_pos;
 23593           int saved_face_id;
 23594           bool saved_avoid_cursor, saved_box_start;
 23595 
 23596           for (row_width = 0, g = row_start; g < row_end; g++)
 23597             row_width += g->pixel_width;
 23598 
 23599           /* FIXME: There are various minor display glitches in R2L
 23600              rows when only one of the fringes is missing.  The
 23601              strange condition below produces the least bad effect.  */
 23602           if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 23603               == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
 23604               || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
 23605             stretch_width = window_box_width (it->w, TEXT_AREA);
 23606           else
 23607             stretch_width = it->last_visible_x - it->first_visible_x;
 23608           stretch_width -= row_width;
 23609 
 23610           if (stretch_width > 0)
 23611             {
 23612               stretch_ascent =
 23613                 (((it->ascent + it->descent)
 23614                   * FONT_BASE (font)) / FONT_HEIGHT (font));
 23615               saved_pos = it->position;
 23616               clear_position (it);
 23617               saved_avoid_cursor = it->avoid_cursor_p;
 23618               it->avoid_cursor_p = true;
 23619               saved_face_id = it->face_id;
 23620               saved_box_start = it->start_of_box_run_p;
 23621               /* The last row's stretch glyph should get the default
 23622                  face, to avoid painting the rest of the window with
 23623                  the region face, if the region ends at ZV.  */
 23624               it->face_id = (it->glyph_row->ends_at_zv_p ?
 23625                              default_face->id : face->id);
 23626 
 23627               it->start_of_box_run_p = false;
 23628               append_stretch_glyph (it, Qnil, stretch_width,
 23629                                     it->ascent + it->descent, stretch_ascent);
 23630               it->position = saved_pos;
 23631               it->avoid_cursor_p = saved_avoid_cursor;
 23632               it->face_id = saved_face_id;
 23633               it->start_of_box_run_p = saved_box_start;
 23634             }
 23635           /* If stretch_width comes out negative, it means that the
 23636              last glyph is only partially visible.  In R2L rows, we
 23637              want the leftmost glyph to be partially visible, so we
 23638              need to give the row the corresponding left offset.  */
 23639           if (stretch_width < 0)
 23640             it->glyph_row->x = stretch_width;
 23641         }
 23642       it->face_id = orig_face_id;
 23643     }
 23644   else
 23645 #endif  /* HAVE_WINDOW_SYSTEM */
 23646     {
 23647       /* Save some values that must not be changed.  */
 23648       int saved_x = it->current_x;
 23649       struct text_pos saved_pos = it->position;
 23650       Lisp_Object saved_object = it->object;;
 23651       enum display_element_type saved_what = it->what;
 23652 
 23653       it->what = IT_CHARACTER;
 23654       clear_position (it);
 23655       it->object = Qnil;
 23656       it->c = it->char_to_display = ' ';
 23657       it->len = 1;
 23658 
 23659       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23660           && (it->glyph_row->used[LEFT_MARGIN_AREA]
 23661               < WINDOW_LEFT_MARGIN_WIDTH (it->w))
 23662           && !it->glyph_row->mode_line_p
 23663           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23664         {
 23665           struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
 23666           struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
 23667 
 23668           for (it->current_x = 0; g < e; g++)
 23669             it->current_x += g->pixel_width;
 23670 
 23671           it->area = LEFT_MARGIN_AREA;
 23672           it->face_id = default_face->id;
 23673           while (it->glyph_row->used[LEFT_MARGIN_AREA]
 23674                  < WINDOW_LEFT_MARGIN_WIDTH (it->w)
 23675                  && g < it->glyph_row->glyphs[TEXT_AREA])
 23676             {
 23677               PRODUCE_GLYPHS (it);
 23678               /* term.c:produce_glyphs advances it->current_x only for
 23679                  TEXT_AREA.  */
 23680               it->current_x += it->pixel_width;
 23681               g++;
 23682             }
 23683 
 23684           it->current_x = saved_x;
 23685           it->area = TEXT_AREA;
 23686         }
 23687 
 23688       /* The last row's blank glyphs should get the default face, to
 23689          avoid painting the rest of the window with the region face,
 23690          if the region ends at ZV.  */
 23691       it->face_id = (it->glyph_row->ends_at_zv_p ?
 23692                      default_face->id : face->id);
 23693 
 23694       /* Make sure our idea of current_x is in sync with the glyphs
 23695          actually in the glyph row.  They might differ because
 23696          append_space_for_newline can insert one glyph without
 23697          updating current_x.  */
 23698       it->current_x = it->glyph_row->used[TEXT_AREA];
 23699 
 23700       /* The above assignment causes the code below to use a
 23701          non-standard semantics of it->current_x: it is measured
 23702          relative to the beginning of the text-area, thus disregarding
 23703          the window's hscroll.  That is why we need to correct the
 23704          indicator column for the hscroll, otherwise the indicator
 23705          will not move together with the text as result of horizontal
 23706          scrolling.  */
 23707       const int indicator_column =
 23708         fill_column_indicator_column (it, 1) - it->first_visible_x;
 23709 
 23710       /* Display fill-column indicator if needed.  */
 23711       while (it->current_x <= it->last_visible_x)
 23712         {
 23713           if (it->current_x != indicator_column)
 23714             PRODUCE_GLYPHS (it);
 23715           else
 23716             {
 23717               int saved_face_id = it->face_id;
 23718               it->face_id
 23719                 = merge_faces (it->w, Qfill_column_indicator, 0, extend_face_id);
 23720               it->c = it->char_to_display
 23721                 = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23722 
 23723               PRODUCE_GLYPHS (it);
 23724 
 23725               it->face_id = saved_face_id;
 23726               it->c = it->char_to_display = ' ';
 23727             }
 23728         }
 23729 
 23730       if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23731           && (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23732               < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
 23733           && !it->glyph_row->mode_line_p
 23734           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23735         {
 23736           struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
 23737           struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
 23738 
 23739           for ( ; g < e; g++)
 23740             it->current_x += g->pixel_width;
 23741 
 23742           it->area = RIGHT_MARGIN_AREA;
 23743           it->face_id = default_face->id;
 23744           while (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23745                  < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
 23746                  && g < it->glyph_row->glyphs[LAST_AREA])
 23747             {
 23748               PRODUCE_GLYPHS (it);
 23749               it->current_x += it->pixel_width;
 23750               g++;
 23751             }
 23752 
 23753           it->area = TEXT_AREA;
 23754         }
 23755 
 23756       /* Don't count these blanks really.  It would let us insert a left
 23757          truncation glyph below and make us set the cursor on them, maybe.  */
 23758       it->current_x = saved_x;
 23759       it->object = saved_object;
 23760       it->position = saved_pos;
 23761       it->what = saved_what;
 23762       it->face_id = orig_face_id;
 23763     }
 23764 }
 23765 
 23766 
 23767 /* Value is true if text starting at CHARPOS in current_buffer is
 23768    trailing whitespace.  */
 23769 
 23770 static bool
 23771 trailing_whitespace_p (ptrdiff_t charpos)
 23772 {
 23773   ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
 23774   int c = 0;
 23775 
 23776   while (bytepos < ZV_BYTE
 23777          && (c = FETCH_BYTE (bytepos),
 23778              c == ' ' || c == '\t'))
 23779     ++bytepos;
 23780 
 23781   if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
 23782     {
 23783       if (bytepos != PT_BYTE)
 23784         return true;
 23785     }
 23786   return false;
 23787 }
 23788 
 23789 
 23790 /* Highlight trailing whitespace, if any, in row at IT.  */
 23791 
 23792 static void
 23793 highlight_trailing_whitespace (struct it *it)
 23794 {
 23795   struct glyph_row *row = it->glyph_row;
 23796   int used = row->used[TEXT_AREA];
 23797 
 23798   if (used)
 23799     {
 23800       struct glyph *start = row->glyphs[TEXT_AREA];
 23801       struct glyph *glyph = start + used - 1;
 23802 
 23803       if (row->reversed_p)
 23804         {
 23805           /* Right-to-left rows need to be processed in the opposite
 23806              direction, so swap the edge pointers. */
 23807           glyph = start;
 23808           start = row->glyphs[TEXT_AREA] + used - 1;
 23809         }
 23810 
 23811       /* Skip over glyphs inserted to display the cursor at the
 23812          end of a line, for extending the face of the last glyph
 23813          to the end of the line on terminals, and for truncation
 23814          and continuation glyphs.  */
 23815       if (!row->reversed_p)
 23816         {
 23817           while (glyph >= start
 23818                  && (glyph->type == CHAR_GLYPH
 23819                      || glyph->type == STRETCH_GLYPH)
 23820                  && NILP (glyph->object))
 23821             --glyph;
 23822         }
 23823       else
 23824         {
 23825           while (glyph <= start
 23826                  && (glyph->type == CHAR_GLYPH || glyph->type == STRETCH_GLYPH)
 23827                  && NILP (glyph->object))
 23828             ++glyph;
 23829         }
 23830 
 23831       /* If last glyph is a space or stretch, and it's trailing
 23832          whitespace, set the face of all trailing whitespace glyphs in
 23833          IT->glyph_row to `trailing-whitespace'.  */
 23834       if ((row->reversed_p ? glyph <= start : glyph >= start)
 23835           && BUFFERP (glyph->object)
 23836           && (glyph->type == STRETCH_GLYPH
 23837               || (glyph->type == CHAR_GLYPH
 23838                   && glyph->u.ch == ' '))
 23839           && trailing_whitespace_p (glyph->charpos))
 23840         {
 23841           int face_id = lookup_named_face (it->w, it->f, Qtrailing_whitespace, false);
 23842           if (face_id < 0)
 23843             return;
 23844 
 23845           if (!row->reversed_p)
 23846             {
 23847               while (glyph >= start
 23848                      && BUFFERP (glyph->object)
 23849                      && (glyph->type == STRETCH_GLYPH
 23850                          || (glyph->type == CHAR_GLYPH
 23851                              && glyph->u.ch == ' ')))
 23852                 (glyph--)->face_id = face_id;
 23853             }
 23854           else
 23855             {
 23856               while (glyph <= start
 23857                      && BUFFERP (glyph->object)
 23858                      && (glyph->type == STRETCH_GLYPH
 23859                          || (glyph->type == CHAR_GLYPH
 23860                              && glyph->u.ch == ' ')))
 23861                 (glyph++)->face_id = face_id;
 23862             }
 23863         }
 23864     }
 23865 }
 23866 
 23867 
 23868 /* Value is true if glyph row ROW should be
 23869    considered to hold the buffer position CHARPOS.  */
 23870 
 23871 static bool
 23872 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
 23873 {
 23874   bool result = true;
 23875 
 23876   if (charpos == CHARPOS (row->end.pos)
 23877       || charpos == MATRIX_ROW_END_CHARPOS (row))
 23878     {
 23879       /* Suppose the row ends on a string.
 23880          Unless the row is continued, that means it ends on a newline
 23881          in the string.  If it's anything other than a display string
 23882          (e.g., a before-string from an overlay), we don't want the
 23883          cursor there.  (This heuristic seems to give the optimal
 23884          behavior for the various types of multi-line strings.)
 23885          One exception: if the string has `cursor' property on one of
 23886          its characters, we _do_ want the cursor there.  */
 23887       if (CHARPOS (row->end.string_pos) >= 0)
 23888         {
 23889           if (row->continued_p)
 23890             result = true;
 23891           else
 23892             {
 23893               /* Check for `display' property.  */
 23894               struct glyph *beg = row->glyphs[TEXT_AREA];
 23895               struct glyph *end = beg + row->used[TEXT_AREA] - 1;
 23896               struct glyph *glyph;
 23897 
 23898               result = false;
 23899               for (glyph = end; glyph >= beg; --glyph)
 23900                 if (STRINGP (glyph->object))
 23901                   {
 23902                     Lisp_Object prop
 23903                       = Fget_char_property (make_fixnum (charpos),
 23904                                             Qdisplay, Qnil);
 23905                     result =
 23906                       (!NILP (prop)
 23907                        && display_prop_string_p (prop, glyph->object));
 23908                     /* If there's a `cursor' property on one of the
 23909                        string's characters, this row is a cursor row,
 23910                        even though this is not a display string.  */
 23911                     if (!result)
 23912                       {
 23913                         Lisp_Object s = glyph->object;
 23914 
 23915                         for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
 23916                           {
 23917                             ptrdiff_t gpos = glyph->charpos;
 23918 
 23919                             if (!NILP (Fget_char_property (make_fixnum (gpos),
 23920                                                            Qcursor, s)))
 23921                               {
 23922                                 result = true;
 23923                                 break;
 23924                               }
 23925                           }
 23926                       }
 23927                     break;
 23928                   }
 23929             }
 23930         }
 23931       else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 23932         {
 23933           /* If the row ends in middle of a real character,
 23934              and the line is continued, we want the cursor here.
 23935              That's because CHARPOS (ROW->end.pos) would equal
 23936              PT if PT is before the character.  */
 23937           if (!row->ends_in_ellipsis_p)
 23938             result = row->continued_p;
 23939           else
 23940           /* If the row ends in an ellipsis, then
 23941              CHARPOS (ROW->end.pos) will equal point after the
 23942              invisible text.  We want that position to be displayed
 23943              after the ellipsis.  */
 23944             result = false;
 23945         }
 23946       /* If the row ends at ZV, display the cursor at the end of that
 23947          row instead of at the start of the row below.  */
 23948       else
 23949         result = row->ends_at_zv_p;
 23950     }
 23951 
 23952   return result;
 23953 }
 23954 
 23955 /* Value is true if glyph row ROW should be
 23956    used to hold the cursor.  */
 23957 
 23958 static bool
 23959 cursor_row_p (struct glyph_row *row)
 23960 {
 23961   return row_for_charpos_p (row, PT);
 23962 }
 23963 
 23964 
 23965 
 23966 /* Push the property PROP so that it will be rendered at the current
 23967    position in IT.  Return true if PROP was successfully pushed, false
 23968    otherwise.  Called from handle_line_prefix to handle the
 23969    `line-prefix' and `wrap-prefix' properties.  */
 23970 
 23971 static bool
 23972 push_prefix_prop (struct it *it, Lisp_Object prop)
 23973 {
 23974   struct text_pos pos =
 23975     STRINGP (it->string) ? it->current.string_pos : it->current.pos;
 23976 
 23977   eassert (it->method == GET_FROM_BUFFER
 23978            || it->method == GET_FROM_DISPLAY_VECTOR
 23979            || it->method == GET_FROM_STRING
 23980            || it->method == GET_FROM_IMAGE);
 23981 
 23982   /* We need to save the current buffer/string position, so it will be
 23983      restored by pop_it, because iterate_out_of_display_property
 23984      depends on that being set correctly, but some situations leave
 23985      it->position not yet set when this function is called.  */
 23986   push_it (it, &pos);
 23987 
 23988   if (STRINGP (prop))
 23989     {
 23990       if (SCHARS (prop) == 0)
 23991         {
 23992           pop_it (it);
 23993           return false;
 23994         }
 23995 
 23996       it->string = prop;
 23997       it->string_from_prefix_prop_p = true;
 23998       it->multibyte_p = STRING_MULTIBYTE (it->string);
 23999       it->current.overlay_string_index = -1;
 24000       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
 24001       it->end_charpos = it->string_nchars = SCHARS (it->string);
 24002       it->method = GET_FROM_STRING;
 24003       it->stop_charpos = 0;
 24004       it->prev_stop = 0;
 24005       it->base_level_stop = 0;
 24006       it->cmp_it.id = -1;
 24007 
 24008       /* Force paragraph direction to be that of the parent
 24009          buffer/string.  */
 24010       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 24011         it->paragraph_embedding = it->bidi_it.paragraph_dir;
 24012       else
 24013         it->paragraph_embedding = L2R;
 24014 
 24015       /* Set up the bidi iterator for this display string.  */
 24016       if (it->bidi_p)
 24017         {
 24018           it->bidi_it.string.lstring = it->string;
 24019           it->bidi_it.string.s = NULL;
 24020           it->bidi_it.string.schars = it->end_charpos;
 24021           it->bidi_it.string.bufpos = IT_CHARPOS (*it);
 24022           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
 24023           it->bidi_it.string.unibyte = !it->multibyte_p;
 24024           it->bidi_it.w = it->w;
 24025           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
 24026         }
 24027     }
 24028   else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
 24029     {
 24030       it->method = GET_FROM_STRETCH;
 24031       it->object = prop;
 24032     }
 24033 #ifdef HAVE_WINDOW_SYSTEM
 24034   else if (IMAGEP (prop))
 24035     {
 24036       it->what = IT_IMAGE;
 24037       it->image_id = lookup_image (it->f, prop, it->face_id);
 24038       it->method = GET_FROM_IMAGE;
 24039     }
 24040 #endif /* HAVE_WINDOW_SYSTEM */
 24041   else
 24042     {
 24043       pop_it (it);              /* bogus display property, give up */
 24044       return false;
 24045     }
 24046 
 24047   return true;
 24048 }
 24049 
 24050 /* Return the character-property PROP at the current position in IT.  */
 24051 
 24052 static Lisp_Object
 24053 get_it_property (struct it *it, Lisp_Object prop)
 24054 {
 24055   Lisp_Object position, object = it->object;
 24056 
 24057   if (STRINGP (object))
 24058     position = make_fixnum (IT_STRING_CHARPOS (*it));
 24059   else if (BUFFERP (object))
 24060     {
 24061       position = make_fixnum (IT_CHARPOS (*it));
 24062       object = it->window;
 24063     }
 24064   else
 24065     return Qnil;
 24066 
 24067   return Fget_char_property (position, prop, object);
 24068 }
 24069 
 24070 /* Return the line-prefix/wrap-prefix property, checking both the
 24071    current IT->OBJECT and the underlying buffer text.  */
 24072 
 24073 static Lisp_Object
 24074 get_line_prefix_it_property (struct it *it, Lisp_Object prop)
 24075 {
 24076   Lisp_Object prefix = get_it_property (it, prop);
 24077 
 24078   /* If we are looking at a display or overlay string, check also the
 24079      underlying buffer text.  */
 24080   if (NILP (prefix) && it->sp > 0 && STRINGP (it->object))
 24081     return Fget_char_property (make_fixnum (IT_CHARPOS (*it)), prop,
 24082                                it->w->contents);
 24083   return prefix;
 24084 }
 24085 
 24086 /* See if there's a line- or wrap-prefix, and if so, push it on IT.  */
 24087 
 24088 static void
 24089 handle_line_prefix (struct it *it)
 24090 {
 24091   Lisp_Object prefix;
 24092 
 24093   if (it->continuation_lines_width > 0)
 24094     {
 24095       prefix = get_line_prefix_it_property (it, Qwrap_prefix);
 24096       if (NILP (prefix))
 24097         prefix = Vwrap_prefix;
 24098     }
 24099   else
 24100     {
 24101       prefix = get_line_prefix_it_property (it, Qline_prefix);
 24102       if (NILP (prefix))
 24103         prefix = Vline_prefix;
 24104     }
 24105   if (! NILP (prefix) && push_prefix_prop (it, prefix))
 24106     {
 24107       /* If the prefix is wider than the window, and we try to wrap
 24108          it, it would acquire its own wrap prefix, and so on till the
 24109          iterator stack overflows.  So, don't wrap the prefix.  */
 24110       it->line_wrap = TRUNCATE;
 24111       it->avoid_cursor_p = true;
 24112     }
 24113 }
 24114 
 24115 
 24116 
 24117 /* Remove N glyphs at the start of a reversed IT->glyph_row.  Called
 24118    only for R2L lines from display_line and display_string, when they
 24119    decide that too many glyphs were produced by PRODUCE_GLYPHS, and
 24120    the line/string needs to be continued on the next glyph row.  */
 24121 static void
 24122 unproduce_glyphs (struct it *it, int n)
 24123 {
 24124   struct glyph *glyph, *end;
 24125 
 24126   eassert (it->glyph_row);
 24127   eassert (it->glyph_row->reversed_p);
 24128   eassert (it->area == TEXT_AREA);
 24129   eassert (n <= it->glyph_row->used[TEXT_AREA]);
 24130 
 24131   if (n > it->glyph_row->used[TEXT_AREA])
 24132     n = it->glyph_row->used[TEXT_AREA];
 24133   glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
 24134   end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
 24135   for ( ; glyph < end; glyph++)
 24136     glyph[-n] = *glyph;
 24137 }
 24138 
 24139 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
 24140    and ROW->maxpos.  */
 24141 static void
 24142 find_row_edges (struct it *it, struct glyph_row *row,
 24143                 ptrdiff_t min_pos, ptrdiff_t min_bpos,
 24144                 ptrdiff_t max_pos, ptrdiff_t max_bpos)
 24145 {
 24146   /* FIXME: Revisit this when glyph ``spilling'' in continuation
 24147      lines' rows is implemented for bidi-reordered rows.  */
 24148 
 24149   /* ROW->minpos is the value of min_pos, the minimal buffer position
 24150      we have in ROW, or ROW->start.pos if that is smaller.  */
 24151   if (min_pos <= ZV && min_pos < row->start.pos.charpos)
 24152     SET_TEXT_POS (row->minpos, min_pos, min_bpos);
 24153   else
 24154     /* We didn't find buffer positions smaller than ROW->start, or
 24155        didn't find _any_ valid buffer positions in any of the glyphs,
 24156        so we must trust the iterator's computed positions.  */
 24157       row->minpos = row->start.pos;
 24158   if (max_pos <= 0)
 24159     {
 24160       max_pos = CHARPOS (it->current.pos);
 24161       max_bpos = BYTEPOS (it->current.pos);
 24162     }
 24163 
 24164   /* Here are the various use-cases for ending the row, and the
 24165      corresponding values for ROW->maxpos:
 24166 
 24167      Line ends in a newline from buffer       eol_pos + 1
 24168      Line is continued from buffer            max_pos + 1
 24169      Line is truncated on right               it->current.pos
 24170      Line ends in a newline from string       max_pos + 1(*)
 24171       (*) + 1 only when line ends in a forward scan
 24172      Line is continued from string            max_pos
 24173      Line is continued from display vector    max_pos
 24174      Line is entirely from a string           min_pos == max_pos
 24175      Line is entirely from a display vector   min_pos == max_pos
 24176      Line that ends at ZV                     ZV
 24177 
 24178      If you discover other use-cases, please add them here as
 24179      appropriate.  */
 24180   if (row->ends_at_zv_p)
 24181     row->maxpos = it->current.pos;
 24182   else if (row->used[TEXT_AREA])
 24183     {
 24184       bool seen_this_string = false;
 24185       struct glyph_row *r1 = row - 1;
 24186 
 24187       /* Did we see the same display string on the previous row?  */
 24188       if (STRINGP (it->object)
 24189           /* this is not the first row */
 24190           && row > it->w->desired_matrix->rows
 24191           /* previous row is not the header line or tab-line */
 24192           && !r1->mode_line_p
 24193           /* previous row also ends in a newline from a string */
 24194           && r1->ends_in_newline_from_string_p)
 24195         {
 24196           struct glyph *start, *end;
 24197 
 24198           /* Search for the last glyph of the previous row that came
 24199              from buffer or string.  Depending on whether the row is
 24200              L2R or R2L, we need to process it front to back or the
 24201              other way round.  */
 24202           if (!r1->reversed_p)
 24203             {
 24204               start = r1->glyphs[TEXT_AREA];
 24205               end = start + r1->used[TEXT_AREA];
 24206               /* Glyphs inserted by redisplay have nil as their object.  */
 24207               while (end > start
 24208                      && NILP ((end - 1)->object)
 24209                      && (end - 1)->charpos <= 0)
 24210                 --end;
 24211               if (end > start)
 24212                 {
 24213                   if (EQ ((end - 1)->object, it->object))
 24214                     seen_this_string = true;
 24215                 }
 24216               else
 24217                 /* If all the glyphs of the previous row were inserted
 24218                    by redisplay, it means the previous row was
 24219                    produced from a single newline, which is only
 24220                    possible if that newline came from the same string
 24221                    as the one which produced this ROW.  */
 24222                 seen_this_string = true;
 24223             }
 24224           else
 24225             {
 24226               end = r1->glyphs[TEXT_AREA] - 1;
 24227               start = end + r1->used[TEXT_AREA];
 24228               while (end < start
 24229                      && NILP ((end + 1)->object)
 24230                      && (end + 1)->charpos <= 0)
 24231                 ++end;
 24232               if (end < start)
 24233                 {
 24234                   if (EQ ((end + 1)->object, it->object))
 24235                     seen_this_string = true;
 24236                 }
 24237               else
 24238                 seen_this_string = true;
 24239             }
 24240         }
 24241       /* Take note of each display string that covers a newline only
 24242          once, the first time we see it.  This is for when a display
 24243          string includes more than one newline in it.  */
 24244       if (row->ends_in_newline_from_string_p && !seen_this_string)
 24245         {
 24246           /* If we were scanning the buffer forward when we displayed
 24247              the string, we want to account for at least one buffer
 24248              position that belongs to this row (position covered by
 24249              the display string), so that cursor positioning will
 24250              consider this row as a candidate when point is at the end
 24251              of the visual line represented by this row.  This is not
 24252              required when scanning back, because max_pos will already
 24253              have a much larger value.  */
 24254           if (CHARPOS (row->end.pos) > max_pos)
 24255             inc_both (&max_pos, &max_bpos);
 24256           SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24257         }
 24258       else if (CHARPOS (it->eol_pos) > 0)
 24259         SET_TEXT_POS (row->maxpos,
 24260                       CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
 24261       else if (row->continued_p)
 24262         {
 24263           /* If max_pos is different from IT's current position, it
 24264              means IT->method does not belong to the display element
 24265              at max_pos.  However, it also means that the display
 24266              element at max_pos was displayed in its entirety on this
 24267              line, which is equivalent to saying that the next line
 24268              starts at the next buffer position.  */
 24269           if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
 24270             SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24271           else
 24272             {
 24273               inc_both (&max_pos, &max_bpos);
 24274               SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24275             }
 24276         }
 24277       else if (row->truncated_on_right_p)
 24278         /* display_line already called reseat_at_next_visible_line_start,
 24279            which puts the iterator at the beginning of the next line, in
 24280            the logical order. */
 24281         row->maxpos = it->current.pos;
 24282       else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
 24283         /* A line that is entirely from a string/image/stretch...  */
 24284         row->maxpos = row->minpos;
 24285       else
 24286         emacs_abort ();
 24287     }
 24288   else
 24289     row->maxpos = it->current.pos;
 24290 }
 24291 
 24292 /* Like display_count_lines, but capable of counting outside of the
 24293    current narrowed region.  */
 24294 static ptrdiff_t
 24295 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
 24296                                ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
 24297 {
 24298   if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
 24299     return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24300 
 24301   ptrdiff_t val;
 24302   specpdl_ref pdl_count = SPECPDL_INDEX ();
 24303   record_unwind_protect (save_restriction_restore, save_restriction_save ());
 24304   labeled_restrictions_remove_in_current_buffer ();
 24305   Fwiden ();
 24306   val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24307   unbind_to (pdl_count, Qnil);
 24308   return val;
 24309 }
 24310 
 24311 /* Count the number of screen lines in window IT->w between character
 24312    position IT_CHARPOS(*IT) and the line showing that window's point.  */
 24313 static ptrdiff_t
 24314 display_count_lines_visually (struct it *it)
 24315 {
 24316   struct it tem_it;
 24317   ptrdiff_t to;
 24318   struct text_pos from;
 24319 
 24320   /* If we already calculated a relative line number, use that.  This
 24321      trick relies on the fact that visual lines (a.k.a. "glyph rows")
 24322      are laid out sequentially, one by one, for each sequence of calls
 24323      to display_line or other similar function that follows a call to
 24324      init_iterator.  */
 24325   if (it->lnum_bytepos > 0)
 24326     return it->lnum + 1;
 24327   else
 24328     {
 24329       specpdl_ref count = SPECPDL_INDEX ();
 24330 
 24331       if (IT_CHARPOS (*it) <= PT)
 24332         {
 24333           from = it->current.pos;
 24334           to = PT;
 24335         }
 24336       else
 24337         {
 24338           SET_TEXT_POS (from, PT, PT_BYTE);
 24339           to = IT_CHARPOS (*it);
 24340         }
 24341       /* Need to disable visual mode temporarily, since otherwise the
 24342          call to move_it_to below and inside start_display will cause
 24343          infinite recursion.  */
 24344       specbind (Qdisplay_line_numbers, Qrelative);
 24345       start_display (&tem_it, it->w, from);
 24346       /* Some redisplay optimizations could invoke us very far from
 24347          PT, which will make the caller painfully slow.  There should
 24348          be no need to go too far beyond the window's bottom, as any
 24349          such optimization will fail to show point anyway.  */
 24350       move_it_to (&tem_it, to, -1,
 24351                   tem_it.last_visible_y
 24352                   + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
 24353                   -1, MOVE_TO_POS | MOVE_TO_Y);
 24354       unbind_to (count, Qnil);
 24355       return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
 24356     }
 24357 }
 24358 
 24359 /* Produce the line-number glyphs for the current glyph_row.  If
 24360    IT->glyph_row is non-NULL, populate the row with the produced
 24361    glyphs.  */
 24362 static void
 24363 maybe_produce_line_number (struct it *it)
 24364 {
 24365   ptrdiff_t last_line = it->lnum;
 24366   ptrdiff_t start_from, bytepos;
 24367   ptrdiff_t this_line;
 24368   bool first_time = false;
 24369   ptrdiff_t beg_byte;
 24370   ptrdiff_t z_byte;
 24371   bool line_numbers_wide;
 24372   void *itdata = bidi_shelve_cache ();
 24373 
 24374   if (display_line_numbers_offset
 24375       && !display_line_numbers_widen
 24376       && !EQ (Vdisplay_line_numbers, Qvisual)
 24377       && !EQ (Vdisplay_line_numbers, Qrelative))
 24378     line_numbers_wide = true;
 24379   else
 24380     line_numbers_wide = display_line_numbers_widen;
 24381 
 24382   beg_byte = line_numbers_wide ? BEG_BYTE : BEGV_BYTE;
 24383   z_byte = line_numbers_wide ? Z_BYTE : ZV_BYTE;
 24384 
 24385   if (EQ (Vdisplay_line_numbers, Qvisual))
 24386     this_line = display_count_lines_visually (it);
 24387   else
 24388     {
 24389       if (!last_line)
 24390         {
 24391           /* If possible, reuse data cached by line-number-mode.  */
 24392           if (it->w->base_line_number > 0
 24393               && it->w->base_line_pos > 0
 24394               && it->w->base_line_pos <= IT_CHARPOS (*it)
 24395               /* line-number-mode always displays narrowed line
 24396                  numbers, so we cannot use its data if the user wants
 24397                  line numbers that disregard narrowing, or if the
 24398                  buffer's narrowing has just changed.  */
 24399               && !(line_numbers_wide
 24400                    && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
 24401               && !current_buffer->clip_changed)
 24402             {
 24403               start_from = CHAR_TO_BYTE (it->w->base_line_pos);
 24404               last_line = it->w->base_line_number - 1;
 24405             }
 24406           else
 24407             start_from = beg_byte;
 24408           if (!it->lnum_bytepos)
 24409             first_time = true;
 24410         }
 24411       else
 24412         start_from = it->lnum_bytepos;
 24413 
 24414       /* Paranoia: what if someone changes the narrowing since the
 24415          last time display_line was called?  Shouldn't really happen,
 24416          but who knows what some crazy Lisp invoked by :eval could do?  */
 24417       if (!(beg_byte <= start_from && start_from <= z_byte))
 24418         {
 24419           last_line = 0;
 24420           start_from = beg_byte;
 24421         }
 24422 
 24423       this_line =
 24424         last_line + display_count_lines_logically (start_from,
 24425                                                    IT_BYTEPOS (*it),
 24426                                                    IT_CHARPOS (*it), &bytepos);
 24427       eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
 24428       eassert (bytepos == IT_BYTEPOS (*it));
 24429     }
 24430 
 24431   /* Record the line number information.  */
 24432   if (this_line != last_line || !it->lnum_bytepos)
 24433     {
 24434       it->lnum = this_line;
 24435       it->lnum_bytepos = IT_BYTEPOS (*it);
 24436     }
 24437 
 24438   /* Produce the glyphs for the line number.  */
 24439   struct it tem_it;
 24440   char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
 24441   bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
 24442   ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
 24443   int lnum_face_id = merge_faces (it->w, Qline_number, 0, DEFAULT_FACE_ID);
 24444   int current_lnum_face_id
 24445     = merge_faces (it->w, Qline_number_current_line, 0, DEFAULT_FACE_ID);
 24446   /* From here onwards, we must prevent freeing realized faces, because
 24447      we are using the above 2 face IDs for the glyphs we produce.  */
 24448   bool save_free_realized_faces = inhibit_free_realized_faces;
 24449   inhibit_free_realized_faces = true;
 24450   /* Compute point's line number if needed.  */
 24451   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24452        || EQ (Vdisplay_line_numbers, Qvisual)
 24453        || lnum_face_id != current_lnum_face_id)
 24454       && !it->pt_lnum)
 24455     {
 24456       ptrdiff_t ignored;
 24457       if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
 24458         it->pt_lnum =
 24459           this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
 24460                                                      PT, &ignored);
 24461       else
 24462         it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
 24463                                                      &ignored);
 24464     }
 24465   /* Compute the required width if needed.  */
 24466   if (!it->lnum_width)
 24467     {
 24468       if (FIXNATP (Vdisplay_line_numbers_width))
 24469         it->lnum_width = XFIXNAT (Vdisplay_line_numbers_width);
 24470 
 24471       /* Max line number to be displayed cannot be more than the one
 24472          corresponding to the last row of the desired matrix.  */
 24473       ptrdiff_t max_lnum;
 24474 
 24475       if (NILP (Vdisplay_line_numbers_current_absolute)
 24476           && (EQ (Vdisplay_line_numbers, Qrelative)
 24477               || EQ (Vdisplay_line_numbers, Qvisual)))
 24478         /* We subtract one more because the current line is always
 24479            zero in this mode.  */
 24480         max_lnum = it->w->desired_matrix->nrows - 2;
 24481       else if (EQ (Vdisplay_line_numbers, Qvisual))
 24482         max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
 24483       else
 24484         max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
 24485       max_lnum = max (1, max_lnum);
 24486       it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
 24487       eassert (it->lnum_width > 0);
 24488     }
 24489   if (EQ (Vdisplay_line_numbers, Qrelative))
 24490     lnum_offset = it->pt_lnum;
 24491   else if (EQ (Vdisplay_line_numbers, Qvisual))
 24492     lnum_offset = 0;
 24493   else if (display_line_numbers_offset)
 24494     lnum_offset -= display_line_numbers_offset;
 24495 
 24496   /* Under 'relative', display the absolute line number for the
 24497      current line, unless the user requests otherwise.  */
 24498   ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
 24499   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24500        || EQ (Vdisplay_line_numbers, Qvisual))
 24501       && lnum_to_display == 0
 24502       && !NILP (Vdisplay_line_numbers_current_absolute))
 24503     lnum_to_display = it->pt_lnum + 1;
 24504   /* In L2R rows we need to append the blank separator, in R2L
 24505      rows we need to prepend it.  But this function is usually
 24506      called when no display elements were produced from the
 24507      following line, so the paragraph direction might be unknown.
 24508      Therefore we cheat and add 2 blanks, one on either side.  */
 24509   pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
 24510   strcat (lnum_buf, " ");
 24511 
 24512   /* Setup for producing the glyphs.  */
 24513   init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
 24514                  /* FIXME: Use specialized face.  */
 24515                  DEFAULT_FACE_ID);
 24516   scratch_glyph_row.reversed_p = false;
 24517   scratch_glyph_row.used[TEXT_AREA] = 0;
 24518   SET_TEXT_POS (tem_it.position, 0, 0);
 24519   tem_it.avoid_cursor_p = true;
 24520   tem_it.bidi_p = true;
 24521   tem_it.bidi_it.type = WEAK_EN;
 24522   /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
 24523      1 level in R2L paragraphs.  Emulate that, assuming we are in
 24524      an L2R paragraph.  */
 24525   tem_it.bidi_it.resolved_level = 2;
 24526 
 24527   /* We must leave space for 2 glyphs for continuation and truncation,
 24528      and at least one glyph for buffer text.  */
 24529   int width_limit =
 24530     tem_it.last_visible_x - tem_it.first_visible_x
 24531     - 3 * FRAME_COLUMN_WIDTH (it->f);
 24532 
 24533   tem_it.face_id = lnum_face_id;
 24534   /* Avoid displaying any face other than line-number on
 24535      empty lines beyond EOB.  */
 24536   if (lnum_face_id != current_lnum_face_id
 24537       && (EQ (Vdisplay_line_numbers, Qvisual)
 24538           ? this_line == 0
 24539           : this_line == it->pt_lnum)
 24540       && it->what != IT_EOB)
 24541     tem_it.face_id = current_lnum_face_id;
 24542   else if (!beyond_zv)
 24543     {
 24544       if (display_line_numbers_major_tick > 0
 24545           && (lnum_to_display % display_line_numbers_major_tick == 0))
 24546         tem_it.face_id = merge_faces (it->w, Qline_number_major_tick,
 24547                                       0, DEFAULT_FACE_ID);
 24548       else if (display_line_numbers_minor_tick > 0
 24549                && (lnum_to_display % display_line_numbers_minor_tick == 0))
 24550         tem_it.face_id = merge_faces (it->w, Qline_number_minor_tick,
 24551                                       0, DEFAULT_FACE_ID);
 24552     }
 24553 
 24554   /* Produce glyphs for the line number in a scratch glyph_row.  */
 24555   for (const char *p = lnum_buf; *p; p++)
 24556     {
 24557       /* For continuation lines and lines after ZV, instead of a line
 24558          number, produce a blank prefix of the same width.  */
 24559       if (beyond_zv
 24560           /* Don't display the same line number more than once.  */
 24561           || (!EQ (Vdisplay_line_numbers, Qvisual)
 24562               && (it->continuation_lines_width > 0
 24563                   || (this_line == last_line && !first_time))))
 24564         tem_it.c = tem_it.char_to_display = ' ';
 24565       else
 24566         tem_it.c = tem_it.char_to_display = *p;
 24567       tem_it.len = 1;
 24568       /* Make sure these glyphs will have a "position" of -1.  */
 24569       SET_TEXT_POS (tem_it.position, -1, -1);
 24570       PRODUCE_GLYPHS (&tem_it);
 24571 
 24572       /* Stop producing glyphs, and refrain from producing the line
 24573          number, if we don't have enough space on this line.  */
 24574       if (tem_it.current_x >= width_limit)
 24575         {
 24576           it->lnum_width = 0;
 24577           it->lnum_pixel_width = 0;
 24578           bidi_unshelve_cache (itdata, false);
 24579           inhibit_free_realized_faces = save_free_realized_faces;
 24580           return;
 24581         }
 24582     }
 24583 
 24584   inhibit_free_realized_faces = save_free_realized_faces;
 24585 
 24586   /* Record the width in pixels we need for the line number display.  */
 24587   it->lnum_pixel_width = tem_it.current_x;
 24588   /* Copy the produced glyphs into IT's glyph_row.  */
 24589   struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
 24590   struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
 24591   struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
 24592   short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
 24593 
 24594   eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
 24595 
 24596   for ( ; g < e; g++)
 24597     {
 24598       it->current_x += g->pixel_width;
 24599       /* The following is important when this function is called
 24600          from move_it_in_display_line_to: HPOS is incremented only
 24601          when we are in the visible portion of the glyph row.  */
 24602       if (it->current_x > it->first_visible_x)
 24603         it->hpos++;
 24604       if (p)
 24605         {
 24606           *p++ = *g;
 24607           (*u)++;
 24608         }
 24609     }
 24610 
 24611   /* Update IT's metrics due to glyphs produced for line numbers.
 24612      Don't do that for rows beyond ZV, to avoid displaying a cursor of
 24613      different dimensions there.  */
 24614   if (!beyond_zv)
 24615     {
 24616       if (it->glyph_row)
 24617         {
 24618           struct glyph_row *row = it->glyph_row;
 24619 
 24620           it->max_ascent = max (row->ascent, tem_it.max_ascent);
 24621           it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
 24622           it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
 24623           it->max_phys_descent = max (row->phys_height - row->phys_ascent,
 24624                                       tem_it.max_phys_descent);
 24625         }
 24626       else
 24627         {
 24628           it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
 24629           it->max_descent = max (it->max_descent, tem_it.max_descent);
 24630           it->max_phys_ascent = max (it->max_phys_ascent,
 24631                                      tem_it.max_phys_ascent);
 24632           it->max_phys_descent = max (it->max_phys_descent,
 24633                                       tem_it.max_phys_descent);
 24634         }
 24635     }
 24636 
 24637   it->line_number_produced_p = true;
 24638 
 24639   bidi_unshelve_cache (itdata, false);
 24640 }
 24641 
 24642 /* Return true if this glyph row needs a line number to be produced
 24643    for it.  */
 24644 static bool
 24645 should_produce_line_number (struct it *it)
 24646 {
 24647   if (NILP (Vdisplay_line_numbers))
 24648     return false;
 24649 
 24650   /* Don't display line numbers in minibuffer windows.  */
 24651   if (MINI_WINDOW_P (it->w))
 24652     return false;
 24653 
 24654 #ifdef HAVE_WINDOW_SYSTEM
 24655   /* Don't display line number in tooltip frames.  */
 24656   if (FRAME_TOOLTIP_P (XFRAME (WINDOW_FRAME (it->w))))
 24657     return false;
 24658 #endif
 24659 
 24660   /* If the character at current position has a non-nil special
 24661      property, disable line numbers for this row.  This is for
 24662      packages such as company-mode, which need this for their tricky
 24663      layout, where line numbers get in the way.  */
 24664   Lisp_Object val = Fget_char_property (make_fixnum (IT_CHARPOS (*it)),
 24665                                         Qdisplay_line_numbers_disable,
 24666                                         it->window);
 24667   /* For ZV, we need to also look in empty overlays at that point,
 24668      because get-char-property always returns nil for ZV, except if
 24669      the property is in 'default-text-properties'.  */
 24670   if (NILP (val) && IT_CHARPOS (*it) >= ZV)
 24671     val = disable_line_numbers_overlay_at_eob ();
 24672   return NILP (val) ? true : false;
 24673 }
 24674 
 24675 /* Return true if ROW has no glyphs except those inserted by the
 24676    display engine.  This is needed for indicate-empty-lines and
 24677    similar features when the glyph row starts with glyphs which didn't
 24678    come from buffer or string.  */
 24679 static bool
 24680 row_text_area_empty (struct glyph_row *row)
 24681 {
 24682   if (!row->reversed_p)
 24683     {
 24684       for (struct glyph *g = row->glyphs[TEXT_AREA];
 24685            g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 24686            g++)
 24687         if (!NILP (g->object) || g->charpos > 0)
 24688           return false;
 24689     }
 24690   else
 24691     {
 24692       for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 24693            g > row->glyphs[TEXT_AREA];
 24694            g--)
 24695         if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
 24696           return false;
 24697     }
 24698 
 24699   return true;
 24700 }
 24701 
 24702 /* Construct the glyph row IT->glyph_row in the desired matrix of
 24703    IT->w from text at the current position of IT.  See dispextern.h
 24704    for an overview of struct it.  Value is true if
 24705    IT->glyph_row displays text, as opposed to a line displaying ZV
 24706    only.  CURSOR_VPOS is the window-relative vertical position of
 24707    the glyph row displaying the cursor, or -1 if unknown.  */
 24708 
 24709 static bool
 24710 display_line (struct it *it, int cursor_vpos)
 24711 {
 24712   struct glyph_row *row = it->glyph_row;
 24713   Lisp_Object overlay_arrow_string;
 24714   struct it wrap_it;
 24715   void *wrap_data = NULL;
 24716   bool may_wrap = false;
 24717   int wrap_x UNINIT;
 24718   int wrap_row_used = -1;
 24719   int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
 24720   int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
 24721   int wrap_row_extra_line_spacing UNINIT;
 24722   ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
 24723   ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
 24724   int cvpos;
 24725   ptrdiff_t min_pos = ZV + 1, max_pos = 0;
 24726   ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
 24727   bool pending_handle_line_prefix = false;
 24728   int tab_line = window_wants_tab_line (it->w);
 24729   int header_line = window_wants_header_line (it->w);
 24730   bool hscroll_this_line = (cursor_vpos >= 0
 24731                             && it->vpos == cursor_vpos - tab_line - header_line
 24732                             && hscrolling_current_line_p (it->w));
 24733   int first_visible_x = it->first_visible_x;
 24734   int last_visible_x = it->last_visible_x;
 24735   int x_incr = 0;
 24736 
 24737   /* We always start displaying at hpos zero even if hscrolled.  */
 24738   eassert (it->hpos == 0 && it->current_x == 0);
 24739 
 24740   if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
 24741       >= it->w->desired_matrix->nrows)
 24742     {
 24743       it->w->nrows_scale_factor++;
 24744       it->f->fonts_changed = true;
 24745       return false;
 24746     }
 24747 
 24748   /* Clear the result glyph row and enable it.  */
 24749   prepare_desired_row (it->w, row, false);
 24750 
 24751   row->y = it->current_y;
 24752   row->start = it->start;
 24753   row->continuation_lines_width = it->continuation_lines_width;
 24754   row->displays_text_p = true;
 24755   row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
 24756   it->starts_in_middle_of_char_p = false;
 24757   it->stretch_adjust = 0;
 24758   it->line_number_produced_p = false;
 24759 
 24760   /* If we are going to display the cursor's line, account for the
 24761      hscroll of that line.  We subtract the window's min_hscroll,
 24762      because that was already accounted for in init_iterator.  */
 24763   if (hscroll_this_line)
 24764     x_incr =
 24765       (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
 24766       * FRAME_COLUMN_WIDTH (it->f);
 24767 
 24768   bool line_number_needed = should_produce_line_number (it);
 24769 
 24770   /* Move over display elements that are not visible because we are
 24771      hscrolled.  This may stop at an x-position < first_visible_x
 24772      if the first glyph is partially visible or if we hit a line end.  */
 24773   if (it->current_x < it->first_visible_x + x_incr)
 24774     {
 24775       enum move_it_result move_result;
 24776 
 24777       this_line_min_pos = row->start.pos;
 24778       if (hscroll_this_line)
 24779         {
 24780           it->first_visible_x += x_incr;
 24781           it->last_visible_x  += x_incr;
 24782         }
 24783       if (current_buffer->long_line_optimizations_p
 24784           && it->line_wrap == TRUNCATE
 24785           && window_hscroll_limited (it->w, it->f) > large_hscroll_threshold)
 24786         {
 24787           /* Special optimization for very long and truncated lines
 24788              which are hscrolled far to the left: jump directly to the
 24789              (approximate) position that is visible, instead of slowly
 24790              walking there.  */
 24791           ptrdiff_t chars_to_skip =
 24792             it->first_visible_x / FRAME_COLUMN_WIDTH (it->f);
 24793           move_result = fast_move_it_horizontally (it, chars_to_skip);
 24794 
 24795           if (move_result == MOVE_X_REACHED)
 24796             it->current_x = it->first_visible_x;
 24797           else  /* use arbitrary value < first_visible_x */
 24798             it->current_x = it->first_visible_x - FRAME_COLUMN_WIDTH (it->f);
 24799         }
 24800       else
 24801         move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
 24802                                                   MOVE_TO_POS | MOVE_TO_X);
 24803       /* If we are under a large hscroll, move_it_in_display_line_to
 24804          could hit the end of the line without reaching
 24805          first_visible_x.  Pretend that we did reach it.  This is
 24806          especially important on a TTY, where we will call
 24807          extend_face_to_end_of_line, which needs to know how many
 24808          blank glyphs to produce.  */
 24809       if (it->current_x < it->first_visible_x
 24810           && (move_result == MOVE_NEWLINE_OR_CR
 24811               || move_result == MOVE_POS_MATCH_OR_ZV))
 24812         it->current_x = it->first_visible_x;
 24813 
 24814       /* In case move_it_in_display_line_to above "produced" the line
 24815          number.  */
 24816       it->line_number_produced_p = false;
 24817 
 24818       /* Record the smallest positions seen while we moved over
 24819          display elements that are not visible.  This is needed by
 24820          redisplay_internal for optimizing the case where the cursor
 24821          stays inside the same line.  The rest of this function only
 24822          considers positions that are actually displayed, so
 24823          RECORD_MAX_MIN_POS will not otherwise record positions that
 24824          are hscrolled to the left of the left edge of the window.  */
 24825       min_pos = CHARPOS (this_line_min_pos);
 24826       min_bpos = BYTEPOS (this_line_min_pos);
 24827 
 24828       /* Produce line number, if needed.  */
 24829       if (line_number_needed)
 24830         maybe_produce_line_number (it);
 24831     }
 24832   else if (it->area == TEXT_AREA)
 24833     {
 24834       /* Line numbers should precede the line-prefix or wrap-prefix.  */
 24835       if (line_number_needed)
 24836         maybe_produce_line_number (it);
 24837 
 24838       /* We only do this when not calling move_it_in_display_line_to
 24839          above, because that function calls itself handle_line_prefix.  */
 24840       handle_line_prefix (it);
 24841     }
 24842   else
 24843     {
 24844       /* Line-prefix and wrap-prefix are always displayed in the text
 24845          area.  But if this is the first call to display_line after
 24846          init_iterator, the iterator might have been set up to write
 24847          into a marginal area, e.g. if the line begins with some
 24848          display property that writes to the margins.  So we need to
 24849          wait with the call to handle_line_prefix until whatever
 24850          writes to the margin has done its job.  */
 24851       pending_handle_line_prefix = true;
 24852     }
 24853 
 24854   /* Get the initial row height.  This is either the height of the
 24855      text hscrolled, if there is any, or zero.  */
 24856   row->ascent = it->max_ascent;
 24857   row->height = it->max_ascent + it->max_descent;
 24858   row->phys_ascent = it->max_phys_ascent;
 24859   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 24860   row->extra_line_spacing = it->max_extra_line_spacing;
 24861 
 24862 /* Utility macro to record max and min buffer positions seen until now.  */
 24863 #define RECORD_MAX_MIN_POS(IT)                                  \
 24864   do                                                            \
 24865     {                                                           \
 24866       bool composition_p                                        \
 24867         = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
 24868       ptrdiff_t current_pos =                                   \
 24869         composition_p ? (IT)->cmp_it.charpos                    \
 24870                       : IT_CHARPOS (*(IT));                     \
 24871       ptrdiff_t current_bpos =                                  \
 24872         composition_p ? CHAR_TO_BYTE (current_pos)              \
 24873                       : IT_BYTEPOS (*(IT));                     \
 24874       if (current_pos < min_pos)                                \
 24875         {                                                       \
 24876           min_pos = current_pos;                                \
 24877           min_bpos = current_bpos;                              \
 24878         }                                                       \
 24879       if (IT_CHARPOS (*it) > max_pos)                           \
 24880         {                                                       \
 24881           max_pos = IT_CHARPOS (*it);                           \
 24882           max_bpos = IT_BYTEPOS (*it);                          \
 24883         }                                                       \
 24884     }                                                           \
 24885   while (false)
 24886 
 24887   /* Loop generating characters.  The loop is left with IT on the next
 24888      character to display.  */
 24889   while (true)
 24890     {
 24891       int n_glyphs_before, hpos_before, x_before;
 24892       int x, nglyphs;
 24893       int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
 24894 
 24895       /* Retrieve the next thing to display.  Value is false if end of
 24896          buffer reached.  */
 24897       if (!get_next_display_element (it))
 24898         {
 24899           bool row_has_glyphs = false;
 24900           /* Maybe add a space at the end of this line that is used to
 24901              display the cursor there under X.  Set the charpos of the
 24902              first glyph of blank lines not corresponding to any text
 24903              to -1.  */
 24904           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 24905             row->exact_window_width_line_p = true;
 24906           else if ((append_space_for_newline (it, true)
 24907                     && row->used[TEXT_AREA] == 1)
 24908                    || row->used[TEXT_AREA] == 0
 24909                    || (row_has_glyphs = row_text_area_empty (row)))
 24910             {
 24911               row->glyphs[TEXT_AREA]->charpos = -1;
 24912               /* Don't reset the displays_text_p flag if we are
 24913                  displaying line numbers or line-prefix.  */
 24914               if (!row_has_glyphs)
 24915                 row->displays_text_p = false;
 24916 
 24917               if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
 24918                   && (!MINI_WINDOW_P (it->w)))
 24919                 row->indicate_empty_line_p = true;
 24920             }
 24921 
 24922           it->continuation_lines_width = 0;
 24923           /* Reset those iterator values set from display property
 24924              values.  This is for the case when the display property
 24925              ends at ZV, and is not a replacing property, so pop_it is
 24926              not called.  */
 24927           it->font_height = Qnil;
 24928           it->voffset = 0;
 24929           row->ends_at_zv_p = true;
 24930           /* A row that displays right-to-left text must always have
 24931              its last face extended all the way to the end of line,
 24932              even if this row ends in ZV, because we still write to
 24933              the screen left to right.  We also need to extend the
 24934              last face if the default face is remapped to some
 24935              different face, otherwise the functions that clear
 24936              portions of the screen will clear with the default face's
 24937              background color.  */
 24938           if (row->reversed_p
 24939               || lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
 24940               != DEFAULT_FACE_ID)
 24941             extend_face_to_end_of_line (it);
 24942           break;
 24943         }
 24944 
 24945       /* Now, get the metrics of what we want to display.  This also
 24946          generates glyphs in `row' (which is IT->glyph_row).  */
 24947       n_glyphs_before = row->used[TEXT_AREA];
 24948       x = it->current_x;
 24949 
 24950       /* Remember the line height so far in case the next element doesn't
 24951          fit on the line.  */
 24952       if (it->line_wrap != TRUNCATE)
 24953         {
 24954           ascent = it->max_ascent;
 24955           descent = it->max_descent;
 24956           phys_ascent = it->max_phys_ascent;
 24957           phys_descent = it->max_phys_descent;
 24958 
 24959           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
 24960             {
 24961               bool next_may_wrap = may_wrap;
 24962               /* Can we wrap after this character?  */
 24963               if (char_can_wrap_after (it))
 24964                 next_may_wrap = true;
 24965               else
 24966                 next_may_wrap = false;
 24967               /* Can we wrap here? */
 24968               if (may_wrap && char_can_wrap_before (it))
 24969                 {
 24970                   SAVE_IT (wrap_it, *it, wrap_data);
 24971                   wrap_x = x;
 24972                   wrap_row_used = row->used[TEXT_AREA];
 24973                   wrap_row_ascent = row->ascent;
 24974                   wrap_row_height = row->height;
 24975                   wrap_row_phys_ascent = row->phys_ascent;
 24976                   wrap_row_phys_height = row->phys_height;
 24977                   wrap_row_extra_line_spacing = row->extra_line_spacing;
 24978                   wrap_row_min_pos = min_pos;
 24979                   wrap_row_min_bpos = min_bpos;
 24980                   wrap_row_max_pos = max_pos;
 24981                   wrap_row_max_bpos = max_bpos;
 24982                 }
 24983               /* Update may_wrap for the next iteration.  */
 24984               may_wrap = next_may_wrap;
 24985             }
 24986         }
 24987 
 24988       PRODUCE_GLYPHS (it);
 24989 
 24990       /* If this display element was in marginal areas, continue with
 24991          the next one.  */
 24992       if (it->area != TEXT_AREA)
 24993         {
 24994           row->ascent = max (row->ascent, it->max_ascent);
 24995           row->height = max (row->height, it->max_ascent + it->max_descent);
 24996           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 24997           row->phys_height = max (row->phys_height,
 24998                                   it->max_phys_ascent + it->max_phys_descent);
 24999           row->extra_line_spacing = max (row->extra_line_spacing,
 25000                                          it->max_extra_line_spacing);
 25001           set_iterator_to_next (it, true);
 25002           /* If we didn't handle the line/wrap prefix above, and the
 25003              call to set_iterator_to_next just switched to TEXT_AREA,
 25004              process the prefix now.  */
 25005           if (it->area == TEXT_AREA && pending_handle_line_prefix)
 25006             {
 25007               /* Line numbers should precede the line-prefix or wrap-prefix.  */
 25008               if (line_number_needed)
 25009                 maybe_produce_line_number (it);
 25010 
 25011               pending_handle_line_prefix = false;
 25012               handle_line_prefix (it);
 25013             }
 25014           continue;
 25015         }
 25016 
 25017       /* Does the display element fit on the line?  If we truncate
 25018          lines, we should draw past the right edge of the window.  If
 25019          we don't truncate, we want to stop so that we can display the
 25020          continuation glyph before the right margin.  If lines are
 25021          continued, there are two possible strategies for characters
 25022          resulting in more than 1 glyph (e.g. tabs): Display as many
 25023          glyphs as possible in this line and leave the rest for the
 25024          continuation line, or display the whole element in the next
 25025          line.  Original redisplay did the former, so we do it also.  */
 25026       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 25027       hpos_before = it->hpos;
 25028       x_before = x;
 25029 
 25030       if (/* Not a newline.  */
 25031           nglyphs > 0
 25032           /* Glyphs produced fit entirely in the line.  */
 25033           && it->current_x < it->last_visible_x)
 25034         {
 25035           it->hpos += nglyphs;
 25036           row->ascent = max (row->ascent, it->max_ascent);
 25037           row->height = max (row->height, it->max_ascent + it->max_descent);
 25038           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25039           row->phys_height = max (row->phys_height,
 25040                                   it->max_phys_ascent + it->max_phys_descent);
 25041           row->extra_line_spacing = max (row->extra_line_spacing,
 25042                                          it->max_extra_line_spacing);
 25043           if (it->current_x - it->pixel_width < it->first_visible_x
 25044               /* When line numbers are displayed, row->x should not be
 25045                  offset, as the first glyph after the line number can
 25046                  never be partially visible.  */
 25047               && !line_number_needed
 25048               /* In R2L rows, we arrange in extend_face_to_end_of_line
 25049                  to add a right offset to the line, by a suitable
 25050                  change to the stretch glyph that is the leftmost
 25051                  glyph of the line.  */
 25052               && !row->reversed_p)
 25053             row->x = x - it->first_visible_x;
 25054           /* Record the maximum and minimum buffer positions seen so
 25055              far in glyphs that will be displayed by this row.  */
 25056           if (it->bidi_p)
 25057             RECORD_MAX_MIN_POS (it);
 25058         }
 25059       else
 25060         {
 25061           int i, new_x;
 25062           struct glyph *glyph;
 25063 
 25064           for (i = 0; i < nglyphs; ++i, x = new_x)
 25065             {
 25066               /* Identify the glyphs added by the last call to
 25067                  PRODUCE_GLYPHS.  In R2L rows, they are prepended to
 25068                  the previous glyphs.  */
 25069               if (!row->reversed_p)
 25070                 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 25071               else
 25072                 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
 25073               new_x = x + glyph->pixel_width;
 25074 
 25075               if (/* Lines are continued.  */
 25076                   it->line_wrap != TRUNCATE
 25077                   && (/* Glyph doesn't fit on the line.  */
 25078                       new_x > it->last_visible_x
 25079                       /* Or it fits exactly on a window system frame.  */
 25080                       || (new_x == it->last_visible_x
 25081                           && FRAME_WINDOW_P (it->f)
 25082                           && (row->reversed_p
 25083                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25084                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
 25085                 {
 25086                   /* End of a continued line.  */
 25087 
 25088                   if (it->hpos == 0
 25089                       || (new_x == it->last_visible_x
 25090                           && FRAME_WINDOW_P (it->f)
 25091                           && (row->reversed_p
 25092                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25093                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
 25094                     {
 25095                       /* Current glyph is the only one on the line or
 25096                          fits exactly on the line.  We must continue
 25097                          the line because we can't draw the cursor
 25098                          after the glyph.  */
 25099                       row->continued_p = true;
 25100                       it->current_x = new_x;
 25101                       it->continuation_lines_width += new_x;
 25102                       ++it->hpos;
 25103                       if (i == nglyphs - 1)
 25104                         {
 25105                           /* If line-wrap is on, check if a previous
 25106                              wrap point was found.  */
 25107                           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 25108                               && wrap_row_used > 0 /* Found.  */
 25109                               /* Even if there is a previous wrap
 25110                                  point, continue the line here as
 25111                                  usual, if (i) the previous character
 25112                                  allows wrapping after it, AND (ii)
 25113                                  the current character allows wrapping
 25114                                  before it.  Because this is a valid
 25115                                  break point, we can just continue to
 25116                                  the next line at here, there is no
 25117                                  need to wrap early at the previous
 25118                                  wrap point.  */
 25119                               && (!may_wrap || !char_can_wrap_before (it)))
 25120                             goto back_to_wrap;
 25121 
 25122                           /* Record the maximum and minimum buffer
 25123                              positions seen so far in glyphs that will be
 25124                              displayed by this row.  */
 25125                           if (it->bidi_p)
 25126                             RECORD_MAX_MIN_POS (it);
 25127                           set_iterator_to_next (it, true);
 25128                           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25129                             {
 25130                               if (!get_next_display_element (it))
 25131                                 {
 25132                                   row->exact_window_width_line_p = true;
 25133                                   it->continuation_lines_width = 0;
 25134                                   it->font_height = Qnil;
 25135                                   it->voffset = 0;
 25136                                   row->continued_p = false;
 25137                                   row->ends_at_zv_p = true;
 25138                                 }
 25139                               else if (ITERATOR_AT_END_OF_LINE_P (it))
 25140                                 {
 25141                                   row->continued_p = false;
 25142                                   row->exact_window_width_line_p = true;
 25143                                 }
 25144                               /* If line-wrap is on, check if a
 25145                                  previous wrap point was found.  */
 25146                               else if (wrap_row_used > 0
 25147                                        /* Even if there is a previous
 25148                                           wrap point, continue the
 25149                                           line here as usual, if (i)
 25150                                           the previous character was a
 25151                                           space or tab AND (ii) the
 25152                                           current character is not,
 25153                                           AND (iii) the current
 25154                                           character allows wrapping
 25155                                           before it.  */
 25156                                        && (!may_wrap || !char_can_wrap_before (it)))
 25157                                 goto back_to_wrap;
 25158 
 25159                             }
 25160                         }
 25161                       else if (it->bidi_p)
 25162                         RECORD_MAX_MIN_POS (it);
 25163                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25164                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25165                         extend_face_to_end_of_line (it);
 25166                     }
 25167                   else if (CHAR_GLYPH_PADDING_P (*glyph)
 25168                            && !FRAME_WINDOW_P (it->f))
 25169                     {
 25170                       /* A padding glyph that doesn't fit on this line.
 25171                          This means the whole character doesn't fit
 25172                          on the line.  */
 25173                       if (row->reversed_p)
 25174                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25175                                                - n_glyphs_before);
 25176                       row->used[TEXT_AREA] = n_glyphs_before;
 25177 
 25178                       /* Fill the rest of the row with continuation
 25179                          glyphs like in 20.x.  */
 25180                       while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
 25181                              < row->glyphs[1 + TEXT_AREA])
 25182                         produce_special_glyphs (it, IT_CONTINUATION);
 25183 
 25184                       row->continued_p = true;
 25185                       it->current_x = x_before;
 25186                       it->continuation_lines_width += x_before;
 25187 
 25188                       /* Restore the height to what it was before the
 25189                          element not fitting on the line.  */
 25190                       it->max_ascent = ascent;
 25191                       it->max_descent = descent;
 25192                       it->max_phys_ascent = phys_ascent;
 25193                       it->max_phys_descent = phys_descent;
 25194                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25195                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25196                         extend_face_to_end_of_line (it);
 25197                     }
 25198                   else if (wrap_row_used > 0)
 25199                     {
 25200                     back_to_wrap:
 25201                       if (row->reversed_p)
 25202                         unproduce_glyphs (it,
 25203                                           row->used[TEXT_AREA] - wrap_row_used);
 25204                       RESTORE_IT (it, &wrap_it, wrap_data);
 25205                       it->continuation_lines_width += wrap_x;
 25206                       row->used[TEXT_AREA] = wrap_row_used;
 25207                       row->ascent = wrap_row_ascent;
 25208                       row->height = wrap_row_height;
 25209                       row->phys_ascent = wrap_row_phys_ascent;
 25210                       row->phys_height = wrap_row_phys_height;
 25211                       row->extra_line_spacing = wrap_row_extra_line_spacing;
 25212                       min_pos = wrap_row_min_pos;
 25213                       min_bpos = wrap_row_min_bpos;
 25214                       max_pos = wrap_row_max_pos;
 25215                       max_bpos = wrap_row_max_bpos;
 25216                       row->continued_p = true;
 25217                       row->ends_at_zv_p = false;
 25218                       row->exact_window_width_line_p = false;
 25219 
 25220                       /* Make sure that a non-default face is extended
 25221                          up to the right margin of the window.  */
 25222                       extend_face_to_end_of_line (it);
 25223                     }
 25224                   else if ((it->what == IT_CHARACTER
 25225                             || it->what == IT_STRETCH
 25226                             || it->what == IT_COMPOSITION)
 25227                            && it->c == '\t' && FRAME_WINDOW_P (it->f))
 25228                     {
 25229                       /* A TAB that extends past the right edge of the
 25230                          window.  This produces a single glyph on
 25231                          window system frames.  We leave the glyph in
 25232                          this row and let it fill the row, but don't
 25233                          consume the TAB.  */
 25234                       if ((row->reversed_p
 25235                            ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25236                            : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25237                         produce_special_glyphs (it, IT_CONTINUATION);
 25238                       it->continuation_lines_width += it->last_visible_x;
 25239                       row->ends_in_middle_of_char_p = true;
 25240                       row->continued_p = true;
 25241                       glyph->pixel_width = it->last_visible_x - x;
 25242                       it->starts_in_middle_of_char_p = true;
 25243                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25244                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25245                         extend_face_to_end_of_line (it);
 25246                     }
 25247                   else
 25248                     {
 25249                       /* Something other than a TAB that draws past
 25250                          the right edge of the window.  Restore
 25251                          positions to values before the element.  */
 25252                       if (row->reversed_p)
 25253                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25254                                                - (n_glyphs_before + i));
 25255                       row->used[TEXT_AREA] = n_glyphs_before + i;
 25256 
 25257                       /* Display continuation glyphs.  */
 25258                       it->current_x = x_before;
 25259                       it->continuation_lines_width += x;
 25260                       if (!FRAME_WINDOW_P (it->f)
 25261                           || (row->reversed_p
 25262                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25263                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25264                         produce_special_glyphs (it, IT_CONTINUATION);
 25265                       row->continued_p = true;
 25266 
 25267                       extend_face_to_end_of_line (it);
 25268 
 25269                       if (nglyphs > 1 && i > 0)
 25270                         {
 25271                           row->ends_in_middle_of_char_p = true;
 25272                           it->starts_in_middle_of_char_p = true;
 25273                         }
 25274 
 25275                       /* Restore the height to what it was before the
 25276                          element not fitting on the line.  */
 25277                       it->max_ascent = ascent;
 25278                       it->max_descent = descent;
 25279                       it->max_phys_ascent = phys_ascent;
 25280                       it->max_phys_descent = phys_descent;
 25281                     }
 25282 
 25283                   break;
 25284                 }
 25285               else if (new_x > it->first_visible_x)
 25286                 {
 25287                   /* Increment number of glyphs actually displayed.  */
 25288                   ++it->hpos;
 25289 
 25290                   /* Record the maximum and minimum buffer positions
 25291                      seen so far in glyphs that will be displayed by
 25292                      this row.  */
 25293                   if (it->bidi_p)
 25294                     RECORD_MAX_MIN_POS (it);
 25295 
 25296                   if (x < it->first_visible_x && !row->reversed_p
 25297                       && !line_number_needed)
 25298                     /* Glyph is partially visible, i.e. row starts at
 25299                        negative X position.  Don't do that in R2L
 25300                        rows, where we arrange to add a right offset to
 25301                        the line in extend_face_to_end_of_line, by a
 25302                        suitable change to the stretch glyph that is
 25303                        the leftmost glyph of the line.  */
 25304                     row->x = x - it->first_visible_x;
 25305                   /* When the last glyph of an R2L row only fits
 25306                      partially on the line, we need to set row->x to a
 25307                      negative offset, so that the leftmost glyph is
 25308                      the one that is partially visible.  But if we are
 25309                      going to produce the truncation glyph, this will
 25310                      be taken care of in produce_special_glyphs.  */
 25311                   if (row->reversed_p
 25312                       && new_x > it->last_visible_x
 25313                       && !line_number_needed
 25314                       && !(it->line_wrap == TRUNCATE
 25315                            && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
 25316                     {
 25317                       eassert (FRAME_WINDOW_P (it->f));
 25318                       row->x = it->last_visible_x - new_x;
 25319                     }
 25320                 }
 25321               else
 25322                 {
 25323                   /* Glyph is completely off the left margin of the
 25324                      window.  This should not happen because of the
 25325                      move_it_in_display_line at the start of this
 25326                      function, unless the text display area of the
 25327                      window is empty.  */
 25328                   eassert (it->first_visible_x <= it->last_visible_x);
 25329                 }
 25330             }
 25331           /* Even if this display element produced no glyphs at all,
 25332              we want to record its position.  */
 25333           if (it->bidi_p && nglyphs == 0)
 25334             RECORD_MAX_MIN_POS (it);
 25335 
 25336           row->ascent = max (row->ascent, it->max_ascent);
 25337           row->height = max (row->height, it->max_ascent + it->max_descent);
 25338           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25339           row->phys_height = max (row->phys_height,
 25340                                   it->max_phys_ascent + it->max_phys_descent);
 25341           row->extra_line_spacing = max (row->extra_line_spacing,
 25342                                          it->max_extra_line_spacing);
 25343 
 25344           /* End of this display line if row is continued.  */
 25345           if (row->continued_p || row->ends_at_zv_p)
 25346             break;
 25347         }
 25348 
 25349     at_end_of_line:
 25350       /* Is this a line end?  If yes, we're also done, after making
 25351          sure that a non-default face is extended up to the right
 25352          margin of the window.  */
 25353       if (ITERATOR_AT_END_OF_LINE_P (it))
 25354         {
 25355           int used_before = row->used[TEXT_AREA];
 25356 
 25357           row->ends_in_newline_from_string_p = STRINGP (it->object);
 25358 
 25359           /* Add a space at the end of the line that is used to
 25360              display the cursor there.  */
 25361           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25362             append_space_for_newline (it, false);
 25363 
 25364           /* Extend the face to the end of the line.  */
 25365           extend_face_to_end_of_line (it);
 25366 
 25367           /* Make sure we have the position.  */
 25368           if (used_before == 0)
 25369             row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
 25370 
 25371           /* Record the position of the newline, for use in
 25372              find_row_edges.  */
 25373           it->eol_pos = it->current.pos;
 25374 
 25375           /* Consume the line end.  This skips over invisible lines.  */
 25376           set_iterator_to_next (it, true);
 25377           it->continuation_lines_width = 0;
 25378           break;
 25379         }
 25380 
 25381       /* Detect overly-wide wrap-prefixes made of (space ...) display
 25382          properties.  When such a wrap prefix reaches past the right
 25383          margin of the window, we need to avoid the call to
 25384          set_iterator_to_next below, so that it->line_wrap is left at
 25385          its TRUNCATE value wisely set by handle_line_prefix.
 25386          Otherwise, set_iterator_to_next will pop the iterator stack,
 25387          restore it->line_wrap, and redisplay might infloop.  */
 25388       bool overwide_wrap_prefix =
 25389         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 25390         && it->sp > 0 && it->method == GET_FROM_STRETCH
 25391         && it->current_x >= it->last_visible_x
 25392         && it->continuation_lines_width > 0
 25393         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 25394 
 25395       /* Proceed with next display element.  Note that this skips
 25396          over lines invisible because of selective display.  */
 25397       if (!overwide_wrap_prefix)
 25398         set_iterator_to_next (it, true);
 25399 
 25400       /* If we truncate lines, we are done when the last displayed
 25401          glyphs reach past the right margin of the window.  */
 25402       if (it->line_wrap == TRUNCATE
 25403           && ((FRAME_WINDOW_P (it->f)
 25404                /* Images are preprocessed in produce_image_glyph such
 25405                   that they are cropped at the right edge of the
 25406                   window, so an image glyph will always end exactly at
 25407                   last_visible_x, even if there's no right fringe.  */
 25408                && ((row->reversed_p
 25409                     ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25410                     : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
 25411                    || it->what == IT_IMAGE))
 25412               ? (it->current_x >= it->last_visible_x)
 25413               : (it->current_x > it->last_visible_x)))
 25414         {
 25415           /* Maybe add truncation glyphs.  */
 25416           if (!FRAME_WINDOW_P (it->f)
 25417               || (row->reversed_p
 25418                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25419                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25420             {
 25421               int i, n;
 25422 
 25423               if (!row->reversed_p)
 25424                 {
 25425                   for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
 25426                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25427                       break;
 25428                 }
 25429               else
 25430                 {
 25431                   for (i = 0; i < row->used[TEXT_AREA]; i++)
 25432                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25433                       break;
 25434                   /* Remove any padding glyphs at the front of ROW, to
 25435                      make room for the truncation glyphs we will be
 25436                      adding below.  The loop below always inserts at
 25437                      least one truncation glyph, so also remove the
 25438                      last glyph added to ROW.  */
 25439                   unproduce_glyphs (it, i + 1);
 25440                   /* Adjust i for the loop below.  */
 25441                   i = row->used[TEXT_AREA] - (i + 1);
 25442                 }
 25443 
 25444               /* produce_special_glyphs overwrites the last glyph, so
 25445                  we don't want that if we want to keep that last
 25446                  glyph, which means it's an image.  */
 25447               if (it->current_x > it->last_visible_x)
 25448                 {
 25449                   it->current_x = x_before;
 25450                   if (!FRAME_WINDOW_P (it->f))
 25451                     {
 25452                       for (n = row->used[TEXT_AREA]; i < n; ++i)
 25453                         {
 25454                           row->used[TEXT_AREA] = i;
 25455                           produce_special_glyphs (it, IT_TRUNCATION);
 25456                         }
 25457                     }
 25458                   else
 25459                     {
 25460                       row->used[TEXT_AREA] = i;
 25461                       produce_special_glyphs (it, IT_TRUNCATION);
 25462                     }
 25463                   it->hpos = hpos_before;
 25464                 }
 25465             }
 25466           else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25467             {
 25468               /* Don't truncate if we can overflow newline into fringe.  */
 25469               if (!get_next_display_element (it))
 25470                 {
 25471                   it->continuation_lines_width = 0;
 25472                   it->font_height = Qnil;
 25473                   it->voffset = 0;
 25474                   row->ends_at_zv_p = true;
 25475                   row->exact_window_width_line_p = true;
 25476                   break;
 25477                 }
 25478               if (ITERATOR_AT_END_OF_LINE_P (it))
 25479                 {
 25480                   row->exact_window_width_line_p = true;
 25481                   goto at_end_of_line;
 25482                 }
 25483               it->current_x = x_before;
 25484               it->hpos = hpos_before;
 25485             }
 25486 
 25487           row->truncated_on_right_p = true;
 25488           it->continuation_lines_width = 0;
 25489           reseat_at_next_visible_line_start (it, false);
 25490           /* We insist below that IT's position be at ZV because in
 25491              bidi-reordered lines the character at visible line start
 25492              might not be the character that follows the newline in
 25493              the logical order.  */
 25494           if (IT_BYTEPOS (*it) > BEG_BYTE)
 25495             row->ends_at_zv_p =
 25496               IT_BYTEPOS (*it) >= ZV_BYTE
 25497               && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n');
 25498           else
 25499             row->ends_at_zv_p = false;
 25500           break;
 25501         }
 25502     }
 25503 
 25504   if (wrap_data)
 25505     bidi_unshelve_cache (wrap_data, true);
 25506 
 25507   /* If line is not empty and hscrolled, maybe insert truncation glyphs
 25508      at the left window margin.  */
 25509   if (it->first_visible_x
 25510       && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
 25511     {
 25512       if (!FRAME_WINDOW_P (it->f)
 25513           || (((row->reversed_p
 25514                 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 25515                 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 25516               /* Don't let insert_left_trunc_glyphs overwrite the
 25517                  first glyph of the row if it is an image.  */
 25518               && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
 25519         insert_left_trunc_glyphs (it);
 25520       row->truncated_on_left_p = true;
 25521     }
 25522 
 25523   /* Remember the position at which this line ends.
 25524 
 25525      BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
 25526      cannot be before the call to find_row_edges below, since that is
 25527      where these positions are determined. */
 25528   row->end = it->current;
 25529   if (!it->bidi_p)
 25530     {
 25531       row->minpos = row->start.pos;
 25532       row->maxpos = row->end.pos;
 25533     }
 25534   else
 25535     {
 25536       /* ROW->minpos and ROW->maxpos must be the smallest and
 25537          `1 + the largest' buffer positions in ROW.  But if ROW was
 25538          bidi-reordered, these two positions can be anywhere in the
 25539          row, so we must determine them now.  */
 25540       find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
 25541     }
 25542 
 25543   /* If the start of this line is the overlay arrow-position, then
 25544      mark this glyph row as the one containing the overlay arrow.
 25545      This is clearly a mess with variable size fonts.  It would be
 25546      better to let it be displayed like cursors under X.  */
 25547   if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
 25548       && (overlay_arrow_string = overlay_arrow_at_row (it, row),
 25549           !NILP (overlay_arrow_string)))
 25550     {
 25551       /* Overlay arrow in window redisplay is a fringe bitmap.  */
 25552       if (STRINGP (overlay_arrow_string))
 25553         {
 25554           struct glyph_row *arrow_row
 25555             = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
 25556           struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
 25557           struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
 25558           struct glyph *p = row->glyphs[TEXT_AREA];
 25559           struct glyph *p2, *end;
 25560 
 25561           /* Copy the arrow glyphs.  */
 25562           while (glyph < arrow_end)
 25563             *p++ = *glyph++;
 25564 
 25565           /* Throw away padding glyphs.  */
 25566           p2 = p;
 25567           end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 25568           while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
 25569             ++p2;
 25570           if (p2 > p)
 25571             {
 25572               while (p2 < end)
 25573                 *p++ = *p2++;
 25574               row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
 25575             }
 25576         }
 25577       else
 25578         {
 25579           eassert (FIXNUMP (overlay_arrow_string));
 25580           row->overlay_arrow_bitmap = XFIXNUM (overlay_arrow_string);
 25581         }
 25582       overlay_arrow_seen = true;
 25583     }
 25584 
 25585   /* Highlight trailing whitespace.  */
 25586   if (!NILP (Vshow_trailing_whitespace))
 25587     highlight_trailing_whitespace (it);
 25588 
 25589   /* Compute pixel dimensions of this line.  */
 25590   compute_line_metrics (it);
 25591 
 25592   /* Implementation note: No changes in the glyphs of ROW or in their
 25593      faces can be done past this point, because compute_line_metrics
 25594      computes ROW's hash value and stores it within the glyph_row
 25595      structure.  */
 25596 
 25597   /* Record whether this row ends inside an ellipsis.  */
 25598   row->ends_in_ellipsis_p
 25599     = (it->method == GET_FROM_DISPLAY_VECTOR
 25600        && it->ellipsis_p);
 25601 
 25602   /* Save fringe bitmaps in this row.  */
 25603   row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
 25604   row->left_user_fringe_face_id = it->left_user_fringe_face_id;
 25605   row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
 25606   row->right_user_fringe_face_id = it->right_user_fringe_face_id;
 25607 
 25608   it->left_user_fringe_bitmap = 0;
 25609   it->left_user_fringe_face_id = 0;
 25610   it->right_user_fringe_bitmap = 0;
 25611   it->right_user_fringe_face_id = 0;
 25612 
 25613   /* When they turn off tooltip-mode on a GUI frame, we call 'message'
 25614      with message-truncate-lines bound to non-nil, which produces
 25615      truncation bitmaps on the fringe.  Force redrawing of the fringes
 25616      in that case, to make sure the fringe bitmaps are removed when a
 25617      shorter message is displayed.  */
 25618   if (MINI_WINDOW_P (it->w) && it->line_wrap == TRUNCATE
 25619       && FRAME_WINDOW_P (it->f) && !cursor_in_echo_area)
 25620     row->redraw_fringe_bitmaps_p = true;
 25621 
 25622   /* Maybe set the cursor.  */
 25623   cvpos = it->w->cursor.vpos;
 25624   if ((cvpos < 0
 25625        /* In bidi-reordered rows, keep checking for proper cursor
 25626           position even if one has been found already, because buffer
 25627           positions in such rows change non-linearly with ROW->VPOS,
 25628           when a line is continued.  One exception: when we are at ZV,
 25629           display cursor on the first suitable glyph row, since all
 25630           the empty rows after that also have their position set to ZV.  */
 25631        /* FIXME: Revisit this when glyph ``spilling'' in continuation
 25632           lines' rows is implemented for bidi-reordered rows.  */
 25633        || (it->bidi_p
 25634            && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
 25635       && PT >= MATRIX_ROW_START_CHARPOS (row)
 25636       && PT <= MATRIX_ROW_END_CHARPOS (row)
 25637       && cursor_row_p (row))
 25638     set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
 25639 
 25640   /* Prepare for the next line.  This line starts horizontally at (X
 25641      HPOS) = (0 0).  Vertical positions are incremented.  As a
 25642      convenience for the caller, IT->glyph_row is set to the next
 25643      row to be used.  */
 25644   it->current_x = it->hpos = 0;
 25645   it->current_y += row->height;
 25646   /* Restore the first and last visible X if we adjusted them for
 25647      current-line hscrolling.  */
 25648   if (hscroll_this_line)
 25649     {
 25650       it->first_visible_x = first_visible_x;
 25651       it->last_visible_x  = last_visible_x;
 25652     }
 25653   SET_TEXT_POS (it->eol_pos, 0, 0);
 25654   ++it->vpos;
 25655   ++it->glyph_row;
 25656   /* The next row should by default use the same value of the
 25657      reversed_p flag as this one.  set_iterator_to_next decides when
 25658      it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
 25659      the flag accordingly.  */
 25660   if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
 25661     it->glyph_row->reversed_p = row->reversed_p;
 25662   it->start = row->end;
 25663   return MATRIX_ROW_DISPLAYS_TEXT_P (row);
 25664 
 25665 #undef RECORD_MAX_MIN_POS
 25666 }
 25667 
 25668 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
 25669        Scurrent_bidi_paragraph_direction, 0, 1, 0,
 25670        doc: /* Return paragraph direction at point in BUFFER.
 25671 Value is either `left-to-right' or `right-to-left'.
 25672 If BUFFER is omitted or nil, it defaults to the current buffer.
 25673 
 25674 Paragraph direction determines how the text in the paragraph is displayed.
 25675 In left-to-right paragraphs, text begins at the left margin of the window
 25676 and the reading direction is generally left to right.  In right-to-left
 25677 paragraphs, text begins at the right margin and is read from right to left.
 25678 
 25679 See also `bidi-paragraph-direction'.  */)
 25680   (Lisp_Object buffer)
 25681 {
 25682   struct buffer *buf = current_buffer;
 25683   struct buffer *old = buf;
 25684 
 25685   if (! NILP (buffer))
 25686     {
 25687       CHECK_BUFFER (buffer);
 25688       buf = XBUFFER (buffer);
 25689     }
 25690 
 25691   if (NILP (BVAR (buf, bidi_display_reordering))
 25692       || NILP (BVAR (buf, enable_multibyte_characters))
 25693       /* When we are loading loadup.el, the character property tables
 25694          needed for bidi iteration are not yet available.  */
 25695       || redisplay__inhibit_bidi)
 25696     return Qleft_to_right;
 25697   else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
 25698     return BVAR (buf, bidi_paragraph_direction);
 25699   else
 25700     {
 25701       /* Determine the direction from buffer text.  We could try to
 25702          use current_matrix if it is up to date, but this seems fast
 25703          enough as it is.  */
 25704       struct bidi_it itb;
 25705       ptrdiff_t pos = BUF_PT (buf);
 25706       ptrdiff_t bytepos = BUF_PT_BYTE (buf);
 25707       int c;
 25708       void *itb_data = bidi_shelve_cache ();
 25709 
 25710       set_buffer_temp (buf);
 25711       /* bidi_paragraph_init finds the base direction of the paragraph
 25712          by searching forward from paragraph start.  We need the base
 25713          direction of the current or _previous_ paragraph, so we need
 25714          to make sure we are within that paragraph.  To that end, find
 25715          the previous non-empty line.  */
 25716       if (pos >= ZV && pos > BEGV)
 25717         dec_both (&pos, &bytepos);
 25718       AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
 25719       if (fast_looking_at (trailing_white_space,
 25720                            pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
 25721         {
 25722           while ((c = FETCH_BYTE (bytepos)) == '\n'
 25723                  || c == ' ' || c == '\t' || c == '\f')
 25724             {
 25725               if (bytepos <= BEGV_BYTE)
 25726                 break;
 25727               bytepos--;
 25728               pos--;
 25729             }
 25730           while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
 25731             bytepos--;
 25732         }
 25733       bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
 25734       itb.paragraph_dir = NEUTRAL_DIR;
 25735       itb.string.s = NULL;
 25736       itb.string.lstring = Qnil;
 25737       itb.string.bufpos = 0;
 25738       itb.string.from_disp_str = false;
 25739       itb.string.unibyte = false;
 25740       /* We have no window to use here for ignoring window-specific
 25741          overlays.  Using NULL for window pointer will cause
 25742          compute_display_string_pos to use the current buffer.  */
 25743       itb.w = NULL;
 25744       bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
 25745       bidi_unshelve_cache (itb_data, false);
 25746       set_buffer_temp (old);
 25747       switch (itb.paragraph_dir)
 25748         {
 25749         case L2R:
 25750           return Qleft_to_right;
 25751           break;
 25752         case R2L:
 25753           return Qright_to_left;
 25754           break;
 25755         default:
 25756           emacs_abort ();
 25757         }
 25758     }
 25759 }
 25760 
 25761 DEFUN ("bidi-find-overridden-directionality",
 25762        Fbidi_find_overridden_directionality,
 25763        Sbidi_find_overridden_directionality, 3, 4, 0,
 25764        doc: /* Return position between FROM and TO where directionality was overridden.
 25765 
 25766 This function returns the first character position in the specified
 25767 region of OBJECT where characters have their bidirectional
 25768 properties affected in a way that might make its text look confusingly
 25769 on display.  For example, characters whose `bidi-class' property is `L',
 25770 could be forced to display as `R' by a directional override, and
 25771 likewise characters whose `bidi-class' is `R' or `AL' that are
 25772 forced to display as `L'.
 25773 
 25774 If no such character is found, the function returns nil.
 25775 
 25776 OBJECT is a Lisp string or buffer to search for overridden
 25777 directionality, and defaults to the current buffer if nil.
 25778 OBJECT can also be a window, in which case the function will search
 25779 the buffer displayed in that window.  Passing the window instead of
 25780 a buffer is preferable when the buffer is displayed in some window,
 25781 because this function will then be able to correctly account for
 25782 window-specific overlays, which can affect the results.
 25783 
 25784 Optional argument BASE-DIR specifies the base paragraph directory
 25785 of the text.  It should be a symbol, either `left-to-right'
 25786 or `right-to-left', and defaults to `left-to-right'.
 25787 
 25788 Strong directional characters `L', `R', and `AL' can have their
 25789 intrinsic directionality overridden by directional override control
 25790 characters RLO (u+202E) and LRO (u+202D).  They can also have their
 25791 directionality affected by other formatting control characters: LRE
 25792 (u+202A), RLE (u+202B), LRI (u+2066), and RLI (u+2067).  See the
 25793 function `get-char-code-property' for a way to inquire about the
 25794 `bidi-class' property of a character.  Characters whose intrinsic
 25795 directionality is weak or neutral, such as numbers or punctuation
 25796 characters, can be forced to display in a very different place with
 25797 respect of its surrounding characters, so as to make the surrounding
 25798 text confuse the user regarding what the text says.
 25799 
 25800 Also see the `highlight-confusing-reorderings' function, which can be
 25801 useful in similar circumstances as this function.  */)
 25802   (Lisp_Object from, Lisp_Object to, Lisp_Object object, Lisp_Object base_dir)
 25803 {
 25804   struct buffer *buf = current_buffer;
 25805   struct buffer *old = buf;
 25806   struct window *w = NULL;
 25807   bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
 25808   struct bidi_it itb;
 25809   ptrdiff_t from_pos, to_pos, from_bpos;
 25810   void *itb_data;
 25811 
 25812   if (!NILP (object))
 25813     {
 25814       if (BUFFERP (object))
 25815         buf = XBUFFER (object);
 25816       else if (WINDOWP (object))
 25817         {
 25818           w = decode_live_window (object);
 25819           buf = XBUFFER (w->contents);
 25820           frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
 25821         }
 25822       else
 25823         CHECK_STRING (object);
 25824     }
 25825 
 25826   if (STRINGP (object))
 25827     {
 25828       /* Characters in unibyte strings are always treated by bidi.c as
 25829          strong LTR.  */
 25830       if (!STRING_MULTIBYTE (object)
 25831           /* When we are loading loadup.el, the character property
 25832              tables needed for bidi iteration are not yet
 25833              available.  */
 25834           || redisplay__inhibit_bidi)
 25835         return Qnil;
 25836 
 25837       validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
 25838       if (from_pos >= SCHARS (object))
 25839         return Qnil;
 25840 
 25841       /* Set up the bidi iterator.  */
 25842       itb_data = bidi_shelve_cache ();
 25843       itb.paragraph_dir = NEUTRAL_DIR;
 25844       itb.string.lstring = object;
 25845       itb.string.s = NULL;
 25846       itb.string.schars = SCHARS (object);
 25847       itb.string.bufpos = 0;
 25848       itb.string.from_disp_str = false;
 25849       itb.string.unibyte = false;
 25850       itb.w = w;
 25851       bidi_init_it (0, 0, frame_window_p, &itb);
 25852     }
 25853   else
 25854     {
 25855       /* Nothing this fancy can happen in unibyte buffers, or in a
 25856          buffer that disabled reordering, or if FROM is at EOB.  */
 25857       if (NILP (BVAR (buf, bidi_display_reordering))
 25858           || NILP (BVAR (buf, enable_multibyte_characters))
 25859           /* When we are loading loadup.el, the character property
 25860              tables needed for bidi iteration are not yet
 25861              available.  */
 25862           || redisplay__inhibit_bidi)
 25863         return Qnil;
 25864 
 25865       set_buffer_temp (buf);
 25866       validate_region (&from, &to);
 25867       from_pos = XFIXNUM (from);
 25868       to_pos = XFIXNUM (to);
 25869       if (from_pos >= ZV)
 25870         return Qnil;
 25871 
 25872       /* Set up the bidi iterator.  */
 25873       itb_data = bidi_shelve_cache ();
 25874       from_bpos = CHAR_TO_BYTE (from_pos);
 25875       if (from_pos == BEGV)
 25876         {
 25877           itb.charpos = BEGV;
 25878           itb.bytepos = BEGV_BYTE;
 25879         }
 25880       else if (FETCH_BYTE (from_bpos - 1) == '\n')
 25881         {
 25882           itb.charpos = from_pos;
 25883           itb.bytepos = from_bpos;
 25884         }
 25885       else
 25886         itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
 25887                                             -1, &itb.bytepos);
 25888       itb.paragraph_dir = NEUTRAL_DIR;
 25889       itb.string.s = NULL;
 25890       itb.string.lstring = Qnil;
 25891       itb.string.bufpos = 0;
 25892       itb.string.from_disp_str = false;
 25893       itb.string.unibyte = false;
 25894       itb.w = w;
 25895       bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
 25896     }
 25897 
 25898   ptrdiff_t found;
 25899   bidi_dir_t bdir = EQ (base_dir, Qright_to_left) ? R2L : L2R;
 25900   do {
 25901     bidi_paragraph_init (bdir, &itb, false);
 25902     while ((found = bidi_find_first_overridden (&itb)) < from_pos)
 25903       ;
 25904   } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
 25905 
 25906   bidi_unshelve_cache (itb_data, false);
 25907   set_buffer_temp (old);
 25908 
 25909   return (from_pos <= found && found < to_pos) ? make_fixnum (found) : Qnil;
 25910 }
 25911 
 25912 DEFUN ("move-point-visually", Fmove_point_visually,
 25913        Smove_point_visually, 1, 1, 0,
 25914        doc: /* Move point in the visual order in the specified DIRECTION.
 25915 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
 25916 left.
 25917 
 25918 Value is the new character position of point.  */)
 25919   (Lisp_Object direction)
 25920 {
 25921   struct window *w = XWINDOW (selected_window);
 25922   struct buffer *b = XBUFFER (w->contents);
 25923   struct glyph_row *row;
 25924   int dir;
 25925   Lisp_Object paragraph_dir;
 25926 
 25927 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH)          \
 25928   (!(ROW)->continued_p                          \
 25929    && NILP ((GLYPH)->object)                    \
 25930    && (GLYPH)->type == CHAR_GLYPH               \
 25931    && (GLYPH)->u.ch == ' '                      \
 25932    && (GLYPH)->charpos >= 0                     \
 25933    && !(GLYPH)->avoid_cursor_p)
 25934 
 25935   CHECK_FIXNUM (direction);
 25936   dir = XFIXNUM (direction);
 25937   if (dir > 0)
 25938     dir = 1;
 25939   else
 25940     dir = -1;
 25941 
 25942   /* If current matrix is up-to-date, we can use the information
 25943      recorded in the glyphs, at least as long as the goal is on the
 25944      screen.  */
 25945   if (w->window_end_valid
 25946       && !windows_or_buffers_changed
 25947       && b
 25948       && !b->clip_changed
 25949       && !b->prevent_redisplay_optimizations_p
 25950       && !window_outdated (w)
 25951       /* We rely below on the cursor coordinates to be up to date, but
 25952          we cannot trust them if some command moved point since the
 25953          last complete redisplay.  */
 25954       && w->last_point == BUF_PT (b)
 25955       && w->cursor.vpos >= 0
 25956       && w->cursor.vpos < w->current_matrix->nrows
 25957       && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
 25958     {
 25959       struct glyph *g = row->glyphs[TEXT_AREA];
 25960       struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
 25961       struct glyph *gpt = g + w->cursor.hpos;
 25962 
 25963       for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
 25964         {
 25965           if (BUFFERP (g->object) && g->charpos != PT)
 25966             {
 25967               SET_PT (g->charpos);
 25968               w->cursor.vpos = -1;
 25969               return make_fixnum (PT);
 25970             }
 25971           else if (!NILP (g->object) && !EQ (g->object, gpt->object))
 25972             {
 25973               ptrdiff_t new_pos;
 25974 
 25975               if (BUFFERP (gpt->object))
 25976                 {
 25977                   new_pos = PT;
 25978                   if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
 25979                     new_pos += (row->reversed_p ? -dir : dir);
 25980                   else
 25981                     new_pos -= (row->reversed_p ? -dir : dir);
 25982                   new_pos = clip_to_bounds (BEGV, new_pos, ZV);
 25983                   /* If we didn't move, we've hit BEGV or ZV, so we
 25984                      need to signal a suitable error.  */
 25985                   if (new_pos == PT)
 25986                     break;
 25987                 }
 25988               else if (BUFFERP (g->object))
 25989                 new_pos = g->charpos;
 25990               else
 25991                 break;
 25992               SET_PT (new_pos);
 25993               w->cursor.vpos = -1;
 25994               return make_fixnum (PT);
 25995             }
 25996           else if (ROW_GLYPH_NEWLINE_P (row, g))
 25997             {
 25998               /* Glyphs inserted at the end of a non-empty line for
 25999                  positioning the cursor have zero charpos, so we must
 26000                  deduce the value of point by other means.  */
 26001               if (g->charpos > 0)
 26002                 SET_PT (g->charpos);
 26003               else if (row->ends_at_zv_p && PT != ZV)
 26004                 SET_PT (ZV);
 26005               else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
 26006                 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26007               else
 26008                 break;
 26009               w->cursor.vpos = -1;
 26010               return make_fixnum (PT);
 26011             }
 26012         }
 26013       if (g == e || NILP (g->object))
 26014         {
 26015           if (row->truncated_on_left_p || row->truncated_on_right_p)
 26016             goto simulate_display;
 26017           if (!row->reversed_p)
 26018             row += dir;
 26019           else
 26020             row -= dir;
 26021           if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
 26022                 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
 26023             goto simulate_display;
 26024 
 26025           if (dir > 0)
 26026             {
 26027               if (row->reversed_p && !row->continued_p)
 26028                 {
 26029                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26030                   w->cursor.vpos = -1;
 26031                   return make_fixnum (PT);
 26032                 }
 26033               g = row->glyphs[TEXT_AREA];
 26034               e = g + row->used[TEXT_AREA];
 26035               for ( ; g < e; g++)
 26036                 {
 26037                   if (BUFFERP (g->object)
 26038                       /* Empty lines have only one glyph, which stands
 26039                          for the newline, and whose charpos is the
 26040                          buffer position of the newline.  */
 26041                       || ROW_GLYPH_NEWLINE_P (row, g)
 26042                       /* When the buffer ends in a newline, the line at
 26043                          EOB also has one glyph, but its charpos is -1.  */
 26044                       || (row->ends_at_zv_p
 26045                           && !row->reversed_p
 26046                           && NILP (g->object)
 26047                           && g->type == CHAR_GLYPH
 26048                           && g->u.ch == ' '))
 26049                     {
 26050                       if (g->charpos > 0)
 26051                         SET_PT (g->charpos);
 26052                       else if (!row->reversed_p
 26053                                && row->ends_at_zv_p
 26054                                && PT != ZV)
 26055                         SET_PT (ZV);
 26056                       else
 26057                         continue;
 26058                       w->cursor.vpos = -1;
 26059                       return make_fixnum (PT);
 26060                     }
 26061                 }
 26062             }
 26063           else
 26064             {
 26065               if (!row->reversed_p && !row->continued_p)
 26066                 {
 26067                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26068                   w->cursor.vpos = -1;
 26069                   return make_fixnum (PT);
 26070                 }
 26071               e = row->glyphs[TEXT_AREA];
 26072               g = e + row->used[TEXT_AREA] - 1;
 26073               for ( ; g >= e; g--)
 26074                 {
 26075                   if (BUFFERP (g->object)
 26076                       || (ROW_GLYPH_NEWLINE_P (row, g)
 26077                           && g->charpos > 0)
 26078                       /* Empty R2L lines on GUI frames have the buffer
 26079                          position of the newline stored in the stretch
 26080                          glyph.  */
 26081                       || g->type == STRETCH_GLYPH
 26082                       || (row->ends_at_zv_p
 26083                           && row->reversed_p
 26084                           && NILP (g->object)
 26085                           && g->type == CHAR_GLYPH
 26086                           && g->u.ch == ' '))
 26087                     {
 26088                       if (g->charpos > 0)
 26089                         SET_PT (g->charpos);
 26090                       else if (row->reversed_p
 26091                                && row->ends_at_zv_p
 26092                                && PT != ZV)
 26093                         SET_PT (ZV);
 26094                       else
 26095                         continue;
 26096                       w->cursor.vpos = -1;
 26097                       return make_fixnum (PT);
 26098                     }
 26099                 }
 26100             }
 26101         }
 26102     }
 26103 
 26104  simulate_display:
 26105 
 26106   /* If we wind up here, we failed to move by using the glyphs, so we
 26107      need to simulate display instead.  */
 26108 
 26109   if (b)
 26110     paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
 26111   else
 26112     paragraph_dir = Qleft_to_right;
 26113   if (EQ (paragraph_dir, Qright_to_left))
 26114     dir = -dir;
 26115   if (PT <= BEGV && dir < 0)
 26116     xsignal0 (Qbeginning_of_buffer);
 26117   else if (PT >= ZV && dir > 0)
 26118     xsignal0 (Qend_of_buffer);
 26119   else
 26120     {
 26121       struct text_pos pt;
 26122       struct it it;
 26123       int pt_x, target_x, pixel_width, pt_vpos;
 26124       bool at_eol_p;
 26125       bool overshoot_expected = false;
 26126       bool target_is_eol_p = false;
 26127       void *itdata = bidi_shelve_cache ();
 26128 
 26129       /* Setup the arena.  */
 26130       SET_TEXT_POS (pt, PT, PT_BYTE);
 26131       start_display (&it, w, pt);
 26132       /* When lines are truncated, we could be called with point
 26133          outside of the windows edges, in which case move_it_*
 26134          functions either prematurely stop at window's edge or jump to
 26135          the next screen line, whereas we rely below on our ability to
 26136          reach point, in order to start from its X coordinate.  So we
 26137          need to disregard the window's horizontal extent in that case.  */
 26138       if (it.line_wrap == TRUNCATE)
 26139         it.last_visible_x = DISP_INFINITY;
 26140 
 26141       if (it.cmp_it.id < 0
 26142           && it.method == GET_FROM_STRING
 26143           && it.area == TEXT_AREA
 26144           && it.string_from_display_prop_p
 26145           && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
 26146         overshoot_expected = true;
 26147 
 26148       /* Find the X coordinate of point.  We start from the beginning
 26149          of this or previous line to make sure we are before point in
 26150          the logical order (since the move_it_* functions can only
 26151          move forward).  */
 26152     reseat:
 26153       reseat_at_previous_visible_line_start (&it);
 26154       it.current_x = it.hpos = it.current_y = it.vpos = 0;
 26155       if (IT_CHARPOS (it) != PT)
 26156         {
 26157           move_it_to (&it, overshoot_expected ? PT - 1 : PT,
 26158                       -1, -1, -1, MOVE_TO_POS);
 26159           /* If we missed point because the character there is
 26160              displayed out of a display vector that has more than one
 26161              glyph, retry expecting overshoot.  */
 26162           if (it.method == GET_FROM_DISPLAY_VECTOR
 26163               && it.current.dpvec_index > 0
 26164               && !overshoot_expected)
 26165             {
 26166               overshoot_expected = true;
 26167               goto reseat;
 26168             }
 26169           else if (IT_CHARPOS (it) != PT && !overshoot_expected)
 26170             move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
 26171         }
 26172       pt_x = it.current_x;
 26173       pt_vpos = it.vpos;
 26174       if (dir > 0 || overshoot_expected)
 26175         {
 26176           struct glyph_row *row = it.glyph_row;
 26177 
 26178           /* When point is at beginning of line, we don't have
 26179              information about the glyph there loaded into struct
 26180              it.  Calling get_next_display_element fixes that.  */
 26181           if (pt_x == 0)
 26182             get_next_display_element (&it);
 26183           at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26184           it.glyph_row = NULL;
 26185           PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
 26186           it.glyph_row = row;
 26187           /* PRODUCE_GLYPHS advances it.current_x, so we must restore
 26188              it, lest it will become out of sync with it's buffer
 26189              position.  */
 26190           it.current_x = pt_x;
 26191         }
 26192       else
 26193         at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26194       pixel_width = it.pixel_width;
 26195       if (overshoot_expected && at_eol_p)
 26196         pixel_width = 0;
 26197       else if (pixel_width <= 0)
 26198         pixel_width = 1;
 26199 
 26200       /* If there's a display string (or something similar) at point,
 26201          we are actually at the glyph to the left of point, so we need
 26202          to correct the X coordinate.  */
 26203       if (overshoot_expected)
 26204         {
 26205           if (it.bidi_p)
 26206             pt_x += pixel_width * it.bidi_it.scan_dir;
 26207           else
 26208             pt_x += pixel_width;
 26209         }
 26210 
 26211       /* Compute target X coordinate, either to the left or to the
 26212          right of point.  On TTY frames, all characters have the same
 26213          pixel width of 1, so we can use that.  On GUI frames we don't
 26214          have an easy way of getting at the pixel width of the
 26215          character to the left of point, so we use a different method
 26216          of getting to that place.  */
 26217       if (dir > 0)
 26218         target_x = pt_x + pixel_width;
 26219       else
 26220         target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
 26221 
 26222       /* Target X coordinate could be one line above or below the line
 26223          of point, in which case we need to adjust the target X
 26224          coordinate.  Also, if moving to the left, we need to begin at
 26225          the left edge of the point's screen line.  */
 26226       if (dir < 0)
 26227         {
 26228           if (pt_x > 0)
 26229             {
 26230               start_display (&it, w, pt);
 26231               if (it.line_wrap == TRUNCATE)
 26232                 it.last_visible_x = DISP_INFINITY;
 26233               reseat_at_previous_visible_line_start (&it);
 26234               it.current_x = it.current_y = it.hpos = 0;
 26235               if (pt_vpos != 0)
 26236                 move_it_by_lines (&it, pt_vpos);
 26237             }
 26238           else
 26239             {
 26240               move_it_by_lines (&it, -1);
 26241               target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
 26242               target_is_eol_p = true;
 26243               /* Under word-wrap, we don't know the x coordinate of
 26244                  the last character displayed on the previous line,
 26245                  which immediately precedes the wrap point.  To find
 26246                  out its x coordinate, we try moving to the right
 26247                  margin of the window, which will stop at the wrap
 26248                  point, and then reset target_x to point at the
 26249                  character that precedes the wrap point.  This is not
 26250                  needed on GUI frames, because (see below) there we
 26251                  move from the left margin one grapheme cluster at a
 26252                  time, and stop when we hit the wrap point.  */
 26253               if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
 26254                 {
 26255                   void *it_data = NULL;
 26256                   struct it it2;
 26257 
 26258                   SAVE_IT (it2, it, it_data);
 26259                   move_it_in_display_line_to (&it, ZV, target_x,
 26260                                               MOVE_TO_POS | MOVE_TO_X);
 26261                   /* If we arrived at target_x, that _is_ the last
 26262                      character on the previous line.  */
 26263                   if (it.current_x != target_x)
 26264                     target_x = it.current_x - 1;
 26265                   RESTORE_IT (&it, &it2, it_data);
 26266                 }
 26267             }
 26268         }
 26269       else
 26270         {
 26271           if (at_eol_p
 26272               || (target_x >= it.last_visible_x
 26273                   && it.line_wrap != TRUNCATE))
 26274             {
 26275               if (pt_x > 0)
 26276                 move_it_by_lines (&it, 0);
 26277               move_it_by_lines (&it, 1);
 26278               target_x = 0;
 26279             }
 26280         }
 26281 
 26282       /* Move to the target X coordinate.  */
 26283       /* On GUI frames, as we don't know the X coordinate of the
 26284          character to the left of point, moving point to the left
 26285          requires walking, one grapheme cluster at a time, until we
 26286          find ourself at a place immediately to the left of the
 26287          character at point.  */
 26288       if (FRAME_WINDOW_P (it.f) && dir < 0)
 26289         {
 26290           struct text_pos new_pos;
 26291           enum move_it_result rc = MOVE_X_REACHED;
 26292 
 26293           if (it.current_x == 0)
 26294             get_next_display_element (&it);
 26295           if (it.what == IT_COMPOSITION)
 26296             {
 26297               new_pos.charpos = it.cmp_it.charpos;
 26298               new_pos.bytepos = -1;
 26299             }
 26300           else
 26301             new_pos = it.current.pos;
 26302 
 26303           while (it.current_x + it.pixel_width <= target_x
 26304                  && (rc == MOVE_X_REACHED
 26305                      /* Under word-wrap, move_it_in_display_line_to
 26306                         stops at correct coordinates, but sometimes
 26307                         returns MOVE_POS_MATCH_OR_ZV.  */
 26308                      || (it.line_wrap == WORD_WRAP
 26309                          && rc == MOVE_POS_MATCH_OR_ZV)))
 26310             {
 26311               int new_x = it.current_x + it.pixel_width;
 26312 
 26313               /* For composed characters, we want the position of the
 26314                  first character in the grapheme cluster (usually, the
 26315                  composition's base character), whereas it.current
 26316                  might give us the position of the _last_ one, e.g. if
 26317                  the composition is rendered in reverse due to bidi
 26318                  reordering.  */
 26319               if (it.what == IT_COMPOSITION)
 26320                 {
 26321                   new_pos.charpos = it.cmp_it.charpos;
 26322                   new_pos.bytepos = -1;
 26323                 }
 26324               else
 26325                 new_pos = it.current.pos;
 26326               if (new_x == it.current_x)
 26327                 new_x++;
 26328               rc = move_it_in_display_line_to (&it, ZV, new_x,
 26329                                                MOVE_TO_POS | MOVE_TO_X);
 26330               if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
 26331                 break;
 26332             }
 26333           /* The previous position we saw in the loop is the one we
 26334              want.  */
 26335           if (new_pos.bytepos == -1)
 26336             new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
 26337           it.current.pos = new_pos;
 26338         }
 26339       else if (it.current_x != target_x)
 26340         move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
 26341 
 26342       /* If we ended up in a display string that covers point, move to
 26343          buffer position to the right in the visual order.  */
 26344       if (dir > 0)
 26345         {
 26346           while (IT_CHARPOS (it) == PT)
 26347             {
 26348               set_iterator_to_next (&it, false);
 26349               if (!get_next_display_element (&it))
 26350                 break;
 26351             }
 26352         }
 26353 
 26354       /* Move point to that position.  */
 26355       SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
 26356       bidi_unshelve_cache (itdata, false);
 26357     }
 26358 
 26359   return make_fixnum (PT);
 26360 
 26361 #undef ROW_GLYPH_NEWLINE_P
 26362 }
 26363 
 26364 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
 26365        Sbidi_resolved_levels, 0, 1, 0,
 26366        doc: /* Return the resolved bidirectional levels of characters at VPOS.
 26367 
 26368 The resolved levels are produced by the Emacs bidi reordering engine
 26369 that implements the UBA, the Unicode Bidirectional Algorithm.  Please
 26370 read the Unicode Standard Annex 9 (UAX#9) for background information
 26371 about these levels.
 26372 
 26373 VPOS is the zero-based number of the current window's screen line
 26374 for which to produce the resolved levels.  If VPOS is nil or omitted,
 26375 it defaults to the screen line of point.  If the window displays a
 26376 header line, VPOS of zero will report on the header line, and first
 26377 line of text in the window will have VPOS of 1.
 26378 
 26379 Value is an array of resolved levels, indexed by glyph number.
 26380 Glyphs are numbered from zero starting from the beginning of the
 26381 screen line, i.e. the left edge of the window for left-to-right lines
 26382 and from the right edge for right-to-left lines.  The resolved levels
 26383 are produced only for the window's text area; text in display margins
 26384 is not included.
 26385 
 26386 If the selected window's display is not up-to-date, or if the specified
 26387 screen line does not display text, this function returns nil.  It is
 26388 highly recommended to bind this function to some simple key, like F8,
 26389 in order to avoid these problems.
 26390 
 26391 This function exists mainly for testing the correctness of the
 26392 Emacs UBA implementation, in particular with the test suite.  */)
 26393   (Lisp_Object vpos)
 26394 {
 26395   struct window *w = XWINDOW (selected_window);
 26396   struct buffer *b = XBUFFER (w->contents);
 26397   int nrow;
 26398   struct glyph_row *row;
 26399 
 26400   if (NILP (vpos))
 26401     {
 26402       int d1, d2, d3, d4, d5;
 26403 
 26404       pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
 26405     }
 26406   else
 26407     {
 26408       CHECK_FIXNUM (vpos);
 26409       nrow = XFIXNUM (vpos);
 26410     }
 26411 
 26412   /* We require up-to-date glyph matrix for this window.  */
 26413   if (w->window_end_valid
 26414       && !windows_or_buffers_changed
 26415       && b
 26416       && !b->clip_changed
 26417       && !b->prevent_redisplay_optimizations_p
 26418       && !window_outdated (w)
 26419       && nrow >= 0
 26420       && nrow < w->current_matrix->nrows
 26421       && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
 26422       && MATRIX_ROW_DISPLAYS_TEXT_P (row))
 26423     {
 26424       struct glyph *g, *e, *g1;
 26425       int nglyphs, i;
 26426       Lisp_Object levels;
 26427 
 26428       if (!row->reversed_p)     /* Left-to-right glyph row.  */
 26429         {
 26430           g = g1 = row->glyphs[TEXT_AREA];
 26431           e = g + row->used[TEXT_AREA];
 26432 
 26433           /* Skip over glyphs at the start of the row that was
 26434              generated by redisplay for its own needs.  */
 26435           while (g < e
 26436                  && NILP (g->object)
 26437                  && g->charpos < 0)
 26438             g++;
 26439           g1 = g;
 26440 
 26441           /* Count the "interesting" glyphs in this row.  */
 26442           for (nglyphs = 0; g < e && !NILP (g->object); g++)
 26443             nglyphs++;
 26444 
 26445           /* Create and fill the array.  */
 26446           levels = make_uninit_vector (nglyphs);
 26447           for (i = 0; g1 < g; i++, g1++)
 26448             ASET (levels, i, make_fixnum (g1->resolved_level));
 26449         }
 26450       else                      /* Right-to-left glyph row.  */
 26451         {
 26452           g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 26453           e = row->glyphs[TEXT_AREA] - 1;
 26454           while (g > e
 26455                  && NILP (g->object)
 26456                  && g->charpos < 0)
 26457             g--;
 26458           g1 = g;
 26459           for (nglyphs = 0; g > e && !NILP (g->object); g--)
 26460             nglyphs++;
 26461           levels = make_uninit_vector (nglyphs);
 26462           for (i = 0; g1 > g; i++, g1--)
 26463             ASET (levels, i, make_fixnum (g1->resolved_level));
 26464         }
 26465       return levels;
 26466     }
 26467   else
 26468     return Qnil;
 26469 }
 26470 
 26471 
 26472 
 26473 /***********************************************************************
 26474                                Menu Bar
 26475  ***********************************************************************/
 26476 
 26477 /* Redisplay the menu bar in the frame for window W.
 26478 
 26479    The menu bar of X frames that don't have X toolkit support is
 26480    displayed in a special window W->frame->menu_bar_window.
 26481 
 26482    The menu bar of terminal frames is treated specially as far as
 26483    glyph matrices are concerned.  Menu bar lines are not part of
 26484    windows, so the update is done directly on the frame matrix rows
 26485    for the menu bar.  */
 26486 
 26487 static void
 26488 display_menu_bar (struct window *w)
 26489 {
 26490   struct frame *f = XFRAME (WINDOW_FRAME (w));
 26491   struct it it;
 26492   Lisp_Object items;
 26493   int i;
 26494 
 26495   /* Don't do all this for graphical frames.  */
 26496 #ifdef HAVE_NTGUI
 26497   if (FRAME_W32_P (f))
 26498     return;
 26499 #endif
 26500 #if defined (HAVE_PGTK)
 26501   if (FRAME_PGTK_P (f))
 26502     return;
 26503 #endif
 26504 
 26505 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26506   if (FRAME_X_P (f))
 26507     return;
 26508 #endif
 26509 
 26510 #ifdef HAVE_NS
 26511   if (FRAME_NS_P (f))
 26512     return;
 26513 #endif /* HAVE_NS */
 26514 
 26515 #ifdef HAVE_HAIKU
 26516   if (FRAME_HAIKU_P (f))
 26517     return;
 26518 #endif /* HAVE_HAIKU */
 26519 
 26520 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26521   eassert (!FRAME_WINDOW_P (f));
 26522   init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
 26523   it.first_visible_x = 0;
 26524   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 26525 #elif defined (HAVE_X_WINDOWS) /* X without toolkit.  */
 26526   if (FRAME_WINDOW_P (f))
 26527     {
 26528       /* Menu bar lines are displayed in the desired matrix of the
 26529          dummy window menu_bar_window.  */
 26530       struct window *menu_w;
 26531       menu_w = XWINDOW (f->menu_bar_window);
 26532       init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
 26533                      MENU_FACE_ID);
 26534       it.first_visible_x = 0;
 26535       it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 26536     }
 26537   else
 26538 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 26539     {
 26540       /* This is a TTY frame, i.e. character hpos/vpos are used as
 26541          pixel x/y.  */
 26542       init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
 26543                      MENU_FACE_ID);
 26544       it.first_visible_x = 0;
 26545       it.last_visible_x = FRAME_COLS (f);
 26546     }
 26547 
 26548   /* FIXME: This should be controlled by a user option.  See the
 26549      comments in redisplay_tool_bar and display_mode_line about
 26550      this.  */
 26551   it.paragraph_embedding = L2R;
 26552 
 26553   /* Clear all rows of the menu bar.  */
 26554   for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
 26555     {
 26556       struct glyph_row *row = it.glyph_row + i;
 26557       clear_glyph_row (row);
 26558       row->enabled_p = true;
 26559       row->full_width_p = true;
 26560       row->reversed_p = false;
 26561     }
 26562 
 26563   /* Display all items of the menu bar.  */
 26564   items = FRAME_MENU_BAR_ITEMS (it.f);
 26565   for (i = 0; i < ASIZE (items); i += 4)
 26566     {
 26567       Lisp_Object string;
 26568 
 26569       /* Stop at nil string.  */
 26570       string = AREF (items, i + 1);
 26571       if (NILP (string))
 26572         break;
 26573 
 26574       /* Remember where item was displayed.  */
 26575       ASET (items, i + 3, make_fixnum (it.hpos));
 26576 
 26577       /* Display the item, pad with one space.  */
 26578       if (it.current_x < it.last_visible_x)
 26579         display_string (NULL, string, Qnil, 0, 0, &it,
 26580                         SCHARS (string) + 1, 0, 0, STRING_MULTIBYTE (string));
 26581     }
 26582 
 26583   /* Fill out the line with spaces.  */
 26584   if (it.current_x < it.last_visible_x)
 26585     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 26586 
 26587   /* Compute the total height of the lines.  */
 26588   compute_line_metrics (&it);
 26589 }
 26590 
 26591 /* Deep copy of a glyph row, including the glyphs.  */
 26592 static void
 26593 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
 26594 {
 26595   struct glyph *pointers[1 + LAST_AREA];
 26596   int to_used = to->used[TEXT_AREA];
 26597 
 26598   /* Save glyph pointers of TO.  */
 26599   memcpy (pointers, to->glyphs, sizeof to->glyphs);
 26600 
 26601   /* Do a structure assignment.  */
 26602   *to = *from;
 26603 
 26604   /* Restore original glyph pointers of TO.  */
 26605   memcpy (to->glyphs, pointers, sizeof to->glyphs);
 26606 
 26607   /* Copy the glyphs.  */
 26608   memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
 26609           min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
 26610 
 26611   /* If we filled only part of the TO row, fill the rest with
 26612      space_glyph (which will display as empty space).  */
 26613   if (to_used > from->used[TEXT_AREA])
 26614     fill_up_frame_row_with_spaces (to, to_used);
 26615 }
 26616 
 26617 /* Display one menu item on a TTY, by overwriting the glyphs in the
 26618    frame F's desired glyph matrix with glyphs produced from the menu
 26619    item text.  Called from term.c to display TTY drop-down menus one
 26620    item at a time.
 26621 
 26622    ITEM_TEXT is the menu item text as a C string.
 26623 
 26624    FACE_ID is the face ID to be used for this menu item.  FACE_ID
 26625    could specify one of 3 faces: a face for an enabled item, a face
 26626    for a disabled item, or a face for a selected item.
 26627 
 26628    X and Y are coordinates of the first glyph in the frame's desired
 26629    matrix to be overwritten by the menu item.  Since this is a TTY, Y
 26630    is the zero-based number of the glyph row and X is the zero-based
 26631    glyph number in the row, starting from left, where to start
 26632    displaying the item.
 26633 
 26634    SUBMENU means this menu item drops down a submenu, which
 26635    should be indicated by displaying a proper visual cue after the
 26636    item text.  */
 26637 
 26638 void
 26639 display_tty_menu_item (const char *item_text, int width, int face_id,
 26640                        int x, int y, bool submenu)
 26641 {
 26642   struct it it;
 26643   struct frame *f = SELECTED_FRAME ();
 26644   struct window *w = XWINDOW (f->selected_window);
 26645   struct glyph_row *row;
 26646   size_t item_len = strlen (item_text);
 26647 
 26648   eassert (FRAME_TERMCAP_P (f));
 26649 
 26650   /* Don't write beyond the matrix's last row.  This can happen for
 26651      TTY screens that are not high enough to show the entire menu.
 26652      (This is actually a bit of defensive programming, as
 26653      tty_menu_display already limits the number of menu items to one
 26654      less than the number of screen lines.)  */
 26655   if (y >= f->desired_matrix->nrows)
 26656     return;
 26657 
 26658   init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
 26659   it.first_visible_x = 0;
 26660   it.last_visible_x = FRAME_COLS (f) - 1;
 26661   row = it.glyph_row;
 26662   /* Start with the row contents from the current matrix.  */
 26663   deep_copy_glyph_row (row, f->current_matrix->rows + y);
 26664   bool saved_width = row->full_width_p;
 26665   row->full_width_p = true;
 26666   bool saved_reversed = row->reversed_p;
 26667   row->reversed_p = false;
 26668   row->enabled_p = true;
 26669 
 26670   /* Arrange for the menu item glyphs to start at (X,Y) and have the
 26671      desired face.  */
 26672   eassert (x < f->desired_matrix->matrix_w);
 26673   it.current_x = it.hpos = x;
 26674   it.current_y = it.vpos = y;
 26675   int saved_used = row->used[TEXT_AREA];
 26676   bool saved_truncated = row->truncated_on_right_p;
 26677   row->used[TEXT_AREA] = x;
 26678   it.face_id = face_id;
 26679   it.line_wrap = TRUNCATE;
 26680 
 26681   /* FIXME: This should be controlled by a user option.  See the
 26682      comments in redisplay_tool_bar and display_mode_line about this.
 26683      Also, if paragraph_embedding could ever be R2L, changes will be
 26684      needed to avoid shifting to the right the row characters in
 26685      term.c:append_glyph.  */
 26686   it.paragraph_embedding = L2R;
 26687 
 26688   /* Pad with a space on the left.  */
 26689   display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
 26690   width--;
 26691   /* Display the menu item, pad with spaces to WIDTH.  */
 26692   if (submenu)
 26693     {
 26694       display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26695                       item_len, 0, FRAME_COLS (f) - 1, -1);
 26696       width -= item_len;
 26697       /* Indicate with " >" that there's a submenu.  */
 26698       display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
 26699                       FRAME_COLS (f) - 1, -1);
 26700     }
 26701   else
 26702     display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26703                     width, 0, FRAME_COLS (f) - 1, -1);
 26704 
 26705   row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
 26706   row->truncated_on_right_p = saved_truncated;
 26707   row->hash = row_hash (row);
 26708   row->full_width_p = saved_width;
 26709   row->reversed_p = saved_reversed;
 26710 }
 26711 
 26712 /***********************************************************************
 26713                               Mode Line
 26714  ***********************************************************************/
 26715 
 26716 /* Redisplay mode lines in the window tree whose root is WINDOW.
 26717    If FORCE, redisplay mode lines unconditionally.
 26718    Otherwise, redisplay only mode lines that are garbaged.  Value is
 26719    the number of windows whose mode lines were redisplayed.  */
 26720 
 26721 static int
 26722 redisplay_mode_lines (Lisp_Object window, bool force)
 26723 {
 26724   int nwindows = 0;
 26725 
 26726   while (!NILP (window))
 26727     {
 26728       struct window *w = XWINDOW (window);
 26729 
 26730       if (WINDOWP (w->contents))
 26731         nwindows += redisplay_mode_lines (w->contents, force);
 26732       else if (force
 26733                || FRAME_GARBAGED_P (XFRAME (w->frame))
 26734                || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
 26735         {
 26736           struct text_pos lpoint;
 26737           struct buffer *old = current_buffer;
 26738 
 26739           /* Set the window's buffer for the mode line display.  */
 26740           SET_TEXT_POS (lpoint, PT, PT_BYTE);
 26741           set_buffer_internal_1 (XBUFFER (w->contents));
 26742 
 26743           /* Point refers normally to the selected window.  For any
 26744              other window, set up appropriate value.  */
 26745           if (!EQ (window, selected_window))
 26746             {
 26747               struct text_pos pt;
 26748 
 26749               CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
 26750               TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
 26751             }
 26752 
 26753           /* Display mode lines.  */
 26754           clear_glyph_matrix (w->desired_matrix);
 26755           if (display_mode_lines (w))
 26756             ++nwindows;
 26757 
 26758           /* Restore old settings.  */
 26759           set_buffer_internal_1 (old);
 26760           TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 26761         }
 26762 
 26763       window = w->next;
 26764     }
 26765 
 26766   return nwindows;
 26767 }
 26768 
 26769 
 26770 /* Display the mode line, the header line, and the tab-line of window
 26771    W.  Value is the sum number of mode lines, header lines, and tab
 26772    lines actually displayed.  */
 26773 
 26774 static int
 26775 display_mode_lines (struct window *w)
 26776 {
 26777   Lisp_Object old_selected_window = selected_window;
 26778   Lisp_Object new_frame = w->frame;
 26779   specpdl_ref count = SPECPDL_INDEX ();
 26780   int n = 0;
 26781 
 26782   record_unwind_protect (restore_selected_window, selected_window);
 26783   record_unwind_protect
 26784     (restore_frame_selected_window, XFRAME (new_frame)->selected_window);
 26785 
 26786   if (window_wants_mode_line (w))
 26787     {
 26788       Lisp_Object window;
 26789       Lisp_Object default_help
 26790         = buffer_local_value (Qmode_line_default_help_echo, w->contents);
 26791 
 26792       /* Set up mode line help echo.  Do this before selecting w so it
 26793          can reasonably tell whether a mouse click will select w.  */
 26794       XSETWINDOW (window, w);
 26795       if (FUNCTIONP (default_help))
 26796         wset_mode_line_help_echo (w, safe_call1 (default_help, window));
 26797       else if (STRINGP (default_help))
 26798         wset_mode_line_help_echo (w, default_help);
 26799       else
 26800         wset_mode_line_help_echo (w, Qnil);
 26801     }
 26802 
 26803   selected_frame = new_frame;
 26804   /* FIXME: If we were to allow the mode-line's computation changing the buffer
 26805      or window's point, then we'd need select_window_1 here as well.  */
 26806   XSETWINDOW (selected_window, w);
 26807   XFRAME (new_frame)->selected_window = selected_window;
 26808 
 26809   /* These will be set while the mode line specs are processed.  */
 26810   line_number_displayed = false;
 26811   w->column_number_displayed = -1;
 26812 
 26813   if (window_wants_mode_line (w))
 26814     {
 26815       Lisp_Object window_mode_line_format
 26816         = window_parameter (w, Qmode_line_format);
 26817       struct window *sel_w = XWINDOW (old_selected_window);
 26818 
 26819       /* Select mode line face based on the real selected window.  */
 26820       display_mode_line (w,
 26821                          CURRENT_MODE_LINE_ACTIVE_FACE_ID_3 (sel_w, sel_w, w),
 26822                          NILP (window_mode_line_format)
 26823                          ? BVAR (current_buffer, mode_line_format)
 26824                          : window_mode_line_format);
 26825       ++n;
 26826     }
 26827 
 26828   if (window_wants_tab_line (w))
 26829     {
 26830       Lisp_Object window_tab_line_format
 26831         = window_parameter (w, Qtab_line_format);
 26832 
 26833       display_mode_line (w, TAB_LINE_FACE_ID,
 26834                          NILP (window_tab_line_format)
 26835                          ? BVAR (current_buffer, tab_line_format)
 26836                          : window_tab_line_format);
 26837       ++n;
 26838     }
 26839 
 26840   if (window_wants_header_line (w))
 26841     {
 26842       Lisp_Object window_header_line_format
 26843         = window_parameter (w, Qheader_line_format);
 26844 
 26845       display_mode_line (w, HEADER_LINE_FACE_ID,
 26846                          NILP (window_header_line_format)
 26847                          ? BVAR (current_buffer, header_line_format)
 26848                          : window_header_line_format);
 26849       ++n;
 26850     }
 26851 
 26852   unbind_to (count, Qnil);
 26853 
 26854   if (n > 0)
 26855     w->must_be_updated_p = true;
 26856   return n;
 26857 }
 26858 
 26859 
 26860 /* Display mode or header/tab line of window W.  FACE_ID specifies
 26861    which line to display; it is either MODE_LINE_ACTIVE_FACE_ID,
 26862    HEADER_LINE_FACE_ID or TAB_LINE_FACE_ID.  FORMAT is the
 26863    mode/header/tab line format to display.  Value is the pixel height
 26864    of the mode/header/tab line displayed.  */
 26865 
 26866 static int
 26867 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
 26868 {
 26869   struct it it;
 26870   struct face *face;
 26871   specpdl_ref count = SPECPDL_INDEX ();
 26872 
 26873   init_iterator (&it, w, -1, -1, NULL, face_id);
 26874   /* Don't extend on a previously drawn mode-line.
 26875      This may happen if called from pos_visible_p.  */
 26876   it.glyph_row->enabled_p = false;
 26877   prepare_desired_row (w, it.glyph_row, true);
 26878 
 26879   it.glyph_row->mode_line_p = true;
 26880   if (face_id == TAB_LINE_FACE_ID)
 26881     {
 26882       it.glyph_row->tab_line_p = true;
 26883       w->desired_matrix->tab_line_p = true;
 26884     }
 26885   else if (face_id == HEADER_LINE_FACE_ID)
 26886     w->desired_matrix->header_line_p = true;
 26887 
 26888   /* FIXME: This should be controlled by a user option.  But
 26889      supporting such an option is not trivial, since the mode line is
 26890      made up of many separate strings.  */
 26891   it.paragraph_embedding = L2R;
 26892 
 26893   record_unwind_protect (unwind_format_mode_line,
 26894                          format_mode_line_unwind_data (NULL, NULL,
 26895                                                        Qnil, false));
 26896 
 26897   /* Temporarily make frame's keyboard the current kboard so that
 26898      kboard-local variables in the mode_line_format will get the right
 26899      values.  */
 26900   push_kboard (FRAME_KBOARD (it.f));
 26901   record_unwind_save_match_data ();
 26902 
 26903   if (NILP (Vmode_line_compact)
 26904       || face_id == HEADER_LINE_FACE_ID || face_id == TAB_LINE_FACE_ID)
 26905     {
 26906       mode_line_target = MODE_LINE_DISPLAY;
 26907       display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 26908     }
 26909   else
 26910     {
 26911       Lisp_Object mode_string = Fformat_mode_line (format, Qnil, Qnil, Qnil);
 26912       if (EQ (Vmode_line_compact, Qlong)
 26913           && WINDOW_TOTAL_COLS (w) >= SCHARS (mode_string))
 26914         {
 26915           /* The window is wide enough; just display the mode line we
 26916              just computed. */
 26917           display_string (NULL, mode_string, Qnil,
 26918                           0, 0, &it, 0, 0, 0,
 26919                           STRING_MULTIBYTE (mode_string));
 26920         }
 26921       else
 26922         {
 26923           /* Compress the mode line. */
 26924           ptrdiff_t i = 0, i_byte = 0, start = 0;
 26925           int prev = 0;
 26926 
 26927           while (i < SCHARS (mode_string))
 26928             {
 26929               int c = fetch_string_char_advance (mode_string, &i, &i_byte);
 26930               if (c == ' ' && prev == ' ')
 26931                 {
 26932                   display_string (NULL,
 26933                                   Fsubstring (mode_string, make_fixnum (start),
 26934                                               make_fixnum (i - 1)),
 26935                                   Qnil, 0, 0, &it, 0, 0, 0,
 26936                                   STRING_MULTIBYTE (mode_string));
 26937                   /* Skip past the rest of the space characters. */
 26938                   while (c == ' ' && i < SCHARS (mode_string))
 26939                       c = fetch_string_char_advance (mode_string, &i, &i_byte);
 26940                   start = i - 1;
 26941                 }
 26942               prev = c;
 26943             }
 26944 
 26945           /* Display the final bit. */
 26946           if (start < i)
 26947             display_string (NULL,
 26948                             Fsubstring (mode_string, make_fixnum (start),
 26949                                         make_fixnum (i)),
 26950                             Qnil, 0, 0, &it, 0, 0, 0,
 26951                             STRING_MULTIBYTE (mode_string));
 26952         }
 26953     }
 26954   pop_kboard ();
 26955 
 26956   unbind_to (count, Qnil);
 26957 
 26958   /* Fill up with spaces.  */
 26959   display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
 26960 
 26961   compute_line_metrics (&it);
 26962   it.glyph_row->full_width_p = true;
 26963   it.glyph_row->continued_p = false;
 26964   it.glyph_row->truncated_on_left_p = false;
 26965   it.glyph_row->truncated_on_right_p = false;
 26966 
 26967   /* Make a 3D mode-line have a shadow at its right end.  */
 26968   face = FACE_FROM_ID (it.f, face_id);
 26969   extend_face_to_end_of_line (&it);
 26970   if (face->box != FACE_NO_BOX)
 26971     {
 26972       struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
 26973                             + it.glyph_row->used[TEXT_AREA] - 1);
 26974       int box_thickness = face->box_vertical_line_width;
 26975       last->right_box_line_p = true;
 26976       /* Add back the space for the right box line we subtracted in
 26977          init_iterator, since the right_box_line_p flag will make the
 26978          glyph wider.  We actually add only as much space as is
 26979          available for the last glyph of the modeline and whatever
 26980          space is left beyond it, since that glyph could be only
 26981          partially visible */
 26982       if (box_thickness > 0)
 26983         last->pixel_width += max (0, (box_thickness
 26984                                       - (it.current_x - it.last_visible_x)));
 26985     }
 26986 
 26987   return it.glyph_row->height;
 26988 }
 26989 
 26990 /* Move element ELT in LIST to the front of LIST.
 26991    Return the updated list.  */
 26992 
 26993 static Lisp_Object
 26994 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
 26995 {
 26996   register Lisp_Object tail, prev;
 26997   register Lisp_Object tem;
 26998 
 26999   tail = list;
 27000   prev = Qnil;
 27001   while (CONSP (tail))
 27002     {
 27003       tem = XCAR (tail);
 27004 
 27005       if (EQ (elt, tem))
 27006         {
 27007           /* Splice out the link TAIL.  */
 27008           if (NILP (prev))
 27009             list = XCDR (tail);
 27010           else
 27011             Fsetcdr (prev, XCDR (tail));
 27012 
 27013           /* Now make it the first.  */
 27014           Fsetcdr (tail, list);
 27015           return tail;
 27016         }
 27017       else
 27018         prev = tail;
 27019       tail = XCDR (tail);
 27020       maybe_quit ();
 27021     }
 27022 
 27023   /* Not found--return unchanged LIST.  */
 27024   return list;
 27025 }
 27026 
 27027 /* Subroutine to call Fset_text_properties through
 27028    internal_condition_case_n.  ARGS are the arguments of
 27029    Fset_text_properties, in order.  */
 27030 
 27031 static Lisp_Object
 27032 safe_set_text_properties (ptrdiff_t nargs, Lisp_Object *args)
 27033 {
 27034   eassert (nargs == 4);
 27035   return Fset_text_properties (args[0], args[1], args[2], args[3]);
 27036 }
 27037 
 27038 /* Contribute ELT to the mode line for window IT->w.  How it
 27039    translates into text depends on its data type.
 27040 
 27041    IT describes the display environment in which we display, as usual.
 27042 
 27043    DEPTH is the depth in recursion.  It is used to prevent
 27044    infinite recursion here.
 27045 
 27046    FIELD_WIDTH is the number of characters the display of ELT should
 27047    occupy in the mode line, and PRECISION is the maximum number of
 27048    characters to display from ELT's representation.  See
 27049    display_string for details.
 27050 
 27051    Returns the hpos of the end of the text generated by ELT.
 27052 
 27053    PROPS is a property list to add to any string we encounter.
 27054 
 27055    If RISKY, remove (disregard) any properties in any string
 27056    we encounter, and ignore :eval and :propertize.
 27057 
 27058    The global variable `mode_line_target' determines whether the
 27059    output is passed to `store_mode_line_noprop',
 27060    `store_mode_line_string', or `display_string'.  */
 27061 
 27062 static int
 27063 display_mode_element (struct it *it, int depth, int field_width, int precision,
 27064                       Lisp_Object elt, Lisp_Object props, bool risky)
 27065 {
 27066   int n = 0, field, prec;
 27067   bool literal = false;
 27068 
 27069  tail_recurse:
 27070   if (depth > 100)
 27071     elt = build_string ("*too-deep*");
 27072 
 27073   depth++;
 27074 
 27075   switch (XTYPE (elt))
 27076     {
 27077     case Lisp_String:
 27078       {
 27079         /* A string: output it and check for %-constructs within it.  */
 27080         unsigned char c;
 27081         ptrdiff_t offset = 0;
 27082 
 27083         if (SCHARS (elt) > 0
 27084             && (!NILP (props) || risky))
 27085           {
 27086             Lisp_Object oprops, aelt;
 27087             oprops = Ftext_properties_at (make_fixnum (0), elt);
 27088 
 27089             /* If the starting string's properties are not what
 27090                we want, translate the string.  Also, if the string
 27091                is risky, do that anyway.  */
 27092 
 27093             if (NILP (Fequal (props, oprops)) || risky)
 27094               {
 27095                 /* If the starting string has properties,
 27096                    merge the specified ones onto the existing ones.  */
 27097                 if (! NILP (oprops) && !risky)
 27098                   {
 27099                     Lisp_Object tem;
 27100 
 27101                     oprops = Fcopy_sequence (oprops);
 27102                     tem = props;
 27103                     while (CONSP (tem) && CONSP (XCDR (tem)))
 27104                       {
 27105                         oprops = plist_put (oprops, XCAR (tem),
 27106                                             XCAR (XCDR (tem)));
 27107                         tem = XCDR (XCDR (tem));
 27108                       }
 27109                     props = oprops;
 27110                   }
 27111 
 27112                 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
 27113                 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
 27114                   {
 27115                     /* AELT is what we want.  Move it to the front
 27116                        without consing.  */
 27117                     elt = XCAR (aelt);
 27118                     mode_line_proptrans_alist
 27119                       = move_elt_to_front (aelt, mode_line_proptrans_alist);
 27120                   }
 27121                 else
 27122                   {
 27123                     Lisp_Object tem;
 27124 
 27125                     /* If AELT has the wrong props, it is useless.
 27126                        so get rid of it.  */
 27127                     if (! NILP (aelt))
 27128                       mode_line_proptrans_alist
 27129                         = Fdelq (aelt, mode_line_proptrans_alist);
 27130 
 27131                     elt = Fcopy_sequence (elt);
 27132                     /* PROPS might cause set-text-properties to signal
 27133                        an error, so we call it via internal_condition_case_n,
 27134                        to avoid an infloop in redisplay due to the error.  */
 27135                     internal_condition_case_n (safe_set_text_properties,
 27136                                                4,
 27137                                                ((Lisp_Object [])
 27138                                                {make_fixnum (0),
 27139                                                    Flength (elt),
 27140                                                    props,
 27141                                                    elt}),
 27142                                                Qt, safe_eval_handler);
 27143                     /* Add this item to mode_line_proptrans_alist.  */
 27144                     mode_line_proptrans_alist
 27145                       = Fcons (Fcons (elt, props),
 27146                                mode_line_proptrans_alist);
 27147                     /* Truncate mode_line_proptrans_alist
 27148                        to at most 50 elements.  */
 27149                     tem = Fnthcdr (make_fixnum (50),
 27150                                    mode_line_proptrans_alist);
 27151                     if (! NILP (tem))
 27152                       XSETCDR (tem, Qnil);
 27153                   }
 27154               }
 27155           }
 27156 
 27157         offset = 0;
 27158 
 27159         if (literal)
 27160           {
 27161             prec = precision - n;
 27162             switch (mode_line_target)
 27163               {
 27164               case MODE_LINE_NOPROP:
 27165               case MODE_LINE_TITLE:
 27166                 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
 27167                 break;
 27168               case MODE_LINE_STRING:
 27169                 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
 27170                 break;
 27171               case MODE_LINE_DISPLAY:
 27172                 n += display_string (NULL, elt, Qnil, 0, 0, it,
 27173                                      0, prec, 0, STRING_MULTIBYTE (elt));
 27174                 break;
 27175               }
 27176 
 27177             break;
 27178           }
 27179 
 27180         /* Handle the non-literal case.  */
 27181 
 27182         while ((precision <= 0 || n < precision)
 27183                && SREF (elt, offset) != 0
 27184                && (mode_line_target != MODE_LINE_DISPLAY
 27185                    || it->current_x < it->last_visible_x))
 27186           {
 27187             ptrdiff_t last_offset = offset;
 27188 
 27189             /* Advance to end of string or next format specifier.  */
 27190             while ((c = SREF (elt, offset++)) != '\0' && c != '%')
 27191               ;
 27192 
 27193             if (offset - 1 != last_offset)
 27194               {
 27195                 ptrdiff_t nchars, nbytes;
 27196 
 27197                 /* Output to end of string or up to '%'.  Field width
 27198                    is length of string.  Don't output more than
 27199                    PRECISION allows us.  */
 27200                 offset--;
 27201 
 27202                 prec = c_string_width (SDATA (elt) + last_offset,
 27203                                        offset - last_offset, precision - n,
 27204                                        &nchars, &nbytes);
 27205 
 27206                 switch (mode_line_target)
 27207                   {
 27208                   case MODE_LINE_NOPROP:
 27209                   case MODE_LINE_TITLE:
 27210                     n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
 27211                     break;
 27212                   case MODE_LINE_STRING:
 27213                     {
 27214                       ptrdiff_t bytepos = last_offset;
 27215                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27216                       ptrdiff_t endpos = (precision <= 0
 27217                                           ? string_byte_to_char (elt, offset)
 27218                                           : charpos + nchars);
 27219                       Lisp_Object mode_string
 27220                         = Fsubstring (elt, make_fixnum (charpos),
 27221                                       make_fixnum (endpos));
 27222                       n += store_mode_line_string (NULL, mode_string, false,
 27223                                                    0, 0, Qnil);
 27224                     }
 27225                     break;
 27226                   case MODE_LINE_DISPLAY:
 27227                     {
 27228                       ptrdiff_t bytepos = last_offset;
 27229                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27230 
 27231                       if (precision <= 0)
 27232                         nchars = string_byte_to_char (elt, offset) - charpos;
 27233                       n += display_string (NULL, elt, Qnil, 0, charpos,
 27234                                            it, 0, nchars, 0,
 27235                                            STRING_MULTIBYTE (elt));
 27236                     }
 27237                     break;
 27238                   }
 27239               }
 27240             else /* c == '%' */
 27241               {
 27242                 ptrdiff_t percent_position = offset;
 27243 
 27244                 /* Get the specified minimum width.  Zero means
 27245                    don't pad.  */
 27246                 field = 0;
 27247                 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
 27248                   field = field * 10 + c - '0';
 27249 
 27250                 /* Don't pad beyond the total padding allowed.  */
 27251                 if (field_width - n > 0 && field > field_width - n)
 27252                   field = field_width - n;
 27253 
 27254                 /* Note that either PRECISION <= 0 or N < PRECISION.  */
 27255                 prec = precision - n;
 27256 
 27257                 if (c == 'M')
 27258                   n += display_mode_element (it, depth, field, prec,
 27259                                              Vglobal_mode_string, props,
 27260                                              risky);
 27261                 else if (c != 0)
 27262                   {
 27263                     bool multibyte;
 27264                     ptrdiff_t bytepos, charpos;
 27265                     const char *spec;
 27266                     Lisp_Object string;
 27267 
 27268                     bytepos = percent_position;
 27269                     charpos = (STRING_MULTIBYTE (elt)
 27270                                ? string_byte_to_char (elt, bytepos)
 27271                                : bytepos);
 27272                     spec = decode_mode_spec (it->w, c, field, &string);
 27273                     eassert (NILP (string) || STRINGP (string));
 27274                     multibyte = !NILP (string) && STRING_MULTIBYTE (string);
 27275                     /* Non-ASCII characters in SPEC should cause mode-line
 27276                        element be displayed as a multibyte string.  */
 27277                     ptrdiff_t nbytes = strlen (spec);
 27278                     ptrdiff_t nchars, mb_nbytes;
 27279                     parse_str_as_multibyte ((const unsigned char *)spec, nbytes,
 27280                                             &nchars, &mb_nbytes);
 27281                     if (!(nbytes == nchars || nbytes != mb_nbytes))
 27282                       multibyte = true;
 27283 
 27284                     switch (mode_line_target)
 27285                       {
 27286                       case MODE_LINE_NOPROP:
 27287                       case MODE_LINE_TITLE:
 27288                         n += store_mode_line_noprop (spec, field, prec);
 27289                         break;
 27290                       case MODE_LINE_STRING:
 27291                         {
 27292                           Lisp_Object tem = build_string (spec);
 27293                           props = Ftext_properties_at (make_fixnum (charpos), elt);
 27294                           /* Should only keep face property in props */
 27295                           n += store_mode_line_string (NULL, tem, false,
 27296                                                        field, prec, props);
 27297                         }
 27298                         break;
 27299                       case MODE_LINE_DISPLAY:
 27300                         {
 27301                           int nglyphs_before, nwritten;
 27302 
 27303                           nglyphs_before = it->glyph_row->used[TEXT_AREA];
 27304                           nwritten = display_string (spec, string, elt,
 27305                                                      charpos, 0, it,
 27306                                                      field, prec, 0,
 27307                                                      multibyte);
 27308 
 27309                           /* Assign to the glyphs written above the
 27310                              string where the `%x' came from, position
 27311                              of the `%'.  */
 27312                           if (nwritten > 0)
 27313                             {
 27314                               struct glyph *glyph
 27315                                 = (it->glyph_row->glyphs[TEXT_AREA]
 27316                                    + nglyphs_before);
 27317                               int i;
 27318 
 27319                               for (i = 0; i < nwritten; ++i)
 27320                                 {
 27321                                   glyph[i].object = elt;
 27322                                   glyph[i].charpos = charpos;
 27323                                 }
 27324 
 27325                               n += nwritten;
 27326                             }
 27327                         }
 27328                         break;
 27329                       }
 27330                   }
 27331                 else /* c == 0 */
 27332                   break;
 27333               }
 27334           }
 27335       }
 27336       break;
 27337 
 27338     case Lisp_Symbol:
 27339       /* A symbol: process the value of the symbol recursively
 27340          as if it appeared here directly.  Avoid error if symbol void.
 27341          Special case: if value of symbol is a string, output the string
 27342          literally.  */
 27343       {
 27344         register Lisp_Object tem;
 27345 
 27346         /* If the variable is not marked as risky to set
 27347            then its contents are risky to use.  */
 27348         if (NILP (Fget (elt, Qrisky_local_variable)))
 27349           risky = true;
 27350 
 27351         tem = Fboundp (elt);
 27352         if (!NILP (tem))
 27353           {
 27354             tem = Fsymbol_value (elt);
 27355             /* If value is a string, output that string literally:
 27356                don't check for % within it.  */
 27357             if (STRINGP (tem))
 27358               literal = true;
 27359 
 27360             if (!EQ (tem, elt))
 27361               {
 27362                 /* Give up right away for nil or t.  */
 27363                 elt = tem;
 27364                 goto tail_recurse;
 27365               }
 27366           }
 27367       }
 27368       break;
 27369 
 27370     case Lisp_Cons:
 27371       {
 27372         register Lisp_Object car, tem;
 27373 
 27374         /* A cons cell: five distinct cases.
 27375            If first element is :eval or :propertize, do something special.
 27376            If first element is a string or a cons, process all the elements
 27377            and effectively concatenate them.
 27378            If first element is a negative number, truncate displaying cdr to
 27379            at most that many characters.  If positive, pad (with spaces)
 27380            to at least that many characters.
 27381            If first element is a symbol, process the cadr or caddr recursively
 27382            according to whether the symbol's value is non-nil or nil.  */
 27383         car = XCAR (elt);
 27384         if (EQ (car, QCeval))
 27385           {
 27386             /* An element of the form (:eval FORM) means evaluate FORM
 27387                and use the result as mode line elements.  */
 27388 
 27389             if (risky)
 27390               break;
 27391 
 27392             if (CONSP (XCDR (elt)))
 27393               {
 27394                 Lisp_Object spec;
 27395                 spec = safe__eval (true, XCAR (XCDR (elt)));
 27396                 /* The :eval form could delete the frame stored in the
 27397                    iterator, which will cause a crash if we try to
 27398                    access faces and other fields (e.g., FRAME_KBOARD)
 27399                    on that frame.  This is a nonsensical thing to do,
 27400                    and signaling an error from redisplay might be
 27401                    dangerous, but we cannot continue with an invalid frame.  */
 27402                 if (!FRAME_LIVE_P (it->f))
 27403                   signal_error (":eval deleted the frame being displayed", elt);
 27404                 n += display_mode_element (it, depth, field_width - n,
 27405                                            precision - n, spec, props,
 27406                                            risky);
 27407               }
 27408           }
 27409         else if (EQ (car, QCpropertize))
 27410           {
 27411             /* An element of the form (:propertize ELT PROPS...)
 27412                means display ELT but applying properties PROPS.  */
 27413 
 27414             if (risky)
 27415               break;
 27416 
 27417             if (CONSP (XCDR (elt)))
 27418               n += display_mode_element (it, depth, field_width - n,
 27419                                          precision - n, XCAR (XCDR (elt)),
 27420                                          XCDR (XCDR (elt)), risky);
 27421           }
 27422         else if (SYMBOLP (car))
 27423           {
 27424             tem = Fboundp (car);
 27425             elt = XCDR (elt);
 27426             if (!CONSP (elt))
 27427               goto invalid;
 27428             /* elt is now the cdr, and we know it is a cons cell.
 27429                Use its car if CAR has a non-nil value.  */
 27430             if (!NILP (tem))
 27431               {
 27432                 tem = Fsymbol_value (car);
 27433                 if (!NILP (tem))
 27434                   {
 27435                     elt = XCAR (elt);
 27436                     goto tail_recurse;
 27437                   }
 27438               }
 27439             /* Symbol's value is nil (or symbol is unbound)
 27440                Get the cddr of the original list
 27441                and if possible find the caddr and use that.  */
 27442             elt = XCDR (elt);
 27443             if (NILP (elt))
 27444               break;
 27445             else if (!CONSP (elt))
 27446               goto invalid;
 27447             elt = XCAR (elt);
 27448             goto tail_recurse;
 27449           }
 27450         else if (FIXNUMP (car))
 27451           {
 27452             register int lim = XFIXNUM (car);
 27453             elt = XCDR (elt);
 27454             if (lim < 0)
 27455               {
 27456                 /* Negative int means reduce maximum width.  */
 27457                 if (precision <= 0)
 27458                   precision = -lim;
 27459                 else
 27460                   precision = min (precision, -lim);
 27461               }
 27462             else if (lim > 0)
 27463               {
 27464                 /* Padding specified.  Don't let it be more than
 27465                    current maximum.  */
 27466                 if (precision > 0)
 27467                   lim = min (precision, lim);
 27468 
 27469                 /* If that's more padding than already wanted, queue it.
 27470                    But don't reduce padding already specified even if
 27471                    that is beyond the current truncation point.  */
 27472                 field_width = max (lim, field_width);
 27473               }
 27474             goto tail_recurse;
 27475           }
 27476         else if (STRINGP (car) || CONSP (car))
 27477           FOR_EACH_TAIL_SAFE (elt)
 27478             {
 27479               if (0 < precision && precision <= n)
 27480                 break;
 27481               n += display_mode_element (it, depth,
 27482                                          /* Pad after only the last
 27483                                             list element.  */
 27484                                          (! CONSP (XCDR (elt))
 27485                                           ? field_width - n
 27486                                           : 0),
 27487                                          precision - n, XCAR (elt),
 27488                                          props, risky);
 27489             }
 27490       }
 27491       break;
 27492 
 27493     default:
 27494     invalid:
 27495       elt = build_string ("*invalid*");
 27496       goto tail_recurse;
 27497     }
 27498 
 27499   /* Pad to FIELD_WIDTH.  */
 27500   if (field_width > 0 && n < field_width)
 27501     {
 27502       switch (mode_line_target)
 27503         {
 27504         case MODE_LINE_NOPROP:
 27505         case MODE_LINE_TITLE:
 27506           n += store_mode_line_noprop ("", field_width - n, 0);
 27507           break;
 27508         case MODE_LINE_STRING:
 27509           n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
 27510                                        Qnil);
 27511           break;
 27512         case MODE_LINE_DISPLAY:
 27513           n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
 27514                                0, 0, 0);
 27515           break;
 27516         }
 27517     }
 27518 
 27519   return n;
 27520 }
 27521 
 27522 /* Store a mode-line string element in mode_line_string_list.
 27523 
 27524    If STRING is non-null, display that C string.  Otherwise, the Lisp
 27525    string LISP_STRING is displayed.
 27526 
 27527    FIELD_WIDTH is the minimum number of output glyphs to produce.
 27528    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 27529    with spaces.  FIELD_WIDTH <= 0 means don't pad.
 27530 
 27531    PRECISION is the maximum number of characters to output from
 27532    STRING.  PRECISION <= 0  means don't truncate the string.
 27533 
 27534    If COPY_STRING, make a copy of LISP_STRING before adding
 27535    properties to the string.
 27536 
 27537    PROPS are the properties to add to the string.
 27538    The mode_line_string_face face property is always added to the string.
 27539  */
 27540 
 27541 static int
 27542 store_mode_line_string (const char *string, Lisp_Object lisp_string,
 27543                         bool copy_string,
 27544                         int field_width, int precision, Lisp_Object props)
 27545 {
 27546   ptrdiff_t len;
 27547   int n = 0;
 27548 
 27549   if (string != NULL)
 27550     {
 27551       len = strnlen (string, precision <= 0 ? SIZE_MAX : precision);
 27552       lisp_string = make_string (string, len);
 27553       if (NILP (props))
 27554         props = mode_line_string_face_prop;
 27555       else if (!NILP (mode_line_string_face))
 27556         {
 27557           Lisp_Object face = plist_get (props, Qface);
 27558           props = Fcopy_sequence (props);
 27559           if (NILP (face))
 27560             face = mode_line_string_face;
 27561           else
 27562             face = list2 (face, mode_line_string_face);
 27563           props = plist_put (props, Qface, face);
 27564         }
 27565       Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27566                             props, lisp_string);
 27567     }
 27568   else
 27569     {
 27570       len = SCHARS (lisp_string);
 27571       if (precision > 0 && len > precision)
 27572         {
 27573           len = precision;
 27574           lisp_string = Fsubstring (lisp_string, make_fixnum (0), make_fixnum (len));
 27575           precision = -1;
 27576         }
 27577       if (!NILP (mode_line_string_face))
 27578         {
 27579           Lisp_Object face;
 27580           if (NILP (props))
 27581             props = Ftext_properties_at (make_fixnum (0), lisp_string);
 27582           face = plist_get (props, Qface);
 27583           if (NILP (face))
 27584             face = mode_line_string_face;
 27585           else
 27586             face = list2 (face, mode_line_string_face);
 27587           props = list2 (Qface, face);
 27588           if (copy_string)
 27589             lisp_string = Fcopy_sequence (lisp_string);
 27590         }
 27591       if (!NILP (props))
 27592         Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27593                               props, lisp_string);
 27594     }
 27595 
 27596   if (len > 0)
 27597     {
 27598       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27599       n += len;
 27600     }
 27601 
 27602   if (field_width > len)
 27603     {
 27604       field_width -= len;
 27605       lisp_string = Fmake_string (make_fixnum (field_width), make_fixnum (' '),
 27606                                   Qnil);
 27607       if (!NILP (props))
 27608         Fadd_text_properties (make_fixnum (0), make_fixnum (field_width),
 27609                               props, lisp_string);
 27610       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27611       n += field_width;
 27612     }
 27613 
 27614   return n;
 27615 }
 27616 
 27617 
 27618 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
 27619        1, 4, 0,
 27620        doc: /* Format a string out of a mode line format specification.
 27621 First arg FORMAT specifies the mode line format (see `mode-line-format'
 27622 for details) to use.
 27623 
 27624 By default, the format is evaluated for the currently selected window.
 27625 
 27626 Optional second arg FACE specifies the face property to put on all
 27627 characters for which no face is specified.  The value nil means the
 27628 default face.  The value t means whatever face the window's mode line
 27629 currently uses (either `mode-line' or `mode-line-inactive',
 27630 depending on whether the window is the selected window or not).
 27631 An integer value means the value string has no text
 27632 properties.
 27633 
 27634 Optional third and fourth args WINDOW and BUFFER specify the window
 27635 and buffer to use as the context for the formatting (defaults
 27636 are the selected window and the WINDOW's buffer).  */)
 27637      (Lisp_Object format, Lisp_Object face,
 27638       Lisp_Object window, Lisp_Object buffer)
 27639 {
 27640   struct it it;
 27641   int len;
 27642   struct window *w;
 27643   struct buffer *old_buffer = NULL;
 27644   int face_id;
 27645   bool no_props = FIXNUMP (face);
 27646   specpdl_ref count = SPECPDL_INDEX ();
 27647   Lisp_Object str;
 27648   int string_start = 0;
 27649 
 27650   w = decode_any_window (window);
 27651   XSETWINDOW (window, w);
 27652 
 27653   if (NILP (buffer))
 27654     buffer = w->contents;
 27655   CHECK_BUFFER (buffer);
 27656   if (!BUFFER_LIVE_P (XBUFFER (buffer)))
 27657     error ("Attempt to format a mode line for a dead buffer");
 27658 
 27659   /* Make formatting the modeline a non-op when noninteractive, otherwise
 27660      there will be problems later caused by a partially initialized frame.  */
 27661   if (NILP (format) || noninteractive)
 27662     return empty_unibyte_string;
 27663 
 27664   if (no_props)
 27665     face = Qnil;
 27666 
 27667   face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
 27668     : EQ (face, Qt) ? (EQ (window, selected_window)
 27669                        ? MODE_LINE_ACTIVE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
 27670     : EQ (face, Qmode_line_active) ? MODE_LINE_ACTIVE_FACE_ID
 27671     : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
 27672     : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
 27673     : EQ (face, Qtab_line) ? TAB_LINE_FACE_ID
 27674     : EQ (face, Qtab_bar) ? TAB_BAR_FACE_ID
 27675     : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
 27676     : DEFAULT_FACE_ID;
 27677 
 27678   old_buffer = current_buffer;
 27679 
 27680   /* Save things including mode_line_proptrans_alist,
 27681      and set that to nil so that we don't alter the outer value.  */
 27682   record_unwind_protect (unwind_format_mode_line,
 27683                          format_mode_line_unwind_data
 27684                            (XFRAME (WINDOW_FRAME (w)),
 27685                             old_buffer, selected_window, true));
 27686   mode_line_proptrans_alist = Qnil;
 27687 
 27688   Fselect_window (window, Qt);
 27689   set_buffer_internal_1 (XBUFFER (buffer));
 27690 
 27691   init_iterator (&it, w, -1, -1, NULL, face_id);
 27692 
 27693   if (no_props)
 27694     {
 27695       mode_line_target = MODE_LINE_NOPROP;
 27696       mode_line_string_face_prop = Qnil;
 27697       mode_line_string_list = Qnil;
 27698       string_start = MODE_LINE_NOPROP_LEN (0);
 27699     }
 27700   else
 27701     {
 27702       mode_line_target = MODE_LINE_STRING;
 27703       mode_line_string_list = Qnil;
 27704       mode_line_string_face = face;
 27705       mode_line_string_face_prop
 27706         = NILP (face) ? Qnil : list2 (Qface, face);
 27707     }
 27708 
 27709   push_kboard (FRAME_KBOARD (it.f));
 27710   display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 27711   pop_kboard ();
 27712 
 27713   if (no_props)
 27714     {
 27715       len = MODE_LINE_NOPROP_LEN (string_start);
 27716       str = make_string (mode_line_noprop_buf + string_start, len);
 27717     }
 27718   else
 27719     {
 27720       mode_line_string_list = Fnreverse (mode_line_string_list);
 27721       str = Fmapconcat (Qidentity, mode_line_string_list,
 27722                         empty_unibyte_string);
 27723     }
 27724 
 27725   return unbind_to (count, str);
 27726 }
 27727 
 27728 /* Write a null-terminated, right justified decimal representation of
 27729    the positive integer D to BUF using a minimal field width WIDTH.  */
 27730 
 27731 static void
 27732 pint2str (register char *buf, register int width, register ptrdiff_t d)
 27733 {
 27734   register char *p = buf;
 27735 
 27736   if (d <= 0)
 27737     *p++ = '0';
 27738   else
 27739     {
 27740       while (d > 0)
 27741         {
 27742           *p++ = d % 10 + '0';
 27743           d /= 10;
 27744         }
 27745     }
 27746 
 27747   for (width -= (int) (p - buf); width > 0; --width)
 27748     *p++ = ' ';
 27749   *p-- = '\0';
 27750   while (p > buf)
 27751     {
 27752       d = *buf;
 27753       *buf++ = *p;
 27754       *p-- = d;
 27755     }
 27756 }
 27757 
 27758 /* Write a null-terminated, right justified decimal and "human
 27759    readable" representation of the nonnegative integer D to BUF using
 27760    a minimal field width WIDTH.  D should be smaller than 999.5e24. */
 27761 
 27762 static const char power_letter[] =
 27763   {
 27764     0,   /* no letter */
 27765     'k', /* kilo */
 27766     'M', /* mega */
 27767     'G', /* giga */
 27768     'T', /* tera */
 27769     'P', /* peta */
 27770     'E', /* exa */
 27771     'Z', /* zetta */
 27772     'Y'  /* yotta */
 27773   };
 27774 
 27775 static void
 27776 pint2hrstr (char *buf, int width, ptrdiff_t d)
 27777 {
 27778   /* We aim to represent the nonnegative integer D as
 27779      QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
 27780   ptrdiff_t quotient = d;
 27781   int remainder = 0;
 27782   /* -1 means: do not use TENTHS. */
 27783   int tenths = -1;
 27784   int exponent = 0;
 27785 
 27786   /* Length of QUOTIENT.TENTHS as a string. */
 27787   int length;
 27788 
 27789   char * psuffix;
 27790   char * p;
 27791 
 27792   if (quotient >= 1000)
 27793     {
 27794       /* Scale to the appropriate EXPONENT. */
 27795       do
 27796         {
 27797           remainder = quotient % 1000;
 27798           quotient /= 1000;
 27799           exponent++;
 27800         }
 27801       while (quotient >= 1000);
 27802 
 27803       /* Round to nearest and decide whether to use TENTHS or not. */
 27804       if (quotient <= 9)
 27805         {
 27806           tenths = remainder / 100;
 27807           if (remainder % 100 >= 50)
 27808             {
 27809               if (tenths < 9)
 27810                 tenths++;
 27811               else
 27812                 {
 27813                   quotient++;
 27814                   if (quotient == 10)
 27815                     tenths = -1;
 27816                   else
 27817                     tenths = 0;
 27818                 }
 27819             }
 27820         }
 27821       else
 27822         if (remainder >= 500)
 27823           {
 27824             if (quotient < 999)
 27825               quotient++;
 27826             else
 27827               {
 27828                 quotient = 1;
 27829                 exponent++;
 27830                 tenths = 0;
 27831               }
 27832           }
 27833     }
 27834 
 27835   /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
 27836   if (tenths == -1 && quotient <= 99)
 27837     if (quotient <= 9)
 27838       length = 1;
 27839     else
 27840       length = 2;
 27841   else
 27842     length = 3;
 27843   p = psuffix = buf + max (width, length);
 27844 
 27845   /* Print EXPONENT. */
 27846   *psuffix++ = power_letter[exponent];
 27847   *psuffix = '\0';
 27848 
 27849   /* Print TENTHS. */
 27850   if (tenths >= 0)
 27851     {
 27852       *--p = '0' + tenths;
 27853       *--p = '.';
 27854     }
 27855 
 27856   /* Print QUOTIENT. */
 27857   do
 27858     {
 27859       int digit = quotient % 10;
 27860       *--p =  '0' + digit;
 27861     }
 27862   while ((quotient /= 10) != 0);
 27863 
 27864   /* Print leading spaces. */
 27865   while (buf < p)
 27866     *--p = ' ';
 27867 }
 27868 
 27869 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
 27870    If EOL_FLAG, set also a mnemonic character for end-of-line
 27871    type of CODING_SYSTEM.  Return updated pointer into BUF.  */
 27872 
 27873 static unsigned char invalid_eol_type[] = "(*invalid*)";
 27874 
 27875 static char *
 27876 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
 27877 {
 27878   Lisp_Object val;
 27879   bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
 27880   const unsigned char *eol_str;
 27881   int eol_str_len;
 27882   /* The EOL conversion we are using.  */
 27883   Lisp_Object eoltype;
 27884 
 27885   val = CODING_SYSTEM_SPEC (coding_system);
 27886   eoltype = Qnil;
 27887 
 27888   if (!VECTORP (val))           /* Not yet decided.  */
 27889     {
 27890       *buf++ = multibyte ? '-' : ' ';
 27891       if (eol_flag)
 27892         eoltype = eol_mnemonic_undecided;
 27893       /* Don't mention EOL conversion if it isn't decided.  */
 27894     }
 27895   else
 27896     {
 27897       Lisp_Object attrs;
 27898       Lisp_Object eolvalue;
 27899 
 27900       attrs = AREF (val, 0);
 27901       eolvalue = AREF (val, 2);
 27902 
 27903       if (multibyte)
 27904         buf += CHAR_STRING (XFIXNAT (CODING_ATTR_MNEMONIC (attrs)),
 27905                             (unsigned char *) buf);
 27906       else
 27907         *buf++ = ' ';
 27908 
 27909       if (eol_flag)
 27910         {
 27911           /* The EOL conversion that is normal on this system.  */
 27912 
 27913           if (NILP (eolvalue))  /* Not yet decided.  */
 27914             eoltype = eol_mnemonic_undecided;
 27915           else if (VECTORP (eolvalue)) /* Not yet decided.  */
 27916             eoltype = eol_mnemonic_undecided;
 27917           else                  /* eolvalue is Qunix, Qdos, or Qmac.  */
 27918             eoltype = (EQ (eolvalue, Qunix)
 27919                        ? eol_mnemonic_unix
 27920                        : EQ (eolvalue, Qdos)
 27921                        ? eol_mnemonic_dos : eol_mnemonic_mac);
 27922         }
 27923     }
 27924 
 27925   if (eol_flag)
 27926     {
 27927       /* Mention the EOL conversion if it is not the usual one.  */
 27928       if (STRINGP (eoltype))
 27929         {
 27930           eol_str = SDATA (eoltype);
 27931           eol_str_len = SBYTES (eoltype);
 27932         }
 27933       else if (CHARACTERP (eoltype))
 27934         {
 27935           int c = XFIXNAT (eoltype);
 27936           return buf + CHAR_STRING (c, (unsigned char *) buf);
 27937         }
 27938       else
 27939         {
 27940           eol_str = invalid_eol_type;
 27941           eol_str_len = sizeof (invalid_eol_type) - 1;
 27942         }
 27943       memcpy (buf, eol_str, eol_str_len);
 27944       buf += eol_str_len;
 27945     }
 27946 
 27947   return buf;
 27948 }
 27949 
 27950 /* Return the approximate percentage N is of D (rounding upward), or 99,
 27951    whichever is less.  Assume 0 < D and 0 <= N <= D * INT_MAX / 100.  */
 27952 
 27953 static int
 27954 percent99 (ptrdiff_t n, ptrdiff_t d)
 27955 {
 27956   int percent = (d - 1 + 100.0 * n) / d;
 27957   return min (percent, 99);
 27958 }
 27959 
 27960 /* Return a string for the output of a mode line %-spec for window W,
 27961    generated by character C.  FIELD_WIDTH > 0 means pad the string
 27962    returned with spaces to that value.  Set *STRING to be a Lisp
 27963    string if the resulting string is taken from that Lisp string;
 27964    otherwise, set *STRING to Qnil.
 27965 
 27966    Note we operate on the current buffer for most purposes.  */
 27967 
 27968 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
 27969 
 27970 static const char *
 27971 decode_mode_spec (struct window *w, register int c, int field_width,
 27972                   Lisp_Object *string)
 27973 {
 27974   Lisp_Object obj;
 27975   struct frame *f = XFRAME (WINDOW_FRAME (w));
 27976   char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
 27977   /* We are going to use f->decode_mode_spec_buffer as the buffer to
 27978      produce strings from numerical values, so limit preposterously
 27979      large values of FIELD_WIDTH to avoid overrunning the buffer's
 27980      end.  The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
 27981      bytes plus the terminating null.  */
 27982   int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
 27983   struct buffer *b = current_buffer;
 27984 
 27985   obj = Qnil;
 27986   *string = Qnil;
 27987 
 27988   switch (c)
 27989     {
 27990     case '*':
 27991       if (!NILP (BVAR (b, read_only)))
 27992         return "%";
 27993       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 27994         return "*";
 27995       return "-";
 27996 
 27997     case '+':
 27998       /* This differs from %* only for a modified read-only buffer.  */
 27999       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28000         return "*";
 28001       if (!NILP (BVAR (b, read_only)))
 28002         return "%";
 28003       return "-";
 28004 
 28005     case '&':
 28006       /* This differs from %* in ignoring read-only-ness.  */
 28007       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28008         return "*";
 28009       return "-";
 28010 
 28011     case '%':
 28012       return "%";
 28013 
 28014     case '[':
 28015       {
 28016         int i;
 28017         char *p;
 28018 
 28019         if (command_loop_level > 5)
 28020           return "[[[... ";
 28021         p = decode_mode_spec_buf;
 28022         for (i = 0; i < command_loop_level; i++)
 28023           *p++ = '[';
 28024         *p = 0;
 28025         return decode_mode_spec_buf;
 28026       }
 28027 
 28028     case ']':
 28029       {
 28030         int i;
 28031         char *p;
 28032 
 28033         if (command_loop_level > 5)
 28034           return " ...]]]";
 28035         p = decode_mode_spec_buf;
 28036         for (i = 0; i < command_loop_level; i++)
 28037           *p++ = ']';
 28038         *p = 0;
 28039         return decode_mode_spec_buf;
 28040       }
 28041 
 28042     case '-':
 28043       {
 28044         register int i;
 28045 
 28046         /* Let lots_of_dashes be a string of infinite length.  */
 28047         if (mode_line_target == MODE_LINE_NOPROP
 28048             || mode_line_target == MODE_LINE_STRING)
 28049           return "--";
 28050         if (field_width <= 0
 28051             || field_width > sizeof (lots_of_dashes))
 28052           {
 28053             for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
 28054               decode_mode_spec_buf[i] = '-';
 28055             decode_mode_spec_buf[i] = '\0';
 28056             return decode_mode_spec_buf;
 28057           }
 28058         else
 28059           return lots_of_dashes;
 28060       }
 28061 
 28062     case 'b':
 28063       obj = BVAR (b, name);
 28064       break;
 28065 
 28066     case 'c':
 28067     case 'C':
 28068       /* %c, %C, and %l are ignored in `frame-title-format'.
 28069          (In redisplay_internal, the frame title is drawn _before_ the
 28070          windows are updated, so the stuff which depends on actual
 28071          window contents (such as %l) may fail to render properly, or
 28072          even crash emacs.)  */
 28073       if (mode_line_target == MODE_LINE_TITLE)
 28074         return "";
 28075       else
 28076         {
 28077           ptrdiff_t col = current_column ();
 28078           int disp_col = (c == 'C') ? col + 1 : col;
 28079           w->column_number_displayed = col;
 28080           pint2str (decode_mode_spec_buf, width, disp_col);
 28081           return decode_mode_spec_buf;
 28082         }
 28083 
 28084     case 'e':
 28085 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
 28086       {
 28087         if (NILP (Vmemory_full))
 28088           return "";
 28089         else
 28090           return "!MEM FULL! ";
 28091       }
 28092 #else
 28093       return "";
 28094 #endif
 28095 
 28096     case 'F':
 28097       /* %F displays the frame name.  */
 28098       if (!NILP (f->title))
 28099         return SSDATA (f->title);
 28100       if (f->explicit_name || ! FRAME_WINDOW_P (f))
 28101         return SSDATA (f->name);
 28102       return "Emacs";
 28103 
 28104     case 'f':
 28105       obj = BVAR (b, filename);
 28106       break;
 28107 
 28108     case 'i':
 28109       {
 28110         ptrdiff_t size = ZV - BEGV;
 28111         pint2str (decode_mode_spec_buf, width, size);
 28112         return decode_mode_spec_buf;
 28113       }
 28114 
 28115     case 'I':
 28116       {
 28117         ptrdiff_t size = ZV - BEGV;
 28118         pint2hrstr (decode_mode_spec_buf, width, size);
 28119         return decode_mode_spec_buf;
 28120       }
 28121 
 28122     case 'l':
 28123       {
 28124         ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
 28125         ptrdiff_t topline, nlines, height;
 28126         ptrdiff_t junk;
 28127 
 28128         /* %c, %C, and %l are ignored in `frame-title-format'.  */
 28129         if (mode_line_target == MODE_LINE_TITLE)
 28130           return "";
 28131 
 28132         startpos = marker_position (w->start);
 28133         startpos_byte = marker_byte_position (w->start);
 28134         height = WINDOW_TOTAL_LINES (w);
 28135         /* We cannot cope with w->start being outside of the
 28136            accessible portion of the buffer; in particular,
 28137            display_count_lines call below might infloop if called with
 28138            startpos_byte outside of the [BEGV_BYTE..ZV_BYTE] region.
 28139            Such w->start means we were called in some "creative" way
 28140            when the buffer's restriction was changed, but the window
 28141            wasn't yet redisplayed after that.  If that happens, we
 28142            need to determine a new base line.  */
 28143         if (!(BUF_BEGV_BYTE (b) <= startpos_byte
 28144               && startpos_byte <= BUF_ZV_BYTE (b)))
 28145           {
 28146             startpos = BUF_BEGV (b);
 28147             startpos_byte = BUF_BEGV_BYTE (b);
 28148             w->base_line_pos = 0;
 28149             w->base_line_number = 0;
 28150           }
 28151 
 28152         /* If we decided that this buffer isn't suitable for line numbers,
 28153            don't forget that too fast.  */
 28154         if (w->base_line_pos == -1)
 28155           goto no_value;
 28156 
 28157         /* If the buffer is very big, don't waste time.  */
 28158         if (FIXNUMP (Vline_number_display_limit)
 28159             && BUF_ZV (b) - BUF_BEGV (b) > XFIXNUM (Vline_number_display_limit))
 28160           {
 28161             w->base_line_pos = 0;
 28162             w->base_line_number = 0;
 28163             goto no_value;
 28164           }
 28165 
 28166         if (w->base_line_number > 0
 28167             && w->base_line_pos > 0
 28168             && w->base_line_pos <= startpos)
 28169           {
 28170             line = w->base_line_number;
 28171             linepos = w->base_line_pos;
 28172             linepos_byte = buf_charpos_to_bytepos (b, linepos);
 28173           }
 28174         else
 28175           {
 28176             line = 1;
 28177             linepos = BUF_BEGV (b);
 28178             linepos_byte = BUF_BEGV_BYTE (b);
 28179           }
 28180 
 28181         /* Count lines from base line to window start position.  */
 28182         nlines = display_count_lines (linepos_byte,
 28183                                       startpos_byte,
 28184                                       startpos, &junk);
 28185 
 28186         topline = nlines + line;
 28187 
 28188         /* Determine a new base line, if the old one is too close
 28189            or too far away, or if we did not have one.
 28190            "Too close" means it's plausible a scroll-down would
 28191            go back past it.  */
 28192         if (startpos == BUF_BEGV (b))
 28193           {
 28194             w->base_line_number = topline;
 28195             w->base_line_pos = BUF_BEGV (b);
 28196           }
 28197         else if (nlines < height + 25 || nlines > height * 3 + 50
 28198                  || linepos == BUF_BEGV (b))
 28199           {
 28200             ptrdiff_t limit = BUF_BEGV (b);
 28201             ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
 28202             ptrdiff_t position;
 28203             ptrdiff_t distance
 28204               = (line_number_display_limit_width < 0 ? 0
 28205                  : INT_MULTIPLY_WRAPV (line_number_display_limit_width,
 28206                                        height * 2 + 30,
 28207                                        &distance)
 28208                  ? PTRDIFF_MAX : distance);
 28209 
 28210             if (startpos - distance > limit)
 28211               {
 28212                 limit = startpos - distance;
 28213                 limit_byte = CHAR_TO_BYTE (limit);
 28214               }
 28215 
 28216             nlines = display_count_lines (startpos_byte,
 28217                                           limit_byte,
 28218                                           - (height * 2 + 30),
 28219                                           &position);
 28220             /* If we couldn't find the lines we wanted within
 28221                line_number_display_limit_width chars per line,
 28222                give up on line numbers for this window.  */
 28223             if (position == limit_byte && limit == startpos - distance)
 28224               {
 28225                 w->base_line_pos = -1;
 28226                 w->base_line_number = 0;
 28227                 goto no_value;
 28228               }
 28229 
 28230             w->base_line_number = topline - nlines;
 28231             w->base_line_pos = BYTE_TO_CHAR (position);
 28232           }
 28233 
 28234         /* Now count lines from the start pos to point.  */
 28235         nlines = display_count_lines (startpos_byte,
 28236                                       PT_BYTE, PT, &junk);
 28237 
 28238         /* Record that we did display the line number.  */
 28239         line_number_displayed = true;
 28240 
 28241         /* Make the string to show.  */
 28242         pint2str (decode_mode_spec_buf, width, topline + nlines);
 28243         return decode_mode_spec_buf;
 28244     no_value:
 28245         {
 28246           char *p = decode_mode_spec_buf;
 28247           int pad = width - 2;
 28248           while (pad-- > 0)
 28249             *p++ = ' ';
 28250           *p++ = '?';
 28251           *p++ = '?';
 28252           *p = '\0';
 28253           return decode_mode_spec_buf;
 28254         }
 28255       }
 28256       break;
 28257 
 28258     case 'm':
 28259       obj = BVAR (b, mode_name);
 28260       break;
 28261 
 28262     case 'n':
 28263       if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
 28264         return " Narrow";
 28265       break;
 28266 
 28267       /* Display the "degree of travel" of the window through the buffer.  */
 28268     case 'o':
 28269       {
 28270         ptrdiff_t toppos = marker_position (w->start);
 28271         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28272         ptrdiff_t begv = BUF_BEGV (b);
 28273         ptrdiff_t zv = BUF_ZV (b);
 28274 
 28275         if (zv <= botpos)
 28276           return toppos <= begv ? "All" : "Bottom";
 28277         else if (toppos <= begv)
 28278           return "Top";
 28279         else
 28280           {
 28281           sprintf (decode_mode_spec_buf, "%2d%%",
 28282                    percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
 28283           return decode_mode_spec_buf;
 28284           }
 28285       }
 28286 
 28287       /* Display percentage of buffer above the top of the screen.  */
 28288     case 'p':
 28289       {
 28290         ptrdiff_t pos = marker_position (w->start);
 28291         ptrdiff_t begv = BUF_BEGV (b);
 28292         ptrdiff_t zv = BUF_ZV (b);
 28293 
 28294         if (w->window_end_pos <= BUF_Z (b) - zv)
 28295           return pos <= begv ? "All" : "Bottom";
 28296         else if (pos <= begv)
 28297           return "Top";
 28298         else
 28299           {
 28300             sprintf (decode_mode_spec_buf, "%2d%%",
 28301                      percent99 (pos - begv, zv - begv));
 28302             return decode_mode_spec_buf;
 28303           }
 28304       }
 28305 
 28306       /* Display percentage of size above the bottom of the screen.  */
 28307     case 'P':
 28308       {
 28309         ptrdiff_t toppos = marker_position (w->start);
 28310         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28311         ptrdiff_t begv = BUF_BEGV (b);
 28312         ptrdiff_t zv = BUF_ZV (b);
 28313 
 28314         if (zv <= botpos)
 28315           return toppos <= begv ? "All" : "Bottom";
 28316         else
 28317           {
 28318             sprintf (decode_mode_spec_buf,
 28319                      &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
 28320                      percent99 (botpos - begv, zv - begv));
 28321             return decode_mode_spec_buf;
 28322           }
 28323       }
 28324 
 28325       /* Display percentage offsets of top and bottom of the window,
 28326          using "All" (but not "Top" or "Bottom") where appropriate.  */
 28327     case 'q':
 28328       {
 28329         ptrdiff_t toppos = marker_position (w->start);
 28330         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28331         ptrdiff_t begv = BUF_BEGV (b);
 28332         ptrdiff_t zv = BUF_ZV (b);
 28333         int top_perc, bot_perc;
 28334 
 28335         if ((toppos <= begv) && (zv <= botpos))
 28336           return "All   ";
 28337 
 28338         top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
 28339         bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
 28340 
 28341         if (top_perc == bot_perc)
 28342           sprintf (decode_mode_spec_buf, "%d%%", top_perc);
 28343         else
 28344           sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
 28345 
 28346         return decode_mode_spec_buf;
 28347       }
 28348 
 28349     case 's':
 28350       /* status of process */
 28351       obj = Fget_buffer_process (Fcurrent_buffer ());
 28352       if (NILP (obj))
 28353         return "no process";
 28354 #ifndef MSDOS
 28355       obj = Fsymbol_name (Fprocess_status (obj));
 28356 #endif
 28357       break;
 28358 
 28359     case '@':
 28360       {
 28361         specpdl_ref count = inhibit_garbage_collection ();
 28362         Lisp_Object curdir = BVAR (current_buffer, directory);
 28363         Lisp_Object val = Qnil;
 28364 
 28365         if (STRINGP (curdir))
 28366           val = safe_call1 (intern ("file-remote-p"), curdir);
 28367 
 28368         val = unbind_to (count, val);
 28369 
 28370         if (NILP (val))
 28371           return "-";
 28372         else
 28373           return "@";
 28374       }
 28375 
 28376     case 'z':
 28377       /* coding-system (not including end-of-line format) */
 28378     case 'Z':
 28379       /* coding-system (including end-of-line type) */
 28380       {
 28381         bool eol_flag = (c == 'Z');
 28382         char *p = decode_mode_spec_buf;
 28383 
 28384         if (! FRAME_WINDOW_P (f))
 28385           {
 28386             /* No need to mention EOL here--the terminal never needs
 28387                to do EOL conversion.  */
 28388             p = decode_mode_spec_coding (CODING_ID_NAME
 28389                                          (FRAME_KEYBOARD_CODING (f)->id),
 28390                                          p, false);
 28391             p = decode_mode_spec_coding (CODING_ID_NAME
 28392                                          (FRAME_TERMINAL_CODING (f)->id),
 28393                                          p, false);
 28394           }
 28395         p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
 28396                                      p, eol_flag);
 28397 
 28398 #if false /* This proves to be annoying; I think we can do without. -- rms.  */
 28399 #ifdef subprocesses
 28400         obj = Fget_buffer_process (Fcurrent_buffer ());
 28401         if (PROCESSP (obj))
 28402           {
 28403             p = decode_mode_spec_coding
 28404               (XPROCESS (obj)->decode_coding_system, p, eol_flag);
 28405             p = decode_mode_spec_coding
 28406               (XPROCESS (obj)->encode_coding_system, p, eol_flag);
 28407           }
 28408 #endif /* subprocesses */
 28409 #endif /* false */
 28410         *p = 0;
 28411         return decode_mode_spec_buf;
 28412       }
 28413     }
 28414 
 28415   if (STRINGP (obj))
 28416     {
 28417       *string = obj;
 28418       return SSDATA (obj);
 28419     }
 28420   else
 28421     return "";
 28422 }
 28423 
 28424 /* Return the number of lines between start_byte and end_byte in the
 28425    current buffer. */
 28426 
 28427 ptrdiff_t
 28428 count_lines (ptrdiff_t start_byte, ptrdiff_t end_byte)
 28429 {
 28430   ptrdiff_t ignored;
 28431   return display_count_lines (start_byte, end_byte, ZV, &ignored);
 28432 }
 28433 
 28434 /* Count up to COUNT lines starting from START_BYTE.  COUNT negative
 28435    means count lines back from START_BYTE.  But don't go beyond
 28436    LIMIT_BYTE.  Return the number of lines thus found (always
 28437    nonnegative).
 28438 
 28439    Set *BYTE_POS_PTR to the byte position where we stopped.  This is
 28440    either the position COUNT lines after/before START_BYTE, if we
 28441    found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
 28442    COUNT lines.  */
 28443 
 28444 static ptrdiff_t
 28445 display_count_lines (ptrdiff_t start_byte,
 28446                      ptrdiff_t limit_byte, ptrdiff_t count,
 28447                      ptrdiff_t *byte_pos_ptr)
 28448 {
 28449   register unsigned char *cursor;
 28450   unsigned char *base;
 28451 
 28452   register ptrdiff_t ceiling;
 28453   register unsigned char *ceiling_addr;
 28454   ptrdiff_t orig_count = count;
 28455 
 28456   /* If we are not in selective display mode,
 28457      check only for newlines.  */
 28458   bool selective_display
 28459     = (!NILP (BVAR (current_buffer, selective_display))
 28460        && !FIXNUMP (BVAR (current_buffer, selective_display)));
 28461 
 28462   if (count > 0)
 28463     {
 28464       while (start_byte < limit_byte)
 28465         {
 28466           ceiling =  BUFFER_CEILING_OF (start_byte);
 28467           ceiling = min (limit_byte - 1, ceiling);
 28468           ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
 28469           base = (cursor = BYTE_POS_ADDR (start_byte));
 28470 
 28471           do
 28472             {
 28473               if (selective_display)
 28474                 {
 28475                   while (*cursor != '\n' && *cursor != 015
 28476                          && ++cursor != ceiling_addr)
 28477                     continue;
 28478                   if (cursor == ceiling_addr)
 28479                     break;
 28480                 }
 28481               else
 28482                 {
 28483                   cursor = memchr (cursor, '\n', ceiling_addr - cursor);
 28484                   if (! cursor)
 28485                     break;
 28486                 }
 28487 
 28488               cursor++;
 28489 
 28490               if (--count == 0)
 28491                 {
 28492                   start_byte += cursor - base;
 28493                   *byte_pos_ptr = start_byte;
 28494                   return orig_count;
 28495                 }
 28496             }
 28497           while (cursor < ceiling_addr);
 28498 
 28499           start_byte += ceiling_addr - base;
 28500         }
 28501     }
 28502   else
 28503     {
 28504       while (start_byte > limit_byte)
 28505         {
 28506           ceiling = BUFFER_FLOOR_OF (start_byte - 1);
 28507           ceiling = max (limit_byte, ceiling);
 28508           ceiling_addr = BYTE_POS_ADDR (ceiling);
 28509           base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
 28510           while (true)
 28511             {
 28512               if (selective_display)
 28513                 {
 28514                   while (--cursor >= ceiling_addr
 28515                          && *cursor != '\n' && *cursor != 015)
 28516                     continue;
 28517                   if (cursor < ceiling_addr)
 28518                     break;
 28519                 }
 28520               else
 28521                 {
 28522                   cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
 28523                   if (! cursor)
 28524                     break;
 28525                 }
 28526 
 28527               if (++count == 0)
 28528                 {
 28529                   start_byte += cursor - base + 1;
 28530                   *byte_pos_ptr = start_byte;
 28531                   /* When scanning backwards, we should
 28532                      not count the newline posterior to which we stop.  */
 28533                   return - orig_count - 1;
 28534                 }
 28535             }
 28536           start_byte += ceiling_addr - base;
 28537         }
 28538     }
 28539 
 28540   *byte_pos_ptr = limit_byte;
 28541 
 28542   if (count < 0)
 28543     return - orig_count + count;
 28544   return orig_count - count;
 28545 
 28546 }
 28547 
 28548 
 28549 
 28550 /***********************************************************************
 28551                          Displaying strings
 28552  ***********************************************************************/
 28553 
 28554 /* Display a NUL-terminated string, starting with index START.
 28555 
 28556    If STRING is non-null, display that C string.  Otherwise, the Lisp
 28557    string LISP_STRING is displayed.  There's a case that STRING is
 28558    non-null and LISP_STRING is not nil.  It means STRING is a string
 28559    data of LISP_STRING.  In that case, we display LISP_STRING while
 28560    ignoring its text properties.
 28561 
 28562    If FACE_STRING is not nil, FACE_STRING_POS is a position in
 28563    FACE_STRING.  Display STRING or LISP_STRING with the face at
 28564    FACE_STRING_POS in FACE_STRING:
 28565 
 28566    Display the string in the environment given by IT, but use the
 28567    standard display table, temporarily.
 28568 
 28569    FIELD_WIDTH is the minimum number of output glyphs to produce.
 28570    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 28571    with spaces.  If STRING has more characters, more than FIELD_WIDTH
 28572    glyphs will be produced.  FIELD_WIDTH <= 0 means don't pad.
 28573 
 28574    PRECISION is the maximum number of characters to output from
 28575    STRING.  PRECISION < 0  means don't truncate the string.
 28576 
 28577    This is roughly equivalent to printf format specifiers:
 28578 
 28579    FIELD_WIDTH  PRECISION       PRINTF
 28580    ----------------------------------------
 28581    -1           -1              %s
 28582    -1           10              %.10s
 28583    10           -1              %10s
 28584    20           10              %20.10s
 28585 
 28586    MULTIBYTE zero means do not display multibyte chars, > 0 means do
 28587    display them, and < 0 means obey the current buffer's value of
 28588    enable_multibyte_characters.
 28589 
 28590    Value is the number of columns displayed.  */
 28591 
 28592 static int
 28593 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
 28594                 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
 28595                 int field_width, int precision, int max_x, int multibyte)
 28596 {
 28597   int hpos_at_start = it->hpos;
 28598   int saved_face_id = it->face_id;
 28599   struct glyph_row *row = it->glyph_row;
 28600   ptrdiff_t it_charpos;
 28601 
 28602   /* Initialize the iterator IT for iteration over STRING beginning
 28603      with index START.  */
 28604   reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string,
 28605                     start, precision, field_width, multibyte);
 28606 
 28607   if (string && STRINGP (lisp_string))
 28608     /* LISP_STRING is the one returned by decode_mode_spec.  We should
 28609        ignore its text properties.  */
 28610     it->stop_charpos = it->end_charpos;
 28611 
 28612   /* If displaying STRING, set up the face of the iterator from
 28613      FACE_STRING, if that's given.  */
 28614   if (STRINGP (face_string))
 28615     {
 28616       ptrdiff_t endptr;
 28617       struct face *face;
 28618 
 28619       it->face_id
 28620         = face_at_string_position (it->w, face_string, face_string_pos,
 28621                                    0, &endptr, it->base_face_id, false, 0);
 28622       face = FACE_FROM_ID (it->f, it->face_id);
 28623       it->face_box_p = face->box != FACE_NO_BOX;
 28624 
 28625       /* If we have a display spec, but there's no Lisp string being
 28626          displayed, then check whether we've got one from the
 28627          :propertize being passed in and use that.  */
 28628       if (NILP (lisp_string))
 28629         {
 28630           Lisp_Object display = Fget_text_property (make_fixnum (0), Qdisplay,
 28631                                                     face_string);
 28632           if (!NILP (display))
 28633             {
 28634               Lisp_Object min_width = plist_get (display, Qmin_width);
 28635               if (!NILP (min_width))
 28636                 display_min_width (it, 0, face_string, min_width);
 28637             }
 28638         }
 28639     }
 28640 
 28641   /* Set max_x to the maximum allowed X position.  Don't let it go
 28642      beyond the right edge of the window.  */
 28643   if (max_x <= 0)
 28644     max_x = it->last_visible_x;
 28645   else
 28646     max_x = min (max_x, it->last_visible_x);
 28647 
 28648   /* Skip over display elements that are not visible because IT->w is
 28649      hscrolled.  */
 28650   if (it->current_x < it->first_visible_x)
 28651     move_it_in_display_line_to (it, 100000, it->first_visible_x,
 28652                                 MOVE_TO_POS | MOVE_TO_X);
 28653 
 28654   row->ascent = it->max_ascent;
 28655   row->height = it->max_ascent + it->max_descent;
 28656   row->phys_ascent = it->max_phys_ascent;
 28657   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 28658   row->extra_line_spacing = it->max_extra_line_spacing;
 28659 
 28660   if (STRINGP (it->string))
 28661     it_charpos = IT_STRING_CHARPOS (*it);
 28662   else
 28663     it_charpos = IT_CHARPOS (*it);
 28664 
 28665   /* This condition is for the case that we are called with current_x
 28666      past last_visible_x.  */
 28667   while (it->current_x < max_x)
 28668     {
 28669       int x_before, x, n_glyphs_before, i, nglyphs;
 28670 
 28671       /* Get the next display element.  */
 28672       if (!get_next_display_element (it))
 28673         break;
 28674 
 28675       /* Produce glyphs.  */
 28676       x_before = it->current_x;
 28677       n_glyphs_before = row->used[TEXT_AREA];
 28678       PRODUCE_GLYPHS (it);
 28679 
 28680       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 28681       i = 0;
 28682       x = x_before;
 28683       while (i < nglyphs)
 28684         {
 28685           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 28686 
 28687           if (it->line_wrap != TRUNCATE
 28688               && x + glyph->pixel_width > max_x)
 28689             {
 28690               /* End of continued line or max_x reached.  */
 28691               if (CHAR_GLYPH_PADDING_P (*glyph))
 28692                 {
 28693                   /* A wide character is unbreakable.  */
 28694                   if (row->reversed_p)
 28695                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28696                                       - n_glyphs_before);
 28697                   row->used[TEXT_AREA] = n_glyphs_before;
 28698                   it->current_x = x_before;
 28699                 }
 28700               else
 28701                 {
 28702                   if (row->reversed_p)
 28703                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28704                                       - (n_glyphs_before + i));
 28705                   row->used[TEXT_AREA] = n_glyphs_before + i;
 28706                   it->current_x = x;
 28707                 }
 28708               break;
 28709             }
 28710           else if (x + glyph->pixel_width >= it->first_visible_x)
 28711             {
 28712               /* Glyph is at least partially visible.  */
 28713               ++it->hpos;
 28714               if (x < it->first_visible_x)
 28715                 row->x = x - it->first_visible_x;
 28716             }
 28717           else
 28718             {
 28719               /* Glyph is off the left margin of the display area.
 28720                  Should not happen.  */
 28721               emacs_abort ();
 28722             }
 28723 
 28724           row->ascent = max (row->ascent, it->max_ascent);
 28725           row->height = max (row->height, it->max_ascent + it->max_descent);
 28726           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 28727           row->phys_height = max (row->phys_height,
 28728                                   it->max_phys_ascent + it->max_phys_descent);
 28729           row->extra_line_spacing = max (row->extra_line_spacing,
 28730                                          it->max_extra_line_spacing);
 28731           x += glyph->pixel_width;
 28732           ++i;
 28733         }
 28734 
 28735       /* Stop if max_x reached.  */
 28736       if (i < nglyphs)
 28737         break;
 28738 
 28739       /* Stop at line ends.  */
 28740       if (ITERATOR_AT_END_OF_LINE_P (it))
 28741         {
 28742           it->continuation_lines_width = 0;
 28743           break;
 28744         }
 28745 
 28746       set_iterator_to_next (it, true);
 28747       if (STRINGP (it->string))
 28748         it_charpos = IT_STRING_CHARPOS (*it);
 28749       else
 28750         it_charpos = IT_CHARPOS (*it);
 28751 
 28752       /* Stop if truncating at the right edge.  */
 28753       if (it->line_wrap == TRUNCATE
 28754           && it->current_x >= it->last_visible_x)
 28755         {
 28756           /* Add truncation mark, but don't do it if the line is
 28757              truncated at a padding space.  */
 28758           if (it_charpos < it->string_nchars)
 28759             {
 28760               if (!FRAME_WINDOW_P (it->f))
 28761                 {
 28762                   int ii, n;
 28763 
 28764                   if (it->current_x > it->last_visible_x)
 28765                     {
 28766                       if (!row->reversed_p)
 28767                         {
 28768                           for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
 28769                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 28770                               break;
 28771                         }
 28772                       else
 28773                         {
 28774                           for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
 28775                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 28776                               break;
 28777                           unproduce_glyphs (it, ii + 1);
 28778                           ii = row->used[TEXT_AREA] - (ii + 1);
 28779                         }
 28780                       for (n = row->used[TEXT_AREA]; ii < n; ++ii)
 28781                         {
 28782                           row->used[TEXT_AREA] = ii;
 28783                           produce_special_glyphs (it, IT_TRUNCATION);
 28784                         }
 28785                     }
 28786                   produce_special_glyphs (it, IT_TRUNCATION);
 28787                 }
 28788               row->truncated_on_right_p = true;
 28789             }
 28790           break;
 28791         }
 28792     }
 28793 
 28794   /* Maybe insert a truncation at the left.  */
 28795   if (it->first_visible_x
 28796       && it_charpos > 0)
 28797     {
 28798       if (!FRAME_WINDOW_P (it->f)
 28799           || (row->reversed_p
 28800               ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 28801               : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 28802         insert_left_trunc_glyphs (it);
 28803       row->truncated_on_left_p = true;
 28804     }
 28805 
 28806   it->face_id = saved_face_id;
 28807 
 28808   /* Value is number of columns displayed.  */
 28809   return it->hpos - hpos_at_start;
 28810 }
 28811 
 28812 
 28813 
 28814 /* This is like a combination of memq and assq.  Return 1/2 if PROPVAL
 28815    appears as an element of LIST or as the car of an element of LIST.
 28816    If PROPVAL is a list, compare each element against LIST in that
 28817    way, and return 1/2 if any element of PROPVAL is found in LIST.
 28818    Otherwise return 0.  This function cannot quit.
 28819    The return value is 2 if the text is invisible but with an ellipsis
 28820    and 1 if it's invisible and without an ellipsis.  */
 28821 
 28822 int
 28823 invisible_prop (Lisp_Object propval, Lisp_Object list)
 28824 {
 28825   Lisp_Object tail, proptail;
 28826 
 28827   for (tail = list; CONSP (tail); tail = XCDR (tail))
 28828     {
 28829       register Lisp_Object tem;
 28830       tem = XCAR (tail);
 28831       if (EQ (propval, tem))
 28832         return 1;
 28833       if (CONSP (tem) && EQ (propval, XCAR (tem)))
 28834         return NILP (XCDR (tem)) ? 1 : 2;
 28835     }
 28836 
 28837   if (CONSP (propval))
 28838     {
 28839       for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
 28840         {
 28841           Lisp_Object propelt;
 28842           propelt = XCAR (proptail);
 28843           for (tail = list; CONSP (tail); tail = XCDR (tail))
 28844             {
 28845               register Lisp_Object tem;
 28846               tem = XCAR (tail);
 28847               if (EQ (propelt, tem))
 28848                 return 1;
 28849               if (CONSP (tem) && EQ (propelt, XCAR (tem)))
 28850                 return NILP (XCDR (tem)) ? 1 : 2;
 28851             }
 28852         }
 28853     }
 28854 
 28855   return 0;
 28856 }
 28857 
 28858 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
 28859        doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
 28860 POS should be a marker or a buffer position; the value of the `invisible'
 28861 property at that position in the current buffer is examined.
 28862 POS can also be the actual value of the `invisible' text or overlay
 28863 property of the text of interest, in which case the value itself is
 28864 examined.
 28865 
 28866 The non-nil value returned can be t for currently invisible text that is
 28867 entirely hidden on display, or some other non-nil, non-t value if the
 28868 text is replaced by an ellipsis.
 28869 
 28870 Note that whether text with `invisible' property is actually hidden on
 28871 display may depend on `buffer-invisibility-spec', which see.  */)
 28872   (Lisp_Object pos)
 28873 {
 28874   Lisp_Object prop
 28875     = (FIXNATP (pos) || MARKERP (pos)
 28876        ? Fget_char_property (pos, Qinvisible, Qnil)
 28877        : pos);
 28878   int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
 28879   return (invis == 0 ? Qnil
 28880           : invis == 1 ? Qt
 28881           : make_fixnum (invis));
 28882 }
 28883 
 28884 /* Calculate a width or height in pixels from a specification using
 28885    the following elements:
 28886 
 28887    SPEC ::=
 28888      NUM      - a (fractional) multiple of the default font width/height
 28889      (NUM)    - specifies exactly NUM pixels
 28890      UNIT     - a fixed number of pixels, see below.
 28891      ELEMENT  - size of a display element in pixels, see below.
 28892      (NUM . SPEC) - equals NUM * SPEC
 28893      (+ SPEC SPEC ...)  - add pixel values
 28894      (- SPEC SPEC ...)  - subtract pixel values
 28895      (- SPEC)           - negate pixel value
 28896 
 28897    NUM ::=
 28898      INT or FLOAT   - a number constant
 28899      SYMBOL         - use symbol's (buffer local) variable binding.
 28900 
 28901    UNIT ::=
 28902      in       - pixels per inch  *)
 28903      mm       - pixels per 1/1000 meter  *)
 28904      cm       - pixels per 1/100 meter   *)
 28905      width    - width of current font in pixels.
 28906      height   - height of current font in pixels.
 28907 
 28908      *) using the ratio(s) defined in display-pixels-per-inch.
 28909 
 28910    ELEMENT ::=
 28911 
 28912      left-fringe          - left fringe width in pixels
 28913      right-fringe         - right fringe width in pixels
 28914 
 28915      left-margin          - left margin width in pixels
 28916      right-margin         - right margin width in pixels
 28917 
 28918      scroll-bar           - scroll-bar area width in pixels
 28919 
 28920    Examples:
 28921 
 28922    Pixels corresponding to 5 inches:
 28923      (5 . in)
 28924 
 28925    Total width of non-text areas on left side of window (if scroll-bar is on left):
 28926      '(space :width (+ left-fringe left-margin scroll-bar))
 28927 
 28928    Align to first text column (in header line):
 28929      '(space :align-to 0)
 28930 
 28931    Align to middle of text area minus half the width of variable `my-image'
 28932    containing a loaded image:
 28933      '(space :align-to (0.5 . (- text my-image)))
 28934 
 28935    Width of left margin minus width of 1 character in the default font:
 28936      '(space :width (- left-margin 1))
 28937 
 28938    Width of left margin minus width of 2 characters in the current font:
 28939      '(space :width (- left-margin (2 . width)))
 28940 
 28941    Center 1 character over left-margin (in header line):
 28942      '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
 28943 
 28944    Different ways to express width of left fringe plus left margin minus one pixel:
 28945      '(space :width (- (+ left-fringe left-margin) (1)))
 28946      '(space :width (+ left-fringe left-margin (- (1))))
 28947      '(space :width (+ left-fringe left-margin (-1)))
 28948 
 28949    If ALIGN_TO is NULL, returns the result in *RES.  If ALIGN_TO is
 28950    non-NULL, the value of *ALIGN_TO is a window-relative pixel
 28951    coordinate, and *RES is the additional pixel width from that point
 28952    till the end of the stretch glyph.
 28953 
 28954    WIDTH_P non-zero means take the width dimension or X coordinate of
 28955    the object specified by PROP, WIDTH_P zero means take the height
 28956    dimension or the Y coordinate.  (Therefore, if ALIGN_TO is
 28957    non-NULL, WIDTH_P should be non-zero.)
 28958 
 28959    FONT is the font of the face of the surrounding text.
 28960 
 28961    The return value is non-zero if width or height were successfully
 28962    calculated, i.e. if PROP is a valid spec.  */
 28963 
 28964 static bool
 28965 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
 28966                             struct font *font, bool width_p, int *align_to)
 28967 {
 28968   /* Don't adjust for line number if we didn't yet produce it for this
 28969      screen line.  This is for when this function is called from
 28970      move_it_in_display_line_to that was called by display_line to get
 28971      past the glyphs hscrolled off the left side of the window.  */
 28972   int lnum_pixel_width = it->line_number_produced_p ? it->lnum_pixel_width : 0;
 28973   double pixels;
 28974 
 28975 # define OK_PIXELS(val) (*res = (val), true)
 28976 # define OK_ALIGN_TO(val) (*align_to = (val), true)
 28977 
 28978   if (NILP (prop))
 28979     return OK_PIXELS (0);
 28980 
 28981   eassert (FRAME_LIVE_P (it->f));
 28982 
 28983   if (SYMBOLP (prop))
 28984     {
 28985       if (SCHARS (SYMBOL_NAME (prop)) == 2)
 28986         {
 28987           char *unit = SSDATA (SYMBOL_NAME (prop));
 28988 
 28989           /* The UNIT expression, e.g. as part of (NUM . UNIT).  */
 28990           if (unit[0] == 'i' && unit[1] == 'n')
 28991             pixels = 1.0;
 28992           else if (unit[0] == 'm' && unit[1] == 'm')
 28993             pixels = 25.4;
 28994           else if (unit[0] == 'c' && unit[1] == 'm')
 28995             pixels = 2.54;
 28996           else
 28997             pixels = 0;
 28998           if (pixels > 0)
 28999             {
 29000               double ppi = (width_p ? FRAME_RES_X (it->f)
 29001                             : FRAME_RES_Y (it->f));
 29002 
 29003               if (ppi > 0)
 29004                 return OK_PIXELS (ppi / pixels);
 29005               return false;
 29006             }
 29007         }
 29008 
 29009 #ifdef HAVE_WINDOW_SYSTEM
 29010       /* 'height': the height of FONT.  */
 29011       if (EQ (prop, Qheight))
 29012         return OK_PIXELS (font
 29013                           ? normal_char_height (font, -1)
 29014                           : FRAME_LINE_HEIGHT (it->f));
 29015       /* 'width': the width of FONT.  */
 29016       if (EQ (prop, Qwidth))
 29017         return OK_PIXELS (font
 29018                           ? FONT_WIDTH (font)
 29019                           : FRAME_COLUMN_WIDTH (it->f));
 29020 #else
 29021       if (EQ (prop, Qheight) || EQ (prop, Qwidth))
 29022         return OK_PIXELS (1);
 29023 #endif
 29024 
 29025       /* 'text': the width or height of the text area.  */
 29026       if (EQ (prop, Qtext))
 29027           return OK_PIXELS (width_p
 29028                             ? (window_box_width (it->w, TEXT_AREA)
 29029                                - lnum_pixel_width)
 29030                             : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
 29031 
 29032       /* ':align_to'.  First time we compute the value, window
 29033          elements are interpreted as the position of the element's
 29034          left edge.  */
 29035       if (align_to && *align_to < 0)
 29036         {
 29037           *res = 0;
 29038           /* 'left': left edge of the text area.  */
 29039           if (EQ (prop, Qleft))
 29040             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29041                                 + lnum_pixel_width);
 29042           /* 'right': right edge of the text area.  */
 29043           if (EQ (prop, Qright))
 29044             return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
 29045           /* 'center': the center of the text area.  */
 29046           if (EQ (prop, Qcenter))
 29047             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29048                                 + lnum_pixel_width
 29049                                 + window_box_width (it->w, TEXT_AREA) / 2);
 29050           /* 'left-fringe': left edge of the left fringe.  */
 29051           if (EQ (prop, Qleft_fringe))
 29052             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29053                                 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
 29054                                 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
 29055           /* 'right-fringe': left edge of the right fringe.  */
 29056           if (EQ (prop, Qright_fringe))
 29057             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29058                                 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29059                                 : window_box_right_offset (it->w, TEXT_AREA));
 29060           /* 'left-margin': left edge of the left display margin.  */
 29061           if (EQ (prop, Qleft_margin))
 29062             return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
 29063           /* 'right-margin': left edge of the right display margin.  */
 29064           if (EQ (prop, Qright_margin))
 29065             return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
 29066           /* 'scroll-bar': left edge of the vertical scroll bar.  */
 29067           if (EQ (prop, Qscroll_bar))
 29068             return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
 29069                                 ? 0
 29070                                 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29071                                    + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29072                                       ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 29073                                       : 0)));
 29074         }
 29075       else
 29076         {
 29077           /* Otherwise, the elements stand for their width.  */
 29078           if (EQ (prop, Qleft_fringe))
 29079             return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
 29080           if (EQ (prop, Qright_fringe))
 29081             return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
 29082           if (EQ (prop, Qleft_margin))
 29083             return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
 29084           if (EQ (prop, Qright_margin))
 29085             return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
 29086           if (EQ (prop, Qscroll_bar))
 29087             return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
 29088         }
 29089 
 29090       prop = buffer_local_value (prop, it->w->contents);
 29091       if (BASE_EQ (prop, Qunbound))
 29092         prop = Qnil;
 29093     }
 29094 
 29095   if (NUMBERP (prop))
 29096     {
 29097       int base_unit = (width_p
 29098                        ? FRAME_COLUMN_WIDTH (it->f)
 29099                        : FRAME_LINE_HEIGHT (it->f));
 29100       if (width_p && align_to && *align_to < 0)
 29101         return OK_PIXELS (XFLOATINT (prop) * base_unit + lnum_pixel_width);
 29102       return OK_PIXELS (XFLOATINT (prop) * base_unit);
 29103     }
 29104 
 29105   if (CONSP (prop))
 29106     {
 29107       Lisp_Object car = XCAR (prop);
 29108       Lisp_Object cdr = XCDR (prop);
 29109 
 29110       if (SYMBOLP (car))
 29111         {
 29112 #ifdef HAVE_WINDOW_SYSTEM
 29113           /* '(image PROPS...)': width or height of the specified image.  */
 29114           if (FRAME_WINDOW_P (it->f)
 29115               && valid_image_p (prop))
 29116             {
 29117               ptrdiff_t id = lookup_image (it->f, prop, it->face_id);
 29118               struct image *img = IMAGE_FROM_ID (it->f, id);
 29119 
 29120               return OK_PIXELS (width_p ? img->width : img->height);
 29121             }
 29122           /* '(xwidget PROPS...)': dimensions of the specified xwidget.  */
 29123           if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
 29124             {
 29125               /* TODO: Don't return dummy size.  */
 29126               return OK_PIXELS (100);
 29127             }
 29128 #endif
 29129           /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
 29130              recursively calculated values.  */
 29131           if (EQ (car, Qplus) || EQ (car, Qminus))
 29132             {
 29133               bool first = true;
 29134               double px;
 29135 
 29136               pixels = 0;
 29137               while (CONSP (cdr))
 29138                 {
 29139                   if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
 29140                                                    font, width_p, align_to))
 29141                     return false;
 29142                   if (first)
 29143                     pixels = (EQ (car, Qplus) ? px : -px), first = false;
 29144                   else
 29145                     pixels += px;
 29146                   cdr = XCDR (cdr);
 29147                 }
 29148               if (EQ (car, Qminus))
 29149                 pixels = -pixels;
 29150               return OK_PIXELS (pixels);
 29151             }
 29152 
 29153           car = buffer_local_value (car, it->w->contents);
 29154           if (BASE_EQ (car, Qunbound))
 29155             car = Qnil;
 29156         }
 29157 
 29158       /* '(NUM)': absolute number of pixels.  */
 29159       if (NUMBERP (car))
 29160         {
 29161           double fact;
 29162           int offset =
 29163             width_p && align_to && *align_to < 0 ? lnum_pixel_width : 0;
 29164           pixels = XFLOATINT (car);
 29165           if (NILP (cdr))
 29166             return OK_PIXELS (pixels + offset);
 29167           if (calc_pixel_width_or_height (&fact, it, cdr,
 29168                                           font, width_p, align_to))
 29169             return OK_PIXELS (pixels * fact + offset);
 29170           return false;
 29171         }
 29172 
 29173       return false;
 29174     }
 29175 
 29176   return false;
 29177 }
 29178 
 29179 void
 29180 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
 29181 {
 29182 #ifdef HAVE_WINDOW_SYSTEM
 29183   normal_char_ascent_descent (font, -1, ascent, descent);
 29184 #else
 29185   *ascent = 1;
 29186   *descent = 0;
 29187 #endif
 29188 }
 29189 
 29190 
 29191 /***********************************************************************
 29192                              Glyph Display
 29193  ***********************************************************************/
 29194 
 29195 #ifdef HAVE_WINDOW_SYSTEM
 29196 
 29197 #ifdef GLYPH_DEBUG
 29198 
 29199 extern void dump_glyph_string (struct glyph_string *) EXTERNALLY_VISIBLE;
 29200 void
 29201 dump_glyph_string (struct glyph_string *s)
 29202 {
 29203   fputs ("glyph string\n", stderr);
 29204   fprintf (stderr, "  x, y, w, h = %d, %d, %d, %d\n",
 29205            s->x, s->y, s->width, s->height);
 29206   fprintf (stderr, "  ybase = %d\n", s->ybase);
 29207   fprintf (stderr, "  hl = %u\n", s->hl);
 29208   fprintf (stderr, "  left overhang = %d, right = %d\n",
 29209            s->left_overhang, s->right_overhang);
 29210   fprintf (stderr, "  nchars = %d\n", s->nchars);
 29211   fprintf (stderr, "  extends to end of line = %d\n",
 29212            s->extends_to_end_of_line_p);
 29213   fprintf (stderr, "  font height = %d\n", FONT_HEIGHT (s->font));
 29214   fprintf (stderr, "  bg width = %d\n", s->background_width);
 29215 }
 29216 
 29217 #endif /* GLYPH_DEBUG */
 29218 
 29219 /* Initialize glyph string S.  CHAR2B is a suitably allocated vector
 29220    of 2-byte unsigned integers for S; it can't be allocated in
 29221    init_glyph_string because it must be allocated via `alloca'.  W
 29222    is the window on which S is drawn.  ROW and AREA are the glyph row
 29223    and area within the row from which S is constructed.  START is the
 29224    index of the first glyph structure covered by S.  HL is a
 29225    face-override for drawing S.  */
 29226 
 29227 #ifdef HAVE_NTGUI
 29228 /* We set inhibit-quit here due to paranoia: get_frame_dc acquires the
 29229    critical section, and we cannot QUIT while we hold the critical
 29230    section.  If any of the code run by callers of ALLOCATE_HDC happens
 29231    to call Lisp (might be possible due to all the hooks lying around),
 29232    we must prevent it from quitting.  */
 29233 # define ALLOCATE_HDC(hdc, f)                   \
 29234   Lisp_Object prev_quit = Vinhibit_quit;        \
 29235   Vinhibit_quit = Qt;                           \
 29236   HDC hdc = get_frame_dc ((f))
 29237 # define RELEASE_HDC(hdc, f)                    \
 29238   release_frame_dc ((f), (hdc));                \
 29239   Vinhibit_quit = prev_quit
 29240 #else
 29241 # define ALLOCATE_HDC(hdc, f)
 29242 # define RELEASE_HDC(hdc, f)
 29243 #endif
 29244 
 29245 static void
 29246 init_glyph_string (struct glyph_string *s,
 29247 #ifdef HAVE_NTGUI
 29248                    HDC hdc,
 29249 #endif
 29250                    unsigned *char2b, struct window *w, struct glyph_row *row,
 29251                    enum glyph_row_area area, int start, enum draw_glyphs_face hl)
 29252 {
 29253   memset (s, 0, sizeof *s);
 29254   s->w = w;
 29255   s->f = XFRAME (w->frame);
 29256 #ifdef HAVE_NTGUI
 29257   s->hdc = hdc;
 29258 #endif
 29259   s->char2b = char2b;
 29260   s->hl = hl;
 29261   s->row = row;
 29262   s->area = area;
 29263   s->first_glyph = row->glyphs[area] + start;
 29264   s->height = row->height;
 29265   s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
 29266   s->ybase = s->y + row->ascent;
 29267 }
 29268 
 29269 
 29270 /* Append the list of glyph strings with head H and tail T to the list
 29271    with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the result.  */
 29272 
 29273 static void
 29274 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29275                            struct glyph_string *h, struct glyph_string *t)
 29276 {
 29277   if (h)
 29278     {
 29279       if (*head)
 29280         (*tail)->next = h;
 29281       else
 29282         *head = h;
 29283       h->prev = *tail;
 29284       *tail = t;
 29285     }
 29286 }
 29287 
 29288 
 29289 /* Prepend the list of glyph strings with head H and tail T to the
 29290    list with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the
 29291    result.  */
 29292 
 29293 static void
 29294 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29295                             struct glyph_string *h, struct glyph_string *t)
 29296 {
 29297   if (h)
 29298     {
 29299       if (*head)
 29300         (*head)->prev = t;
 29301       else
 29302         *tail = t;
 29303       t->next = *head;
 29304       *head = h;
 29305     }
 29306 }
 29307 
 29308 
 29309 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
 29310    Set *HEAD and *TAIL to the resulting list.  */
 29311 
 29312 static void
 29313 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
 29314                      struct glyph_string *s)
 29315 {
 29316   s->next = s->prev = NULL;
 29317   append_glyph_string_lists (head, tail, s, s);
 29318 }
 29319 
 29320 
 29321 /* Get face and two-byte form of character C in face FACE_ID on frame F.
 29322    The encoding of C is returned in *CHAR2B.  DISPLAY_P means
 29323    make sure that X resources for the face returned are allocated.
 29324    Value is a pointer to a realized face that is ready for display if
 29325    DISPLAY_P.  */
 29326 
 29327 static struct face *
 29328 get_char_face_and_encoding (struct frame *f, int c, int face_id,
 29329                             unsigned *char2b, bool display_p)
 29330 {
 29331   struct face *face = FACE_FROM_ID (f, face_id);
 29332   unsigned code = 0;
 29333 
 29334   if (face->font)
 29335     {
 29336       code = face->font->driver->encode_char (face->font, c);
 29337 
 29338       if (code == FONT_INVALID_CODE)
 29339         code = 0;
 29340     }
 29341   /* Ensure that the code is only 2 bytes wide.  */
 29342   *char2b = code & 0xFFFF;
 29343 
 29344   /* Make sure X resources of the face are allocated.  */
 29345 #ifdef HAVE_X_WINDOWS
 29346   if (display_p)
 29347 #endif
 29348     {
 29349       eassert (face != NULL);
 29350       prepare_face_for_display (f, face);
 29351     }
 29352 
 29353   return face;
 29354 }
 29355 
 29356 
 29357 /* Get face and two-byte form of character glyph GLYPH on frame F.
 29358    The encoding of GLYPH->u.ch is returned in *CHAR2B.  Value is
 29359    a pointer to a realized face that is ready for display.  */
 29360 
 29361 static struct face *
 29362 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
 29363                              unsigned *char2b)
 29364 {
 29365   struct face *face;
 29366   unsigned code = 0;
 29367 
 29368   eassert (glyph->type == CHAR_GLYPH);
 29369   face = FACE_FROM_ID (f, glyph->face_id);
 29370 
 29371   /* Make sure X resources of the face are allocated.  */
 29372   prepare_face_for_display (f, face);
 29373 
 29374   if (face->font)
 29375     {
 29376       if (CHAR_BYTE8_P (glyph->u.ch))
 29377         code = CHAR_TO_BYTE8 (glyph->u.ch);
 29378       else
 29379         code = face->font->driver->encode_char (face->font, glyph->u.ch);
 29380 
 29381       if (code == FONT_INVALID_CODE)
 29382         code = 0;
 29383     }
 29384 
 29385   /* Ensure that the code is only 2 bytes wide.  */
 29386   *char2b = code & 0xFFFF;
 29387   return face;
 29388 }
 29389 
 29390 
 29391 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
 29392    Return true iff FONT has a glyph for C.  */
 29393 
 29394 static bool
 29395 get_char_glyph_code (int c, struct font *font, unsigned *char2b)
 29396 {
 29397   unsigned code;
 29398 
 29399   if (CHAR_BYTE8_P (c))
 29400     code = CHAR_TO_BYTE8 (c);
 29401   else
 29402     code = font->driver->encode_char (font, c);
 29403 
 29404   if (code == FONT_INVALID_CODE)
 29405     return false;
 29406 
 29407   /* Ensure that the code is only 2 bytes wide.  */
 29408   *char2b = code & 0xFFFF;
 29409   return true;
 29410 }
 29411 
 29412 
 29413 /* Fill glyph string S with composition components specified by S->cmp.
 29414 
 29415    BASE_FACE is the base face of the composition.
 29416    S->cmp_from is the index of the first component for S.
 29417 
 29418    OVERLAPS non-zero means S should draw the foreground only, and use
 29419    its physical height for clipping.  See also draw_glyphs.
 29420 
 29421    Value is the index of a component not in S.  */
 29422 
 29423 static int
 29424 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
 29425                              int overlaps)
 29426 {
 29427   int i;
 29428   /* For all glyphs of this composition, starting at the offset
 29429      S->cmp_from, until we reach the end of the definition or encounter a
 29430      glyph that requires the different face, add it to S.  */
 29431   struct face *face;
 29432 
 29433   eassert (s);
 29434 
 29435   s->for_overlaps = overlaps;
 29436   s->face = NULL;
 29437   s->font = NULL;
 29438   for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
 29439     {
 29440       int c = COMPOSITION_GLYPH (s->cmp, i);
 29441 
 29442       /* TAB in a composition means display glyphs with padding space
 29443          on the left or right.  */
 29444       if (c != '\t')
 29445         {
 29446           int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
 29447                                        -1, Qnil);
 29448 
 29449           face = get_char_face_and_encoding (s->f, c, face_id,
 29450                                              s->char2b + i, true);
 29451           if (face)
 29452             {
 29453               if (! s->face)
 29454                 {
 29455                   s->face = face;
 29456                   s->font = s->face->font;
 29457                 }
 29458               else if (s->face != face)
 29459                 break;
 29460             }
 29461         }
 29462       ++s->nchars;
 29463     }
 29464   s->cmp_to = i;
 29465 
 29466   if (s->face == NULL)
 29467     {
 29468       s->face = base_face->ascii_face;
 29469       s->font = s->face->font;
 29470     }
 29471 
 29472   if (s->hl == DRAW_MOUSE_FACE
 29473       || (s->hl == DRAW_CURSOR
 29474           && MATRIX_ROW (s->w->current_matrix,
 29475                          s->w->phys_cursor.vpos)->mouse_face_p
 29476           && cursor_in_mouse_face_p (s->w)))
 29477     {
 29478       int c = COMPOSITION_GLYPH (s->cmp, 0);
 29479       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29480       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29481       if (!s->face)
 29482         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29483 
 29484       s->face = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face, c, -1, Qnil));
 29485       prepare_face_for_display (s->f, s->face);
 29486     }
 29487 
 29488   /* All glyph strings for the same composition has the same width,
 29489      i.e. the width set for the first component of the composition.  */
 29490   s->width = s->first_glyph->pixel_width;
 29491 
 29492   /* If the specified font could not be loaded, use the frame's
 29493      default font, but record the fact that we couldn't load it in
 29494      the glyph string so that we can draw rectangles for the
 29495      characters of the glyph string.  */
 29496   if (s->font == NULL)
 29497     {
 29498       s->font_not_found_p = true;
 29499       s->font = FRAME_FONT (s->f);
 29500     }
 29501 
 29502   /* Adjust base line for subscript/superscript text.  */
 29503   s->ybase += s->first_glyph->voffset;
 29504 
 29505   return s->cmp_to;
 29506 }
 29507 
 29508 static int
 29509 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
 29510                            int start, int end, int overlaps)
 29511 {
 29512   struct glyph *glyph, *last;
 29513   int voffset;
 29514   Lisp_Object lgstring;
 29515   int i;
 29516   bool glyph_not_available_p;
 29517 
 29518   s->for_overlaps = overlaps;
 29519   glyph = s->row->glyphs[s->area] + start;
 29520   last = s->row->glyphs[s->area] + end;
 29521   voffset = glyph->voffset;
 29522   glyph_not_available_p = glyph->glyph_not_available_p;
 29523   s->cmp_id = glyph->u.cmp.id;
 29524   s->cmp_from = glyph->slice.cmp.from;
 29525   s->cmp_to = glyph->slice.cmp.to + 1;
 29526   if (s->hl == DRAW_MOUSE_FACE
 29527       || (s->hl == DRAW_CURSOR
 29528           && MATRIX_ROW (s->w->current_matrix,
 29529                          s->w->phys_cursor.vpos)->mouse_face_p
 29530           && cursor_in_mouse_face_p (s->w)))
 29531     {
 29532       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29533       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29534       if (!s->face)
 29535         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29536       prepare_face_for_display (s->f, s->face);
 29537     }
 29538   else
 29539     s->face = FACE_FROM_ID (s->f, face_id);
 29540   lgstring = composition_gstring_from_id (s->cmp_id);
 29541   s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
 29542   /* The width of a composition glyph string is the sum of the
 29543      composition's glyph widths.  */
 29544   s->width = s->first_glyph->pixel_width;
 29545   glyph++;
 29546   while (glyph < last
 29547          && glyph->u.cmp.automatic
 29548          && glyph->u.cmp.id == s->cmp_id
 29549          && glyph->face_id == face_id
 29550          && s->cmp_to == glyph->slice.cmp.from
 29551          && glyph->glyph_not_available_p == glyph_not_available_p)
 29552     {
 29553       s->width += glyph->pixel_width;
 29554       s->cmp_to = (glyph++)->slice.cmp.to + 1;
 29555     }
 29556 
 29557   for (i = s->cmp_from; i < s->cmp_to; i++)
 29558     {
 29559       Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
 29560       unsigned code = LGLYPH_CODE (lglyph);
 29561 
 29562       /* Ensure that the code is only 2 bytes wide.  */
 29563       s->char2b[i] = code & 0xFFFF;
 29564     }
 29565 
 29566   /* If the specified font could not be loaded, record that fact in
 29567      S->font_not_found_p so that we can draw rectangles for the
 29568      characters of the glyph string.  */
 29569   if (glyph_not_available_p)
 29570     s->font_not_found_p = true;
 29571 
 29572   /* Adjust base line for subscript/superscript text.  */
 29573   s->ybase += voffset;
 29574 
 29575   return glyph - s->row->glyphs[s->area];
 29576 }
 29577 
 29578 
 29579 /* Fill glyph string S from a sequence glyphs for glyphless characters.
 29580    See the comment of fill_glyph_string for arguments.
 29581    Value is the index of the first glyph not in S.  */
 29582 
 29583 
 29584 static int
 29585 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
 29586                              int start, int end, int overlaps)
 29587 {
 29588   struct glyph *glyph, *last;
 29589   int voffset;
 29590 
 29591   eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
 29592   s->for_overlaps = overlaps;
 29593   glyph = s->row->glyphs[s->area] + start;
 29594   last = s->row->glyphs[s->area] + end;
 29595   voffset = glyph->voffset;
 29596   s->face = FACE_FROM_ID (s->f, face_id);
 29597   s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
 29598   if (s->hl == DRAW_MOUSE_FACE
 29599       || (s->hl == DRAW_CURSOR
 29600           && MATRIX_ROW (s->w->current_matrix,
 29601                          s->w->phys_cursor.vpos)->mouse_face_p
 29602           && cursor_in_mouse_face_p (s->w)))
 29603     {
 29604       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29605       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29606       if (!s->face)
 29607         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29608       prepare_face_for_display (s->f, s->face);
 29609     }
 29610   s->nchars = 1;
 29611   s->width = glyph->pixel_width;
 29612   glyph++;
 29613   while (glyph < last
 29614          && glyph->type == GLYPHLESS_GLYPH
 29615          && glyph->voffset == voffset
 29616          && glyph->face_id == face_id)
 29617     {
 29618       s->nchars++;
 29619       s->width += glyph->pixel_width;
 29620       glyph++;
 29621     }
 29622   s->ybase += voffset;
 29623   return glyph - s->row->glyphs[s->area];
 29624 }
 29625 
 29626 
 29627 /* Fill glyph string S from a sequence of character glyphs.
 29628 
 29629    FACE_ID is the face id of the string.  START is the index of the
 29630    first glyph to consider, END is the index of the last + 1.
 29631    OVERLAPS non-zero means S should draw the foreground only, and use
 29632    its physical height for clipping.  See also draw_glyphs.
 29633 
 29634    Value is the index of the first glyph not in S.  */
 29635 
 29636 static int
 29637 fill_glyph_string (struct glyph_string *s, int face_id,
 29638                    int start, int end, int overlaps)
 29639 {
 29640   struct glyph *glyph, *last;
 29641   int voffset;
 29642   bool glyph_not_available_p;
 29643 
 29644   eassert (s->f == XFRAME (s->w->frame));
 29645   eassert (s->nchars == 0);
 29646   eassert (start >= 0 && end > start);
 29647 
 29648   s->for_overlaps = overlaps;
 29649   glyph = s->row->glyphs[s->area] + start;
 29650   last = s->row->glyphs[s->area] + end;
 29651   voffset = glyph->voffset;
 29652   s->padding_p = glyph->padding_p;
 29653   glyph_not_available_p = glyph->glyph_not_available_p;
 29654 
 29655   while (glyph < last
 29656          && glyph->type == CHAR_GLYPH
 29657          && glyph->voffset == voffset
 29658          /* Same face id implies same font, nowadays.  */
 29659          && glyph->face_id == face_id
 29660          && glyph->glyph_not_available_p == glyph_not_available_p)
 29661     {
 29662       s->face = get_glyph_face_and_encoding (s->f, glyph,
 29663                                              s->char2b + s->nchars);
 29664       ++s->nchars;
 29665       eassert (s->nchars <= end - start);
 29666       s->width += glyph->pixel_width;
 29667       if (glyph++->padding_p != s->padding_p)
 29668         break;
 29669     }
 29670 
 29671   s->font = s->face->font;
 29672 
 29673   if (s->hl == DRAW_MOUSE_FACE
 29674       || (s->hl == DRAW_CURSOR
 29675           && MATRIX_ROW (s->w->current_matrix,
 29676                          s->w->phys_cursor.vpos)->mouse_face_p
 29677           && cursor_in_mouse_face_p (s->w)))
 29678     {
 29679       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29680       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29681       if (!s->face)
 29682         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29683       s->face
 29684         = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face,
 29685                                              s->first_glyph->u.ch, -1, Qnil));
 29686       prepare_face_for_display (s->f, s->face);
 29687     }
 29688 
 29689   /* If the specified font could not be loaded, use the frame's font,
 29690      but record the fact that we couldn't load it in
 29691      S->font_not_found_p so that we can draw rectangles for the
 29692      characters of the glyph string.  */
 29693   if (s->font == NULL || glyph_not_available_p)
 29694     {
 29695       s->font_not_found_p = true;
 29696       s->font = FRAME_FONT (s->f);
 29697     }
 29698 
 29699   /* Adjust base line for subscript/superscript text.  */
 29700   s->ybase += voffset;
 29701 
 29702   eassert (s->face && s->face->gc);
 29703   return glyph - s->row->glyphs[s->area];
 29704 }
 29705 
 29706 
 29707 /* Fill glyph string S from image glyph S->first_glyph.  */
 29708 
 29709 static void
 29710 fill_image_glyph_string (struct glyph_string *s)
 29711 {
 29712   eassert (s->first_glyph->type == IMAGE_GLYPH);
 29713   s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
 29714   eassert (s->img);
 29715   s->slice = s->first_glyph->slice.img;
 29716   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 29717   s->font = s->face->font;
 29718   if (s->hl == DRAW_MOUSE_FACE
 29719       || (s->hl == DRAW_CURSOR
 29720           && MATRIX_ROW (s->w->current_matrix,
 29721                          s->w->phys_cursor.vpos)->mouse_face_p
 29722           && cursor_in_mouse_face_p (s->w)))
 29723     {
 29724       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29725       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29726       if (!s->face)
 29727         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29728       prepare_face_for_display (s->f, s->face);
 29729     }
 29730   s->width = s->first_glyph->pixel_width;
 29731 
 29732   /* Adjust base line for subscript/superscript text.  */
 29733   s->ybase += s->first_glyph->voffset;
 29734 }
 29735 
 29736 
 29737 #ifdef HAVE_XWIDGETS
 29738 static void
 29739 fill_xwidget_glyph_string (struct glyph_string *s)
 29740 {
 29741   eassert (s->first_glyph->type == XWIDGET_GLYPH);
 29742   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 29743   s->font = s->face->font;
 29744   if (s->hl == DRAW_MOUSE_FACE
 29745       || (s->hl == DRAW_CURSOR
 29746           && MATRIX_ROW (s->w->current_matrix,
 29747                          s->w->phys_cursor.vpos)->mouse_face_p
 29748           && cursor_in_mouse_face_p (s->w)))
 29749     {
 29750       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29751       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29752       if (!s->face)
 29753         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29754       prepare_face_for_display (s->f, s->face);
 29755     }
 29756   s->width = s->first_glyph->pixel_width;
 29757   s->ybase += s->first_glyph->voffset;
 29758   s->xwidget = xwidget_from_id (s->first_glyph->u.xwidget);
 29759 }
 29760 #endif
 29761 /* Fill glyph string S from a sequence of stretch glyphs.
 29762 
 29763    START is the index of the first glyph to consider,
 29764    END is the index of the last + 1.
 29765 
 29766    Value is the index of the first glyph not in S.  */
 29767 
 29768 static int
 29769 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
 29770 {
 29771   struct glyph *glyph, *last;
 29772   int voffset, face_id;
 29773 
 29774   eassert (s->first_glyph->type == STRETCH_GLYPH);
 29775 
 29776   glyph = s->row->glyphs[s->area] + start;
 29777   last = s->row->glyphs[s->area] + end;
 29778   face_id = glyph->face_id;
 29779   s->face = FACE_FROM_ID (s->f, face_id);
 29780   s->font = s->face->font;
 29781   if (s->hl == DRAW_MOUSE_FACE
 29782       || (s->hl == DRAW_CURSOR
 29783           && MATRIX_ROW (s->w->current_matrix,
 29784                          s->w->phys_cursor.vpos)->mouse_face_p
 29785           && cursor_in_mouse_face_p (s->w)))
 29786     {
 29787       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29788       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29789       if (!s->face)
 29790         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29791       prepare_face_for_display (s->f, s->face);
 29792     }
 29793   s->width = glyph->pixel_width;
 29794   s->nchars = 1;
 29795   voffset = glyph->voffset;
 29796 
 29797   for (++glyph;
 29798        (glyph < last
 29799         && glyph->type == STRETCH_GLYPH
 29800         && glyph->voffset == voffset
 29801         && glyph->face_id == face_id);
 29802        ++glyph)
 29803     s->width += glyph->pixel_width;
 29804 
 29805   /* Adjust base line for subscript/superscript text.  */
 29806   s->ybase += voffset;
 29807 
 29808   /* The case that face->gc == 0 is handled when drawing the glyph
 29809      string by calling prepare_face_for_display.  */
 29810   eassert (s->face);
 29811   return glyph - s->row->glyphs[s->area];
 29812 }
 29813 
 29814 static struct font_metrics *
 29815 get_per_char_metric (struct font *font, const unsigned *char2b)
 29816 {
 29817   static struct font_metrics metrics;
 29818 
 29819   if (! font)
 29820     return NULL;
 29821   if (*char2b == FONT_INVALID_CODE)
 29822     return NULL;
 29823 
 29824   font->driver->text_extents (font, char2b, 1, &metrics);
 29825   return &metrics;
 29826 }
 29827 
 29828 /* A subroutine that computes "normal" values of ASCENT and DESCENT
 29829    for FONT.  Values are taken from font-global ones, except for fonts
 29830    that claim preposterously large values, but whose glyphs actually
 29831    have reasonable dimensions.  C is the character to use for metrics
 29832    if the font-global values are too large; if C is negative, the
 29833    function selects a default character.  */
 29834 static void
 29835 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
 29836 {
 29837   *ascent = FONT_BASE (font);
 29838   *descent = FONT_DESCENT (font);
 29839 
 29840   if (FONT_TOO_HIGH (font))
 29841     {
 29842       unsigned char2b;
 29843 
 29844       /* Get metrics of C, defaulting to a reasonably sized ASCII
 29845          character.  */
 29846       if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
 29847         {
 29848           struct font_metrics *pcm = get_per_char_metric (font, &char2b);
 29849           eassume (pcm);
 29850 
 29851           if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
 29852             {
 29853               /* We add 1 pixel to character dimensions as heuristics
 29854                  that produces nicer display, e.g. when the face has
 29855                  the box attribute.  */
 29856               *ascent = pcm->ascent + 1;
 29857               *descent = pcm->descent + 1;
 29858             }
 29859         }
 29860     }
 29861 }
 29862 
 29863 /* A subroutine that computes a reasonable "normal character height"
 29864    for fonts that claim preposterously large vertical dimensions, but
 29865    whose glyphs are actually reasonably sized.  C is the character
 29866    whose metrics to use for those fonts, or -1 for default
 29867    character.  */
 29868 static int
 29869 normal_char_height (struct font *font, int c)
 29870 {
 29871   int ascent, descent;
 29872 
 29873   normal_char_ascent_descent (font, c, &ascent, &descent);
 29874 
 29875   return ascent + descent;
 29876 }
 29877 
 29878 /* EXPORT for RIF:
 29879    Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
 29880    frame F.  Overhangs of glyphs other than type CHAR_GLYPH are
 29881    assumed to be zero.  */
 29882 
 29883 void
 29884 gui_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
 29885 {
 29886   *left = *right = 0;
 29887 
 29888   if (glyph->type == CHAR_GLYPH)
 29889     {
 29890       unsigned char2b;
 29891       struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
 29892       if (face->font)
 29893         {
 29894           struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
 29895           if (pcm)
 29896             {
 29897               if (pcm->rbearing > pcm->width)
 29898                 *right = pcm->rbearing - pcm->width;
 29899               if (pcm->lbearing < 0)
 29900                 *left = -pcm->lbearing;
 29901             }
 29902         }
 29903     }
 29904   else if (glyph->type == COMPOSITE_GLYPH)
 29905     {
 29906       if (! glyph->u.cmp.automatic)
 29907         {
 29908           struct composition *cmp = composition_table[glyph->u.cmp.id];
 29909 
 29910           if (cmp->rbearing > cmp->pixel_width)
 29911             *right = cmp->rbearing - cmp->pixel_width;
 29912           if (cmp->lbearing < 0)
 29913             *left = - cmp->lbearing;
 29914         }
 29915       else
 29916         {
 29917           Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
 29918           struct font_metrics metrics;
 29919 
 29920           composition_gstring_width (gstring, glyph->slice.cmp.from,
 29921                                      glyph->slice.cmp.to + 1, &metrics);
 29922           if (metrics.rbearing > metrics.width)
 29923             *right = metrics.rbearing - metrics.width;
 29924           if (metrics.lbearing < 0)
 29925             *left = - metrics.lbearing;
 29926         }
 29927     }
 29928 }
 29929 
 29930 
 29931 /* Return the index of the first glyph preceding glyph string S that
 29932    is overwritten by S because of S's left overhang.  Value is -1
 29933    if no glyphs are overwritten.  */
 29934 
 29935 static int
 29936 left_overwritten (struct glyph_string *s)
 29937 {
 29938   int k;
 29939 
 29940   if (s->left_overhang)
 29941     {
 29942       int x = 0, i;
 29943       struct glyph *glyphs = s->row->glyphs[s->area];
 29944       int first = s->first_glyph - glyphs;
 29945 
 29946       for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
 29947         x -= glyphs[i].pixel_width;
 29948 
 29949       k = i + 1;
 29950     }
 29951   else
 29952     k = -1;
 29953 
 29954   return k;
 29955 }
 29956 
 29957 
 29958 /* Return the index of the first glyph preceding glyph string S that
 29959    is overwriting S because of its right overhang.  Value is -1 if no
 29960    glyph in front of S overwrites S.  */
 29961 
 29962 static int
 29963 left_overwriting (struct glyph_string *s)
 29964 {
 29965   int i, k, x;
 29966   struct glyph *glyphs = s->row->glyphs[s->area];
 29967   int first = s->first_glyph - glyphs;
 29968 
 29969   k = -1;
 29970   x = 0;
 29971   for (i = first - 1; i >= 0; --i)
 29972     {
 29973       int left, right;
 29974       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 29975       if (x + right > 0)
 29976         k = i;
 29977       x -= glyphs[i].pixel_width;
 29978     }
 29979 
 29980   return k;
 29981 }
 29982 
 29983 
 29984 /* Return the index of the last glyph following glyph string S that is
 29985    overwritten by S because of S's right overhang.  Value is -1 if
 29986    no such glyph is found.  */
 29987 
 29988 static int
 29989 right_overwritten (struct glyph_string *s)
 29990 {
 29991   int k = -1;
 29992 
 29993   if (s->right_overhang)
 29994     {
 29995       int x = 0, i;
 29996       struct glyph *glyphs = s->row->glyphs[s->area];
 29997       int first = (s->first_glyph - glyphs
 29998                    + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 29999       int end = s->row->used[s->area];
 30000 
 30001       for (i = first; i < end && s->right_overhang > x; ++i)
 30002         x += glyphs[i].pixel_width;
 30003 
 30004       k = i;
 30005     }
 30006 
 30007   return k;
 30008 }
 30009 
 30010 
 30011 /* Return the index of the last glyph following glyph string S that
 30012    overwrites S because of its left overhang.  Value is negative
 30013    if no such glyph is found.  */
 30014 
 30015 static int
 30016 right_overwriting (struct glyph_string *s)
 30017 {
 30018   int i, k, x;
 30019   int end = s->row->used[s->area];
 30020   struct glyph *glyphs = s->row->glyphs[s->area];
 30021   int first = (s->first_glyph - glyphs
 30022                + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 30023 
 30024   k = -1;
 30025   x = 0;
 30026   for (i = first; i < end; ++i)
 30027     {
 30028       int left, right;
 30029       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 30030       if (x - left < 0)
 30031         k = i;
 30032       x += glyphs[i].pixel_width;
 30033     }
 30034 
 30035   return k;
 30036 }
 30037 
 30038 
 30039 /* Set background width of glyph string S.  START is the index of the
 30040    first glyph following S.  LAST_X is the right-most x-position + 1
 30041    in the drawing area.
 30042 
 30043    If S->hl is DRAW_CURSOR, S->f is a window system frame, and the
 30044    cursor in S's window is currently inside mouse face, also update
 30045    S->width to take into account potentially differing :box
 30046    properties between the original face and the mouse face.  */
 30047 
 30048 static void
 30049 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
 30050 {
 30051   /* If the face of this glyph string has to be drawn to the end of
 30052      the drawing area, set S->extends_to_end_of_line_p.  */
 30053 
 30054   if (start == s->row->used[s->area]
 30055       && ((s->row->fill_line_p
 30056            && (s->hl == DRAW_NORMAL_TEXT
 30057                || s->hl == DRAW_IMAGE_RAISED
 30058                || s->hl == DRAW_IMAGE_SUNKEN))
 30059           || s->hl == DRAW_MOUSE_FACE))
 30060     s->extends_to_end_of_line_p = true;
 30061 
 30062   /* If S extends its face to the end of the line, set its
 30063      background_width to the distance to the right edge of the drawing
 30064      area.  */
 30065   if (s->extends_to_end_of_line_p)
 30066     s->background_width = last_x - s->x + 1;
 30067   else
 30068     {
 30069       s->background_width = s->width;
 30070 #ifdef HAVE_WINDOW_SYSTEM
 30071       if (FRAME_WINDOW_P (s->f)
 30072           && s->hl == DRAW_CURSOR
 30073           && MATRIX_ROW (s->w->current_matrix,
 30074                          s->w->phys_cursor.vpos)->mouse_face_p
 30075           && cursor_in_mouse_face_p (s->w))
 30076         {
 30077           /* Adjust the background width of the glyph string, because
 30078              if the glyph's face has the :box attribute, its
 30079              pixel_width might be different when it's displayed in the
 30080              mouse-face, if that also has the :box attribute.  */
 30081           struct glyph *g = s->first_glyph;
 30082           struct face *regular_face = FACE_FROM_ID (s->f, g->face_id);
 30083           s->background_width +=
 30084             adjust_glyph_width_for_mouse_face (g, s->row, s->w,
 30085                                                regular_face, s->face);
 30086           /* S->width is probably worth adjusting here as well.  */
 30087           s->width = s->background_width;
 30088         }
 30089 #endif
 30090     }
 30091 }
 30092 
 30093 
 30094 /* Return glyph string that shares background with glyph string S and
 30095    whose `background_width' member has been set.  */
 30096 
 30097 static struct glyph_string *
 30098 glyph_string_containing_background_width (struct glyph_string *s)
 30099 {
 30100   if (s->cmp)
 30101     while (s->cmp_from)
 30102       s = s->prev;
 30103 
 30104   return s;
 30105 }
 30106 
 30107 
 30108 /* Compute overhangs and x-positions for glyph string S and its
 30109    predecessors, or successors.  X is the starting x-position for S.
 30110    BACKWARD_P means process predecessors.  */
 30111 
 30112 static void
 30113 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
 30114 {
 30115   if (backward_p)
 30116     {
 30117       while (s)
 30118         {
 30119           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30120             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30121           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30122             x -= s->width;
 30123           s->x = x;
 30124           s = s->prev;
 30125         }
 30126     }
 30127   else
 30128     {
 30129       while (s)
 30130         {
 30131           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30132             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30133           s->x = x;
 30134           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30135             x += s->width;
 30136           s = s->next;
 30137         }
 30138     }
 30139 }
 30140 
 30141 
 30142 
 30143 /* The following macros are only called from draw_glyphs below.
 30144    They reference the following parameters of that function directly:
 30145      `w', `row', `area', and `overlap_p'
 30146    as well as the following local variables:
 30147      `s', `f', and `hdc' (in W32)  */
 30148 
 30149 #ifdef HAVE_NTGUI
 30150 /* On W32, silently add local `hdc' variable to argument list of
 30151    init_glyph_string.  */
 30152 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30153   init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
 30154 #else
 30155 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30156   init_glyph_string (s, char2b, w, row, area, start, hl)
 30157 #endif
 30158 
 30159 /* Add a glyph string for a stretch glyph to the list of strings
 30160    between HEAD and TAIL.  START is the index of the stretch glyph in
 30161    row area AREA of glyph row ROW.  END is the index of the last glyph
 30162    in that glyph row area.  X is the current output position assigned
 30163    to the new glyph string constructed.  HL overrides that face of the
 30164    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30165    is the right-most x-position of the drawing area.  */
 30166 
 30167 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
 30168    and below -- keep them on one line.  */
 30169 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30170      do                                                                     \
 30171        {                                                                    \
 30172          s = alloca (sizeof *s);                                            \
 30173          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);              \
 30174          START = fill_stretch_glyph_string (s, START, END);                 \
 30175          append_glyph_string (&HEAD, &TAIL, s);                             \
 30176          s->x = (X);                                                        \
 30177        }                                                                    \
 30178      while (false)
 30179 
 30180 
 30181 /* Add a glyph string for an image glyph to the list of strings
 30182    between HEAD and TAIL.  START is the index of the image glyph in
 30183    row area AREA of glyph row ROW.  END is the index of the last glyph
 30184    in that glyph row area.  X is the current output position assigned
 30185    to the new glyph string constructed.  HL overrides that face of the
 30186    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30187    is the right-most x-position of the drawing area.  */
 30188 
 30189 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30190      do                                                                 \
 30191        {                                                                \
 30192          s = alloca (sizeof *s);                                        \
 30193          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30194          fill_image_glyph_string (s);                                   \
 30195          append_glyph_string (&HEAD, &TAIL, s);                         \
 30196          ++START;                                                       \
 30197          s->x = (X);                                                    \
 30198        }                                                                \
 30199      while (false)
 30200 
 30201 #ifndef HAVE_XWIDGETS
 30202 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30203      eassume (false)
 30204 #else
 30205 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30206      do                                                                 \
 30207        {                                                                \
 30208          s = alloca (sizeof *s);                                        \
 30209          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30210          fill_xwidget_glyph_string (s);                                 \
 30211          append_glyph_string (&(HEAD), &(TAIL), s);                     \
 30212          ++(START);                                                     \
 30213          s->x = (X);                                                    \
 30214        }                                                                \
 30215      while (false)
 30216 #endif
 30217 
 30218 /* Add a glyph string for a sequence of character glyphs to the list
 30219    of strings between HEAD and TAIL.  START is the index of the first
 30220    glyph in row area AREA of glyph row ROW that is part of the new
 30221    glyph string.  END is the index of the last glyph in that glyph row
 30222    area.  X is the current output position assigned to the new glyph
 30223    string constructed.  HL overrides that face of the glyph; e.g. it
 30224    is DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the
 30225    right-most x-position of the drawing area.  */
 30226 
 30227 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30228      do                                                                    \
 30229        {                                                                   \
 30230          int face_id;                                                      \
 30231          unsigned *char2b;                                         \
 30232                                                                            \
 30233          face_id = (row)->glyphs[area][START].face_id;                     \
 30234                                                                            \
 30235          s = alloca (sizeof *s);                                           \
 30236          SAFE_NALLOCA (char2b, 1, (END) - (START));                        \
 30237          INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);           \
 30238          append_glyph_string (&HEAD, &TAIL, s);                            \
 30239          s->x = (X);                                                       \
 30240          START = fill_glyph_string (s, face_id, START, END, overlaps);     \
 30241        }                                                                   \
 30242      while (false)
 30243 
 30244 
 30245 /* Add a glyph string for a composite sequence to the list of strings
 30246    between HEAD and TAIL.  START is the index of the first glyph in
 30247    row area AREA of glyph row ROW that is part of the new glyph
 30248    string.  END is the index of the last glyph in that glyph row area.
 30249    X is the current output position assigned to the new glyph string
 30250    constructed.  HL overrides that face of the glyph; e.g. it is
 30251    DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the right-most
 30252    x-position of the drawing area.  */
 30253 
 30254 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30255   do {                                                                      \
 30256     int face_id = (row)->glyphs[area][START].face_id;                       \
 30257     struct face *base_face = FACE_FROM_ID (f, face_id);             \
 30258     ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id;                 \
 30259     struct composition *cmp = composition_table[cmp_id];                    \
 30260     unsigned *char2b;                                                       \
 30261     struct glyph_string *first_s = NULL;                                    \
 30262     int n;                                                                  \
 30263                                                                             \
 30264     SAFE_NALLOCA (char2b, 1, cmp->glyph_len);                               \
 30265                                                                             \
 30266     /* Make glyph_strings for each glyph sequence that is drawable by       \
 30267        the same face, and append them to HEAD/TAIL.  */                     \
 30268     for (n = 0; n < cmp->glyph_len;)                                        \
 30269       {                                                                     \
 30270         s = alloca (sizeof *s);                                             \
 30271         INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);             \
 30272         append_glyph_string (&(HEAD), &(TAIL), s);                          \
 30273         s->cmp = cmp;                                                       \
 30274         s->cmp_from = n;                                                    \
 30275         s->x = (X);                                                         \
 30276         if (n == 0)                                                         \
 30277           first_s = s;                                                      \
 30278         n = fill_composite_glyph_string (s, base_face, overlaps);           \
 30279       }                                                                     \
 30280                                                                             \
 30281     ++START;                                                                \
 30282     s = first_s;                                                            \
 30283   } while (false)
 30284 
 30285 
 30286 /* Add a glyph string for a glyph-string sequence to the list of strings
 30287    between HEAD and TAIL.  */
 30288 
 30289 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30290   do {                                                                    \
 30291     int face_id;                                                          \
 30292     unsigned *char2b;                                                     \
 30293     Lisp_Object gstring;                                                  \
 30294                                                                           \
 30295     face_id = (row)->glyphs[area][START].face_id;                         \
 30296     gstring = (composition_gstring_from_id                                \
 30297                ((row)->glyphs[area][START].u.cmp.id));                    \
 30298     s = alloca (sizeof *s);                                               \
 30299     SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring));               \
 30300     INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);               \
 30301     append_glyph_string (&(HEAD), &(TAIL), s);                            \
 30302     s->x = (X);                                                           \
 30303     START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
 30304   } while (false)
 30305 
 30306 
 30307 /* Add a glyph string for a sequence of glyphless character's glyphs
 30308    to the list of strings between HEAD and TAIL.  The meanings of
 30309    arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS.  */
 30310 
 30311 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30312   do                                                                        \
 30313     {                                                                       \
 30314       int face_id;                                                          \
 30315                                                                             \
 30316       face_id = (row)->glyphs[area][START].face_id;                         \
 30317                                                                             \
 30318       s = alloca (sizeof *s);                                               \
 30319       INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);                 \
 30320       append_glyph_string (&HEAD, &TAIL, s);                                \
 30321       s->x = (X);                                                           \
 30322       START = fill_glyphless_glyph_string (s, face_id, START, END,          \
 30323                                            overlaps);                       \
 30324     }                                                                       \
 30325   while (false)
 30326 
 30327 
 30328 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
 30329    of AREA of glyph row ROW on window W between indices START and END.
 30330    HL overrides the face for drawing glyph strings, e.g. it is
 30331    DRAW_CURSOR to draw a cursor.  X and LAST_X are start and end
 30332    x-positions of the drawing area.
 30333 
 30334    This is an ugly monster macro construct because we must use alloca
 30335    to allocate glyph strings (because draw_glyphs can be called
 30336    asynchronously).  */
 30337 
 30338 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30339   do                                                                    \
 30340     {                                                                   \
 30341       HEAD = TAIL = NULL;                                               \
 30342       while (START < END)                                               \
 30343         {                                                               \
 30344           struct glyph *first_glyph = (row)->glyphs[area] + START;      \
 30345           switch (first_glyph->type)                                    \
 30346             {                                                           \
 30347             case CHAR_GLYPH:                                            \
 30348               BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL,         \
 30349                                         HL, X, LAST_X);                 \
 30350               break;                                                    \
 30351                                                                         \
 30352             case COMPOSITE_GLYPH:                                       \
 30353               if (first_glyph->u.cmp.automatic)                         \
 30354                 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30355                                             HL, X, LAST_X);             \
 30356               else                                                      \
 30357                 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL,   \
 30358                                               HL, X, LAST_X);           \
 30359               break;                                                    \
 30360                                                                         \
 30361             case STRETCH_GLYPH:                                         \
 30362               BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30363                                           HL, X, LAST_X);               \
 30364               break;                                                    \
 30365                                                                         \
 30366             case IMAGE_GLYPH:                                           \
 30367               BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL,         \
 30368                                         HL, X, LAST_X);                 \
 30369               break;
 30370 
 30371 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30372             case XWIDGET_GLYPH:                                         \
 30373               BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30374                                           HL, X, LAST_X);               \
 30375               break;
 30376 
 30377 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30378             case GLYPHLESS_GLYPH:                                       \
 30379               BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30380                                             HL, X, LAST_X);             \
 30381               break;                                                    \
 30382                                                                         \
 30383             default:                                                    \
 30384               emacs_abort ();                                           \
 30385             }                                                           \
 30386                                                                         \
 30387           if (s)                                                        \
 30388             {                                                           \
 30389               set_glyph_string_background_width (s, START, LAST_X);     \
 30390               (X) += s->width;                                          \
 30391             }                                                           \
 30392         }                                                               \
 30393     } while (false)
 30394 
 30395 
 30396 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)      \
 30397     BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)        \
 30398     BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)       \
 30399     BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
 30400 
 30401 
 30402 /* Draw glyphs between START and END in AREA of ROW on window W,
 30403    starting at x-position X.  X is relative to AREA in W.  HL is a
 30404    face-override with the following meaning:
 30405 
 30406    DRAW_NORMAL_TEXT     draw normally
 30407    DRAW_CURSOR          draw in cursor face
 30408    DRAW_MOUSE_FACE      draw in mouse face.
 30409    DRAW_INVERSE_VIDEO   draw in mode line face
 30410    DRAW_IMAGE_SUNKEN    draw an image with a sunken relief around it
 30411    DRAW_IMAGE_RAISED    draw an image with a raised relief around it
 30412 
 30413    If OVERLAPS is non-zero, draw only the foreground of characters and
 30414    clip to the physical height of ROW.  Non-zero value also defines
 30415    the overlapping part to be drawn:
 30416 
 30417    OVERLAPS_PRED                overlap with preceding rows
 30418    OVERLAPS_SUCC                overlap with succeeding rows
 30419    OVERLAPS_BOTH                overlap with both preceding/succeeding rows
 30420    OVERLAPS_ERASED_CURSOR       overlap with erased cursor area
 30421 
 30422    Value is the x-position reached, relative to AREA of W.  */
 30423 
 30424 static int
 30425 draw_glyphs (struct window *w, int x, struct glyph_row *row,
 30426              enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
 30427              enum draw_glyphs_face hl, int overlaps)
 30428 {
 30429   struct glyph_string *head, *tail;
 30430   struct glyph_string *s;
 30431   struct glyph_string *clip_head = NULL, *clip_tail = NULL;
 30432   int i, j, x_reached, last_x, area_left = 0;
 30433   struct frame *f = XFRAME (WINDOW_FRAME (w));
 30434 
 30435   ALLOCATE_HDC (hdc, f);
 30436 
 30437   /* Let's rather be paranoid than getting a SEGV.  */
 30438   end = min (end, row->used[area]);
 30439   start = clip_to_bounds (0, start, end);
 30440 
 30441   /* Translate X to frame coordinates.  Set last_x to the right
 30442      end of the drawing area.  */
 30443   if (row->full_width_p)
 30444     {
 30445       /* X is relative to the left edge of W, without scroll bars
 30446          or fringes.  */
 30447       area_left = WINDOW_LEFT_EDGE_X (w);
 30448       last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
 30449                 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 30450     }
 30451   else
 30452     {
 30453       area_left = window_box_left (w, area);
 30454       last_x = area_left + window_box_width (w, area);
 30455     }
 30456   x += area_left;
 30457 
 30458   /* Build a doubly-linked list of glyph_string structures between
 30459      head and tail from what we have to draw.  Note that the macro
 30460      BUILD_GLYPH_STRINGS will modify its start parameter.  That's
 30461      the reason we use a separate variable `i'.  */
 30462   i = start;
 30463   USE_SAFE_ALLOCA;
 30464   BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
 30465   if (tail)
 30466     {
 30467       s = glyph_string_containing_background_width (tail);
 30468       x_reached = s->x + s->background_width;
 30469     }
 30470   else
 30471     x_reached = x;
 30472 
 30473   /* If there are any glyphs with lbearing < 0 or rbearing > width in
 30474      the row, redraw some glyphs in front or following the glyph
 30475      strings built above.  */
 30476   if (head && !overlaps && row->contains_overlapping_glyphs_p)
 30477     {
 30478       struct glyph_string *h, *t;
 30479       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 30480       int mouse_beg_col UNINIT, mouse_end_col UNINIT;
 30481       bool check_mouse_face = false;
 30482       int dummy_x = 0;
 30483 
 30484       /* If mouse highlighting is on, we may need to draw adjacent
 30485          glyphs using mouse-face highlighting.  */
 30486       if (area == TEXT_AREA && row->mouse_face_p
 30487           && hlinfo->mouse_face_beg_row >= 0
 30488           && hlinfo->mouse_face_end_row >= 0)
 30489         {
 30490           ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
 30491 
 30492           if (row_vpos >= hlinfo->mouse_face_beg_row
 30493               && row_vpos <= hlinfo->mouse_face_end_row)
 30494             {
 30495               check_mouse_face = true;
 30496               mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
 30497                 ? hlinfo->mouse_face_beg_col : 0;
 30498               mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
 30499                 ? hlinfo->mouse_face_end_col
 30500                 : row->used[TEXT_AREA];
 30501             }
 30502         }
 30503 
 30504       /* Compute overhangs for all glyph strings.  */
 30505       if (FRAME_RIF (f)->compute_glyph_string_overhangs)
 30506         for (s = head; s; s = s->next)
 30507           FRAME_RIF (f)->compute_glyph_string_overhangs (s);
 30508 
 30509       /* Prepend glyph strings for glyphs in front of the first glyph
 30510          string that are overwritten because of the first glyph
 30511          string's left overhang.  The background of all strings
 30512          prepended must be drawn because the first glyph string
 30513          draws over it.  */
 30514       i = left_overwritten (head);
 30515       if (i >= 0)
 30516         {
 30517           enum draw_glyphs_face overlap_hl;
 30518 
 30519           /* If this row contains mouse highlighting, attempt to draw
 30520              the overlapped glyphs with the correct highlight.  This
 30521              code fails if the overlap encompasses more than one glyph
 30522              and mouse-highlight spans only some of these glyphs.
 30523              However, making it work perfectly involves a lot more
 30524              code, and I don't know if the pathological case occurs in
 30525              practice, so we'll stick to this for now.  --- cyd  */
 30526           if (check_mouse_face
 30527               && mouse_beg_col < start && mouse_end_col > i)
 30528             overlap_hl = DRAW_MOUSE_FACE;
 30529           else
 30530             overlap_hl = DRAW_NORMAL_TEXT;
 30531 
 30532           if (hl != overlap_hl)
 30533             clip_head = head;
 30534           j = i;
 30535           BUILD_GLYPH_STRINGS (j, start, h, t,
 30536                                overlap_hl, dummy_x, last_x);
 30537           start = i;
 30538           compute_overhangs_and_x (t, head->x, true);
 30539           prepend_glyph_string_lists (&head, &tail, h, t);
 30540           if (clip_head == NULL)
 30541             clip_head = head;
 30542         }
 30543 
 30544       /* Prepend glyph strings for glyphs in front of the first glyph
 30545          string that overwrite that glyph string because of their
 30546          right overhang.  For these strings, only the foreground must
 30547          be drawn, because it draws over the glyph string at `head'.
 30548          The background must not be drawn because this would overwrite
 30549          right overhangs of preceding glyphs for which no glyph
 30550          strings exist.  */
 30551       i = left_overwriting (head);
 30552       if (i >= 0)
 30553         {
 30554           enum draw_glyphs_face overlap_hl;
 30555 
 30556           if (check_mouse_face
 30557               && mouse_beg_col < start && mouse_end_col > i)
 30558             overlap_hl = DRAW_MOUSE_FACE;
 30559           else
 30560             overlap_hl = DRAW_NORMAL_TEXT;
 30561 
 30562           if (hl == overlap_hl || clip_head == NULL)
 30563             clip_head = head;
 30564           BUILD_GLYPH_STRINGS (i, start, h, t,
 30565                                overlap_hl, dummy_x, last_x);
 30566           for (s = h; s; s = s->next)
 30567             s->background_filled_p = true;
 30568           compute_overhangs_and_x (t, head->x, true);
 30569           prepend_glyph_string_lists (&head, &tail, h, t);
 30570         }
 30571 
 30572       /* Append glyphs strings for glyphs following the last glyph
 30573          string tail that are overwritten by tail.  The background of
 30574          these strings has to be drawn because tail's foreground draws
 30575          over it.  */
 30576       i = right_overwritten (tail);
 30577       if (i >= 0)
 30578         {
 30579           enum draw_glyphs_face overlap_hl;
 30580 
 30581           if (check_mouse_face
 30582               && mouse_beg_col < i && mouse_end_col > end)
 30583             overlap_hl = DRAW_MOUSE_FACE;
 30584           else
 30585             overlap_hl = DRAW_NORMAL_TEXT;
 30586 
 30587           if (hl != overlap_hl)
 30588             clip_tail = tail;
 30589           BUILD_GLYPH_STRINGS (end, i, h, t,
 30590                                overlap_hl, x, last_x);
 30591           /* Because BUILD_GLYPH_STRINGS updates the first argument,
 30592              we don't have `end = i;' here.  */
 30593           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30594           append_glyph_string_lists (&head, &tail, h, t);
 30595           if (clip_tail == NULL)
 30596             clip_tail = tail;
 30597         }
 30598 
 30599       /* Append glyph strings for glyphs following the last glyph
 30600          string tail that overwrite tail.  The foreground of such
 30601          glyphs has to be drawn because it writes into the background
 30602          of tail.  The background must not be drawn because it could
 30603          paint over the foreground of following glyphs.  */
 30604       i = right_overwriting (tail);
 30605       if (i >= 0)
 30606         {
 30607           enum draw_glyphs_face overlap_hl;
 30608           if (check_mouse_face
 30609               && mouse_beg_col < i && mouse_end_col > end)
 30610             overlap_hl = DRAW_MOUSE_FACE;
 30611           else
 30612             overlap_hl = DRAW_NORMAL_TEXT;
 30613 
 30614           if (hl == overlap_hl || clip_tail == NULL)
 30615             clip_tail = tail;
 30616           i++;                  /* We must include the Ith glyph.  */
 30617           BUILD_GLYPH_STRINGS (end, i, h, t,
 30618                                overlap_hl, x, last_x);
 30619           for (s = h; s; s = s->next)
 30620             s->background_filled_p = true;
 30621           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30622           append_glyph_string_lists (&head, &tail, h, t);
 30623         }
 30624       tail = glyph_string_containing_background_width (tail);
 30625       if (clip_tail)
 30626         clip_tail = glyph_string_containing_background_width (clip_tail);
 30627       if (clip_head || clip_tail)
 30628         for (s = head; s; s = s->next)
 30629           {
 30630             s->clip_head = clip_head;
 30631             s->clip_tail = clip_tail;
 30632           }
 30633     }
 30634 
 30635   /* Draw all strings.  */
 30636   for (s = head; s; s = s->next)
 30637     FRAME_RIF (f)->draw_glyph_string (s);
 30638 
 30639   /* When focus a sole frame and move horizontally, this clears on_p
 30640      causing a failure to erase prev cursor position. */
 30641   if (area == TEXT_AREA
 30642       && !row->full_width_p
 30643       /* When drawing overlapping rows, only the glyph strings'
 30644          foreground is drawn, which doesn't erase a cursor
 30645          completely. */
 30646       && !overlaps)
 30647     {
 30648       int x0 = clip_head ? clip_head->x : (head ? head->x : x);
 30649       int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
 30650                 : (tail ? tail->x + tail->background_width : x));
 30651       x0 -= area_left;
 30652       x1 -= area_left;
 30653 
 30654       notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
 30655                                  row->y, MATRIX_ROW_BOTTOM_Y (row));
 30656     }
 30657 
 30658   /* Value is the x-position up to which drawn, relative to AREA of W.
 30659      This doesn't include parts drawn because of overhangs.  */
 30660   if (row->full_width_p)
 30661     x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
 30662   else
 30663     x_reached -= area_left;
 30664 
 30665   RELEASE_HDC (hdc, f);
 30666 
 30667   SAFE_FREE ();
 30668   return x_reached;
 30669 }
 30670 
 30671 /* Find the first glyph in the run of underlined glyphs preceding the
 30672    beginning of glyph string S, and return its font (which could be
 30673    NULL).  This is needed because that font determines the underline
 30674    position and thickness for the entire run of the underlined glyphs.
 30675    This function is called from the draw_glyph_string method of GUI
 30676    frame's redisplay interface (RIF) when it needs to draw in an
 30677    underlined face.  */
 30678 struct font *
 30679 font_for_underline_metrics (struct glyph_string *s)
 30680 {
 30681   struct glyph *g0 = s->row->glyphs[s->area], *g;
 30682 
 30683   for (g = s->first_glyph - 1; g >= g0; g--)
 30684     {
 30685       struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
 30686       if (!(prev_face && prev_face->underline != FACE_NO_UNDERLINE))
 30687         break;
 30688     }
 30689 
 30690   /* If preceding glyphs are not underlined, use the font of S.  */
 30691   if (g == s->first_glyph - 1)
 30692     return s->font;
 30693   else
 30694     {
 30695       /* Otherwise use the font of the last glyph we saw in the above
 30696          loop whose face had the underline_p flag set.  */
 30697       return FACE_FROM_ID (s->f, g[1].face_id)->font;
 30698     }
 30699 }
 30700 
 30701 /* Expand row matrix if too narrow.  Don't expand if area
 30702    is not present.  */
 30703 
 30704 #define IT_EXPAND_MATRIX_WIDTH(it, area)                \
 30705   {                                                     \
 30706     if (!it->f->fonts_changed                           \
 30707         && (it->glyph_row->glyphs[area]                 \
 30708             < it->glyph_row->glyphs[area + 1]))         \
 30709       {                                                 \
 30710         it->w->ncols_scale_factor++;                    \
 30711         it->f->fonts_changed = true;                    \
 30712       }                                                 \
 30713   }
 30714 
 30715 /* Store one glyph for IT->char_to_display in IT->glyph_row.
 30716    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 30717 
 30718 static void
 30719 append_glyph (struct it *it)
 30720 {
 30721   struct glyph *glyph;
 30722   enum glyph_row_area area = it->area;
 30723 
 30724   eassert (it->glyph_row);
 30725   eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
 30726 
 30727   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 30728   if (glyph < it->glyph_row->glyphs[area + 1])
 30729     {
 30730       /* If the glyph row is reversed, we need to prepend the glyph
 30731          rather than append it.  */
 30732       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30733         {
 30734           struct glyph *g;
 30735 
 30736           /* Make room for the additional glyph.  */
 30737           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 30738             g[1] = *g;
 30739           glyph = it->glyph_row->glyphs[area];
 30740         }
 30741       glyph->charpos = CHARPOS (it->position);
 30742       glyph->object = it->object;
 30743       if (it->pixel_width > 0)
 30744         {
 30745           eassert (it->pixel_width <= SHRT_MAX);
 30746           glyph->pixel_width = it->pixel_width;
 30747           glyph->padding_p = false;
 30748         }
 30749       else
 30750         {
 30751           /* Assure at least 1-pixel width.  Otherwise, cursor can't
 30752              be displayed correctly.  */
 30753           glyph->pixel_width = 1;
 30754           glyph->padding_p = true;
 30755         }
 30756       glyph->ascent = it->ascent;
 30757       glyph->descent = it->descent;
 30758       glyph->voffset = it->voffset;
 30759       glyph->type = CHAR_GLYPH;
 30760       glyph->avoid_cursor_p = it->avoid_cursor_p;
 30761       glyph->multibyte_p = it->multibyte_p;
 30762       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30763         {
 30764           /* In R2L rows, the left and the right box edges need to be
 30765              drawn in reverse direction.  */
 30766           glyph->right_box_line_p = it->start_of_box_run_p;
 30767           glyph->left_box_line_p = it->end_of_box_run_p;
 30768         }
 30769       else
 30770         {
 30771           glyph->left_box_line_p = it->start_of_box_run_p;
 30772           glyph->right_box_line_p = it->end_of_box_run_p;
 30773         }
 30774       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 30775                                       || it->phys_descent > it->descent);
 30776       glyph->glyph_not_available_p = it->glyph_not_available_p;
 30777       glyph->face_id = it->face_id;
 30778       glyph->u.ch = it->char_to_display;
 30779       glyph->slice.img = null_glyph_slice;
 30780       glyph->font_type = FONT_TYPE_UNKNOWN;
 30781       if (it->bidi_p)
 30782         {
 30783           glyph->resolved_level = it->bidi_it.resolved_level;
 30784           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 30785           glyph->bidi_type = it->bidi_it.type;
 30786         }
 30787       else
 30788         {
 30789           glyph->resolved_level = 0;
 30790           glyph->bidi_type = UNKNOWN_BT;
 30791         }
 30792       ++it->glyph_row->used[area];
 30793     }
 30794   else
 30795     IT_EXPAND_MATRIX_WIDTH (it, area);
 30796 }
 30797 
 30798 /* Store one glyph for the composition IT->cmp_it.id in IT->glyph_row.
 30799    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 30800 
 30801 static void
 30802 append_composite_glyph (struct it *it)
 30803 {
 30804   struct glyph *glyph;
 30805   enum glyph_row_area area = it->area;
 30806 
 30807   eassert (it->glyph_row);
 30808 
 30809   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 30810   if (glyph < it->glyph_row->glyphs[area + 1])
 30811     {
 30812       /* If the glyph row is reversed, we need to prepend the glyph
 30813          rather than append it.  */
 30814       if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
 30815         {
 30816           struct glyph *g;
 30817 
 30818           /* Make room for the new glyph.  */
 30819           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 30820             g[1] = *g;
 30821           glyph = it->glyph_row->glyphs[it->area];
 30822         }
 30823       glyph->charpos = it->cmp_it.charpos;
 30824       glyph->object = it->object;
 30825       eassert (it->pixel_width <= SHRT_MAX);
 30826       glyph->pixel_width = it->pixel_width;
 30827       glyph->ascent = it->ascent;
 30828       glyph->descent = it->descent;
 30829       glyph->voffset = it->voffset;
 30830       glyph->type = COMPOSITE_GLYPH;
 30831       if (it->cmp_it.ch < 0)
 30832         {
 30833           glyph->u.cmp.automatic = false;
 30834           glyph->u.cmp.id = it->cmp_it.id;
 30835           glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
 30836         }
 30837       else
 30838         {
 30839           glyph->u.cmp.automatic = true;
 30840           glyph->u.cmp.id = it->cmp_it.id;
 30841           glyph->slice.cmp.from = it->cmp_it.from;
 30842           glyph->slice.cmp.to = it->cmp_it.to - 1;
 30843         }
 30844       glyph->avoid_cursor_p = it->avoid_cursor_p;
 30845       glyph->multibyte_p = it->multibyte_p;
 30846       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30847         {
 30848           /* In R2L rows, the left and the right box edges need to be
 30849              drawn in reverse direction.  */
 30850           glyph->right_box_line_p = it->start_of_box_run_p;
 30851           glyph->left_box_line_p = it->end_of_box_run_p;
 30852         }
 30853       else
 30854         {
 30855           glyph->left_box_line_p = it->start_of_box_run_p;
 30856           glyph->right_box_line_p = it->end_of_box_run_p;
 30857         }
 30858       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 30859                                       || it->phys_descent > it->descent);
 30860       glyph->padding_p = false;
 30861       glyph->glyph_not_available_p = it->glyph_not_available_p;
 30862       glyph->face_id = it->face_id;
 30863       glyph->font_type = FONT_TYPE_UNKNOWN;
 30864       if (it->bidi_p)
 30865         {
 30866           glyph->resolved_level = it->bidi_it.resolved_level;
 30867           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 30868           glyph->bidi_type = it->bidi_it.type;
 30869         }
 30870       ++it->glyph_row->used[area];
 30871     }
 30872   else
 30873     IT_EXPAND_MATRIX_WIDTH (it, area);
 30874 }
 30875 
 30876 
 30877 /* Change IT->ascent and IT->height according to the setting of
 30878    IT->voffset.  */
 30879 
 30880 static void
 30881 take_vertical_position_into_account (struct it *it)
 30882 {
 30883   if (it->voffset)
 30884     {
 30885       if (it->voffset < 0)
 30886         /* Increase the ascent so that we can display the text higher
 30887            in the line.  */
 30888         it->ascent -= it->voffset;
 30889       else
 30890         /* Increase the descent so that we can display the text lower
 30891            in the line.  */
 30892         it->descent += it->voffset;
 30893     }
 30894 }
 30895 
 30896 
 30897 /* Produce glyphs/get display metrics for the image IT is loaded with.
 30898    See the description of struct display_iterator in dispextern.h for
 30899    an overview of struct display_iterator.  */
 30900 
 30901 static void
 30902 produce_image_glyph (struct it *it)
 30903 {
 30904   struct image *img;
 30905   struct face *face;
 30906   int glyph_ascent, crop;
 30907   struct glyph_slice slice;
 30908 
 30909   eassert (it->what == IT_IMAGE);
 30910 
 30911   face = FACE_FROM_ID (it->f, it->face_id);
 30912   /* Make sure X resources of the face is loaded.  */
 30913   prepare_face_for_display (it->f, face);
 30914 
 30915   if (it->image_id < 0)
 30916     {
 30917       /* Fringe bitmap.  */
 30918       it->ascent = it->phys_ascent = 0;
 30919       it->descent = it->phys_descent = 0;
 30920       it->pixel_width = 0;
 30921       it->nglyphs = 0;
 30922       return;
 30923     }
 30924 
 30925   img = IMAGE_FROM_ID (it->f, it->image_id);
 30926   /* Make sure X resources of the image is loaded.  */
 30927   prepare_image_for_display (it->f, img);
 30928 
 30929   slice.x = slice.y = 0;
 30930   slice.width = img->width;
 30931   slice.height = img->height;
 30932 
 30933   if (FIXNUMP (it->slice.x))
 30934     slice.x = XFIXNUM (it->slice.x);
 30935   else if (FLOATP (it->slice.x))
 30936     slice.x = XFLOAT_DATA (it->slice.x) * img->width;
 30937 
 30938   if (FIXNUMP (it->slice.y))
 30939     slice.y = XFIXNUM (it->slice.y);
 30940   else if (FLOATP (it->slice.y))
 30941     slice.y = XFLOAT_DATA (it->slice.y) * img->height;
 30942 
 30943   if (FIXNUMP (it->slice.width))
 30944     slice.width = XFIXNUM (it->slice.width);
 30945   else if (FLOATP (it->slice.width))
 30946     slice.width = XFLOAT_DATA (it->slice.width) * img->width;
 30947 
 30948   if (FIXNUMP (it->slice.height))
 30949     slice.height = XFIXNUM (it->slice.height);
 30950   else if (FLOATP (it->slice.height))
 30951     slice.height = XFLOAT_DATA (it->slice.height) * img->height;
 30952 
 30953   if (slice.x >= img->width)
 30954     slice.x = img->width;
 30955   if (slice.y >= img->height)
 30956     slice.y = img->height;
 30957   if (slice.x + slice.width >= img->width)
 30958     slice.width = img->width - slice.x;
 30959   if (slice.y + slice.height > img->height)
 30960     slice.height = img->height - slice.y;
 30961 
 30962   if (slice.width == 0 || slice.height == 0)
 30963     return;
 30964 
 30965   it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
 30966 
 30967   it->descent = slice.height - glyph_ascent;
 30968   if (slice.y == 0)
 30969     it->descent += img->vmargin;
 30970   if (slice.y + slice.height == img->height)
 30971     it->descent += img->vmargin;
 30972   it->phys_descent = it->descent;
 30973 
 30974   it->pixel_width = slice.width;
 30975   if (slice.x == 0)
 30976     it->pixel_width += img->hmargin;
 30977   if (slice.x + slice.width == img->width)
 30978     it->pixel_width += img->hmargin;
 30979 
 30980   /* It's quite possible for images to have an ascent greater than
 30981      their height, so don't get confused in that case.  */
 30982   if (it->descent < 0)
 30983     it->descent = 0;
 30984 
 30985   it->nglyphs = 1;
 30986 
 30987   if (face->box != FACE_NO_BOX)
 30988     {
 30989       /* If you change the logic here, please change it in
 30990          get_cursor_offset_for_mouse_face as well. */
 30991       if (face->box_horizontal_line_width > 0)
 30992         {
 30993           if (slice.y == 0)
 30994             it->ascent += face->box_horizontal_line_width;
 30995           if (slice.y + slice.height == img->height)
 30996             it->descent += face->box_horizontal_line_width;
 30997         }
 30998 
 30999       if (face->box_vertical_line_width > 0)
 31000         {
 31001           if (it->start_of_box_run_p && slice.x == 0)
 31002             it->pixel_width += face->box_vertical_line_width;
 31003           if (it->end_of_box_run_p && slice.x + slice.width == img->width)
 31004             it->pixel_width += face->box_vertical_line_width;
 31005         }
 31006     }
 31007 
 31008   take_vertical_position_into_account (it);
 31009 
 31010   /* Automatically crop wide image glyphs at right edge so we can
 31011      draw the cursor on same display row.  */
 31012   if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
 31013       && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31014     {
 31015       it->pixel_width -= crop;
 31016       slice.width -= crop;
 31017     }
 31018 
 31019   if (it->glyph_row)
 31020     {
 31021       struct glyph *glyph;
 31022       enum glyph_row_area area = it->area;
 31023 
 31024       glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31025       if (it->glyph_row->reversed_p)
 31026         {
 31027           struct glyph *g;
 31028 
 31029           /* Make room for the new glyph.  */
 31030           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31031             g[1] = *g;
 31032           glyph = it->glyph_row->glyphs[it->area];
 31033         }
 31034       if (glyph < it->glyph_row->glyphs[area + 1])
 31035         {
 31036           glyph->charpos = CHARPOS (it->position);
 31037           glyph->object = it->object;
 31038           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31039           glyph->ascent = glyph_ascent;
 31040           glyph->descent = it->descent;
 31041           glyph->voffset = it->voffset;
 31042           glyph->type = IMAGE_GLYPH;
 31043           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31044           glyph->multibyte_p = it->multibyte_p;
 31045           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31046             {
 31047               /* In R2L rows, the left and the right box edges need to be
 31048                  drawn in reverse direction.  */
 31049               glyph->right_box_line_p = it->start_of_box_run_p;
 31050               glyph->left_box_line_p = it->end_of_box_run_p;
 31051             }
 31052           else
 31053             {
 31054               glyph->left_box_line_p = it->start_of_box_run_p;
 31055               glyph->right_box_line_p = it->end_of_box_run_p;
 31056             }
 31057           glyph->overlaps_vertically_p = false;
 31058           glyph->padding_p = false;
 31059           glyph->glyph_not_available_p = false;
 31060           glyph->face_id = it->face_id;
 31061           glyph->u.img_id = img->id;
 31062           glyph->slice.img = slice;
 31063           glyph->font_type = FONT_TYPE_UNKNOWN;
 31064           if (it->bidi_p)
 31065             {
 31066               glyph->resolved_level = it->bidi_it.resolved_level;
 31067               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31068               glyph->bidi_type = it->bidi_it.type;
 31069             }
 31070           ++it->glyph_row->used[area];
 31071         }
 31072       else
 31073         IT_EXPAND_MATRIX_WIDTH (it, area);
 31074     }
 31075 }
 31076 
 31077 static void
 31078 produce_xwidget_glyph (struct it *it)
 31079 {
 31080 #ifdef HAVE_XWIDGETS
 31081   struct xwidget *xw;
 31082   int glyph_ascent, crop;
 31083   eassert (it->what == IT_XWIDGET);
 31084 
 31085   struct face *face = FACE_FROM_ID (it->f, it->face_id);
 31086   /* Make sure X resources of the face is loaded.  */
 31087   prepare_face_for_display (it->f, face);
 31088 
 31089   xw = it->xwidget;
 31090   it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
 31091   it->descent = xw->height/2;
 31092   it->phys_descent = it->descent;
 31093   it->pixel_width = xw->width;
 31094   /* It's quite possible for images to have an ascent greater than
 31095      their height, so don't get confused in that case.  */
 31096   if (it->descent < 0)
 31097     it->descent = 0;
 31098 
 31099   it->nglyphs = 1;
 31100 
 31101   if (face->box != FACE_NO_BOX)
 31102     {
 31103       if (face->box_horizontal_line_width > 0)
 31104         {
 31105           it->ascent += face->box_horizontal_line_width;
 31106           it->descent += face->box_horizontal_line_width;
 31107         }
 31108 
 31109       if (face->box_vertical_line_width > 0)
 31110         {
 31111           if (it->start_of_box_run_p)
 31112             it->pixel_width += face->box_vertical_line_width;
 31113           it->pixel_width += face->box_vertical_line_width;
 31114         }
 31115     }
 31116 
 31117   take_vertical_position_into_account (it);
 31118 
 31119   /* Automatically crop wide image glyphs at right edge so we can
 31120      draw the cursor on same display row.  */
 31121   crop = it->pixel_width - (it->last_visible_x - it->current_x);
 31122   if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31123     it->pixel_width -= crop;
 31124 
 31125   if (it->glyph_row)
 31126     {
 31127       enum glyph_row_area area = it->area;
 31128       struct glyph *glyph
 31129         = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31130 
 31131       if (it->glyph_row->reversed_p)
 31132         {
 31133           struct glyph *g;
 31134 
 31135           /* Make room for the new glyph.  */
 31136           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31137             g[1] = *g;
 31138           glyph = it->glyph_row->glyphs[it->area];
 31139         }
 31140       if (glyph < it->glyph_row->glyphs[area + 1])
 31141         {
 31142           glyph->charpos = CHARPOS (it->position);
 31143           glyph->object = it->object;
 31144           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31145           glyph->ascent = glyph_ascent;
 31146           glyph->descent = it->descent;
 31147           glyph->voffset = it->voffset;
 31148           glyph->type = XWIDGET_GLYPH;
 31149           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31150           glyph->multibyte_p = it->multibyte_p;
 31151           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31152             {
 31153               /* In R2L rows, the left and the right box edges need to be
 31154                  drawn in reverse direction.  */
 31155               glyph->right_box_line_p = it->start_of_box_run_p;
 31156               glyph->left_box_line_p = it->end_of_box_run_p;
 31157             }
 31158           else
 31159             {
 31160               glyph->left_box_line_p = it->start_of_box_run_p;
 31161               glyph->right_box_line_p = it->end_of_box_run_p;
 31162             }
 31163           glyph->overlaps_vertically_p = 0;
 31164           glyph->padding_p = 0;
 31165           glyph->glyph_not_available_p = 0;
 31166           glyph->face_id = it->face_id;
 31167           glyph->u.xwidget = it->xwidget->xwidget_id;
 31168           glyph->font_type = FONT_TYPE_UNKNOWN;
 31169           if (it->bidi_p)
 31170             {
 31171               glyph->resolved_level = it->bidi_it.resolved_level;
 31172               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31173               glyph->bidi_type = it->bidi_it.type;
 31174             }
 31175           ++it->glyph_row->used[area];
 31176         }
 31177       else
 31178         IT_EXPAND_MATRIX_WIDTH (it, area);
 31179     }
 31180 #endif
 31181 }
 31182 
 31183 /* Append a stretch glyph to IT->glyph_row.  OBJECT is the source
 31184    of the glyph, WIDTH and HEIGHT are the width and height of the
 31185    stretch.  ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT).  */
 31186 
 31187 static void
 31188 append_stretch_glyph (struct it *it, Lisp_Object object,
 31189                       int width, int height, int ascent)
 31190 {
 31191   struct glyph *glyph;
 31192   enum glyph_row_area area = it->area;
 31193 
 31194   eassert (ascent >= 0 && ascent <= height);
 31195 
 31196   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31197   if (glyph < it->glyph_row->glyphs[area + 1])
 31198     {
 31199       /* If the glyph row is reversed, we need to prepend the glyph
 31200          rather than append it.  */
 31201       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31202         {
 31203           struct glyph *g;
 31204 
 31205           /* Make room for the additional glyph.  */
 31206           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31207             g[1] = *g;
 31208           glyph = it->glyph_row->glyphs[area];
 31209 
 31210           /* Decrease the width of the first glyph of the row that
 31211              begins before first_visible_x (e.g., due to hscroll).
 31212              This is so the overall width of the row becomes smaller
 31213              by the scroll amount, and the stretch glyph appended by
 31214              extend_face_to_end_of_line will be wider, to shift the
 31215              row glyphs to the right.  (In L2R rows, the corresponding
 31216              left-shift effect is accomplished by setting row->x to a
 31217              negative value, which won't work with R2L rows.)
 31218 
 31219              This must leave us with a positive value of WIDTH, since
 31220              otherwise the call to move_it_in_display_line_to at the
 31221              beginning of display_line would have got past the entire
 31222              first glyph, and then it->current_x would have been
 31223              greater or equal to it->first_visible_x.  */
 31224           if (it->current_x < it->first_visible_x)
 31225             width -= it->first_visible_x - it->current_x;
 31226           eassert (width > 0);
 31227         }
 31228       glyph->charpos = CHARPOS (it->position);
 31229       glyph->object = object;
 31230       /* FIXME: It would be better to use TYPE_MAX here, but
 31231          __typeof__ is not portable enough...  */
 31232       glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
 31233       glyph->ascent = ascent;
 31234       glyph->descent = height - ascent;
 31235       glyph->voffset = it->voffset;
 31236       glyph->type = STRETCH_GLYPH;
 31237       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31238       glyph->multibyte_p = it->multibyte_p;
 31239       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31240         {
 31241           /* In R2L rows, the left and the right box edges need to be
 31242              drawn in reverse direction.  */
 31243           glyph->right_box_line_p = it->start_of_box_run_p;
 31244           glyph->left_box_line_p = it->end_of_box_run_p;
 31245         }
 31246       else
 31247         {
 31248           glyph->left_box_line_p = it->start_of_box_run_p;
 31249           glyph->right_box_line_p = it->end_of_box_run_p;
 31250         }
 31251       glyph->overlaps_vertically_p = false;
 31252       glyph->padding_p = false;
 31253       glyph->glyph_not_available_p = false;
 31254       glyph->face_id = it->face_id;
 31255       glyph->u.stretch.ascent = ascent;
 31256       glyph->u.stretch.height = height;
 31257       glyph->slice.img = null_glyph_slice;
 31258       glyph->font_type = FONT_TYPE_UNKNOWN;
 31259       if (it->bidi_p)
 31260         {
 31261           glyph->resolved_level = it->bidi_it.resolved_level;
 31262           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31263           glyph->bidi_type = it->bidi_it.type;
 31264         }
 31265       else
 31266         {
 31267           glyph->resolved_level = 0;
 31268           glyph->bidi_type = UNKNOWN_BT;
 31269         }
 31270       ++it->glyph_row->used[area];
 31271     }
 31272   else
 31273     IT_EXPAND_MATRIX_WIDTH (it, area);
 31274 }
 31275 
 31276 #endif  /* HAVE_WINDOW_SYSTEM */
 31277 
 31278 /* Produce a stretch glyph for iterator IT.  IT->object is the value
 31279    of the display property.  The value must be a list of the form
 31280    `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
 31281    being recognized:
 31282 
 31283    1. `:width WIDTH' specifies that the space should be WIDTH *
 31284    canonical char width wide.  WIDTH may be an integer or floating
 31285    point number.
 31286 
 31287    2. `:relative-width FACTOR' specifies that the width of the stretch
 31288    should be computed from the width of the first character having the
 31289    `display' property, and should be FACTOR times that width.
 31290 
 31291    3. `:align-to HPOS' specifies that the space should be wide enough
 31292    to reach HPOS, a value in canonical character units.
 31293 
 31294    Exactly one of the above pairs must be present.
 31295 
 31296    4. `:height HEIGHT' specifies that the height of the stretch produced
 31297    should be HEIGHT, measured in canonical character units.
 31298 
 31299    5. `:relative-height FACTOR' specifies that the height of the
 31300    stretch should be FACTOR times the height of the characters having
 31301    the display property.
 31302 
 31303    Either none or exactly one of 4 or 5 must be present.
 31304 
 31305    6. `:ascent ASCENT'  specifies that ASCENT percent of the height
 31306    of the stretch should be used for the ascent of the stretch.
 31307    ASCENT must be in the range 0 <= ASCENT <= 100.  */
 31308 
 31309 void
 31310 produce_stretch_glyph (struct it *it)
 31311 {
 31312   /* (space :width WIDTH :height HEIGHT ...)  */
 31313   Lisp_Object prop, plist;
 31314   int width = 0, height = 0, align_to = -1;
 31315   bool zero_width_ok_p = false;
 31316   double tem;
 31317   struct font *font = NULL;
 31318 
 31319 #ifdef HAVE_WINDOW_SYSTEM
 31320   int ascent = 0;
 31321   bool zero_height_ok_p = false;
 31322   struct face *face = NULL;     /* shut up GCC's -Wmaybe-uninitialized */
 31323 
 31324   if (FRAME_WINDOW_P (it->f))
 31325     {
 31326       face = FACE_FROM_ID (it->f, it->face_id);
 31327       font = face->font ? face->font : FRAME_FONT (it->f);
 31328       prepare_face_for_display (it->f, face);
 31329     }
 31330 #endif
 31331 
 31332   /* List should start with `space'.  */
 31333   eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
 31334   plist = XCDR (it->object);
 31335 
 31336   /* Compute the width of the stretch.  */
 31337   if ((prop = plist_get (plist, QCwidth), !NILP (prop))
 31338       && calc_pixel_width_or_height (&tem, it, prop, font, true, NULL))
 31339     {
 31340       /* Absolute width `:width WIDTH' specified and valid.  */
 31341       zero_width_ok_p = true;
 31342       width = (int)tem;
 31343     }
 31344   else if (prop = plist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
 31345     {
 31346       /* Relative width `:relative-width FACTOR' specified and valid.
 31347          Compute the width of the characters having this `display'
 31348          property.  */
 31349       struct it it2;
 31350       Lisp_Object object =
 31351         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31352       unsigned char *p = (STRINGP (object)
 31353                           ? SDATA (object) + IT_STRING_BYTEPOS (*it)
 31354                           : BYTE_POS_ADDR (IT_BYTEPOS (*it)));
 31355       bool multibyte_p =
 31356         STRINGP (object) ? STRING_MULTIBYTE (object) : it->multibyte_p;
 31357 
 31358       it2 = *it;
 31359       if (multibyte_p)
 31360         {
 31361           it2.c = it2.char_to_display = string_char_and_length (p, &it2.len);
 31362 #ifdef HAVE_WINDOW_SYSTEM
 31363           if (FRAME_WINDOW_P (it->f) && ! ASCII_CHAR_P (it2.c))
 31364             it2.face_id = FACE_FOR_CHAR (it->f, face, it2.c,
 31365                                          IT_CHARPOS (*it),
 31366                                          STRINGP (object)? object : Qnil);
 31367 #endif
 31368         }
 31369       else
 31370         {
 31371           it2.c = it2.char_to_display = *p, it2.len = 1;
 31372           if (! ASCII_CHAR_P (it2.c))
 31373             it2.char_to_display = BYTE8_TO_CHAR (it2.c);
 31374         }
 31375 
 31376       it2.glyph_row = NULL;
 31377       it2.what = IT_CHARACTER;
 31378       PRODUCE_GLYPHS (&it2);
 31379       width = NUMVAL (prop) * it2.pixel_width;
 31380     }
 31381   else if ((prop = plist_get (plist, QCalign_to), !NILP (prop))
 31382            && calc_pixel_width_or_height (&tem, it, prop, font, true,
 31383                                           &align_to))
 31384     {
 31385       int x = it->current_x + it->continuation_lines_width;
 31386       int x0 = x;
 31387       /* Adjust for line numbers, if needed.   */
 31388       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31389         {
 31390           x -= it->lnum_pixel_width;
 31391           /* Restore the original width, if required.  */
 31392           if (x + it->stretch_adjust >= it->first_visible_x)
 31393             x += it->stretch_adjust;
 31394         }
 31395 
 31396       if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
 31397         align_to = (align_to < 0
 31398                     ? 0
 31399                     : align_to - window_box_left_offset (it->w, TEXT_AREA));
 31400       else if (align_to < 0)
 31401         align_to = window_box_left_offset (it->w, TEXT_AREA);
 31402       width = max (0, (int)tem + align_to - x);
 31403 
 31404       int next_x = x + width;
 31405       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31406         {
 31407           /* If the line is hscrolled, and the stretch starts before
 31408              the first visible pixel, simulate negative row->x.  */
 31409           if (x < it->first_visible_x)
 31410             {
 31411               next_x -= it->first_visible_x - x;
 31412               it->stretch_adjust = it->first_visible_x - x;
 31413             }
 31414           else
 31415             next_x -= it->stretch_adjust;
 31416         }
 31417       width = next_x - x0;
 31418       zero_width_ok_p = true;
 31419     }
 31420   else
 31421     /* Nothing specified -> width defaults to canonical char width.  */
 31422     width = FRAME_COLUMN_WIDTH (it->f);
 31423 
 31424   if (width <= 0 && (width < 0 || !zero_width_ok_p))
 31425     width = 1;
 31426 
 31427 #ifdef HAVE_WINDOW_SYSTEM
 31428   /* Compute height.  */
 31429   if (FRAME_WINDOW_P (it->f))
 31430     {
 31431       int default_height = normal_char_height (font, ' ');
 31432 
 31433       if ((prop = plist_get (plist, QCheight), !NILP (prop))
 31434           && calc_pixel_width_or_height (&tem, it, prop, font, false, NULL))
 31435         {
 31436           height = (int)tem;
 31437           zero_height_ok_p = true;
 31438         }
 31439       else if (prop = plist_get (plist, QCrelative_height),
 31440                NUMVAL (prop) > 0)
 31441         height = default_height * NUMVAL (prop);
 31442       else
 31443         height = default_height;
 31444 
 31445       if (height <= 0 && (height < 0 || !zero_height_ok_p))
 31446         height = 1;
 31447 
 31448       /* Compute percentage of height used for ascent.  If
 31449          `:ascent ASCENT' is present and valid, use that.  Otherwise,
 31450          derive the ascent from the font in use.  */
 31451       if (prop = plist_get (plist, QCascent),
 31452           NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
 31453         ascent = height * NUMVAL (prop) / 100.0;
 31454       else if (!NILP (prop)
 31455                && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
 31456         ascent = min (max (0, (int)tem), height);
 31457       else
 31458         ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
 31459     }
 31460   else
 31461 #endif  /* HAVE_WINDOW_SYSTEM */
 31462     height = 1;
 31463 
 31464   if (width > 0
 31465       && it->area == TEXT_AREA && it->line_wrap != TRUNCATE
 31466       && it->current_x + width > it->last_visible_x)
 31467     {
 31468       width = it->last_visible_x - it->current_x;
 31469 #ifdef HAVE_WINDOW_SYSTEM
 31470       /* Subtract one more pixel from the stretch width, but only on
 31471          GUI frames, since on a TTY each glyph is one "pixel" wide.  */
 31472       width -= FRAME_WINDOW_P (it->f);
 31473 #endif
 31474     }
 31475 
 31476   if (width > 0 && height > 0 && it->glyph_row)
 31477     {
 31478       Lisp_Object o_object = it->object;
 31479       Lisp_Object object =
 31480         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31481       int n = width;
 31482 
 31483       if (!STRINGP (object))
 31484         object = it->w->contents;
 31485 #ifdef HAVE_WINDOW_SYSTEM
 31486       if (FRAME_WINDOW_P (it->f))
 31487         append_stretch_glyph (it, object, width, height, ascent);
 31488       else
 31489 #endif
 31490         {
 31491           it->object = object;
 31492           it->char_to_display = ' ';
 31493           it->pixel_width = it->len = 1;
 31494           while (n--)
 31495             tty_append_glyph (it);
 31496           it->object = o_object;
 31497         }
 31498     }
 31499 
 31500   it->pixel_width = width;
 31501 #ifdef HAVE_WINDOW_SYSTEM
 31502   if (FRAME_WINDOW_P (it->f))
 31503     {
 31504       it->ascent = it->phys_ascent = ascent;
 31505       it->descent = it->phys_descent = height - it->ascent;
 31506       it->nglyphs = width > 0 && height > 0;
 31507       take_vertical_position_into_account (it);
 31508     }
 31509   else
 31510 #endif
 31511     it->nglyphs = width;
 31512 }
 31513 
 31514 /* Get information about special display element WHAT in an
 31515    environment described by IT.  WHAT is one of IT_TRUNCATION or
 31516    IT_CONTINUATION.  Maybe produce glyphs for WHAT if IT has a
 31517    non-null glyph_row member.  This function ensures that fields like
 31518    face_id, c, len of IT are left untouched.  */
 31519 
 31520 static void
 31521 produce_special_glyphs (struct it *it, enum display_element_type what)
 31522 {
 31523   struct it temp_it;
 31524   Lisp_Object gc;
 31525   GLYPH glyph;
 31526 
 31527   temp_it = *it;
 31528   temp_it.object = Qnil;
 31529   memset (&temp_it.current, 0, sizeof temp_it.current);
 31530 
 31531   if (what == IT_CONTINUATION)
 31532     {
 31533       /* Continuation glyph.  For R2L lines, we mirror it by hand.  */
 31534       if (it->bidi_it.paragraph_dir == R2L)
 31535         SET_GLYPH_FROM_CHAR (glyph, '/');
 31536       else
 31537         SET_GLYPH_FROM_CHAR (glyph, '\\');
 31538       if (it->dp
 31539           && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31540         {
 31541           /* FIXME: Should we mirror GC for R2L lines?  */
 31542           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31543           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31544         }
 31545     }
 31546   else if (what == IT_TRUNCATION)
 31547     {
 31548       /* Truncation glyph.  */
 31549       SET_GLYPH_FROM_CHAR (glyph, '$');
 31550       if (it->dp
 31551           && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31552         {
 31553           /* FIXME: Should we mirror GC for R2L lines?  */
 31554           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31555           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31556         }
 31557     }
 31558   else
 31559     emacs_abort ();
 31560 
 31561 #ifdef HAVE_WINDOW_SYSTEM
 31562   /* On a GUI frame, when the right fringe (left fringe for R2L rows)
 31563      is turned off, we precede the truncation/continuation glyphs by a
 31564      stretch glyph whose width is computed such that these special
 31565      glyphs are aligned at the window margin, even when very different
 31566      fonts are used in different glyph rows.  */
 31567   if (FRAME_WINDOW_P (temp_it.f)
 31568       /* init_iterator calls this with it->glyph_row == NULL, and it
 31569          wants only the pixel width of the truncation/continuation
 31570          glyphs.  */
 31571       && temp_it.glyph_row
 31572       /* insert_left_trunc_glyphs calls us at the beginning of the
 31573          row, and it has its own calculation of the stretch glyph
 31574          width.  */
 31575       && temp_it.glyph_row->used[TEXT_AREA] > 0
 31576       && (temp_it.glyph_row->reversed_p
 31577           ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
 31578           : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
 31579     {
 31580       int stretch_width = temp_it.last_visible_x - temp_it.current_x;
 31581 
 31582       if (stretch_width > 0)
 31583         {
 31584           struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
 31585           struct font *font =
 31586             face->font ? face->font : FRAME_FONT (temp_it.f);
 31587           int stretch_ascent =
 31588             (((temp_it.ascent + temp_it.descent)
 31589               * FONT_BASE (font)) / FONT_HEIGHT (font));
 31590 
 31591           append_stretch_glyph (&temp_it, Qnil, stretch_width,
 31592                                 temp_it.ascent + temp_it.descent,
 31593                                 stretch_ascent);
 31594         }
 31595     }
 31596 #endif
 31597 
 31598   temp_it.dp = NULL;
 31599   temp_it.what = IT_CHARACTER;
 31600   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
 31601   temp_it.face_id = GLYPH_FACE (glyph);
 31602   temp_it.len = CHAR_BYTES (temp_it.c);
 31603 
 31604   PRODUCE_GLYPHS (&temp_it);
 31605   it->pixel_width = temp_it.pixel_width;
 31606   it->nglyphs = temp_it.nglyphs;
 31607 }
 31608 
 31609 #ifdef HAVE_WINDOW_SYSTEM
 31610 
 31611 /* Calculate line-height and line-spacing properties.
 31612    An integer value specifies explicit pixel value.
 31613    A float value specifies relative value to current face height.
 31614    A cons (float . face-name) specifies relative value to
 31615    height of specified face font.
 31616 
 31617    Returns height in pixels, or nil.  */
 31618 
 31619 static Lisp_Object
 31620 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
 31621                            int boff, bool override)
 31622 {
 31623   Lisp_Object face_name = Qnil;
 31624   int ascent, descent, height;
 31625 
 31626   if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
 31627     return val;
 31628 
 31629   if (CONSP (val))
 31630     {
 31631       face_name = XCAR (val);
 31632       val = XCDR (val);
 31633       if (!NUMBERP (val))
 31634         val = make_fixnum (1);
 31635       if (NILP (face_name))
 31636         {
 31637           height = it->ascent + it->descent;
 31638           goto scale;
 31639         }
 31640     }
 31641 
 31642   if (NILP (face_name))
 31643     {
 31644       font = FRAME_FONT (it->f);
 31645       boff = FRAME_BASELINE_OFFSET (it->f);
 31646     }
 31647   else if (EQ (face_name, Qt))
 31648     {
 31649       override = false;
 31650     }
 31651   else
 31652     {
 31653       int face_id;
 31654       struct face *face;
 31655 
 31656       face_id = lookup_named_face (it->w, it->f, face_name, false);
 31657       face = FACE_FROM_ID_OR_NULL (it->f, face_id);
 31658       if (face == NULL || ((font = face->font) == NULL))
 31659         return make_fixnum (-1);
 31660       boff = font->baseline_offset;
 31661       if (font->vertical_centering)
 31662         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 31663     }
 31664 
 31665   normal_char_ascent_descent (font, -1, &ascent, &descent);
 31666 
 31667   if (override)
 31668     {
 31669       it->override_ascent = ascent;
 31670       it->override_descent = descent;
 31671       it->override_boff = boff;
 31672     }
 31673 
 31674   height = ascent + descent;
 31675 
 31676  scale:
 31677   /* FIXME: Check for overflow in multiplication or conversion.  */
 31678   if (FLOATP (val))
 31679     height = (int)(XFLOAT_DATA (val) * height);
 31680   else if (INTEGERP (val))
 31681     {
 31682       intmax_t v;
 31683       if (integer_to_intmax (val, &v))
 31684         height *= v;
 31685     }
 31686 
 31687   return make_fixnum (height);
 31688 }
 31689 
 31690 
 31691 /* Append a glyph for a glyphless character to IT->glyph_row.  FACE_ID
 31692    is a face ID to be used for the glyph.  FOR_NO_FONT is true if
 31693    and only if this is for a character for which no font was found.
 31694 
 31695    If the display method (it->glyphless_method) is
 31696    GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
 31697    length of the acronym or the hexadecimal string, UPPER_XOFF and
 31698    UPPER_YOFF are pixel offsets for the upper part of the string,
 31699    LOWER_XOFF and LOWER_YOFF are for the lower part.
 31700 
 31701    For the other display methods, LEN through LOWER_YOFF are zero.  */
 31702 
 31703 static void
 31704 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
 31705                         short upper_xoff, short upper_yoff,
 31706                         short lower_xoff, short lower_yoff)
 31707 {
 31708   struct glyph *glyph;
 31709   enum glyph_row_area area = it->area;
 31710 
 31711   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31712   if (glyph < it->glyph_row->glyphs[area + 1])
 31713     {
 31714       /* If the glyph row is reversed, we need to prepend the glyph
 31715          rather than append it.  */
 31716       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31717         {
 31718           struct glyph *g;
 31719 
 31720           /* Make room for the additional glyph.  */
 31721           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31722             g[1] = *g;
 31723           glyph = it->glyph_row->glyphs[area];
 31724         }
 31725       glyph->charpos = CHARPOS (it->position);
 31726       glyph->object = it->object;
 31727       eassert (it->pixel_width <= SHRT_MAX);
 31728       glyph->pixel_width = it->pixel_width;
 31729       glyph->ascent = it->ascent;
 31730       glyph->descent = it->descent;
 31731       glyph->voffset = it->voffset;
 31732       glyph->type = GLYPHLESS_GLYPH;
 31733       glyph->u.glyphless.method = it->glyphless_method;
 31734       glyph->u.glyphless.for_no_font = for_no_font;
 31735       glyph->u.glyphless.len = len;
 31736       glyph->u.glyphless.ch = it->c;
 31737       glyph->slice.glyphless.upper_xoff = upper_xoff;
 31738       glyph->slice.glyphless.upper_yoff = upper_yoff;
 31739       glyph->slice.glyphless.lower_xoff = lower_xoff;
 31740       glyph->slice.glyphless.lower_yoff = lower_yoff;
 31741       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31742       glyph->multibyte_p = it->multibyte_p;
 31743       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31744         {
 31745           /* In R2L rows, the left and the right box edges need to be
 31746              drawn in reverse direction.  */
 31747           glyph->right_box_line_p = it->start_of_box_run_p;
 31748           glyph->left_box_line_p = it->end_of_box_run_p;
 31749         }
 31750       else
 31751         {
 31752           glyph->left_box_line_p = it->start_of_box_run_p;
 31753           glyph->right_box_line_p = it->end_of_box_run_p;
 31754         }
 31755       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 31756                                       || it->phys_descent > it->descent);
 31757       glyph->padding_p = false;
 31758       glyph->glyph_not_available_p = false;
 31759       glyph->face_id = face_id;
 31760       glyph->font_type = FONT_TYPE_UNKNOWN;
 31761       if (it->bidi_p)
 31762         {
 31763           glyph->resolved_level = it->bidi_it.resolved_level;
 31764           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31765           glyph->bidi_type = it->bidi_it.type;
 31766         }
 31767       ++it->glyph_row->used[area];
 31768     }
 31769   else
 31770     IT_EXPAND_MATRIX_WIDTH (it, area);
 31771 }
 31772 
 31773 
 31774 /* Produce a glyph for a glyphless character for iterator IT.
 31775    IT->glyphless_method specifies which method to use for displaying
 31776    the character.  See the description of enum
 31777    glyphless_display_method in dispextern.h for the detail.
 31778 
 31779    FOR_NO_FONT is true if and only if this is for a character for
 31780    which no font was found.  ACRONYM, if non-nil, is an acronym string
 31781    for the character.  */
 31782 
 31783 static void
 31784 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
 31785 {
 31786   int face_id;
 31787   struct face *face;
 31788   struct font *font;
 31789   int base_width, base_height, width, height;
 31790   short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
 31791   int len;
 31792 
 31793   /* Get the metrics of the base font.  We always refer to the current
 31794      ASCII face, but if some faulty setup of fontsets causes that to
 31795      be NULL, we fall back to the frame's default font.  */
 31796   face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
 31797   if (face)
 31798     face = face->ascii_face;
 31799   font = (face && face->font) ? face->font : FRAME_FONT (it->f);
 31800   normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 31801   it->ascent += font->baseline_offset;
 31802   it->descent -= font->baseline_offset;
 31803   base_height = it->ascent + it->descent;
 31804   base_width = font->average_width;
 31805 
 31806   face_id = merge_glyphless_glyph_face (it);
 31807 
 31808   if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
 31809     {
 31810       it->pixel_width = THIN_SPACE_WIDTH;
 31811       len = 0;
 31812       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 31813     }
 31814   else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
 31815     {
 31816       width = CHARACTER_WIDTH (it->c);
 31817       if (width == 0)
 31818         width = 1;
 31819       else if (width > 4)
 31820         width = 4;
 31821       it->pixel_width = base_width * width;
 31822       len = 0;
 31823       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 31824     }
 31825   else
 31826     {
 31827       char buf[7];
 31828       const char *str;
 31829       unsigned int code[6];
 31830       int upper_len;
 31831       int ascent, descent;
 31832       struct font_metrics metrics_upper, metrics_lower;
 31833 
 31834       face = FACE_FROM_ID (it->f, face_id);
 31835       font = face->font ? face->font : FRAME_FONT (it->f);
 31836       prepare_face_for_display (it->f, face);
 31837 
 31838       if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
 31839         {
 31840           if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
 31841             acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
 31842           if (CONSP (acronym))
 31843             acronym = XCAR (acronym);
 31844           str = STRINGP (acronym) ? SSDATA (acronym) : "";
 31845         }
 31846       else
 31847         {
 31848           eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
 31849           sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
 31850           str = buf;
 31851         }
 31852       for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
 31853         code[len] = font->driver->encode_char (font, str[len]);
 31854       upper_len = (len + 1) / 2;
 31855       font->driver->text_extents (font, code, upper_len,
 31856                                   &metrics_upper);
 31857       font->driver->text_extents (font, code + upper_len, len - upper_len,
 31858                                   &metrics_lower);
 31859 
 31860 
 31861 
 31862       /* +4 is for vertical bars of a box plus 1-pixel spaces at both side.  */
 31863       width = max (metrics_upper.width, metrics_lower.width) + 4;
 31864       upper_xoff = lower_xoff = 2; /* the typical case */
 31865       if (base_width >= width)
 31866         {
 31867           /* Align the upper to the left, the lower to the right.  */
 31868           it->pixel_width = base_width;
 31869           lower_xoff = base_width - 2 - metrics_lower.width;
 31870         }
 31871       else
 31872         {
 31873           /* Center the shorter one.  */
 31874           it->pixel_width = width;
 31875           if (metrics_upper.width >= metrics_lower.width)
 31876             lower_xoff = (width - metrics_lower.width) / 2;
 31877           else
 31878             upper_xoff = (width - metrics_upper.width) / 2;
 31879         }
 31880 
 31881       /* +5 is for horizontal bars of a box plus 1-pixel spaces at
 31882          top, bottom, and between upper and lower strings.  */
 31883       height = (metrics_upper.ascent + metrics_upper.descent
 31884                 + metrics_lower.ascent + metrics_lower.descent) + 5;
 31885       /* Center vertically.
 31886          H:base_height, D:base_descent
 31887          h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
 31888 
 31889          ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
 31890          descent = D - H/2 + h/2;
 31891          lower_yoff = descent - 2 - ld;
 31892          upper_yoff = lower_yoff - la - 1 - ud;  */
 31893       ascent = - (it->descent - (base_height + height + 1) / 2);
 31894       descent = it->descent - (base_height - height) / 2;
 31895       lower_yoff = descent - 2 - metrics_lower.descent;
 31896       upper_yoff = (lower_yoff - metrics_lower.ascent - 1
 31897                     - metrics_upper.descent);
 31898       /* Don't make the height shorter than the base height.  */
 31899       if (height > base_height)
 31900         {
 31901           it->ascent = ascent;
 31902           it->descent = descent;
 31903         }
 31904     }
 31905 
 31906   it->phys_ascent = it->ascent;
 31907   it->phys_descent = it->descent;
 31908   if (it->glyph_row)
 31909     append_glyphless_glyph (it, face_id, for_no_font, len,
 31910                             upper_xoff, upper_yoff,
 31911                             lower_xoff, lower_yoff);
 31912   it->nglyphs = 1;
 31913   take_vertical_position_into_account (it);
 31914 }
 31915 
 31916 
 31917 /* If face has a box, add the box thickness to the character
 31918    height.  If character has a box line to the left and/or
 31919    right, add the box line width to the character's width.  */
 31920 #define IT_APPLY_FACE_BOX(it, face)                             \
 31921   do {                                                          \
 31922     if (face->box != FACE_NO_BOX)                               \
 31923       {                                                         \
 31924         int thick = face->box_horizontal_line_width;            \
 31925         if (thick > 0)                                          \
 31926           {                                                     \
 31927             it->ascent += thick;                                \
 31928             it->descent += thick;                               \
 31929           }                                                     \
 31930                                                                 \
 31931         thick = face->box_vertical_line_width;                  \
 31932         if (thick > 0)                                          \
 31933           {                                                     \
 31934             if (it->start_of_box_run_p)                         \
 31935               it->pixel_width += thick;                         \
 31936             if (it->end_of_box_run_p)                           \
 31937               it->pixel_width += thick;                         \
 31938           }                                                     \
 31939       }                                                         \
 31940     } while (false)
 31941 
 31942 /* RIF:
 31943    Produce glyphs/get display metrics for the display element IT is
 31944    loaded with.  See the description of struct it in dispextern.h
 31945    for an overview of struct it.  */
 31946 
 31947 void
 31948 gui_produce_glyphs (struct it *it)
 31949 {
 31950   int extra_line_spacing = it->extra_line_spacing;
 31951 
 31952   it->glyph_not_available_p = false;
 31953 
 31954   if (it->what == IT_CHARACTER)
 31955     {
 31956       unsigned char2b;
 31957       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 31958       struct font *font = face->font;
 31959       struct font_metrics *pcm = NULL;
 31960       int boff;                 /* Baseline offset.  */
 31961 
 31962       if (font == NULL)
 31963         {
 31964           /* When no suitable font is found, display this character by
 31965              the method specified in the first extra slot of
 31966              Vglyphless_char_display.  */
 31967           Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
 31968 
 31969           eassert (it->what == IT_GLYPHLESS);
 31970           produce_glyphless_glyph (it, true,
 31971                                    STRINGP (acronym) ? acronym : Qnil);
 31972           goto done;
 31973         }
 31974 
 31975       boff = font->baseline_offset;
 31976       if (font->vertical_centering)
 31977         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 31978 
 31979       if (it->char_to_display != '\n' && it->char_to_display != '\t')
 31980         {
 31981           it->nglyphs = 1;
 31982 
 31983           if (it->override_ascent >= 0)
 31984             {
 31985               it->ascent = it->override_ascent;
 31986               it->descent = it->override_descent;
 31987               boff = it->override_boff;
 31988             }
 31989           else
 31990             {
 31991               it->ascent = FONT_BASE (font) + boff;
 31992               it->descent = FONT_DESCENT (font) - boff;
 31993             }
 31994 
 31995           if (get_char_glyph_code (it->char_to_display, font, &char2b))
 31996             {
 31997               pcm = get_per_char_metric (font, &char2b);
 31998               if (pcm->width == 0
 31999                   && pcm->rbearing == 0 && pcm->lbearing == 0)
 32000                 pcm = NULL;
 32001             }
 32002 
 32003           if (pcm)
 32004             {
 32005               it->phys_ascent = pcm->ascent + boff;
 32006               it->phys_descent = pcm->descent - boff;
 32007               it->pixel_width = pcm->width;
 32008               /* Don't use font-global values for ascent and descent
 32009                  if they result in an exceedingly large line height.  */
 32010               if (it->override_ascent < 0)
 32011                 {
 32012                   if (FONT_TOO_HIGH (font))
 32013                     {
 32014                       it->ascent = it->phys_ascent;
 32015                       it->descent = it->phys_descent;
 32016                       /* These limitations are enforced by an
 32017                          assertion near the end of this function.  */
 32018                       if (it->ascent < 0)
 32019                         it->ascent = 0;
 32020                       if (it->descent < 0)
 32021                         it->descent = 0;
 32022                     }
 32023                 }
 32024             }
 32025           else
 32026             {
 32027               it->glyph_not_available_p = true;
 32028               it->phys_ascent = it->ascent;
 32029               it->phys_descent = it->descent;
 32030               it->pixel_width = font->space_width;
 32031             }
 32032 
 32033           if (it->constrain_row_ascent_descent_p)
 32034             {
 32035               if (it->descent > it->max_descent)
 32036                 {
 32037                   it->ascent += it->descent - it->max_descent;
 32038                   it->descent = it->max_descent;
 32039                 }
 32040               if (it->ascent > it->max_ascent)
 32041                 {
 32042                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32043                   it->ascent = it->max_ascent;
 32044                 }
 32045               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32046               it->phys_descent = min (it->phys_descent, it->descent);
 32047               extra_line_spacing = 0;
 32048             }
 32049 
 32050           /* If this is a space inside a region of text with
 32051              `space-width' property, change its width.  */
 32052           bool stretched_p
 32053             = it->char_to_display == ' ' && !NILP (it->space_width);
 32054           if (stretched_p)
 32055             it->pixel_width *= XFLOATINT (it->space_width);
 32056 
 32057           IT_APPLY_FACE_BOX(it, face);
 32058 
 32059           /* If face has an overline, add the height of the overline
 32060              (1 pixel) and a 1 pixel margin to the character height.  */
 32061           if (face->overline_p)
 32062             it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32063 
 32064           if (it->constrain_row_ascent_descent_p)
 32065             {
 32066               if (it->ascent > it->max_ascent)
 32067                 it->ascent = it->max_ascent;
 32068               if (it->descent > it->max_descent)
 32069                 it->descent = it->max_descent;
 32070             }
 32071 
 32072           take_vertical_position_into_account (it);
 32073 
 32074           /* If we have to actually produce glyphs, do it.  */
 32075           if (it->glyph_row)
 32076             {
 32077               if (stretched_p)
 32078                 {
 32079                   /* Translate a space with a `space-width' property
 32080                      into a stretch glyph.  */
 32081                   int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
 32082                                 / FONT_HEIGHT (font));
 32083                   append_stretch_glyph (it, it->object, it->pixel_width,
 32084                                         it->ascent + it->descent, ascent);
 32085                 }
 32086               else
 32087                 append_glyph (it);
 32088 
 32089               /* If characters with lbearing or rbearing are displayed
 32090                  in this line, record that fact in a flag of the
 32091                  glyph row.  This is used to optimize X output code.  */
 32092               if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
 32093                 it->glyph_row->contains_overlapping_glyphs_p = true;
 32094             }
 32095           if (! stretched_p && it->pixel_width == 0)
 32096             /* We assure that all visible glyphs have at least 1-pixel
 32097                width.  */
 32098             it->pixel_width = 1;
 32099         }
 32100       else if (it->char_to_display == '\n')
 32101         {
 32102           /* A newline has no width, but we need the height of the
 32103              line.  But if previous part of the line sets a height,
 32104              don't increase that height.  */
 32105 
 32106           Lisp_Object height;
 32107           Lisp_Object total_height = Qnil;
 32108 
 32109           it->override_ascent = -1;
 32110           it->pixel_width = 0;
 32111           it->nglyphs = 0;
 32112 
 32113           height = get_it_property (it, Qline_height);
 32114           /* Split (line-height total-height) list.  */
 32115           if (CONSP (height)
 32116               && CONSP (XCDR (height))
 32117               && NILP (XCDR (XCDR (height))))
 32118             {
 32119               total_height = XCAR (XCDR (height));
 32120               height = XCAR (height);
 32121             }
 32122           height = calc_line_height_property (it, height, font, boff, true);
 32123 
 32124           if (it->override_ascent >= 0)
 32125             {
 32126               it->ascent = it->override_ascent;
 32127               it->descent = it->override_descent;
 32128               boff = it->override_boff;
 32129             }
 32130           else
 32131             {
 32132               if (FONT_TOO_HIGH (font))
 32133                 {
 32134                   it->ascent = font->pixel_size + boff - 1;
 32135                   it->descent = -boff + 1;
 32136                   if (it->descent < 0)
 32137                     it->descent = 0;
 32138                 }
 32139               else
 32140                 {
 32141                   it->ascent = FONT_BASE (font) + boff;
 32142                   it->descent = FONT_DESCENT (font) - boff;
 32143                 }
 32144             }
 32145 
 32146           if (EQ (height, Qt))
 32147             {
 32148               if (it->descent > it->max_descent)
 32149                 {
 32150                   it->ascent += it->descent - it->max_descent;
 32151                   it->descent = it->max_descent;
 32152                 }
 32153               if (it->ascent > it->max_ascent)
 32154                 {
 32155                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32156                   it->ascent = it->max_ascent;
 32157                 }
 32158               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32159               it->phys_descent = min (it->phys_descent, it->descent);
 32160               it->constrain_row_ascent_descent_p = true;
 32161               extra_line_spacing = 0;
 32162             }
 32163           else
 32164             {
 32165               Lisp_Object spacing;
 32166 
 32167               it->phys_ascent = it->ascent;
 32168               it->phys_descent = it->descent;
 32169 
 32170               if ((it->max_ascent > 0 || it->max_descent > 0)
 32171                   && face->box != FACE_NO_BOX
 32172                   && face->box_horizontal_line_width > 0)
 32173                 {
 32174                   it->ascent += face->box_horizontal_line_width;
 32175                   it->descent += face->box_horizontal_line_width;
 32176                 }
 32177               if (!NILP (height)
 32178                   && XFIXNUM (height) > it->ascent + it->descent)
 32179                 it->ascent = XFIXNUM (height) - it->descent;
 32180 
 32181               if (!NILP (total_height))
 32182                 spacing = calc_line_height_property (it, total_height, font,
 32183                                                      boff, false);
 32184               else
 32185                 {
 32186                   spacing = get_it_property (it, Qline_spacing);
 32187                   spacing = calc_line_height_property (it, spacing, font,
 32188                                                        boff, false);
 32189                 }
 32190               if (FIXNUMP (spacing))
 32191                 {
 32192                   extra_line_spacing = XFIXNUM (spacing);
 32193                   if (!NILP (total_height))
 32194                     extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 32195                 }
 32196             }
 32197         }
 32198       else                    /* i.e. (it->char_to_display == '\t') */
 32199         {
 32200           if (font->space_width > 0)
 32201             {
 32202               int tab_width = it->tab_width * font->space_width;
 32203               int x = it->current_x + it->continuation_lines_width;
 32204               int x0 = x;
 32205               /* Adjust for line numbers, if needed.   */
 32206               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32207                 {
 32208                   x -= it->lnum_pixel_width;
 32209                   /* Restore the original TAB width, if required.  */
 32210                   if (x + it->stretch_adjust >= it->first_visible_x)
 32211                     x += it->stretch_adjust;
 32212                 }
 32213 
 32214               int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
 32215 
 32216               /* If the distance from the current position to the next tab
 32217                  stop is less than a space character width, use the
 32218                  tab stop after that.  */
 32219               if (next_tab_x - x < font->space_width)
 32220                 next_tab_x += tab_width;
 32221               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32222                 {
 32223                   next_tab_x += it->lnum_pixel_width;
 32224                   /* If the line is hscrolled, and the TAB starts before
 32225                      the first visible pixel, simulate negative row->x.  */
 32226                   if (x < it->first_visible_x)
 32227                     {
 32228                       next_tab_x -= it->first_visible_x - x;
 32229                       it->stretch_adjust = it->first_visible_x - x;
 32230                     }
 32231                   else
 32232                     next_tab_x -= it->stretch_adjust;
 32233                 }
 32234 
 32235               it->pixel_width = next_tab_x - x0;
 32236               it->nglyphs = 1;
 32237               if (FONT_TOO_HIGH (font))
 32238                 {
 32239                   if (get_char_glyph_code (' ', font, &char2b))
 32240                     {
 32241                       pcm = get_per_char_metric (font, &char2b);
 32242                       if (pcm->width == 0
 32243                           && pcm->rbearing == 0 && pcm->lbearing == 0)
 32244                         pcm = NULL;
 32245                     }
 32246 
 32247                   if (pcm)
 32248                     {
 32249                       it->ascent = pcm->ascent + boff;
 32250                       it->descent = pcm->descent - boff;
 32251                     }
 32252                   else
 32253                     {
 32254                       it->ascent = font->pixel_size + boff - 1;
 32255                       it->descent = -boff + 1;
 32256                     }
 32257                   if (it->ascent < 0)
 32258                     it->ascent = 0;
 32259                   if (it->descent < 0)
 32260                     it->descent = 0;
 32261                 }
 32262               else
 32263                 {
 32264                   it->ascent = FONT_BASE (font) + boff;
 32265                   it->descent = FONT_DESCENT (font) - boff;
 32266                 }
 32267               it->phys_ascent = it->ascent;
 32268               it->phys_descent = it->descent;
 32269 
 32270               if (it->glyph_row)
 32271                 {
 32272                   append_stretch_glyph (it, it->object, it->pixel_width,
 32273                                         it->ascent + it->descent, it->ascent);
 32274                 }
 32275             }
 32276           else
 32277             {
 32278               it->pixel_width = 0;
 32279               it->nglyphs = 1;
 32280             }
 32281         }
 32282 
 32283       if (FONT_TOO_HIGH (font))
 32284         {
 32285           int font_ascent, font_descent;
 32286 
 32287           /* For very large fonts, where we ignore the declared font
 32288              dimensions, and go by per-character metrics instead,
 32289              don't let the row ascent and descent values (and the row
 32290              height computed from them) be smaller than the "normal"
 32291              character metrics.  This avoids unpleasant effects
 32292              whereby lines on display would change their height
 32293              depending on which characters are shown.  */
 32294           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32295           it->max_ascent = max (it->max_ascent, font_ascent);
 32296           it->max_descent = max (it->max_descent, font_descent);
 32297         }
 32298 
 32299       if (it->ascent < 0)
 32300         it->ascent = 0;
 32301       if (it->descent < 0)
 32302         it->descent = 0;
 32303     }
 32304   else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
 32305     {
 32306       /* A static composition.
 32307 
 32308          Note: A composition is represented as one glyph in the
 32309          glyph matrix.  There are no padding glyphs.
 32310 
 32311          Important note: pixel_width, ascent, and descent are the
 32312          values of what is drawn by draw_glyphs (i.e. the values of
 32313          the overall glyphs composed).  */
 32314       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32315       int boff;                 /* baseline offset */
 32316       struct composition *cmp = composition_table[it->cmp_it.id];
 32317       int glyph_len = cmp->glyph_len;
 32318       struct font *font = face->font;
 32319 
 32320       it->nglyphs = 1;
 32321 
 32322       /* If we have not yet calculated pixel size data of glyphs of
 32323          the composition for the current face font, calculate them
 32324          now.  Theoretically, we have to check all fonts for the
 32325          glyphs, but that requires much time and memory space.  So,
 32326          here we check only the font of the first glyph.  This may
 32327          lead to incorrect display, but it's very rare, and C-l
 32328          (recenter-top-bottom) can correct the display anyway.  */
 32329       if (! cmp->font || cmp->font != font)
 32330         {
 32331           /* Ascent and descent of the font of the first character
 32332              of this composition (adjusted by baseline offset).
 32333              Ascent and descent of overall glyphs should not be less
 32334              than these, respectively.  */
 32335           int font_ascent, font_descent, font_height;
 32336           /* Bounding box of the overall glyphs.  */
 32337           int leftmost, rightmost, lowest, highest;
 32338           int lbearing, rbearing;
 32339           int i, width, ascent, descent;
 32340           int c;
 32341           unsigned char2b;
 32342           struct font_metrics *pcm;
 32343           ptrdiff_t pos;
 32344 
 32345           eassume (0 < glyph_len); /* See Bug#8512.  */
 32346           do
 32347             c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
 32348           while (c == '\t' && 0 < --glyph_len);
 32349 
 32350           bool right_padded = glyph_len < cmp->glyph_len;
 32351           for (i = 0; i < glyph_len; i++)
 32352             {
 32353               c = COMPOSITION_GLYPH (cmp, i);
 32354               if (c != '\t')
 32355                 break;
 32356               cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32357             }
 32358           bool left_padded = i > 0;
 32359 
 32360           pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
 32361                  : IT_CHARPOS (*it));
 32362           /* If no suitable font is found, use the default font.  */
 32363           bool font_not_found_p = font == NULL;
 32364           if (font_not_found_p)
 32365             {
 32366               face = face->ascii_face;
 32367               font = face->font;
 32368             }
 32369           boff = font->baseline_offset;
 32370           if (font->vertical_centering)
 32371             boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 32372           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32373           font_ascent +=  boff;
 32374           font_descent -= boff;
 32375           font_height = font_ascent + font_descent;
 32376 
 32377           cmp->font = font;
 32378 
 32379           pcm = NULL;
 32380           if (! font_not_found_p)
 32381             {
 32382               get_char_face_and_encoding (it->f, c, it->face_id,
 32383                                           &char2b, false);
 32384               pcm = get_per_char_metric (font, &char2b);
 32385             }
 32386 
 32387           /* Initialize the bounding box.  */
 32388           if (pcm)
 32389             {
 32390               width = cmp->glyph_len > 0 ? pcm->width : 0;
 32391               ascent = pcm->ascent;
 32392               descent = pcm->descent;
 32393               lbearing = pcm->lbearing;
 32394               rbearing = pcm->rbearing;
 32395             }
 32396           else
 32397             {
 32398               width = cmp->glyph_len > 0 ? font->space_width : 0;
 32399               ascent = FONT_BASE (font);
 32400               descent = FONT_DESCENT (font);
 32401               lbearing = 0;
 32402               rbearing = width;
 32403             }
 32404 
 32405           rightmost = width;
 32406           leftmost = 0;
 32407           lowest = - descent + boff;
 32408           highest = ascent + boff;
 32409 
 32410           if (! font_not_found_p
 32411               && font->default_ascent
 32412               && CHAR_TABLE_P (Vuse_default_ascent)
 32413               && !NILP (Faref (Vuse_default_ascent,
 32414                                make_fixnum (it->char_to_display))))
 32415             highest = font->default_ascent + boff;
 32416 
 32417           /* Draw the first glyph at the normal position.  It may be
 32418              shifted to right later if some other glyphs are drawn
 32419              at the left.  */
 32420           cmp->offsets[i * 2] = 0;
 32421           cmp->offsets[i * 2 + 1] = boff;
 32422           cmp->lbearing = lbearing;
 32423           cmp->rbearing = rbearing;
 32424 
 32425           /* Set cmp->offsets for the remaining glyphs.  */
 32426           for (i++; i < glyph_len; i++)
 32427             {
 32428               int left, right, btm, top;
 32429               int ch = COMPOSITION_GLYPH (cmp, i);
 32430               int face_id;
 32431               struct face *this_face;
 32432 
 32433               if (ch == '\t')
 32434                 ch = ' ';
 32435               face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
 32436               this_face = FACE_FROM_ID (it->f, face_id);
 32437               font = this_face->font;
 32438 
 32439               if (font == NULL)
 32440                 pcm = NULL;
 32441               else
 32442                 {
 32443                   get_char_face_and_encoding (it->f, ch, face_id,
 32444                                               &char2b, false);
 32445                   pcm = get_per_char_metric (font, &char2b);
 32446                 }
 32447               if (! pcm)
 32448                 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32449               else
 32450                 {
 32451                   width = pcm->width;
 32452                   ascent = pcm->ascent;
 32453                   descent = pcm->descent;
 32454                   lbearing = pcm->lbearing;
 32455                   rbearing = pcm->rbearing;
 32456                   if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
 32457                     {
 32458                       /* Relative composition with or without
 32459                          alternate chars.  */
 32460                       left = (leftmost + rightmost - width) / 2;
 32461                       btm = - descent + boff;
 32462                       if (font->relative_compose
 32463                           && (! CHAR_TABLE_P (Vignore_relative_composition)
 32464                               || NILP (Faref (Vignore_relative_composition,
 32465                                               make_fixnum (ch)))))
 32466                         {
 32467 
 32468                           if (- descent >= font->relative_compose)
 32469                             /* One extra pixel between two glyphs.  */
 32470                             btm = highest + 1;
 32471                           else if (ascent <= 0)
 32472                             /* One extra pixel between two glyphs.  */
 32473                             btm = lowest - 1 - ascent - descent;
 32474                         }
 32475                     }
 32476                   else
 32477                     {
 32478                       /* A composition rule is specified by an integer
 32479                          value that encodes global and new reference
 32480                          points (GREF and NREF).  GREF and NREF are
 32481                          specified by numbers as below:
 32482 
 32483                          0---1---2 -- ascent
 32484                          |       |
 32485                          |       |
 32486                          |       |
 32487                          9--10--11 -- center
 32488                          |       |
 32489                          ---3---4---5--- baseline
 32490                          |       |
 32491                          6---7---8 -- descent
 32492                       */
 32493                       int rule = COMPOSITION_RULE (cmp, i);
 32494                       int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
 32495 
 32496                       COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
 32497                       grefx = gref % 3, nrefx = nref % 3;
 32498                       grefy = gref / 3, nrefy = nref / 3;
 32499                       if (xoff)
 32500                         xoff = font_height * (xoff - 128) / 256;
 32501                       if (yoff)
 32502                         yoff = font_height * (yoff - 128) / 256;
 32503 
 32504                       left = (leftmost
 32505                               + grefx * (rightmost - leftmost) / 2
 32506                               - nrefx * width / 2
 32507                               + xoff);
 32508 
 32509                       btm = ((grefy == 0 ? highest
 32510                               : grefy == 1 ? 0
 32511                               : grefy == 2 ? lowest
 32512                               : (highest + lowest) / 2)
 32513                              - (nrefy == 0 ? ascent + descent
 32514                                 : nrefy == 1 ? descent - boff
 32515                                 : nrefy == 2 ? 0
 32516                                 : (ascent + descent) / 2)
 32517                              + yoff);
 32518                     }
 32519 
 32520                   cmp->offsets[i * 2] = left;
 32521                   cmp->offsets[i * 2 + 1] = btm + descent;
 32522 
 32523                   /* Update the bounding box of the overall glyphs. */
 32524                   if (width > 0)
 32525                     {
 32526                       right = left + width;
 32527                       if (left < leftmost)
 32528                         leftmost = left;
 32529                       if (right > rightmost)
 32530                         rightmost = right;
 32531                     }
 32532                   top = btm + descent + ascent;
 32533                   if (top > highest)
 32534                     highest = top;
 32535                   if (btm < lowest)
 32536                     lowest = btm;
 32537 
 32538                   if (cmp->lbearing > left + lbearing)
 32539                     cmp->lbearing = left + lbearing;
 32540                   if (cmp->rbearing < left + rbearing)
 32541                     cmp->rbearing = left + rbearing;
 32542                 }
 32543             }
 32544 
 32545           /* If there are glyphs whose x-offsets are negative,
 32546              shift all glyphs to the right and make all x-offsets
 32547              non-negative.  */
 32548           if (leftmost < 0)
 32549             {
 32550               for (i = 0; i < cmp->glyph_len; i++)
 32551                 cmp->offsets[i * 2] -= leftmost;
 32552               rightmost -= leftmost;
 32553               cmp->lbearing -= leftmost;
 32554               cmp->rbearing -= leftmost;
 32555             }
 32556 
 32557           if (left_padded && cmp->lbearing < 0)
 32558             {
 32559               for (i = 0; i < cmp->glyph_len; i++)
 32560                 cmp->offsets[i * 2] -= cmp->lbearing;
 32561               rightmost -= cmp->lbearing;
 32562               cmp->rbearing -= cmp->lbearing;
 32563               cmp->lbearing = 0;
 32564             }
 32565           if (right_padded && rightmost < cmp->rbearing)
 32566             {
 32567               rightmost = cmp->rbearing;
 32568             }
 32569 
 32570           cmp->pixel_width = rightmost;
 32571           cmp->ascent = highest;
 32572           cmp->descent = - lowest;
 32573           if (cmp->ascent < font_ascent)
 32574             cmp->ascent = font_ascent;
 32575           if (cmp->descent < font_descent)
 32576             cmp->descent = font_descent;
 32577         }
 32578 
 32579       if (it->glyph_row
 32580           && (cmp->lbearing < 0
 32581               || cmp->rbearing > cmp->pixel_width))
 32582         it->glyph_row->contains_overlapping_glyphs_p = true;
 32583 
 32584       it->pixel_width = cmp->pixel_width;
 32585       it->ascent = it->phys_ascent = cmp->ascent;
 32586       it->descent = it->phys_descent = cmp->descent;
 32587       IT_APPLY_FACE_BOX(it, face);
 32588 
 32589       /* If face has an overline, add the height of the overline
 32590          (1 pixel) and a 1 pixel margin to the character height.  */
 32591       if (face->overline_p)
 32592         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32593 
 32594       take_vertical_position_into_account (it);
 32595       if (it->ascent < 0)
 32596         it->ascent = 0;
 32597       if (it->descent < 0)
 32598         it->descent = 0;
 32599 
 32600       if (it->glyph_row && cmp->glyph_len > 0)
 32601         append_composite_glyph (it);
 32602     }
 32603   else if (it->what == IT_COMPOSITION)
 32604     {
 32605       /* A dynamic (automatic) composition.  */
 32606       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32607       Lisp_Object gstring;
 32608       struct font_metrics metrics;
 32609 
 32610       it->nglyphs = 1;
 32611 
 32612       gstring = composition_gstring_from_id (it->cmp_it.id);
 32613       it->pixel_width
 32614         = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
 32615                                      &metrics);
 32616       if (it->pixel_width == 0)
 32617         {
 32618           it->glyph_not_available_p = true;
 32619           it->phys_ascent = it->ascent;
 32620           it->phys_descent = it->descent;
 32621           it->pixel_width = face->font->space_width;
 32622         }
 32623       else
 32624         {
 32625           if (it->glyph_row
 32626               && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
 32627             it->glyph_row->contains_overlapping_glyphs_p = true;
 32628           it->ascent = it->phys_ascent = metrics.ascent;
 32629           it->descent = it->phys_descent = metrics.descent;
 32630         }
 32631       IT_APPLY_FACE_BOX(it, face);
 32632 
 32633       /* If face has an overline, add the height of the overline
 32634          (1 pixel) and a 1 pixel margin to the character height.  */
 32635       if (face->overline_p)
 32636         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32637       take_vertical_position_into_account (it);
 32638       if (it->ascent < 0)
 32639         it->ascent = 0;
 32640       if (it->descent < 0)
 32641         it->descent = 0;
 32642 
 32643       if (it->glyph_row)
 32644         append_composite_glyph (it);
 32645     }
 32646   else if (it->what == IT_GLYPHLESS)
 32647     produce_glyphless_glyph (it, false, Qnil);
 32648   else if (it->what == IT_IMAGE)
 32649     produce_image_glyph (it);
 32650   else if (it->what == IT_STRETCH)
 32651     produce_stretch_glyph (it);
 32652   else if (it->what == IT_XWIDGET)
 32653     produce_xwidget_glyph (it);
 32654 
 32655  done:
 32656   /* Accumulate dimensions.  Note: can't assume that it->descent > 0
 32657      because this isn't true for images with `:ascent 100'.  */
 32658   eassert (it->ascent >= 0 && it->descent >= 0);
 32659   if (it->area == TEXT_AREA)
 32660     it->current_x += it->pixel_width;
 32661 
 32662   if (extra_line_spacing > 0)
 32663     {
 32664       it->descent += extra_line_spacing;
 32665       if (extra_line_spacing > it->max_extra_line_spacing)
 32666         it->max_extra_line_spacing = extra_line_spacing;
 32667     }
 32668 
 32669   it->max_ascent = max (it->max_ascent, it->ascent);
 32670   it->max_descent = max (it->max_descent, it->descent);
 32671   it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
 32672   it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
 32673 }
 32674 
 32675 /* EXPORT for RIF:
 32676    Output LEN glyphs starting at START at the nominal cursor position.
 32677    Advance the nominal cursor over the text.  UPDATED_ROW is the glyph row
 32678    being updated, and UPDATED_AREA is the area of that row being updated.  */
 32679 
 32680 void
 32681 gui_write_glyphs (struct window *w, struct glyph_row *updated_row,
 32682                 struct glyph *start, enum glyph_row_area updated_area, int len)
 32683 {
 32684   int x, hpos, chpos = w->phys_cursor.hpos;
 32685 
 32686   eassert (updated_row);
 32687   /* When the window is hscrolled, cursor hpos can legitimately be out
 32688      of bounds, but we draw the cursor at the corresponding window
 32689      margin in that case.  */
 32690   if (!updated_row->reversed_p && chpos < 0)
 32691     chpos = 0;
 32692   if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
 32693     chpos = updated_row->used[TEXT_AREA] - 1;
 32694 
 32695   block_input ();
 32696 
 32697   /* Write glyphs.  */
 32698 
 32699   hpos = start - updated_row->glyphs[updated_area];
 32700   x = draw_glyphs (w, w->output_cursor.x,
 32701                    updated_row, updated_area,
 32702                    hpos, hpos + len,
 32703                    DRAW_NORMAL_TEXT, 0);
 32704 
 32705   /* Invalidate old phys cursor if the glyph at its hpos is redrawn.  */
 32706   if (updated_area == TEXT_AREA
 32707       && w->phys_cursor_on_p
 32708       && w->phys_cursor.vpos == w->output_cursor.vpos
 32709       && chpos >= hpos
 32710       && chpos < hpos + len)
 32711     w->phys_cursor_on_p = false;
 32712 
 32713   unblock_input ();
 32714 
 32715   /* Advance the output cursor.  */
 32716   w->output_cursor.hpos += len;
 32717   w->output_cursor.x = x;
 32718 }
 32719 
 32720 
 32721 /* EXPORT for RIF:
 32722    Insert LEN glyphs from START at the nominal cursor position.  */
 32723 
 32724 void
 32725 gui_insert_glyphs (struct window *w, struct glyph_row *updated_row,
 32726                  struct glyph *start, enum glyph_row_area updated_area, int len)
 32727 {
 32728   struct frame *f;
 32729   int line_height, shift_by_width, shifted_region_width;
 32730   struct glyph_row *row;
 32731   struct glyph *glyph;
 32732   int frame_x, frame_y;
 32733   ptrdiff_t hpos;
 32734 
 32735   eassert (updated_row);
 32736   block_input ();
 32737   f = XFRAME (WINDOW_FRAME (w));
 32738 
 32739   /* Get the height of the line we are in.  */
 32740   row = updated_row;
 32741   line_height = row->height;
 32742 
 32743   /* Get the width of the glyphs to insert.  */
 32744   shift_by_width = 0;
 32745   for (glyph = start; glyph < start + len; ++glyph)
 32746     shift_by_width += glyph->pixel_width;
 32747 
 32748   /* Get the width of the region to shift right.  */
 32749   shifted_region_width = (window_box_width (w, updated_area)
 32750                           - w->output_cursor.x
 32751                           - shift_by_width);
 32752 
 32753   /* Shift right.  */
 32754   frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
 32755   frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
 32756 
 32757   FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
 32758                                           line_height, shift_by_width);
 32759 
 32760   /* Write the glyphs.  */
 32761   hpos = start - row->glyphs[updated_area];
 32762   draw_glyphs (w, w->output_cursor.x, row, updated_area,
 32763                hpos, hpos + len,
 32764                DRAW_NORMAL_TEXT, 0);
 32765 
 32766   /* Advance the output cursor.  */
 32767   w->output_cursor.hpos += len;
 32768   w->output_cursor.x += shift_by_width;
 32769   unblock_input ();
 32770 }
 32771 
 32772 
 32773 /* EXPORT for RIF:
 32774    Erase the current text line from the nominal cursor position
 32775    (inclusive) to pixel column TO_X (exclusive).  The idea is that
 32776    everything from TO_X onward is already erased.
 32777 
 32778    TO_X is a pixel position relative to UPDATED_AREA of currently
 32779    updated window W.  TO_X == -1 means clear to the end of this area.  */
 32780 
 32781 void
 32782 gui_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
 32783                        enum glyph_row_area updated_area, int to_x)
 32784 {
 32785   struct frame *f;
 32786   int max_x, min_y, max_y;
 32787   int from_x, from_y, to_y;
 32788   struct face *face;
 32789 
 32790   eassert (updated_row);
 32791   f = XFRAME (w->frame);
 32792   face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
 32793 
 32794   if (updated_row->full_width_p)
 32795     max_x = (WINDOW_PIXEL_WIDTH (w)
 32796              - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 32797   else
 32798     max_x = window_box_width (w, updated_area);
 32799   max_y = window_text_bottom_y (w);
 32800 
 32801   /* TO_X == 0 means don't do anything.  TO_X < 0 means clear to end
 32802      of window.  For TO_X > 0, truncate to end of drawing area.  */
 32803   if (to_x == 0)
 32804     return;
 32805   else if (to_x < 0)
 32806     to_x = max_x;
 32807   else
 32808     to_x = min (to_x, max_x);
 32809 
 32810   to_y = min (max_y, w->output_cursor.y + updated_row->height);
 32811 
 32812   /* Notice if the cursor will be cleared by this operation.  */
 32813   if (!updated_row->full_width_p)
 32814     notice_overwritten_cursor (w, updated_area,
 32815                                w->output_cursor.x, -1,
 32816                                updated_row->y,
 32817                                MATRIX_ROW_BOTTOM_Y (updated_row));
 32818 
 32819   from_x = w->output_cursor.x;
 32820 
 32821   /* Translate to frame coordinates.  */
 32822   if (updated_row->full_width_p)
 32823     {
 32824       from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
 32825       to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
 32826     }
 32827   else
 32828     {
 32829       int area_left = window_box_left (w, updated_area);
 32830       from_x += area_left;
 32831       to_x += area_left;
 32832     }
 32833 
 32834   min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 32835   from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
 32836   to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
 32837 
 32838   /* Prevent inadvertently clearing to end of the X window.  */
 32839   if (to_x > from_x && to_y > from_y)
 32840     {
 32841       block_input ();
 32842       FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
 32843                                        to_x - from_x, to_y - from_y);
 32844 
 32845       if (face && !updated_row->stipple_p)
 32846         updated_row->stipple_p = face->stipple;
 32847       unblock_input ();
 32848     }
 32849 }
 32850 
 32851 #endif /* HAVE_WINDOW_SYSTEM */
 32852 
 32853 
 32854 
 32855 /***********************************************************************
 32856                              Cursor types
 32857  ***********************************************************************/
 32858 
 32859 /* Value is the internal representation of the specified cursor type
 32860    ARG.  If type is BAR_CURSOR, return in *WIDTH the specified width
 32861    of the bar cursor.  */
 32862 
 32863 static enum text_cursor_kinds
 32864 get_specified_cursor_type (Lisp_Object arg, int *width)
 32865 {
 32866   enum text_cursor_kinds type;
 32867 
 32868   if (NILP (arg))
 32869     return NO_CURSOR;
 32870 
 32871   if (EQ (arg, Qbox))
 32872     return FILLED_BOX_CURSOR;
 32873 
 32874   if (EQ (arg, Qhollow))
 32875     return HOLLOW_BOX_CURSOR;
 32876 
 32877   if (EQ (arg, Qbar))
 32878     {
 32879       *width = 2;
 32880       return BAR_CURSOR;
 32881     }
 32882 
 32883   if (EQ (arg, Qhbar))
 32884     {
 32885       *width = 2;
 32886       return HBAR_CURSOR;
 32887     }
 32888 
 32889   if (CONSP (arg)
 32890       && RANGED_FIXNUMP (0, XCDR (arg), INT_MAX))
 32891     {
 32892       *width = XFIXNUM (XCDR (arg));
 32893 
 32894       if (EQ (XCAR (arg), Qbox))
 32895           return FILLED_BOX_CURSOR;
 32896       else if (EQ (XCAR (arg), Qbar))
 32897           return BAR_CURSOR;
 32898       else if (EQ (XCAR (arg), Qhbar))
 32899           return HBAR_CURSOR;
 32900     }
 32901 
 32902   /* Treat anything unknown as "hollow box cursor".
 32903      It was bad to signal an error; people have trouble fixing
 32904      .Xdefaults with Emacs, when it has something bad in it.  */
 32905   type = HOLLOW_BOX_CURSOR;
 32906 
 32907   return type;
 32908 }
 32909 
 32910 /* Set the default cursor types for specified frame.  */
 32911 void
 32912 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
 32913 {
 32914   int width = 1;
 32915   Lisp_Object tem;
 32916 
 32917   FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
 32918   FRAME_CURSOR_WIDTH (f) = width;
 32919 
 32920   /* By default, set up the blink-off state depending on the on-state.  */
 32921 
 32922   tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
 32923   if (!NILP (tem))
 32924     {
 32925       FRAME_BLINK_OFF_CURSOR (f)
 32926         = get_specified_cursor_type (XCDR (tem), &width);
 32927       FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
 32928     }
 32929   else
 32930     FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
 32931 
 32932   /* Make sure the cursor gets redrawn.  */
 32933   f->cursor_type_changed = true;
 32934 }
 32935 
 32936 
 32937 #ifdef HAVE_WINDOW_SYSTEM
 32938 
 32939 /* Return the cursor we want to be displayed in window W.  Return
 32940    width of bar/hbar cursor through WIDTH arg.  Return with
 32941    ACTIVE_CURSOR arg set to true if cursor in window W is `active'
 32942    (i.e. if the `system caret' should track this cursor).
 32943 
 32944    In a mini-buffer window, we want the cursor only to appear if we
 32945    are reading input from this window.  For the selected window, we
 32946    want the cursor type given by the frame parameter or buffer local
 32947    setting of cursor-type.  If explicitly marked off, draw no cursor.
 32948    In all other cases, we want a hollow box cursor.  */
 32949 
 32950 static enum text_cursor_kinds
 32951 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
 32952                         bool *active_cursor)
 32953 {
 32954   struct frame *f = XFRAME (w->frame);
 32955   struct buffer *b = XBUFFER (w->contents);
 32956   int cursor_type = DEFAULT_CURSOR;
 32957   Lisp_Object alt_cursor;
 32958   bool non_selected = false;
 32959 
 32960   *active_cursor = true;
 32961 
 32962   /* Echo area */
 32963   if (cursor_in_echo_area
 32964       && FRAME_HAS_MINIBUF_P (f)
 32965       && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
 32966     {
 32967       if (w == XWINDOW (echo_area_window))
 32968         {
 32969           if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
 32970             {
 32971               *width = FRAME_CURSOR_WIDTH (f);
 32972               return FRAME_DESIRED_CURSOR (f);
 32973             }
 32974           else
 32975             return get_specified_cursor_type (BVAR (b, cursor_type), width);
 32976         }
 32977 
 32978       *active_cursor = false;
 32979       non_selected = true;
 32980     }
 32981 
 32982   /* Detect a nonselected window or nonselected frame.  */
 32983   else if (w != XWINDOW (f->selected_window)
 32984            || f != FRAME_DISPLAY_INFO (f)->highlight_frame)
 32985     {
 32986       *active_cursor = false;
 32987 
 32988       if (MINI_WINDOW_P (w) &&
 32989           (minibuf_level == 0
 32990            || is_minibuffer (0, w->contents)))
 32991         return NO_CURSOR;
 32992 
 32993       non_selected = true;
 32994     }
 32995 
 32996   /* Never display a cursor in a window in which cursor-type is nil.  */
 32997   if (NILP (BVAR (b, cursor_type)))
 32998     return NO_CURSOR;
 32999 
 33000   /* Get the normal cursor type for this window.  */
 33001   if (EQ (BVAR (b, cursor_type), Qt))
 33002     {
 33003       cursor_type = FRAME_DESIRED_CURSOR (f);
 33004       *width = FRAME_CURSOR_WIDTH (f);
 33005     }
 33006   else
 33007     cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
 33008 
 33009   /* Use cursor-in-non-selected-windows instead
 33010      for non-selected window or frame.  */
 33011   if (non_selected)
 33012     {
 33013       alt_cursor = BVAR (b, cursor_in_non_selected_windows);
 33014       if (!EQ (Qt, alt_cursor))
 33015         return get_specified_cursor_type (alt_cursor, width);
 33016       /* t means modify the normal cursor type.  */
 33017       if (cursor_type == FILLED_BOX_CURSOR)
 33018         cursor_type = HOLLOW_BOX_CURSOR;
 33019       else if (cursor_type == BAR_CURSOR && *width > 1)
 33020         --*width;
 33021       return cursor_type;
 33022     }
 33023 
 33024   /* Use normal cursor if not blinked off.  */
 33025   if (!w->cursor_off_p)
 33026     {
 33027       if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
 33028         return NO_CURSOR;
 33029       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 33030         {
 33031           if (cursor_type == FILLED_BOX_CURSOR)
 33032             {
 33033               /* Using a block cursor on large images can be very
 33034                  annoying.  So use a hollow cursor for "large" images.
 33035                  If image is not transparent (no mask), also use
 33036                  hollow cursor.  */
 33037               struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 33038               if (img != NULL && IMAGEP (img->spec))
 33039                 {
 33040                   /* Interpret "large" as >SIZExSIZE and >NxN where
 33041                      SIZE is the value from cursor-type of the form
 33042                      (box . SIZE), where N = size of default frame
 33043                      font size.  So, setting cursor-type to (box . 32)
 33044                      should cover most of the "tiny" icons people may
 33045                      use.  */
 33046                   if (!img->mask
 33047                       || (CONSP (BVAR (b, cursor_type))
 33048                           && img->width > max (*width, WINDOW_FRAME_COLUMN_WIDTH (w))
 33049                           && img->height > max (*width, WINDOW_FRAME_LINE_HEIGHT (w))))
 33050                     cursor_type = HOLLOW_BOX_CURSOR;
 33051                 }
 33052             }
 33053           else if (cursor_type != NO_CURSOR)
 33054             {
 33055               /* Display current only supports BOX and HOLLOW cursors for images.
 33056                  So for now, unconditionally use a HOLLOW cursor when cursor is
 33057                  not a solid box cursor.  */
 33058               cursor_type = HOLLOW_BOX_CURSOR;
 33059             }
 33060       }
 33061       return cursor_type;
 33062     }
 33063 
 33064   /* Cursor is blinked off, so determine how to "toggle" it.  */
 33065 
 33066   /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist.  */
 33067   if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
 33068     return get_specified_cursor_type (XCDR (alt_cursor), width);
 33069 
 33070   /* Then see if frame has specified a specific blink off cursor type.  */
 33071   if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
 33072     {
 33073       *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
 33074       return FRAME_BLINK_OFF_CURSOR (f);
 33075     }
 33076 
 33077 #if false
 33078   /* Some people liked having a permanently visible blinking cursor,
 33079      while others had very strong opinions against it.  So it was
 33080      decided to remove it.  KFS 2003-09-03 */
 33081 
 33082   /* Finally perform built-in cursor blinking:
 33083        filled box      <->   hollow box
 33084        wide [h]bar     <->   narrow [h]bar
 33085        narrow [h]bar   <->   no cursor
 33086        other type      <->   no cursor  */
 33087 
 33088   if (cursor_type == FILLED_BOX_CURSOR)
 33089     return HOLLOW_BOX_CURSOR;
 33090 
 33091   if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
 33092     {
 33093       *width = 1;
 33094       return cursor_type;
 33095     }
 33096 #endif
 33097 
 33098   return NO_CURSOR;
 33099 }
 33100 
 33101 
 33102 /* Notice when the text cursor of window W has been completely
 33103    overwritten by a drawing operation that outputs glyphs in AREA
 33104    starting at X0 and ending at X1 in the line starting at Y0 and
 33105    ending at Y1.  X coordinates are area-relative.  X1 < 0 means all
 33106    the rest of the line after X0 has been written.  Y coordinates
 33107    are window-relative.  */
 33108 
 33109 static void
 33110 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
 33111                            int x0, int x1, int y0, int y1)
 33112 {
 33113   int cx0, cx1, cy0, cy1;
 33114   struct glyph_row *row;
 33115 
 33116   if (!w->phys_cursor_on_p)
 33117     return;
 33118   if (area != TEXT_AREA)
 33119     return;
 33120 
 33121   if (w->phys_cursor.vpos < 0
 33122       || w->phys_cursor.vpos >= w->current_matrix->nrows
 33123       || (row = w->current_matrix->rows + w->phys_cursor.vpos,
 33124           !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
 33125     return;
 33126 
 33127   if (row->cursor_in_fringe_p)
 33128     {
 33129       row->cursor_in_fringe_p = false;
 33130       draw_fringe_bitmap (w, row, row->reversed_p);
 33131       w->phys_cursor_on_p = false;
 33132       return;
 33133     }
 33134 
 33135   cx0 = w->phys_cursor.x;
 33136   cx1 = cx0 + w->phys_cursor_width;
 33137   if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
 33138     return;
 33139 
 33140   /* The cursor image will be completely removed from the
 33141      screen if the output area intersects the cursor area in
 33142      y-direction.  When we draw in [y0 y1[, and some part of
 33143      the cursor is at y < y0, that part must have been drawn
 33144      before.  When scrolling, the cursor is erased before
 33145      actually scrolling, so we don't come here.  When not
 33146      scrolling, the rows above the old cursor row must have
 33147      changed, and in this case these rows must have written
 33148      over the cursor image.
 33149 
 33150      Likewise if part of the cursor is below y1, with the
 33151      exception of the cursor being in the first blank row at
 33152      the buffer and window end because update_text_area
 33153      doesn't draw that row.  (Except when it does, but
 33154      that's handled in update_text_area.)  */
 33155 
 33156   cy0 = w->phys_cursor.y;
 33157   cy1 = cy0 + w->phys_cursor_height;
 33158   if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
 33159     return;
 33160 
 33161   w->phys_cursor_on_p = false;
 33162 }
 33163 
 33164 #endif /* HAVE_WINDOW_SYSTEM */
 33165 
 33166 
 33167 /************************************************************************
 33168                               Mouse Face
 33169  ************************************************************************/
 33170 
 33171 #ifdef HAVE_WINDOW_SYSTEM
 33172 
 33173 /* EXPORT for RIF:
 33174    Fix the display of area AREA of overlapping row ROW in window W
 33175    with respect to the overlapping part OVERLAPS.  */
 33176 
 33177 void
 33178 gui_fix_overlapping_area (struct window *w, struct glyph_row *row,
 33179                         enum glyph_row_area area, int overlaps)
 33180 {
 33181   int i, x;
 33182 
 33183   block_input ();
 33184 
 33185   x = 0;
 33186   for (i = 0; i < row->used[area];)
 33187     {
 33188       if (row->glyphs[area][i].overlaps_vertically_p)
 33189         {
 33190           int start = i, start_x = x;
 33191 
 33192           do
 33193             {
 33194               x += row->glyphs[area][i].pixel_width;
 33195               ++i;
 33196             }
 33197           while (i < row->used[area]
 33198                  && row->glyphs[area][i].overlaps_vertically_p);
 33199 
 33200           draw_glyphs (w, start_x, row, area,
 33201                        start, i,
 33202                        DRAW_NORMAL_TEXT, overlaps);
 33203         }
 33204       else
 33205         {
 33206           x += row->glyphs[area][i].pixel_width;
 33207           ++i;
 33208         }
 33209     }
 33210 
 33211   unblock_input ();
 33212 }
 33213 
 33214 
 33215 /* EXPORT:
 33216    Draw the cursor glyph of window W in glyph row ROW.  See the
 33217    comment of draw_glyphs for the meaning of HL.  */
 33218 
 33219 void
 33220 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
 33221                         enum draw_glyphs_face hl)
 33222 {
 33223   /* If cursor hpos is out of bounds, don't draw garbage.  This can
 33224      happen in mini-buffer windows when switching between echo area
 33225      glyphs and mini-buffer.  */
 33226   if ((row->reversed_p
 33227        ? (w->phys_cursor.hpos >= 0)
 33228        : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
 33229     {
 33230       bool on_p = w->phys_cursor_on_p;
 33231       int x1;
 33232       int hpos = w->phys_cursor.hpos;
 33233 
 33234       /* When the window is hscrolled, cursor hpos can legitimately be
 33235          out of bounds, but we draw the cursor at the corresponding
 33236          window margin in that case.  */
 33237       if (!row->reversed_p && hpos < 0)
 33238         hpos = 0;
 33239       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33240         hpos = row->used[TEXT_AREA] - 1;
 33241 
 33242       x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
 33243                         hl, 0);
 33244       w->phys_cursor_on_p = on_p;
 33245 
 33246       if (hl == DRAW_CURSOR)
 33247         w->phys_cursor_width = x1 - w->phys_cursor.x;
 33248       /* When we erase the cursor, and ROW is overlapped by other
 33249          rows, make sure that these overlapping parts of other rows
 33250          are redrawn.  */
 33251       else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
 33252         {
 33253           w->phys_cursor_width = x1 - w->phys_cursor.x;
 33254 
 33255           if (row > w->current_matrix->rows
 33256               && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
 33257             gui_fix_overlapping_area (w, row - 1, TEXT_AREA,
 33258                                     OVERLAPS_ERASED_CURSOR);
 33259 
 33260           if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
 33261               && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
 33262             gui_fix_overlapping_area (w, row + 1, TEXT_AREA,
 33263                                     OVERLAPS_ERASED_CURSOR);
 33264         }
 33265     }
 33266 }
 33267 
 33268 
 33269 /* Erase the image of a cursor of window W from the screen.  */
 33270 
 33271 void
 33272 erase_phys_cursor (struct window *w)
 33273 {
 33274   struct frame *f = XFRAME (w->frame);
 33275   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 33276   int hpos = w->phys_cursor.hpos;
 33277   int vpos = w->phys_cursor.vpos;
 33278   bool mouse_face_here_p = false;
 33279   struct glyph_matrix *active_glyphs = w->current_matrix;
 33280   struct glyph_row *cursor_row;
 33281   struct glyph *cursor_glyph;
 33282   enum draw_glyphs_face hl;
 33283 
 33284   /* No cursor displayed or row invalidated => nothing to do on the
 33285      screen.  */
 33286   if (w->phys_cursor_type == NO_CURSOR)
 33287     goto mark_cursor_off;
 33288 
 33289   /* VPOS >= active_glyphs->nrows means that window has been resized.
 33290      Don't bother to erase the cursor.  */
 33291   if (vpos >= active_glyphs->nrows)
 33292     goto mark_cursor_off;
 33293 
 33294   /* If row containing cursor is marked invalid, there is nothing we
 33295      can do.  */
 33296   cursor_row = MATRIX_ROW (active_glyphs, vpos);
 33297   if (!cursor_row->enabled_p)
 33298     goto mark_cursor_off;
 33299 
 33300   /* If line spacing is > 0, old cursor may only be partially visible in
 33301      window after split-window.  So adjust visible height.  */
 33302   cursor_row->visible_height = min (cursor_row->visible_height,
 33303                                     window_text_bottom_y (w) - cursor_row->y);
 33304 
 33305   /* If row is completely invisible, don't attempt to delete a cursor which
 33306      isn't there.  This can happen if cursor is at top of a window, and
 33307      we switch to a buffer with a header line in that window.  */
 33308   if (cursor_row->visible_height <= 0)
 33309     goto mark_cursor_off;
 33310 
 33311   /* If cursor is in the fringe, erase by drawing actual bitmap there.  */
 33312   if (cursor_row->cursor_in_fringe_p)
 33313     {
 33314       cursor_row->cursor_in_fringe_p = false;
 33315       draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
 33316       goto mark_cursor_off;
 33317     }
 33318 
 33319   /* This can happen when the new row is shorter than the old one.
 33320      In this case, either draw_glyphs or clear_end_of_line
 33321      should have cleared the cursor.  Note that we wouldn't be
 33322      able to erase the cursor in this case because we don't have a
 33323      cursor glyph at hand.  */
 33324   if ((cursor_row->reversed_p
 33325        ? (w->phys_cursor.hpos < 0)
 33326        : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
 33327     goto mark_cursor_off;
 33328 
 33329   /* When the window is hscrolled, cursor hpos can legitimately be out
 33330      of bounds, but we draw the cursor at the corresponding window
 33331      margin in that case.  */
 33332   if (!cursor_row->reversed_p && hpos < 0)
 33333     hpos = 0;
 33334   if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
 33335     hpos = cursor_row->used[TEXT_AREA] - 1;
 33336 
 33337   /* If the cursor is in the mouse face area, redisplay that when
 33338      we clear the cursor.  */
 33339   if (! NILP (hlinfo->mouse_face_window)
 33340       && coords_in_mouse_face_p (w, hpos, vpos)
 33341       /* Don't redraw the cursor's spot in mouse face if it is at the
 33342          end of a line (on a newline).  The cursor appears there, but
 33343          mouse highlighting does not.  */
 33344       && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
 33345     mouse_face_here_p = true;
 33346 
 33347 #ifdef HAVE_WINDOW_SYSTEM
 33348   /* Since erasing the phys cursor will probably lead to corruption of
 33349      the mouse face display if the glyph's pixel_width is not kept up
 33350      to date with the :box property of the mouse face, just redraw the
 33351      mouse face.  */
 33352   if (FRAME_WINDOW_P (WINDOW_XFRAME (w)) && mouse_face_here_p)
 33353     {
 33354       w->phys_cursor_on_p = false;
 33355       w->phys_cursor_type = NO_CURSOR;
 33356       show_mouse_face (MOUSE_HL_INFO (WINDOW_XFRAME (w)), DRAW_MOUSE_FACE);
 33357       return;
 33358     }
 33359 #endif
 33360 
 33361   /* Maybe clear the display under the cursor.  */
 33362   if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
 33363     {
 33364       int x, y;
 33365       int tab_line_height = WINDOW_TAB_LINE_HEIGHT (w);
 33366       int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
 33367       int width;
 33368 
 33369       cursor_glyph = get_phys_cursor_glyph (w);
 33370       if (cursor_glyph == NULL)
 33371         goto mark_cursor_off;
 33372 
 33373       width = cursor_glyph->pixel_width;
 33374       x = w->phys_cursor.x;
 33375       if (x < 0)
 33376         {
 33377           width += x;
 33378           x = 0;
 33379         }
 33380       width = min (width, window_box_width (w, TEXT_AREA) - x);
 33381       y = WINDOW_TO_FRAME_PIXEL_Y (w, max (tab_line_height, max (header_line_height, cursor_row->y)));
 33382       x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
 33383 
 33384       if (width > 0)
 33385         FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
 33386     }
 33387 
 33388   /* Erase the cursor by redrawing the character underneath it.  */
 33389   if (mouse_face_here_p)
 33390     hl = DRAW_MOUSE_FACE;
 33391   else
 33392     hl = DRAW_NORMAL_TEXT;
 33393   draw_phys_cursor_glyph (w, cursor_row, hl);
 33394 
 33395  mark_cursor_off:
 33396   w->phys_cursor_on_p = false;
 33397   w->phys_cursor_type = NO_CURSOR;
 33398 }
 33399 
 33400 
 33401 /* Display or clear cursor of window W.  If !ON, clear the cursor.
 33402    If ON, display the cursor; where to put the cursor is specified by
 33403    HPOS, VPOS, X and Y.  */
 33404 
 33405 void
 33406 display_and_set_cursor (struct window *w, bool on,
 33407                         int hpos, int vpos, int x, int y)
 33408 {
 33409   struct frame *f = XFRAME (w->frame);
 33410   int new_cursor_type;
 33411   int new_cursor_width UNINIT;
 33412   bool active_cursor;
 33413   struct glyph_row *glyph_row;
 33414   struct glyph *glyph;
 33415 
 33416   /* This is pointless on invisible frames, and dangerous on garbaged
 33417      windows and frames; in the latter case, the frame or window may
 33418      be in the midst of changing its size, and x and y may be off the
 33419      window.  */
 33420   if (! FRAME_VISIBLE_P (f)
 33421       || vpos >= w->current_matrix->nrows
 33422       || hpos >= w->current_matrix->matrix_w)
 33423     return;
 33424 
 33425   /* If cursor is off and we want it off, return quickly.  */
 33426   if (!on && !w->phys_cursor_on_p)
 33427     return;
 33428 
 33429   glyph_row = MATRIX_ROW (w->current_matrix, vpos);
 33430   /* If cursor row is not enabled, we don't really know where to
 33431      display the cursor.  */
 33432   if (!glyph_row->enabled_p)
 33433     {
 33434       w->phys_cursor_on_p = false;
 33435       return;
 33436     }
 33437 
 33438   /* A frame might be marked garbaged even though its cursor position
 33439      is correct, and will not change upon subsequent redisplay.  This
 33440      happens in some rare situations, like toggling the sort order in
 33441      Dired windows.  We've already established that VPOS is valid, so
 33442      it shouldn't do any harm to record the cursor position, as we are
 33443      going to return without acting on it anyway.  Otherwise, expose
 33444      events might come in and call update_window_cursor, which will
 33445      blindly use outdated values in w->phys_cursor.  */
 33446   if (FRAME_GARBAGED_P (f))
 33447     {
 33448       if (on)
 33449         {
 33450           w->phys_cursor.x = x;
 33451           w->phys_cursor.y = glyph_row->y;
 33452           w->phys_cursor.hpos = hpos;
 33453           w->phys_cursor.vpos = vpos;
 33454         }
 33455       return;
 33456     }
 33457 
 33458   glyph = NULL;
 33459   if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
 33460     glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
 33461 
 33462   eassert (input_blocked_p ());
 33463 
 33464   /* Set new_cursor_type to the cursor we want to be displayed.  */
 33465   new_cursor_type = get_window_cursor_type (w, glyph,
 33466                                             &new_cursor_width, &active_cursor);
 33467 
 33468   /* If cursor is currently being shown and we don't want it to be or
 33469      it is in the wrong place, or the cursor type is not what we want,
 33470      erase it.  */
 33471   if (w->phys_cursor_on_p
 33472       && (!on
 33473           || w->phys_cursor.x != x
 33474           || w->phys_cursor.y != y
 33475           /* HPOS can be negative in R2L rows whose
 33476              exact_window_width_line_p flag is set (i.e. their newline
 33477              would "overflow into the fringe").  */
 33478           || hpos < 0
 33479           || new_cursor_type != w->phys_cursor_type
 33480           || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
 33481               && new_cursor_width != w->phys_cursor_width)))
 33482     erase_phys_cursor (w);
 33483 
 33484   /* Don't check phys_cursor_on_p here because that flag is only set
 33485      to false in some cases where we know that the cursor has been
 33486      completely erased, to avoid the extra work of erasing the cursor
 33487      twice.  In other words, phys_cursor_on_p can be true and the cursor
 33488      still not be visible, or it has only been partly erased.  */
 33489   if (on)
 33490     {
 33491       w->phys_cursor_ascent = glyph_row->ascent;
 33492       w->phys_cursor_height = glyph_row->height;
 33493 
 33494       /* Set phys_cursor_.* before x_draw_.* is called because some
 33495          of them may need the information.  */
 33496       w->phys_cursor.x = x;
 33497       w->phys_cursor.y = glyph_row->y;
 33498       w->phys_cursor.hpos = hpos;
 33499       w->phys_cursor.vpos = vpos;
 33500     }
 33501 
 33502   FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
 33503                                      new_cursor_type, new_cursor_width,
 33504                                      on, active_cursor);
 33505 }
 33506 
 33507 
 33508 /* Switch the display of W's cursor on or off, according to the value
 33509    of ON.  */
 33510 
 33511 static void
 33512 update_window_cursor (struct window *w, bool on)
 33513 {
 33514   /* Don't update cursor in windows whose frame is in the process
 33515      of being deleted.  */
 33516   if (w->current_matrix)
 33517     {
 33518       int hpos = w->phys_cursor.hpos;
 33519       int vpos = w->phys_cursor.vpos;
 33520       struct glyph_row *row;
 33521 
 33522       if (vpos >= w->current_matrix->nrows
 33523           || hpos >= w->current_matrix->matrix_w)
 33524         return;
 33525 
 33526       row = MATRIX_ROW (w->current_matrix, vpos);
 33527 
 33528       /* When the window is hscrolled, cursor hpos can legitimately be
 33529          out of bounds, but we draw the cursor at the corresponding
 33530          window margin in that case.  */
 33531       if (!row->reversed_p && hpos < 0)
 33532         hpos = 0;
 33533       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33534         hpos = row->used[TEXT_AREA] - 1;
 33535 
 33536       block_input ();
 33537       display_and_set_cursor (w, on, hpos, vpos,
 33538                               w->phys_cursor.x, w->phys_cursor.y);
 33539       unblock_input ();
 33540     }
 33541 }
 33542 
 33543 
 33544 /* Call update_window_cursor with parameter ON_P on all leaf windows
 33545    in the window tree rooted at W.  */
 33546 
 33547 static void
 33548 update_cursor_in_window_tree (struct window *w, bool on_p)
 33549 {
 33550   while (w)
 33551     {
 33552       if (WINDOWP (w->contents))
 33553         update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
 33554       else
 33555         update_window_cursor (w, on_p);
 33556 
 33557       w = NILP (w->next) ? 0 : XWINDOW (w->next);
 33558     }
 33559 }
 33560 
 33561 
 33562 /* EXPORT:
 33563    Display the cursor on window W, or clear it, according to ON_P.
 33564    Don't change the cursor's position.  */
 33565 
 33566 void
 33567 gui_update_cursor (struct frame *f, bool on_p)
 33568 {
 33569   update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
 33570 }
 33571 
 33572 
 33573 /* EXPORT:
 33574    Clear the cursor of window W to background color, and mark the
 33575    cursor as not shown.  This is used when the text where the cursor
 33576    is about to be rewritten.  */
 33577 
 33578 void
 33579 gui_clear_cursor (struct window *w)
 33580 {
 33581   if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
 33582     update_window_cursor (w, false);
 33583 }
 33584 
 33585 #endif /* HAVE_WINDOW_SYSTEM */
 33586 
 33587 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
 33588    and MSDOS.  */
 33589 static void
 33590 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
 33591                           int start_hpos, int end_hpos,
 33592                           enum draw_glyphs_face draw)
 33593 {
 33594 #ifdef HAVE_WINDOW_SYSTEM
 33595   if (FRAME_WINDOW_P (XFRAME (w->frame)))
 33596     {
 33597       draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
 33598       return;
 33599     }
 33600 #endif
 33601 
 33602   tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
 33603 }
 33604 
 33605 /* Display the active region described by mouse_face_* according to DRAW.  */
 33606 
 33607 static void
 33608 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
 33609 {
 33610   /* Don't bother doing anything if the mouse-face window is not set
 33611      up.  */
 33612   if (!WINDOWP (hlinfo->mouse_face_window))
 33613     return;
 33614 
 33615   struct window *w = XWINDOW (hlinfo->mouse_face_window);
 33616   struct frame *f = XFRAME (WINDOW_FRAME (w));
 33617 
 33618   /* Don't bother doing anything if we are on a wrong frame.  */
 33619   if (f != hlinfo->mouse_face_mouse_frame)
 33620     return;
 33621 
 33622   if (/* If window is in the process of being destroyed, don't bother
 33623          to do anything.  */
 33624       w->current_matrix != NULL
 33625       /* Don't update mouse highlight if hidden.  */
 33626       && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
 33627       /* Recognize when we are called to operate on rows that don't exist
 33628          anymore.  This can happen when a window is split.  */
 33629       && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
 33630     {
 33631       bool phys_cursor_on_p = w->phys_cursor_on_p;
 33632 #ifdef HAVE_WINDOW_SYSTEM
 33633       int mouse_off = 0;
 33634 #endif
 33635       struct glyph_row *row, *first, *last;
 33636 
 33637       first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
 33638       last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
 33639 
 33640       for (row = first; row <= last && row->enabled_p; ++row)
 33641         {
 33642           int start_hpos, end_hpos, start_x;
 33643 
 33644           /* For all but the first row, the highlight starts at column 0.  */
 33645           if (row == first)
 33646             {
 33647               /* R2L rows have BEG and END in reversed order, but the
 33648                  screen drawing geometry is always left to right.  So
 33649                  we need to mirror the beginning and end of the
 33650                  highlighted area in R2L rows.  */
 33651               if (!row->reversed_p)
 33652                 {
 33653                   start_hpos = hlinfo->mouse_face_beg_col;
 33654                   start_x = hlinfo->mouse_face_beg_x;
 33655                 }
 33656               else if (row == last)
 33657                 {
 33658                   start_hpos = hlinfo->mouse_face_end_col;
 33659                   start_x = hlinfo->mouse_face_end_x;
 33660                 }
 33661               else
 33662                 {
 33663                   start_hpos = 0;
 33664                   start_x = 0;
 33665                 }
 33666             }
 33667           else if (row->reversed_p && row == last)
 33668             {
 33669               start_hpos = hlinfo->mouse_face_end_col;
 33670               start_x = hlinfo->mouse_face_end_x;
 33671             }
 33672           else
 33673             {
 33674               start_hpos = 0;
 33675               start_x = 0;
 33676             }
 33677 
 33678           if (row == last)
 33679             {
 33680               if (!row->reversed_p)
 33681                 end_hpos = hlinfo->mouse_face_end_col;
 33682               else if (row == first)
 33683                 end_hpos = hlinfo->mouse_face_beg_col;
 33684               else
 33685                 {
 33686                   end_hpos = row->used[TEXT_AREA];
 33687                   if (draw == DRAW_NORMAL_TEXT)
 33688                     row->fill_line_p = true; /* Clear to end of line.  */
 33689                 }
 33690             }
 33691           else if (row->reversed_p && row == first)
 33692             end_hpos = hlinfo->mouse_face_beg_col;
 33693           else
 33694             {
 33695               end_hpos = row->used[TEXT_AREA];
 33696               if (draw == DRAW_NORMAL_TEXT)
 33697                 row->fill_line_p = true; /* Clear to end of line.  */
 33698             }
 33699 
 33700           if (end_hpos > start_hpos)
 33701             {
 33702               draw_row_with_mouse_face (w, start_x, row,
 33703                                         start_hpos, end_hpos, draw);
 33704 
 33705               row->mouse_face_p
 33706                 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
 33707             }
 33708 #ifdef HAVE_WINDOW_SYSTEM
 33709           /* Compute the cursor offset due to mouse-highlight.  */
 33710           if ((MATRIX_ROW_VPOS (row, w->current_matrix) == w->phys_cursor.vpos)
 33711               /* But not when highlighting a pseudo window, such as
 33712                  the toolbar, which can't have a cursor anyway.  */
 33713               && !w->pseudo_window_p
 33714               && draw == DRAW_MOUSE_FACE)
 33715             get_cursor_offset_for_mouse_face (w, row, &mouse_off);
 33716 #endif
 33717         }
 33718 
 33719       /* When we've written over the cursor, arrange for it to
 33720          be displayed again.  */
 33721       if (FRAME_WINDOW_P (f)
 33722           && phys_cursor_on_p && !w->phys_cursor_on_p)
 33723         {
 33724 #ifdef HAVE_WINDOW_SYSTEM
 33725           int hpos = w->phys_cursor.hpos;
 33726           int old_phys_cursor_x = w->phys_cursor.x;
 33727 
 33728           /* When the window is hscrolled, cursor hpos can legitimately be
 33729              out of bounds, but we draw the cursor at the corresponding
 33730              window margin in that case.  */
 33731           if (!row->reversed_p && hpos < 0)
 33732             hpos = 0;
 33733           if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33734             hpos = row->used[TEXT_AREA] - 1;
 33735 
 33736           block_input ();
 33737           display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
 33738                                   w->phys_cursor.x + mouse_off,
 33739                                   w->phys_cursor.y);
 33740           /* Restore the original cursor coordinates, perhaps modified
 33741              to account for mouse-highlight.  */
 33742           w->phys_cursor.x = old_phys_cursor_x;
 33743           unblock_input ();
 33744 #endif  /* HAVE_WINDOW_SYSTEM */
 33745         }
 33746     }
 33747 
 33748 #ifdef HAVE_WINDOW_SYSTEM
 33749   /* Change the mouse cursor.  */
 33750   if (FRAME_WINDOW_P (f) && NILP (track_mouse))
 33751     {
 33752       if (draw == DRAW_NORMAL_TEXT
 33753 #ifndef HAVE_EXT_TOOL_BAR
 33754           && !EQ (hlinfo->mouse_face_window, f->tool_bar_window)
 33755 #endif
 33756           && !EQ (hlinfo->mouse_face_window, f->tab_bar_window))
 33757         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->text_cursor);
 33758       else
 33759       if (draw == DRAW_MOUSE_FACE)
 33760         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->hand_cursor);
 33761       else
 33762         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->nontext_cursor);
 33763     }
 33764 #endif  /* HAVE_WINDOW_SYSTEM */
 33765 }
 33766 
 33767 /* EXPORT:
 33768    Clear out the mouse-highlighted active region.
 33769    Redraw it un-highlighted first.  Value is true if mouse
 33770    face was actually drawn unhighlighted.  */
 33771 
 33772 bool
 33773 clear_mouse_face (Mouse_HLInfo *hlinfo)
 33774 {
 33775   bool cleared
 33776     = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
 33777   if (cleared)
 33778     show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
 33779   hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
 33780   hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
 33781   hlinfo->mouse_face_window = Qnil;
 33782   hlinfo->mouse_face_overlay = Qnil;
 33783   return cleared;
 33784 }
 33785 
 33786 /* Return true if the coordinates HPOS and VPOS on windows W are
 33787    within the mouse face on that window.  */
 33788 static bool
 33789 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
 33790 {
 33791   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 33792 
 33793   /* Quickly resolve the easy cases.  */
 33794   if (!(WINDOWP (hlinfo->mouse_face_window)
 33795         && XWINDOW (hlinfo->mouse_face_window) == w))
 33796     return false;
 33797   if (vpos < hlinfo->mouse_face_beg_row
 33798       || vpos > hlinfo->mouse_face_end_row)
 33799     return false;
 33800   if (vpos > hlinfo->mouse_face_beg_row
 33801       && vpos < hlinfo->mouse_face_end_row)
 33802     return true;
 33803 
 33804   if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
 33805     {
 33806       if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 33807         {
 33808           if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
 33809             return true;
 33810         }
 33811       else if ((vpos == hlinfo->mouse_face_beg_row
 33812                 && hpos >= hlinfo->mouse_face_beg_col)
 33813                || (vpos == hlinfo->mouse_face_end_row
 33814                    && hpos < hlinfo->mouse_face_end_col))
 33815         return true;
 33816     }
 33817   else
 33818     {
 33819        if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 33820         {
 33821           if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
 33822             return true;
 33823         }
 33824       else if ((vpos == hlinfo->mouse_face_beg_row
 33825                 && hpos <= hlinfo->mouse_face_beg_col)
 33826                || (vpos == hlinfo->mouse_face_end_row
 33827                    && hpos > hlinfo->mouse_face_end_col))
 33828         return true;
 33829     }
 33830   return false;
 33831 }
 33832 
 33833 
 33834 /* EXPORT:
 33835    True if physical cursor of window W is within mouse face.  */
 33836 
 33837 bool
 33838 cursor_in_mouse_face_p (struct window *w)
 33839 {
 33840   int vpos = w->phys_cursor.vpos;
 33841 
 33842   /* If the cursor is outside the matrix glyph rows, it cannot be
 33843      within the mouse face.  */
 33844   if (!(0 <= vpos && vpos < w->current_matrix->nrows))
 33845     return false;
 33846 
 33847   int hpos = w->phys_cursor.hpos;
 33848   struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
 33849 
 33850   /* When the window is hscrolled, cursor hpos can legitimately be out
 33851      of bounds, but we draw the cursor at the corresponding window
 33852      margin in that case.  */
 33853   if (!row->reversed_p && hpos < 0)
 33854     hpos = 0;
 33855   if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33856     hpos = row->used[TEXT_AREA] - 1;
 33857 
 33858   return coords_in_mouse_face_p (w, hpos, vpos);
 33859 }
 33860 
 33861 
 33862 
 33863 /* Find the glyph rows START_ROW and END_ROW of window W that display
 33864    characters between buffer positions START_CHARPOS and END_CHARPOS
 33865    (excluding END_CHARPOS).  DISP_STRING is a display string that
 33866    covers these buffer positions.  This is similar to
 33867    row_containing_pos, but is more accurate when bidi reordering makes
 33868    buffer positions change non-linearly with glyph rows.  */
 33869 static void
 33870 rows_from_pos_range (struct window *w,
 33871                      ptrdiff_t start_charpos, ptrdiff_t end_charpos,
 33872                      Lisp_Object disp_string,
 33873                      struct glyph_row **start, struct glyph_row **end)
 33874 {
 33875   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 33876   int last_y = window_text_bottom_y (w);
 33877   struct glyph_row *row;
 33878 
 33879   *start = NULL;
 33880   *end = NULL;
 33881 
 33882   while (!first->enabled_p
 33883          && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
 33884     first++;
 33885 
 33886   /* Find the START row.  */
 33887   for (row = first;
 33888        row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
 33889        row++)
 33890     {
 33891       /* A row can potentially be the START row if the range of the
 33892          characters it displays intersects the range
 33893          [START_CHARPOS..END_CHARPOS).  */
 33894       if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
 33895               && end_charpos < MATRIX_ROW_START_CHARPOS (row))
 33896              /* See the commentary in row_containing_pos, for the
 33897                 explanation of the complicated way to check whether
 33898                 some position is beyond the end of the characters
 33899                 displayed by a row.  */
 33900              || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
 33901                   || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
 33902                       && !row->ends_at_zv_p
 33903                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 33904                  && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
 33905                      || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
 33906                          && !row->ends_at_zv_p
 33907                          && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
 33908         {
 33909           /* Found a candidate row.  Now make sure at least one of the
 33910              glyphs it displays has a charpos from the range
 33911              [START_CHARPOS..END_CHARPOS).
 33912 
 33913              This is not obvious because bidi reordering could make
 33914              buffer positions of a row be 1,2,3,102,101,100, and if we
 33915              want to highlight characters in [50..60), we don't want
 33916              this row, even though [50..60) does intersect [1..103),
 33917              the range of character positions given by the row's start
 33918              and end positions.  */
 33919           struct glyph *g = row->glyphs[TEXT_AREA];
 33920           struct glyph *e = g + row->used[TEXT_AREA];
 33921 
 33922           while (g < e)
 33923             {
 33924               if (((BUFFERP (g->object) || NILP (g->object))
 33925                    && start_charpos <= g->charpos && g->charpos < end_charpos)
 33926                   /* A glyph that comes from DISP_STRING is by
 33927                      definition to be highlighted.  */
 33928                   || EQ (g->object, disp_string))
 33929                 *start = row;
 33930               g++;
 33931             }
 33932           if (*start)
 33933             break;
 33934         }
 33935     }
 33936 
 33937   /* Find the END row.  */
 33938   if (!*start
 33939       /* If the last row is partially visible, start looking for END
 33940          from that row, instead of starting from FIRST.  */
 33941       && !(row->enabled_p
 33942            && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
 33943     row = first;
 33944   for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
 33945     {
 33946       struct glyph_row *next = row + 1;
 33947       ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
 33948 
 33949       if (!next->enabled_p
 33950           || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
 33951           /* The first row >= START whose range of displayed characters
 33952              does NOT intersect the range [START_CHARPOS..END_CHARPOS]
 33953              is the row END + 1.  */
 33954           || (start_charpos < next_start
 33955               && end_charpos < next_start)
 33956           || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
 33957                || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
 33958                    && !next->ends_at_zv_p
 33959                    && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
 33960               && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
 33961                   || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
 33962                       && !next->ends_at_zv_p
 33963                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
 33964         {
 33965           *end = row;
 33966           break;
 33967         }
 33968       else
 33969         {
 33970           /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
 33971              but none of the characters it displays are in the range, it is
 33972              also END + 1. */
 33973           struct glyph *g = next->glyphs[TEXT_AREA];
 33974           struct glyph *s = g;
 33975           struct glyph *e = g + next->used[TEXT_AREA];
 33976 
 33977           while (g < e)
 33978             {
 33979               if (((BUFFERP (g->object) || NILP (g->object))
 33980                    && ((start_charpos <= g->charpos && g->charpos < end_charpos)
 33981                        /* If the buffer position of the first glyph in
 33982                           the row is equal to END_CHARPOS, it means
 33983                           the last character to be highlighted is the
 33984                           newline of ROW, and we must consider NEXT as
 33985                           END, not END+1.  */
 33986                        || (((!next->reversed_p && g == s)
 33987                             || (next->reversed_p && g == e - 1))
 33988                            && (g->charpos == end_charpos
 33989                                /* Special case for when NEXT is an
 33990                                   empty line at ZV.  */
 33991                                || (g->charpos == -1
 33992                                    && !row->ends_at_zv_p
 33993                                    && next_start == end_charpos)))))
 33994                   /* A glyph that comes from DISP_STRING is by
 33995                      definition to be highlighted.  */
 33996                   || EQ (g->object, disp_string))
 33997                 break;
 33998               g++;
 33999             }
 34000           if (g == e)
 34001             {
 34002               *end = row;
 34003               break;
 34004             }
 34005           /* The first row that ends at ZV must be the last to be
 34006              highlighted.  */
 34007           else if (next->ends_at_zv_p)
 34008             {
 34009               *end = next;
 34010               break;
 34011             }
 34012         }
 34013     }
 34014 }
 34015 
 34016 /* This function sets the mouse_face_* elements of HLINFO, assuming
 34017    the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
 34018    window WINDOW.  START_CHARPOS and END_CHARPOS are buffer positions
 34019    for the overlay or run of text properties specifying the mouse
 34020    face.  BEFORE_STRING and AFTER_STRING, if non-nil, are a
 34021    before-string and after-string that must also be highlighted.
 34022    DISP_STRING, if non-nil, is a display string that may cover some
 34023    or all of the highlighted text.  */
 34024 
 34025 static void
 34026 mouse_face_from_buffer_pos (Lisp_Object window,
 34027                             Mouse_HLInfo *hlinfo,
 34028                             ptrdiff_t mouse_charpos,
 34029                             ptrdiff_t start_charpos,
 34030                             ptrdiff_t end_charpos,
 34031                             Lisp_Object before_string,
 34032                             Lisp_Object after_string,
 34033                             Lisp_Object disp_string)
 34034 {
 34035   struct window *w = XWINDOW (window);
 34036   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34037   struct glyph_row *r1, *r2;
 34038   struct glyph *glyph, *end;
 34039   ptrdiff_t ignore, pos;
 34040   int x;
 34041 
 34042   eassert (NILP (disp_string) || STRINGP (disp_string));
 34043   eassert (NILP (before_string) || STRINGP (before_string));
 34044   eassert (NILP (after_string) || STRINGP (after_string));
 34045 
 34046   /* Find the rows corresponding to START_CHARPOS and END_CHARPOS.  */
 34047   rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
 34048   if (r1 == NULL)
 34049     r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34050   /* If the before-string or display-string contains newlines,
 34051      rows_from_pos_range skips to its last row.  Move back.  */
 34052   if (!NILP (before_string) || !NILP (disp_string))
 34053     {
 34054       struct glyph_row *prev;
 34055       while ((prev = r1 - 1, prev >= first)
 34056              && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
 34057              && prev->used[TEXT_AREA] > 0)
 34058         {
 34059           struct glyph *beg = prev->glyphs[TEXT_AREA];
 34060           glyph = beg + prev->used[TEXT_AREA];
 34061           while (--glyph >= beg && NILP (glyph->object));
 34062           if (glyph < beg
 34063               || !(EQ (glyph->object, before_string)
 34064                    || EQ (glyph->object, disp_string)))
 34065             break;
 34066           r1 = prev;
 34067         }
 34068     }
 34069   if (r2 == NULL)
 34070     {
 34071       r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34072       hlinfo->mouse_face_past_end = true;
 34073     }
 34074   else if (!NILP (after_string))
 34075     {
 34076       /* If the after-string has newlines, advance to its last row.  */
 34077       struct glyph_row *next;
 34078       struct glyph_row *last
 34079         = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34080 
 34081       for (next = r2 + 1;
 34082            next <= last
 34083              && next->used[TEXT_AREA] > 0
 34084              && EQ (next->glyphs[TEXT_AREA]->object, after_string);
 34085            ++next)
 34086         r2 = next;
 34087     }
 34088   /* The rest of the display engine assumes that mouse_face_beg_row is
 34089      either above mouse_face_end_row or identical to it.  But with
 34090      bidi-reordered continued lines, the row for START_CHARPOS could
 34091      be below the row for END_CHARPOS.  If so, swap the rows and store
 34092      them in correct order.  */
 34093   if (r1->y > r2->y)
 34094     {
 34095       struct glyph_row *tem = r2;
 34096 
 34097       r2 = r1;
 34098       r1 = tem;
 34099     }
 34100 
 34101   hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
 34102   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
 34103 
 34104   /* For a bidi-reordered row, the positions of BEFORE_STRING,
 34105      AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
 34106      could be anywhere in the row and in any order.  The strategy
 34107      below is to find the leftmost and the rightmost glyph that
 34108      belongs to either of these 3 strings, or whose position is
 34109      between START_CHARPOS and END_CHARPOS, and highlight all the
 34110      glyphs between those two.  This may cover more than just the text
 34111      between START_CHARPOS and END_CHARPOS if the range of characters
 34112      strides the bidi level boundary, e.g. if the beginning is in R2L
 34113      text while the end is in L2R text or vice versa.  */
 34114   if (!r1->reversed_p)
 34115     {
 34116       /* This row is in a left to right paragraph.  Scan it left to
 34117          right.  */
 34118       glyph = r1->glyphs[TEXT_AREA];
 34119       end = glyph + r1->used[TEXT_AREA];
 34120       x = r1->x;
 34121 
 34122       /* Skip truncation glyphs at the start of the glyph row.  */
 34123       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34124         for (; glyph < end
 34125                && NILP (glyph->object)
 34126                && glyph->charpos < 0;
 34127              ++glyph)
 34128           x += glyph->pixel_width;
 34129 
 34130       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34131          or DISP_STRING, and the first glyph from buffer whose
 34132          position is between START_CHARPOS and END_CHARPOS.  */
 34133       for (; glyph < end
 34134              && !NILP (glyph->object)
 34135              && !EQ (glyph->object, disp_string)
 34136              && !(BUFFERP (glyph->object)
 34137                   && (glyph->charpos >= start_charpos
 34138                       && glyph->charpos < end_charpos));
 34139            ++glyph)
 34140         {
 34141           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34142              are present at buffer positions between START_CHARPOS and
 34143              END_CHARPOS, or if they come from an overlay.  */
 34144           if (EQ (glyph->object, before_string))
 34145             {
 34146               pos = string_buffer_position (before_string,
 34147                                             start_charpos);
 34148               /* If pos == 0, it means before_string came from an
 34149                  overlay, not from a buffer position.  */
 34150               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34151                 break;
 34152             }
 34153           else if (EQ (glyph->object, after_string))
 34154             {
 34155               pos = string_buffer_position (after_string, end_charpos);
 34156               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34157                 break;
 34158             }
 34159           x += glyph->pixel_width;
 34160         }
 34161       hlinfo->mouse_face_beg_x = x;
 34162       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34163     }
 34164   else
 34165     {
 34166       /* This row is in a right to left paragraph.  Scan it right to
 34167          left.  */
 34168       struct glyph *g;
 34169 
 34170       end = r1->glyphs[TEXT_AREA] - 1;
 34171       glyph = end + r1->used[TEXT_AREA];
 34172 
 34173       /* Skip truncation glyphs at the start of the glyph row.  */
 34174       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34175         for (; glyph > end
 34176                && NILP (glyph->object)
 34177                && glyph->charpos < 0;
 34178              --glyph)
 34179           ;
 34180 
 34181       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34182          or DISP_STRING, and the first glyph from buffer whose
 34183          position is between START_CHARPOS and END_CHARPOS.  */
 34184       for (; glyph > end
 34185              && !NILP (glyph->object)
 34186              && !EQ (glyph->object, disp_string)
 34187              && !(BUFFERP (glyph->object)
 34188                   && (glyph->charpos >= start_charpos
 34189                       && glyph->charpos < end_charpos));
 34190            --glyph)
 34191         {
 34192           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34193              are present at buffer positions between START_CHARPOS and
 34194              END_CHARPOS, or if they come from an overlay.  */
 34195           if (EQ (glyph->object, before_string))
 34196             {
 34197               pos = string_buffer_position (before_string, start_charpos);
 34198               /* If pos == 0, it means before_string came from an
 34199                  overlay, not from a buffer position.  */
 34200               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34201                 break;
 34202             }
 34203           else if (EQ (glyph->object, after_string))
 34204             {
 34205               pos = string_buffer_position (after_string, end_charpos);
 34206               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34207                 break;
 34208             }
 34209         }
 34210 
 34211       glyph++; /* first glyph to the right of the highlighted area */
 34212       for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
 34213         x += g->pixel_width;
 34214       hlinfo->mouse_face_beg_x = x;
 34215       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34216     }
 34217 
 34218   /* If the highlight ends in a different row, compute GLYPH and END
 34219      for the end row.  Otherwise, reuse the values computed above for
 34220      the row where the highlight begins.  */
 34221   if (r2 != r1)
 34222     {
 34223       if (!r2->reversed_p)
 34224         {
 34225           glyph = r2->glyphs[TEXT_AREA];
 34226           end = glyph + r2->used[TEXT_AREA];
 34227           x = r2->x;
 34228         }
 34229       else
 34230         {
 34231           end = r2->glyphs[TEXT_AREA] - 1;
 34232           glyph = end + r2->used[TEXT_AREA];
 34233         }
 34234     }
 34235 
 34236   if (!r2->reversed_p)
 34237     {
 34238       /* Skip truncation and continuation glyphs near the end of the
 34239          row, and also blanks and stretch glyphs inserted by
 34240          extend_face_to_end_of_line.  */
 34241       while (end > glyph
 34242              && NILP ((end - 1)->object))
 34243         --end;
 34244       /* Scan the rest of the glyph row from the end, looking for the
 34245          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34246          DISP_STRING, or whose position is between START_CHARPOS
 34247          and END_CHARPOS */
 34248       for (--end;
 34249              end > glyph
 34250              && !NILP (end->object)
 34251              && !EQ (end->object, disp_string)
 34252              && !(BUFFERP (end->object)
 34253                   && (end->charpos >= start_charpos
 34254                       && end->charpos < end_charpos));
 34255            --end)
 34256         {
 34257           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34258              are present at buffer positions between START_CHARPOS and
 34259              END_CHARPOS, or if they come from an overlay.  */
 34260           if (EQ (end->object, before_string))
 34261             {
 34262               pos = string_buffer_position (before_string, start_charpos);
 34263               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34264                 break;
 34265             }
 34266           else if (EQ (end->object, after_string))
 34267             {
 34268               pos = string_buffer_position (after_string, end_charpos);
 34269               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34270                 break;
 34271             }
 34272         }
 34273       /* Find the X coordinate of the last glyph to be highlighted.  */
 34274       for (; glyph <= end; ++glyph)
 34275         x += glyph->pixel_width;
 34276 
 34277       hlinfo->mouse_face_end_x = x;
 34278       hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
 34279     }
 34280   else
 34281     {
 34282       /* Skip truncation and continuation glyphs near the end of the
 34283          row, and also blanks and stretch glyphs inserted by
 34284          extend_face_to_end_of_line.  */
 34285       x = r2->x;
 34286       end++;
 34287       while (end < glyph
 34288              && NILP (end->object))
 34289         {
 34290           x += end->pixel_width;
 34291           ++end;
 34292         }
 34293       /* Scan the rest of the glyph row from the end, looking for the
 34294          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34295          DISP_STRING, or whose position is between START_CHARPOS
 34296          and END_CHARPOS */
 34297       for ( ;
 34298              end < glyph
 34299              && !NILP (end->object)
 34300              && !EQ (end->object, disp_string)
 34301              && !(BUFFERP (end->object)
 34302                   && (end->charpos >= start_charpos
 34303                       && end->charpos < end_charpos));
 34304            ++end)
 34305         {
 34306           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34307              are present at buffer positions between START_CHARPOS and
 34308              END_CHARPOS, or if they come from an overlay.  */
 34309           if (EQ (end->object, before_string))
 34310             {
 34311               pos = string_buffer_position (before_string, start_charpos);
 34312               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34313                 break;
 34314             }
 34315           else if (EQ (end->object, after_string))
 34316             {
 34317               pos = string_buffer_position (after_string, end_charpos);
 34318               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34319                 break;
 34320             }
 34321           x += end->pixel_width;
 34322         }
 34323       /* If we exited the above loop because we arrived at the last
 34324          glyph of the row, and its buffer position is still not in
 34325          range, it means the last character in range is the preceding
 34326          newline.  Bump the end column and x values to get past the
 34327          last glyph.  */
 34328       if (end == glyph
 34329           && BUFFERP (end->object)
 34330           && (end->charpos < start_charpos
 34331               || end->charpos >= end_charpos))
 34332         {
 34333           x += end->pixel_width;
 34334           ++end;
 34335         }
 34336       hlinfo->mouse_face_end_x = x;
 34337       hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
 34338     }
 34339 
 34340   hlinfo->mouse_face_window = window;
 34341   hlinfo->mouse_face_face_id
 34342     = face_at_buffer_position (w, mouse_charpos, &ignore,
 34343                                mouse_charpos + 1,
 34344                                !hlinfo->mouse_face_hidden, -1, 0);
 34345   show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 34346 }
 34347 
 34348 /* The following function is not used anymore (replaced with
 34349    mouse_face_from_string_pos), but I leave it here for the time
 34350    being, in case someone would.  */
 34351 
 34352 #if false       /* not used */
 34353 
 34354 /* Find the position of the glyph for position POS in OBJECT in
 34355    window W's current matrix, and return in *X, *Y the pixel
 34356    coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
 34357 
 34358    RIGHT_P means return the position of the right edge of the glyph.
 34359    !RIGHT_P means return the left edge position.
 34360 
 34361    If no glyph for POS exists in the matrix, return the position of
 34362    the glyph with the next smaller position that is in the matrix, if
 34363    RIGHT_P is false.  If RIGHT_P, and no glyph for POS
 34364    exists in the matrix, return the position of the glyph with the
 34365    next larger position in OBJECT.
 34366 
 34367    Value is true if a glyph was found.  */
 34368 
 34369 static bool
 34370 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
 34371                       int *hpos, int *vpos, int *x, int *y, bool right_p)
 34372 {
 34373   int yb = window_text_bottom_y (w);
 34374   struct glyph_row *r;
 34375   struct glyph *best_glyph = NULL;
 34376   struct glyph_row *best_row = NULL;
 34377   int best_x = 0;
 34378 
 34379   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34380        r->enabled_p && r->y < yb;
 34381        ++r)
 34382     {
 34383       struct glyph *g = r->glyphs[TEXT_AREA];
 34384       struct glyph *e = g + r->used[TEXT_AREA];
 34385       int gx;
 34386 
 34387       for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34388         if (EQ (g->object, object))
 34389           {
 34390             if (g->charpos == pos)
 34391               {
 34392                 best_glyph = g;
 34393                 best_x = gx;
 34394                 best_row = r;
 34395                 goto found;
 34396               }
 34397             else if (best_glyph == NULL
 34398                      || ((eabs (g->charpos - pos)
 34399                          < eabs (best_glyph->charpos - pos))
 34400                          && (right_p
 34401                              ? g->charpos < pos
 34402                              : g->charpos > pos)))
 34403               {
 34404                 best_glyph = g;
 34405                 best_x = gx;
 34406                 best_row = r;
 34407               }
 34408           }
 34409     }
 34410 
 34411  found:
 34412 
 34413   if (best_glyph)
 34414     {
 34415       *x = best_x;
 34416       *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
 34417 
 34418       if (right_p)
 34419         {
 34420           *x += best_glyph->pixel_width;
 34421           ++*hpos;
 34422         }
 34423 
 34424       *y = best_row->y;
 34425       *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
 34426     }
 34427 
 34428   return best_glyph != NULL;
 34429 }
 34430 #endif  /* not used */
 34431 
 34432 /* Find the positions of the first and the last glyphs in window W's
 34433    current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
 34434    (assumed to be a string), and return in HLINFO's mouse_face_*
 34435    members the pixel and column/row coordinates of those glyphs.  */
 34436 
 34437 static void
 34438 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
 34439                             Lisp_Object object,
 34440                             ptrdiff_t startpos, ptrdiff_t endpos)
 34441 {
 34442   int yb = window_text_bottom_y (w);
 34443   struct glyph_row *r;
 34444   struct glyph *g, *e;
 34445   int gx;
 34446   bool found = false;
 34447 
 34448   /* Find the glyph row with at least one position in the range
 34449      [STARTPOS..ENDPOS), and the first glyph in that row whose
 34450      position belongs to that range.  */
 34451   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34452        r->enabled_p && r->y < yb;
 34453        ++r)
 34454     {
 34455       if (!r->reversed_p)
 34456         {
 34457           g = r->glyphs[TEXT_AREA];
 34458           e = g + r->used[TEXT_AREA];
 34459           for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34460             if (EQ (g->object, object)
 34461                 && startpos <= g->charpos && g->charpos < endpos)
 34462               {
 34463                 hlinfo->mouse_face_beg_row
 34464                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34465                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34466                 hlinfo->mouse_face_beg_x = gx;
 34467                 found = true;
 34468                 break;
 34469               }
 34470         }
 34471       else
 34472         {
 34473           struct glyph *g1;
 34474 
 34475           e = r->glyphs[TEXT_AREA];
 34476           g = e + r->used[TEXT_AREA];
 34477           for ( ; g > e; --g)
 34478             if (EQ ((g-1)->object, object)
 34479                 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
 34480               {
 34481                 hlinfo->mouse_face_beg_row
 34482                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34483                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34484                 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
 34485                   gx += g1->pixel_width;
 34486                 hlinfo->mouse_face_beg_x = gx;
 34487                 found = true;
 34488                 break;
 34489               }
 34490         }
 34491       if (found)
 34492         break;
 34493     }
 34494 
 34495   if (!found)
 34496     return;
 34497 
 34498   /* Starting with the next row, look for the first row which does NOT
 34499      include any glyphs whose positions are in the range.  */
 34500   for (++r; r->enabled_p && r->y < yb; ++r)
 34501     {
 34502       g = r->glyphs[TEXT_AREA];
 34503       e = g + r->used[TEXT_AREA];
 34504       found = false;
 34505       for ( ; g < e; ++g)
 34506         if (EQ (g->object, object)
 34507             && startpos <= g->charpos && g->charpos < endpos)
 34508           {
 34509             found = true;
 34510             break;
 34511           }
 34512       if (!found)
 34513         break;
 34514     }
 34515 
 34516   /* The highlighted region ends on the previous row.  */
 34517   r--;
 34518 
 34519   /* Set the end row.  */
 34520   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
 34521 
 34522   /* Compute and set the end column and the end column's horizontal
 34523      pixel coordinate.  */
 34524   if (!r->reversed_p)
 34525     {
 34526       g = r->glyphs[TEXT_AREA];
 34527       e = g + r->used[TEXT_AREA];
 34528       for ( ; e > g; --e)
 34529         if (EQ ((e-1)->object, object)
 34530             && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
 34531           break;
 34532       hlinfo->mouse_face_end_col = e - g;
 34533 
 34534       for (gx = r->x; g < e; ++g)
 34535         gx += g->pixel_width;
 34536       hlinfo->mouse_face_end_x = gx;
 34537     }
 34538   else
 34539     {
 34540       e = r->glyphs[TEXT_AREA];
 34541       g = e + r->used[TEXT_AREA];
 34542       for (gx = r->x ; e < g; ++e)
 34543         {
 34544           if (EQ (e->object, object)
 34545               && startpos <= e->charpos && e->charpos < endpos)
 34546             break;
 34547           gx += e->pixel_width;
 34548         }
 34549       hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
 34550       hlinfo->mouse_face_end_x = gx;
 34551     }
 34552 }
 34553 
 34554 #ifdef HAVE_WINDOW_SYSTEM
 34555 
 34556 /* See if position X, Y is within a hot-spot of an image.  */
 34557 
 34558 static bool
 34559 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
 34560 {
 34561   if (!CONSP (hot_spot))
 34562     return false;
 34563 
 34564   if (EQ (XCAR (hot_spot), Qrect))
 34565     {
 34566       /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1))  */
 34567       Lisp_Object rect = XCDR (hot_spot);
 34568       Lisp_Object tem;
 34569       if (!CONSP (rect))
 34570         return false;
 34571       if (!CONSP (XCAR (rect)))
 34572         return false;
 34573       if (!CONSP (XCDR (rect)))
 34574         return false;
 34575       if (!(tem = XCAR (XCAR (rect)), FIXNUMP (tem) && x >= XFIXNUM (tem)))
 34576         return false;
 34577       if (!(tem = XCDR (XCAR (rect)), FIXNUMP (tem) && y >= XFIXNUM (tem)))
 34578         return false;
 34579       if (!(tem = XCAR (XCDR (rect)), FIXNUMP (tem) && x <= XFIXNUM (tem)))
 34580         return false;
 34581       if (!(tem = XCDR (XCDR (rect)), FIXNUMP (tem) && y <= XFIXNUM (tem)))
 34582         return false;
 34583       return true;
 34584     }
 34585   else if (EQ (XCAR (hot_spot), Qcircle))
 34586     {
 34587       /* CDR is (Center . Radius) = ((x0 . y0) . r) */
 34588       Lisp_Object circ = XCDR (hot_spot);
 34589       Lisp_Object lr, lx0, ly0;
 34590       if (CONSP (circ)
 34591           && CONSP (XCAR (circ))
 34592           && (lr = XCDR (circ), NUMBERP (lr))
 34593           && (lx0 = XCAR (XCAR (circ)), FIXNUMP (lx0))
 34594           && (ly0 = XCDR (XCAR (circ)), FIXNUMP (ly0)))
 34595         {
 34596           double r = XFLOATINT (lr);
 34597           double dx = XFIXNUM (lx0) - x;
 34598           double dy = XFIXNUM (ly0) - y;
 34599           return (dx * dx + dy * dy <= r * r);
 34600         }
 34601     }
 34602   else if (EQ (XCAR (hot_spot), Qpoly))
 34603     {
 34604       /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
 34605       if (VECTORP (XCDR (hot_spot)))
 34606         {
 34607           struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
 34608           Lisp_Object *poly = v->contents;
 34609           ptrdiff_t n = v->header.size;
 34610           ptrdiff_t i;
 34611           bool inside = false;
 34612           Lisp_Object lx, ly;
 34613           int x0, y0;
 34614 
 34615           /* Need an even number of coordinates, and at least 3 edges.  */
 34616           if (n < 6 || n & 1)
 34617             return false;
 34618 
 34619           /* Count edge segments intersecting line from (X,Y) to (X,infinity).
 34620              If count is odd, we are inside polygon.  Pixels on edges
 34621              may or may not be included depending on actual geometry of the
 34622              polygon.  */
 34623           if ((lx = poly[n-2], !FIXNUMP (lx))
 34624               || (ly = poly[n-1], !FIXNUMP (lx)))
 34625             return false;
 34626           x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34627           for (i = 0; i < n; i += 2)
 34628             {
 34629               int x1 = x0, y1 = y0;
 34630               if ((lx = poly[i], !FIXNUMP (lx))
 34631                   || (ly = poly[i+1], !FIXNUMP (ly)))
 34632                 return false;
 34633               x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34634 
 34635               /* Does this segment cross the X line?  */
 34636               if (x0 >= x)
 34637                 {
 34638                   if (x1 >= x)
 34639                     continue;
 34640                 }
 34641               else if (x1 < x)
 34642                 continue;
 34643               if (y > y0 && y > y1)
 34644                 continue;
 34645               if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
 34646                 inside = !inside;
 34647             }
 34648           return inside;
 34649         }
 34650     }
 34651   return false;
 34652 }
 34653 
 34654 Lisp_Object
 34655 find_hot_spot (Lisp_Object map, int x, int y)
 34656 {
 34657   while (CONSP (map))
 34658     {
 34659       if (CONSP (XCAR (map))
 34660           && on_hot_spot_p (XCAR (XCAR (map)), x, y))
 34661         return XCAR (map);
 34662       map = XCDR (map);
 34663     }
 34664 
 34665   return Qnil;
 34666 }
 34667 
 34668 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
 34669        3, 3, 0,
 34670        doc: /* Lookup in image map MAP coordinates X and Y.
 34671 An image map is an alist where each element has the format (AREA ID PLIST).
 34672 An AREA is specified as either a rectangle, a circle, or a polygon:
 34673 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
 34674 pixel coordinates of the upper left and bottom right corners.
 34675 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
 34676 and the radius of the circle; r may be a float or integer.
 34677 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
 34678 vector describes one corner in the polygon.
 34679 Returns the alist element for the first matching AREA in MAP.  */)
 34680   (Lisp_Object map, Lisp_Object x, Lisp_Object y)
 34681 {
 34682   if (NILP (map))
 34683     return Qnil;
 34684 
 34685   CHECK_FIXNUM (x);
 34686   CHECK_FIXNUM (y);
 34687 
 34688   return find_hot_spot (map,
 34689                         clip_to_bounds (INT_MIN, XFIXNUM (x), INT_MAX),
 34690                         clip_to_bounds (INT_MIN, XFIXNUM (y), INT_MAX));
 34691 }
 34692 #endif  /* HAVE_WINDOW_SYSTEM */
 34693 
 34694 
 34695 /* Display frame CURSOR, optionally using shape defined by POINTER.  */
 34696 static void
 34697 define_frame_cursor1 (struct frame *f, Emacs_Cursor cursor, Lisp_Object pointer)
 34698 {
 34699 #ifdef HAVE_WINDOW_SYSTEM
 34700   if (!FRAME_WINDOW_P (f))
 34701     return;
 34702 
 34703   /* Do not change cursor shape while dragging mouse.  */
 34704   if (EQ (track_mouse, Qdragging) || EQ (track_mouse, Qdropping)
 34705       || EQ (track_mouse, Qdrag_source))
 34706     return;
 34707 
 34708   if (!NILP (pointer))
 34709     {
 34710       if (EQ (pointer, Qarrow))
 34711         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34712       else if (EQ (pointer, Qhand))
 34713         cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 34714       else if (EQ (pointer, Qtext))
 34715         cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 34716       else if (EQ (pointer, intern ("hdrag")))
 34717         cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 34718       else if (EQ (pointer, intern ("nhdrag")))
 34719         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 34720 # ifdef HAVE_X_WINDOWS
 34721       else if (EQ (pointer, intern ("vdrag")))
 34722         cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
 34723 # endif
 34724       else if (EQ (pointer, intern ("hourglass")))
 34725         cursor = FRAME_OUTPUT_DATA (f)->hourglass_cursor;
 34726       else if (EQ (pointer, Qmodeline))
 34727         cursor = FRAME_OUTPUT_DATA (f)->modeline_cursor;
 34728       else
 34729         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34730     }
 34731 
 34732   if (cursor != No_Cursor)
 34733     FRAME_RIF (f)->define_frame_cursor (f, cursor);
 34734 #endif
 34735 }
 34736 
 34737 /* Take proper action when mouse has moved to the mode or header line
 34738    or marginal area AREA of window W, x-position X and y-position Y.
 34739    X is relative to the start of the text display area of W, so the
 34740    width of bitmap areas and scroll bars must be subtracted to get a
 34741    position relative to the start of the mode line.  */
 34742 
 34743 static void
 34744 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
 34745                                     enum window_part area)
 34746 {
 34747   struct window *w = XWINDOW (window);
 34748   struct frame *f = XFRAME (w->frame);
 34749   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 34750   Emacs_Cursor cursor = No_Cursor;
 34751   Lisp_Object pointer = Qnil;
 34752   int dx, dy, width, height;
 34753   ptrdiff_t charpos;
 34754   Lisp_Object string, object = Qnil;
 34755   Lisp_Object pos UNINIT;
 34756   Lisp_Object mouse_face;
 34757   int original_x_pixel = x;
 34758   struct glyph * glyph = NULL, * row_start_glyph = NULL;
 34759   struct glyph_row *row UNINIT;
 34760 
 34761   if (area == ON_MODE_LINE || area == ON_HEADER_LINE || area == ON_TAB_LINE)
 34762     {
 34763       int x0;
 34764       struct glyph *end;
 34765 
 34766       /* Kludge alert: mode_line_string takes X/Y in pixels, but
 34767          returns them in row/column units!  */
 34768       string = mode_line_string (w, area, &x, &y, &charpos,
 34769                                  &object, &dx, &dy, &width, &height);
 34770 
 34771       row = (area == ON_MODE_LINE
 34772              ? MATRIX_MODE_LINE_ROW (w->current_matrix)
 34773              : (area == ON_TAB_LINE
 34774                 ? MATRIX_TAB_LINE_ROW (w->current_matrix)
 34775                 : MATRIX_HEADER_LINE_ROW (w->current_matrix)));
 34776 
 34777       /* Find the glyph under the mouse pointer.  */
 34778       if (row->mode_line_p && row->enabled_p)
 34779         {
 34780           glyph = row_start_glyph = row->glyphs[TEXT_AREA];
 34781           end = glyph + row->used[TEXT_AREA];
 34782 
 34783           for (x0 = original_x_pixel;
 34784                glyph < end && x0 >= glyph->pixel_width;
 34785                ++glyph)
 34786             x0 -= glyph->pixel_width;
 34787 
 34788           if (glyph >= end)
 34789             glyph = NULL;
 34790         }
 34791     }
 34792   else
 34793     {
 34794       x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
 34795       /* Kludge alert: marginal_area_string takes X/Y in pixels, but
 34796          returns them in row/column units!  */
 34797       string = marginal_area_string (w, area, &x, &y, &charpos,
 34798                                      &object, &dx, &dy, &width, &height);
 34799     }
 34800 
 34801   Lisp_Object help = Qnil;
 34802 
 34803 #ifdef HAVE_WINDOW_SYSTEM
 34804   if (IMAGEP (object))
 34805     {
 34806       Lisp_Object image_map, hotspot;
 34807       if ((image_map = plist_get (XCDR (object), QCmap),
 34808            !NILP (image_map))
 34809           && (hotspot = find_hot_spot (image_map, dx, dy),
 34810               CONSP (hotspot))
 34811           && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 34812         {
 34813           Lisp_Object plist;
 34814 
 34815           /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
 34816              If so, we could look for mouse-enter, mouse-leave
 34817              properties in PLIST (and do something...).  */
 34818           hotspot = XCDR (hotspot);
 34819           if (CONSP (hotspot)
 34820               && (plist = XCAR (hotspot), CONSP (plist)))
 34821             {
 34822               pointer = plist_get (plist, Qpointer);
 34823               if (NILP (pointer))
 34824                 pointer = Qhand;
 34825               help = plist_get (plist, Qhelp_echo);
 34826               if (!NILP (help))
 34827                 {
 34828                   help_echo_string = help;
 34829                   XSETWINDOW (help_echo_window, w);
 34830                   help_echo_object = w->contents;
 34831                   help_echo_pos = charpos;
 34832                 }
 34833             }
 34834         }
 34835       if (NILP (pointer))
 34836         pointer = plist_get (XCDR (object), QCpointer);
 34837     }
 34838 #endif  /* HAVE_WINDOW_SYSTEM */
 34839 
 34840   /* CHARPOS can be beyond the last position of STRING due, e.g., to
 34841      min-width 'display' property.  Fix that, to let all the calls to
 34842      get-text-property below do their thing.  */
 34843   if (STRINGP (string))
 34844     pos = make_fixnum (min (charpos, SCHARS (string) - 1));
 34845 
 34846   /* Set the help text and mouse pointer.  If the mouse is on a part
 34847      of the mode line without any text (e.g. past the right edge of
 34848      the mode line text), use that windows's mode line help echo if it
 34849      has been set.  */
 34850   if (STRINGP (string) || area == ON_MODE_LINE || area == ON_HEADER_LINE
 34851       || area == ON_TAB_LINE)
 34852     {
 34853       /* Arrange to display the help by setting the global variables
 34854          help_echo_string, help_echo_object, and help_echo_pos.  */
 34855       if (NILP (help))
 34856         {
 34857           if (STRINGP (string))
 34858             help = Fget_text_property (pos, Qhelp_echo, string);
 34859 
 34860           if (!NILP (help))
 34861             {
 34862               help_echo_string = help;
 34863               XSETWINDOW (help_echo_window, w);
 34864               help_echo_object = string;
 34865               help_echo_pos = charpos;
 34866             }
 34867           else if (area == ON_MODE_LINE
 34868                    && !NILP (w->mode_line_help_echo))
 34869             {
 34870               help_echo_string =  w->mode_line_help_echo;
 34871               XSETWINDOW (help_echo_window, w);
 34872               help_echo_object = Qnil;
 34873               help_echo_pos = -1;
 34874             }
 34875         }
 34876 
 34877 #ifdef HAVE_WINDOW_SYSTEM
 34878       /* Change the mouse pointer according to what is under it.  */
 34879       if (FRAME_WINDOW_P (f))
 34880         {
 34881           bool draggable = (! WINDOW_BOTTOMMOST_P (w)
 34882                             || minibuf_level
 34883                             || NILP (Vresize_mini_windows));
 34884 
 34885           if (STRINGP (string))
 34886             {
 34887               cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34888 
 34889               if (NILP (pointer))
 34890                 pointer = Fget_text_property (pos, Qpointer, string);
 34891 
 34892               /* Change the mouse pointer according to what is under X/Y.  */
 34893               if (NILP (pointer)
 34894                   && (area == ON_MODE_LINE || area == ON_HEADER_LINE
 34895                       || area == ON_TAB_LINE))
 34896                 {
 34897                   Lisp_Object map;
 34898 
 34899                   map = Fget_text_property (pos, Qlocal_map, string);
 34900                   if (!KEYMAPP (map))
 34901                     map = Fget_text_property (pos, Qkeymap, string);
 34902                   if (!KEYMAPP (map) && draggable && area == ON_MODE_LINE)
 34903                     cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 34904                 }
 34905             }
 34906           else if (draggable && area == ON_MODE_LINE)
 34907             cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 34908           else if ((area == ON_MODE_LINE
 34909                     && WINDOW_BOTTOMMOST_P (w)
 34910                     && !FRAME_HAS_MINIBUF_P (f)
 34911                     && !NILP (Fframe_parameter
 34912                               (w->frame, Qdrag_with_mode_line)))
 34913                    || (((area == ON_HEADER_LINE
 34914                          && !NILP (Fframe_parameter
 34915                                    (w->frame, Qdrag_with_header_line)))
 34916                         || (area == ON_TAB_LINE
 34917                             && !NILP (Fframe_parameter
 34918                                       (w->frame, Qdrag_with_tab_line))))
 34919                        && WINDOW_TOPMOST_P (w)))
 34920             cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 34921           else
 34922             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34923         }
 34924 #endif
 34925     }
 34926 
 34927   /* Change the mouse face according to what is under X/Y.  */
 34928   bool mouse_face_shown = false;
 34929 
 34930   if (STRINGP (string))
 34931     {
 34932       mouse_face = Fget_text_property (pos, Qmouse_face, string);
 34933       if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
 34934           && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)
 34935               || (area == ON_TAB_LINE))
 34936           && glyph)
 34937         {
 34938           Lisp_Object b, e;
 34939 
 34940           struct glyph * tmp_glyph;
 34941 
 34942           int gpos;
 34943           int gseq_length;
 34944           int total_pixel_width;
 34945           ptrdiff_t begpos, endpos, ignore;
 34946 
 34947           int vpos, hpos;
 34948 
 34949           b = Fprevious_single_property_change (make_fixnum (charpos + 1),
 34950                                                 Qmouse_face, string, Qnil);
 34951           if (NILP (b))
 34952             begpos = 0;
 34953           else
 34954             begpos = XFIXNUM (b);
 34955 
 34956           e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
 34957           if (NILP (e))
 34958             endpos = SCHARS (string);
 34959           else
 34960             endpos = XFIXNUM (e);
 34961 
 34962           /* Calculate the glyph position GPOS of GLYPH in the
 34963              displayed string, relative to the beginning of the
 34964              highlighted part of the string.
 34965 
 34966              Note: GPOS is different from CHARPOS.  CHARPOS is the
 34967              position of GLYPH in the internal string object.  A mode
 34968              line string format has structures which are converted to
 34969              a flattened string by the Emacs Lisp interpreter.  The
 34970              internal string is an element of those structures.  The
 34971              displayed string is the flattened string.  */
 34972           tmp_glyph = row_start_glyph;
 34973           while (tmp_glyph < glyph
 34974                  && (!(EQ (tmp_glyph->object, glyph->object)
 34975                        && begpos <= tmp_glyph->charpos
 34976                        && tmp_glyph->charpos < endpos)))
 34977             tmp_glyph++;
 34978           gpos = glyph - tmp_glyph;
 34979 
 34980           /* Calculate the length GSEQ_LENGTH of the glyph sequence of
 34981              the highlighted part of the displayed string to which
 34982              GLYPH belongs.  Note: GSEQ_LENGTH is different from
 34983              SCHARS (STRING), because the latter returns the length of
 34984              the internal string.  */
 34985           for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 34986                tmp_glyph > glyph
 34987                  && (!(EQ (tmp_glyph->object, glyph->object)
 34988                        && begpos <= tmp_glyph->charpos
 34989                        && tmp_glyph->charpos < endpos));
 34990                tmp_glyph--)
 34991             ;
 34992           gseq_length = gpos + (tmp_glyph - glyph) + 1;
 34993 
 34994           /* Calculate the total pixel width of all the glyphs between
 34995              the beginning of the highlighted area and GLYPH.  */
 34996           total_pixel_width = 0;
 34997           for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
 34998             total_pixel_width += tmp_glyph->pixel_width;
 34999 
 35000           /* Pre calculation of re-rendering position.  Note: X is in
 35001              column units here, after the call to mode_line_string or
 35002              marginal_area_string.  */
 35003           hpos = x - gpos;
 35004           vpos = (area == ON_MODE_LINE
 35005                   ? (w->current_matrix)->nrows - 1
 35006                   : (area == ON_TAB_LINE
 35007                      ? 0
 35008                      : (w->current_matrix->tab_line_p
 35009                         ? 1
 35010                         : 0)));
 35011 
 35012           /* If GLYPH's position is included in the region that is
 35013              already drawn in mouse face, we have nothing to do.  */
 35014           if ( EQ (window, hlinfo->mouse_face_window)
 35015                && (!row->reversed_p
 35016                    ? (hlinfo->mouse_face_beg_col <= hpos
 35017                       && hpos < hlinfo->mouse_face_end_col)
 35018                    /* In R2L rows we swap BEG and END, see below.  */
 35019                    : (hlinfo->mouse_face_end_col <= hpos
 35020                       && hpos < hlinfo->mouse_face_beg_col))
 35021                && hlinfo->mouse_face_beg_row == vpos )
 35022             return;
 35023 
 35024           if (clear_mouse_face (hlinfo))
 35025             cursor = No_Cursor;
 35026 
 35027           if (!row->reversed_p)
 35028             {
 35029               hlinfo->mouse_face_beg_col = hpos;
 35030               hlinfo->mouse_face_beg_x   = original_x_pixel
 35031                                             - (total_pixel_width + dx);
 35032               hlinfo->mouse_face_end_col = hpos + gseq_length;
 35033               hlinfo->mouse_face_end_x   = 0;
 35034             }
 35035           else
 35036             {
 35037               /* In R2L rows, show_mouse_face expects BEG and END
 35038                  coordinates to be swapped.  */
 35039               hlinfo->mouse_face_end_col = hpos;
 35040               hlinfo->mouse_face_end_x   = original_x_pixel
 35041                                             - (total_pixel_width + dx);
 35042               hlinfo->mouse_face_beg_col = hpos + gseq_length;
 35043               hlinfo->mouse_face_beg_x   = 0;
 35044             }
 35045 
 35046           hlinfo->mouse_face_beg_row  = vpos;
 35047           hlinfo->mouse_face_end_row  = hlinfo->mouse_face_beg_row;
 35048           hlinfo->mouse_face_past_end = false;
 35049           hlinfo->mouse_face_window   = window;
 35050 
 35051           hlinfo->mouse_face_face_id =
 35052             face_at_string_position (w, string, charpos, 0, &ignore,
 35053                                      glyph->face_id, true, 0);
 35054 
 35055           show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35056           mouse_face_shown = true;
 35057 
 35058           if (NILP (pointer))
 35059             pointer = Qhand;
 35060         }
 35061     }
 35062 
 35063   /* If mouse-face doesn't need to be shown, clear any existing
 35064      mouse-face.  */
 35065   if ((area == ON_MODE_LINE || area == ON_HEADER_LINE
 35066        || area == ON_TAB_LINE) && !mouse_face_shown)
 35067     clear_mouse_face (hlinfo);
 35068 
 35069   define_frame_cursor1 (f, cursor, pointer);
 35070 }
 35071 
 35072 
 35073 /* EXPORT:
 35074    Take proper action when the mouse has moved to position X, Y on
 35075    frame F with regards to highlighting portions of display that have
 35076    mouse-face properties.  Also de-highlight portions of display where
 35077    the mouse was before, set the mouse pointer shape as appropriate
 35078    for the mouse coordinates, and activate help echo (tooltips).
 35079    X and Y can be negative or out of range.  */
 35080 
 35081 void
 35082 note_mouse_highlight (struct frame *f, int x, int y)
 35083 {
 35084   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35085   enum window_part part = ON_NOTHING;
 35086   Lisp_Object window;
 35087   struct window *w;
 35088   Emacs_Cursor cursor = No_Cursor;
 35089   Lisp_Object pointer = Qnil;  /* Takes precedence over cursor!  */
 35090   struct buffer *b;
 35091 
 35092   /* When a menu is active, don't highlight because this looks odd.  */
 35093 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NS) || defined (MSDOS)
 35094   if (popup_activated ())
 35095     return;
 35096 #endif
 35097 
 35098 #if defined (HAVE_HAIKU)
 35099   if (popup_activated_p)
 35100     return;
 35101 #endif
 35102 
 35103   if (!f->glyphs_initialized_p
 35104       || f->pointer_invisible)
 35105     return;
 35106 
 35107   hlinfo->mouse_face_mouse_x = x;
 35108   hlinfo->mouse_face_mouse_y = y;
 35109   hlinfo->mouse_face_mouse_frame = f;
 35110 
 35111   if (hlinfo->mouse_face_defer)
 35112     return;
 35113 
 35114   /* Which window is that in?  */
 35115   window = window_from_coordinates (f, x, y, &part, true, true);
 35116 
 35117   /* If displaying active text in another window, clear that.  */
 35118   if (! EQ (window, hlinfo->mouse_face_window)
 35119       /* Also clear if we move out of text area in same window.  */
 35120       || (!NILP (hlinfo->mouse_face_window)
 35121           && !NILP (window)
 35122           && part != ON_TEXT
 35123           && part != ON_MODE_LINE
 35124           && part != ON_HEADER_LINE
 35125           && part != ON_TAB_LINE))
 35126     clear_mouse_face (hlinfo);
 35127 
 35128   /* Reset help_echo_string.  It will get recomputed below.  */
 35129   help_echo_string = Qnil;
 35130 
 35131   /* Handle tab-bar highlight on mouse-capable TTY frames.  */
 35132   if (!FRAME_WINDOW_P (f)
 35133       && (y >= FRAME_MENU_BAR_LINES (f)
 35134           && y < FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f)))
 35135     {
 35136       int prop_idx;
 35137       bool ignore;
 35138       Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &ignore);
 35139 
 35140       if (!NILP (caption))
 35141         {
 35142           help_echo_object = help_echo_window = Qnil;
 35143           help_echo_pos = -1;
 35144           help_echo_string = AREF (f->tab_bar_items,
 35145                                    prop_idx * TAB_BAR_ITEM_NSLOTS
 35146                                    + TAB_BAR_ITEM_HELP);
 35147           if (NILP (help_echo_string))
 35148             help_echo_string = caption;
 35149         }
 35150     }
 35151 
 35152 #ifdef HAVE_WINDOW_SYSTEM
 35153   /* If the cursor is on the internal border of FRAME and FRAME's
 35154      internal border is draggable, provide some visual feedback.  */
 35155   if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
 35156       && !NILP (get_frame_param (f, Qdrag_internal_border)))
 35157     {
 35158       enum internal_border_part part = frame_internal_border_part (f, x, y);
 35159 
 35160       switch (part)
 35161         {
 35162         case INTERNAL_BORDER_NONE:
 35163           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35164             /* Reset cursor.  */
 35165             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35166           break;
 35167         case INTERNAL_BORDER_LEFT_EDGE:
 35168           cursor = FRAME_OUTPUT_DATA (f)->left_edge_cursor;
 35169           break;
 35170         case INTERNAL_BORDER_TOP_LEFT_CORNER:
 35171           cursor = FRAME_OUTPUT_DATA (f)->top_left_corner_cursor;
 35172           break;
 35173         case INTERNAL_BORDER_TOP_EDGE:
 35174           cursor = FRAME_OUTPUT_DATA (f)->top_edge_cursor;
 35175           break;
 35176         case INTERNAL_BORDER_TOP_RIGHT_CORNER:
 35177           cursor = FRAME_OUTPUT_DATA (f)->top_right_corner_cursor;
 35178           break;
 35179         case INTERNAL_BORDER_RIGHT_EDGE:
 35180           cursor = FRAME_OUTPUT_DATA (f)->right_edge_cursor;
 35181           break;
 35182         case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
 35183           cursor = FRAME_OUTPUT_DATA (f)->bottom_right_corner_cursor;
 35184           break;
 35185         case INTERNAL_BORDER_BOTTOM_EDGE:
 35186           cursor = FRAME_OUTPUT_DATA (f)->bottom_edge_cursor;
 35187           break;
 35188         case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
 35189           cursor = FRAME_OUTPUT_DATA (f)->bottom_left_corner_cursor;
 35190           break;
 35191         default:
 35192           /* This should not happen.  */
 35193           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35194             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35195         }
 35196 
 35197       if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35198         {
 35199           /* Do we really want a help echo here?  */
 35200           help_echo_string = build_string ("drag-mouse-1: resize frame");
 35201           goto set_cursor;
 35202         }
 35203     }
 35204 #endif /* HAVE_WINDOW_SYSTEM */
 35205 
 35206   /* Not on a window -> return.  */
 35207   if (!WINDOWP (window))
 35208     return;
 35209 
 35210   /* Convert to window-relative pixel coordinates.  */
 35211   w = XWINDOW (window);
 35212   frame_to_window_pixel_xy (w, &x, &y);
 35213 
 35214 #if defined (HAVE_WINDOW_SYSTEM)
 35215   /* Handle tab-bar window differently since it doesn't display a
 35216      buffer.  */
 35217   if (EQ (window, f->tab_bar_window))
 35218     {
 35219       note_tab_bar_highlight (f, x, y);
 35220       if (tab_bar__dragging_in_progress)
 35221         {
 35222           cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35223           goto set_cursor;
 35224         }
 35225       else
 35226         return;
 35227     }
 35228   else
 35229     {
 35230       /* The mouse might have pressed into the tab bar, but might
 35231          also have been released outside the tab bar, so
 35232          f->last_tab_bar_item must be reset, in order to make sure the
 35233          item can be still highlighted again in the future.  */
 35234       f->last_tab_bar_item = -1;
 35235     }
 35236 #endif
 35237 
 35238 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 35239   /* Handle tool-bar window differently since it doesn't display a
 35240      buffer.  */
 35241   if (EQ (window, f->tool_bar_window))
 35242     {
 35243       note_tool_bar_highlight (f, x, y);
 35244       return;
 35245     }
 35246 #endif
 35247 
 35248   /* Mouse is on the mode, header line or margin?  */
 35249   if (part == ON_MODE_LINE || part == ON_HEADER_LINE || part == ON_TAB_LINE
 35250       || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35251     {
 35252       note_mode_line_or_margin_highlight (window, x, y, part);
 35253 
 35254 #ifdef HAVE_WINDOW_SYSTEM
 35255       if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35256         {
 35257           cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35258           /* Show non-text cursor (Bug#16647).  */
 35259           goto set_cursor;
 35260         }
 35261       else
 35262 #endif
 35263         return;
 35264     }
 35265 
 35266 #ifdef HAVE_WINDOW_SYSTEM
 35267   if (part == ON_VERTICAL_BORDER)
 35268     {
 35269       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35270       help_echo_string = build_string ("drag-mouse-1: resize");
 35271       goto set_cursor;
 35272     }
 35273   else if (part == ON_RIGHT_DIVIDER)
 35274     {
 35275       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35276       help_echo_string = build_string ("drag-mouse-1: resize");
 35277       goto set_cursor;
 35278     }
 35279   else if (part == ON_BOTTOM_DIVIDER)
 35280     if (! WINDOW_BOTTOMMOST_P (w)
 35281         || minibuf_level
 35282         || NILP (Vresize_mini_windows))
 35283       {
 35284         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35285         help_echo_string = build_string ("drag-mouse-1: resize");
 35286         goto set_cursor;
 35287       }
 35288     else
 35289       cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35290   else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
 35291            || part == ON_VERTICAL_SCROLL_BAR
 35292            || part == ON_HORIZONTAL_SCROLL_BAR)
 35293     cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35294   else
 35295     cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 35296 #endif
 35297 
 35298   /* Are we in a window whose display is up to date?
 35299      And verify the buffer's text has not changed.  */
 35300   b = XBUFFER (w->contents);
 35301   if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
 35302     {
 35303       int hpos, vpos, dx, dy, area = LAST_AREA;
 35304       ptrdiff_t pos;
 35305       struct glyph *glyph;
 35306       Lisp_Object object;
 35307       Lisp_Object mouse_face = Qnil, position;
 35308       Lisp_Object *overlay_vec = NULL;
 35309       ptrdiff_t i, noverlays;
 35310       struct buffer *obuf;
 35311       ptrdiff_t obegv, ozv;
 35312       bool same_region;
 35313 
 35314       /* Find the glyph under X/Y.  */
 35315       glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
 35316 
 35317 #ifdef HAVE_WINDOW_SYSTEM
 35318       /* Look for :pointer property on image.  */
 35319       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 35320         {
 35321           struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 35322           if (img != NULL && IMAGEP (img->spec))
 35323             {
 35324               Lisp_Object image_map, hotspot;
 35325               if ((image_map = plist_get (XCDR (img->spec), QCmap),
 35326                    !NILP (image_map))
 35327                   && (hotspot = find_hot_spot (image_map,
 35328                                                glyph->slice.img.x + dx,
 35329                                                glyph->slice.img.y + dy),
 35330                       CONSP (hotspot))
 35331                   && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 35332                 {
 35333                   Lisp_Object plist;
 35334 
 35335                   /* Could check XCAR (hotspot) to see if we enter/leave
 35336                      this hot-spot.
 35337                      If so, we could look for mouse-enter, mouse-leave
 35338                      properties in PLIST (and do something...).  */
 35339                   hotspot = XCDR (hotspot);
 35340                   if (CONSP (hotspot)
 35341                       && (plist = XCAR (hotspot), CONSP (plist)))
 35342                     {
 35343                       pointer = plist_get (plist, Qpointer);
 35344                       if (NILP (pointer))
 35345                         pointer = Qhand;
 35346                       help_echo_string = plist_get (plist, Qhelp_echo);
 35347                       if (!NILP (help_echo_string))
 35348                         {
 35349                           help_echo_window = window;
 35350                           help_echo_object = glyph->object;
 35351                           help_echo_pos = glyph->charpos;
 35352                         }
 35353                     }
 35354                 }
 35355               if (NILP (pointer))
 35356                 pointer = plist_get (XCDR (img->spec), QCpointer);
 35357             }
 35358         }
 35359 #endif  /* HAVE_WINDOW_SYSTEM */
 35360 
 35361       /* Clear mouse face if X/Y not over text.  */
 35362       if (glyph == NULL
 35363           || area != TEXT_AREA
 35364           || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
 35365           /* Glyph's OBJECT is nil for glyphs inserted by the
 35366              display engine for its internal purposes, like truncation
 35367              and continuation glyphs and blanks beyond the end of
 35368              line's text on text terminals.  If we are over such a
 35369              glyph, we are not over any text.  */
 35370           || NILP (glyph->object)
 35371           /* R2L rows have a stretch glyph at their front, which
 35372              stands for no text, whereas L2R rows have no glyphs at
 35373              all beyond the end of text.  Treat such stretch glyphs
 35374              like we do with NULL glyphs in L2R rows.  */
 35375           || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
 35376               && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
 35377               && glyph->type == STRETCH_GLYPH
 35378               && glyph->avoid_cursor_p))
 35379         {
 35380           if (clear_mouse_face (hlinfo))
 35381             cursor = No_Cursor;
 35382           if (FRAME_WINDOW_P (f) && NILP (pointer))
 35383             {
 35384 #ifdef HAVE_WINDOW_SYSTEM
 35385               if (area != TEXT_AREA)
 35386                 cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35387               else
 35388                 pointer = Vvoid_text_area_pointer;
 35389 #endif
 35390             }
 35391           goto set_cursor;
 35392         }
 35393 
 35394       pos = glyph->charpos;
 35395       object = glyph->object;
 35396       if (!STRINGP (object) && !BUFFERP (object))
 35397         goto set_cursor;
 35398 
 35399       /* If we get an out-of-range value, return now; avoid an error.  */
 35400       if (BUFFERP (object) && pos > BUF_Z (b))
 35401         goto set_cursor;
 35402 
 35403       /* Make the window's buffer temporarily current for
 35404          overlays_at and compute_char_face.  */
 35405       obuf = current_buffer;
 35406       current_buffer = b;
 35407       obegv = BEGV;
 35408       ozv = ZV;
 35409       BEGV = BEG;
 35410       ZV = Z;
 35411 
 35412       /* Is this char mouse-active or does it have help-echo?  */
 35413       position = make_fixnum (pos);
 35414 
 35415       USE_SAFE_ALLOCA;
 35416 
 35417       if (BUFFERP (object))
 35418         {
 35419           /* Put all the overlays we want in a vector in overlay_vec.  */
 35420           GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL);
 35421           /* Sort overlays into increasing priority order.  */
 35422           noverlays = sort_overlays (overlay_vec, noverlays, w);
 35423         }
 35424       else
 35425         noverlays = 0;
 35426 
 35427       if (NILP (Vmouse_highlight))
 35428         {
 35429           clear_mouse_face (hlinfo);
 35430           goto check_help_echo;
 35431         }
 35432 
 35433       same_region = coords_in_mouse_face_p (w, hpos, vpos);
 35434 
 35435       if (same_region)
 35436         cursor = No_Cursor;
 35437 
 35438       /* Check mouse-face highlighting.  */
 35439       if (! same_region
 35440           /* If there exists an overlay with mouse-face overlapping
 35441              the one we are currently highlighting, we have to check
 35442              if we enter the overlapping overlay, and then highlight
 35443              only that.  Skip the check when mouse-face highlighting
 35444              is currently hidden to avoid Bug#30519.  */
 35445           || (!hlinfo->mouse_face_hidden
 35446               && OVERLAYP (hlinfo->mouse_face_overlay)
 35447               /* It's possible the overlay was deleted (Bug#35273).  */
 35448               && OVERLAY_BUFFER (hlinfo->mouse_face_overlay)
 35449               && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
 35450         {
 35451           /* Find the highest priority overlay with a mouse-face.  */
 35452           Lisp_Object overlay = Qnil;
 35453           for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
 35454             {
 35455               mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
 35456               if (!NILP (mouse_face))
 35457                 overlay = overlay_vec[i];
 35458             }
 35459 
 35460           /* If we're highlighting the same overlay as before, there's
 35461              no need to do that again.  */
 35462           if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
 35463             goto check_help_echo;
 35464 
 35465           /* Clear the display of the old active region, if any.  */
 35466           if (clear_mouse_face (hlinfo))
 35467             cursor = No_Cursor;
 35468 
 35469           /* Record the overlay, if any, to be highlighted.  */
 35470           hlinfo->mouse_face_overlay = overlay;
 35471 
 35472           /* If no overlay applies, get a text property.  */
 35473           if (NILP (overlay))
 35474             mouse_face = Fget_text_property (position, Qmouse_face, object);
 35475 
 35476           /* Next, compute the bounds of the mouse highlighting and
 35477              display it.  */
 35478           if (!NILP (mouse_face) && STRINGP (object))
 35479             {
 35480               /* The mouse-highlighting comes from a display string
 35481                  with a mouse-face.  */
 35482               Lisp_Object s, e;
 35483               ptrdiff_t ignore;
 35484 
 35485               s = Fprevious_single_property_change
 35486                 (make_fixnum (pos + 1), Qmouse_face, object, Qnil);
 35487               e = Fnext_single_property_change
 35488                 (position, Qmouse_face, object, Qnil);
 35489               if (NILP (s))
 35490                 s = make_fixnum (0);
 35491               if (NILP (e))
 35492                 e = make_fixnum (SCHARS (object));
 35493               mouse_face_from_string_pos (w, hlinfo, object,
 35494                                           XFIXNUM (s), XFIXNUM (e));
 35495               hlinfo->mouse_face_past_end = false;
 35496               hlinfo->mouse_face_window = window;
 35497               hlinfo->mouse_face_face_id
 35498                 = face_at_string_position (w, object, pos, 0, &ignore,
 35499                                            glyph->face_id, true, 0);
 35500               show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35501               cursor = No_Cursor;
 35502             }
 35503           else
 35504             {
 35505               /* The mouse-highlighting, if any, comes from an overlay
 35506                  or text property in the buffer.  */
 35507               Lisp_Object buffer UNINIT;
 35508               Lisp_Object disp_string UNINIT;
 35509 
 35510               if (STRINGP (object))
 35511                 {
 35512                   /* If we are on a display string with no mouse-face,
 35513                      check if the text under it has one.  */
 35514                   struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
 35515                   ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35516                   pos = string_buffer_position (object, start);
 35517                   if (pos > 0)
 35518                     {
 35519                       mouse_face = get_char_property_and_overlay
 35520                         (make_fixnum (pos), Qmouse_face, w->contents, &overlay);
 35521                       buffer = w->contents;
 35522                       disp_string = object;
 35523                     }
 35524                 }
 35525               else
 35526                 {
 35527                   buffer = object;
 35528                   disp_string = Qnil;
 35529                 }
 35530 
 35531               if (!NILP (mouse_face))
 35532                 {
 35533                   Lisp_Object before, after;
 35534                   Lisp_Object before_string, after_string;
 35535                   /* To correctly find the limits of mouse highlight
 35536                      in a bidi-reordered buffer, we must not use the
 35537                      optimization of limiting the search in
 35538                      previous-single-property-change and
 35539                      next-single-property-change, because
 35540                      rows_from_pos_range needs the real start and end
 35541                      positions to DTRT in this case.  That's because
 35542                      the first row visible in a window does not
 35543                      necessarily display the character whose position
 35544                      is the smallest.  */
 35545                   Lisp_Object lim1
 35546                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35547                     ? Fmarker_position (w->start)
 35548                     : Qnil;
 35549                   Lisp_Object lim2
 35550                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35551                     ? make_fixnum (BUF_Z (XBUFFER (buffer))
 35552                                    - w->window_end_pos)
 35553                     : Qnil;
 35554 
 35555                   if (NILP (overlay))
 35556                     {
 35557                       /* Handle the text property case.  */
 35558                       before = Fprevious_single_property_change
 35559                         (make_fixnum (pos + 1), Qmouse_face, buffer, lim1);
 35560                       after = Fnext_single_property_change
 35561                         (make_fixnum (pos), Qmouse_face, buffer, lim2);
 35562                       before_string = after_string = Qnil;
 35563                     }
 35564                   else
 35565                     {
 35566                       /* Handle the overlay case.  */
 35567                       before = Foverlay_start (overlay);
 35568                       after = Foverlay_end (overlay);
 35569                       before_string = Foverlay_get (overlay, Qbefore_string);
 35570                       after_string = Foverlay_get (overlay, Qafter_string);
 35571 
 35572                       if (!STRINGP (before_string)) before_string = Qnil;
 35573                       if (!STRINGP (after_string))  after_string = Qnil;
 35574                     }
 35575 
 35576                   mouse_face_from_buffer_pos (window, hlinfo, pos,
 35577                                               NILP (before)
 35578                                               ? 1
 35579                                               : XFIXNAT (before),
 35580                                               NILP (after)
 35581                                               ? BUF_Z (XBUFFER (buffer))
 35582                                               : XFIXNAT (after),
 35583                                               before_string, after_string,
 35584                                               disp_string);
 35585                   cursor = No_Cursor;
 35586                 }
 35587             }
 35588         }
 35589 
 35590     check_help_echo:
 35591 
 35592       /* Look for a `help-echo' property.  */
 35593       if (NILP (help_echo_string)) {
 35594         Lisp_Object help, overlay;
 35595 
 35596         /* Check overlays first.  */
 35597         help = overlay = Qnil;
 35598         for (i = noverlays - 1; i >= 0 && NILP (help); --i)
 35599           {
 35600             overlay = overlay_vec[i];
 35601             help = Foverlay_get (overlay, Qhelp_echo);
 35602           }
 35603 
 35604         if (!NILP (help))
 35605           {
 35606             help_echo_string = help;
 35607             help_echo_window = window;
 35608             help_echo_object = overlay;
 35609             help_echo_pos = pos;
 35610           }
 35611         else
 35612           {
 35613             Lisp_Object obj = glyph->object;
 35614             ptrdiff_t charpos = glyph->charpos;
 35615 
 35616             /* Try text properties.  */
 35617             if (STRINGP (obj)
 35618                 && charpos >= 0
 35619                 && charpos < SCHARS (obj))
 35620               {
 35621                 help = Fget_text_property (make_fixnum (charpos),
 35622                                            Qhelp_echo, obj);
 35623                 if (NILP (help))
 35624                   {
 35625                     /* If the string itself doesn't specify a help-echo,
 35626                        see if the buffer text ``under'' it does.  */
 35627                     struct glyph_row *r
 35628                       = MATRIX_ROW (w->current_matrix, vpos);
 35629                     ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35630                     ptrdiff_t p = string_buffer_position (obj, start);
 35631                     if (p > 0)
 35632                       {
 35633                         help = Fget_char_property (make_fixnum (p),
 35634                                                    Qhelp_echo, w->contents);
 35635                         if (!NILP (help))
 35636                           {
 35637                             charpos = p;
 35638                             obj = w->contents;
 35639                           }
 35640                       }
 35641                   }
 35642               }
 35643             else if (BUFFERP (obj)
 35644                      && charpos >= BEGV
 35645                      && charpos < ZV)
 35646               help = Fget_text_property (make_fixnum (charpos), Qhelp_echo,
 35647                                          obj);
 35648 
 35649             if (!NILP (help))
 35650               {
 35651                 help_echo_string = help;
 35652                 help_echo_window = window;
 35653                 help_echo_object = obj;
 35654                 help_echo_pos = charpos;
 35655               }
 35656           }
 35657       }
 35658 
 35659 #ifdef HAVE_WINDOW_SYSTEM
 35660       /* Look for a `pointer' property.  */
 35661       if (FRAME_WINDOW_P (f) && NILP (pointer))
 35662         {
 35663           /* Check overlays first.  */
 35664           for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
 35665             pointer = Foverlay_get (overlay_vec[i], Qpointer);
 35666 
 35667           if (NILP (pointer))
 35668             {
 35669               Lisp_Object obj = glyph->object;
 35670               ptrdiff_t charpos = glyph->charpos;
 35671 
 35672               /* Try text properties.  */
 35673               if (STRINGP (obj)
 35674                   && charpos >= 0
 35675                   && charpos < SCHARS (obj))
 35676                 {
 35677                   pointer = Fget_text_property (make_fixnum (charpos),
 35678                                                 Qpointer, obj);
 35679                   if (NILP (pointer))
 35680                     {
 35681                       /* If the string itself doesn't specify a pointer,
 35682                          see if the buffer text ``under'' it does.  */
 35683                       struct glyph_row *r
 35684                         = MATRIX_ROW (w->current_matrix, vpos);
 35685                       ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35686                       ptrdiff_t p = string_buffer_position (obj, start);
 35687                       if (p > 0)
 35688                         pointer = Fget_char_property (make_fixnum (p),
 35689                                                       Qpointer, w->contents);
 35690                     }
 35691                 }
 35692               else if (BUFFERP (obj)
 35693                        && charpos >= BEGV
 35694                        && charpos < ZV)
 35695                 pointer = Fget_text_property (make_fixnum (charpos),
 35696                                               Qpointer, obj);
 35697             }
 35698         }
 35699 #endif  /* HAVE_WINDOW_SYSTEM */
 35700 
 35701       BEGV = obegv;
 35702       ZV = ozv;
 35703       current_buffer = obuf;
 35704       SAFE_FREE ();
 35705     }
 35706 
 35707  set_cursor:
 35708   define_frame_cursor1 (f, cursor, pointer);
 35709 }
 35710 
 35711 
 35712 /* EXPORT for RIF:
 35713    Clear any mouse-face on window W.  This function is part of the
 35714    redisplay interface, and is called from try_window_id and similar
 35715    functions to ensure the mouse-highlight is off.  */
 35716 
 35717 void
 35718 gui_clear_window_mouse_face (struct window *w)
 35719 {
 35720   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 35721   Lisp_Object window;
 35722 
 35723   block_input ();
 35724   XSETWINDOW (window, w);
 35725   if (EQ (window, hlinfo->mouse_face_window))
 35726     clear_mouse_face (hlinfo);
 35727   unblock_input ();
 35728 }
 35729 
 35730 
 35731 /* EXPORT:
 35732    Just discard the mouse face information for frame F, if any.
 35733    This is used when the size of F is changed.  */
 35734 
 35735 void
 35736 cancel_mouse_face (struct frame *f)
 35737 {
 35738   Lisp_Object window;
 35739   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35740 
 35741   window = hlinfo->mouse_face_window;
 35742   if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
 35743     reset_mouse_highlight (hlinfo);
 35744 }
 35745 
 35746 
 35747 
 35748 /***********************************************************************
 35749                            Exposure Events
 35750  ***********************************************************************/
 35751 
 35752 #ifdef HAVE_WINDOW_SYSTEM
 35753 
 35754 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
 35755    which intersects rectangle R.  R is in window-relative coordinates.  */
 35756 
 35757 static void
 35758 expose_area (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r,
 35759              enum glyph_row_area area)
 35760 {
 35761   struct glyph *first = row->glyphs[area];
 35762   struct glyph *end = row->glyphs[area] + row->used[area];
 35763   struct glyph *last;
 35764   int first_x, start_x, x;
 35765 
 35766   if (area == TEXT_AREA && row->fill_line_p)
 35767     /* If row extends face to end of line write the whole line.  */
 35768     draw_glyphs (w, row->x, row, area,
 35769                  0, row->used[area],
 35770                  DRAW_NORMAL_TEXT, 0);
 35771   else
 35772     {
 35773       /* Set START_X to the window-relative start position for drawing glyphs of
 35774          AREA.  The first glyph of the text area can be partially visible.
 35775          The first glyphs of other areas cannot.  */
 35776       start_x = window_box_left_offset (w, area);
 35777       x = start_x;
 35778       if (area == TEXT_AREA)
 35779         x += row->x;
 35780 
 35781       /* Find the first glyph that must be redrawn.  */
 35782       while (first < end
 35783              && x + first->pixel_width < r->x)
 35784         {
 35785           x += first->pixel_width;
 35786           ++first;
 35787         }
 35788 
 35789       /* Find the last one.  */
 35790       last = first;
 35791       first_x = x;
 35792       /* Use a signed int intermediate value to avoid catastrophic
 35793          failures due to comparison between signed and unsigned, when
 35794          x is negative (can happen for wide images that are hscrolled).  */
 35795       int r_end = r->x + r->width;
 35796       while (last < end && x < r_end)
 35797         {
 35798           x += last->pixel_width;
 35799           ++last;
 35800         }
 35801 
 35802       /* Repaint.  */
 35803       if (last > first)
 35804         draw_glyphs (w, first_x - start_x, row, area,
 35805                      first - row->glyphs[area], last - row->glyphs[area],
 35806                      DRAW_NORMAL_TEXT, 0);
 35807     }
 35808 }
 35809 
 35810 
 35811 /* Redraw the parts of the glyph row ROW on window W intersecting
 35812    rectangle R.  R is in window-relative coordinates.  Value is
 35813    true if mouse-face was overwritten.  */
 35814 
 35815 static bool
 35816 expose_line (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r)
 35817 {
 35818   eassert (row->enabled_p);
 35819 
 35820   if (row->mode_line_p || w->pseudo_window_p)
 35821     draw_glyphs (w, 0, row, TEXT_AREA,
 35822                  0, row->used[TEXT_AREA],
 35823                  DRAW_NORMAL_TEXT, 0);
 35824   else
 35825     {
 35826       if (row->used[LEFT_MARGIN_AREA])
 35827         expose_area (w, row, r, LEFT_MARGIN_AREA);
 35828       if (row->used[TEXT_AREA])
 35829         expose_area (w, row, r, TEXT_AREA);
 35830       if (row->used[RIGHT_MARGIN_AREA])
 35831         expose_area (w, row, r, RIGHT_MARGIN_AREA);
 35832       draw_row_fringe_bitmaps (w, row);
 35833     }
 35834 
 35835   return row->mouse_face_p;
 35836 }
 35837 
 35838 
 35839 /* Redraw those parts of glyphs rows during expose event handling that
 35840    overlap other rows.  Redrawing of an exposed line writes over parts
 35841    of lines overlapping that exposed line; this function fixes that.
 35842 
 35843    W is the window being exposed.  FIRST_OVERLAPPING_ROW is the first
 35844    row in W's current matrix that is exposed and overlaps other rows.
 35845    LAST_OVERLAPPING_ROW is the last such row.  */
 35846 
 35847 static void
 35848 expose_overlaps (struct window *w,
 35849                  struct glyph_row *first_overlapping_row,
 35850                  struct glyph_row *last_overlapping_row,
 35851                  const Emacs_Rectangle *r)
 35852 {
 35853   struct glyph_row *row;
 35854 
 35855   for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
 35856     if (row->overlapping_p)
 35857       {
 35858         eassert (row->enabled_p && !row->mode_line_p);
 35859 
 35860         row->clip = r;
 35861         if (row->used[LEFT_MARGIN_AREA])
 35862           gui_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
 35863 
 35864         if (row->used[TEXT_AREA])
 35865           gui_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
 35866 
 35867         if (row->used[RIGHT_MARGIN_AREA])
 35868           gui_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
 35869         row->clip = NULL;
 35870       }
 35871 }
 35872 
 35873 
 35874 /* Return true if W's cursor intersects rectangle R.  */
 35875 
 35876 static bool
 35877 phys_cursor_in_rect_p (struct window *w, const Emacs_Rectangle *r)
 35878 {
 35879   Emacs_Rectangle cr, result;
 35880   struct glyph *cursor_glyph;
 35881   struct glyph_row *row;
 35882 
 35883   if (w->phys_cursor.vpos >= 0
 35884       && w->phys_cursor.vpos < w->current_matrix->nrows
 35885       && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
 35886           row->enabled_p)
 35887       && row->cursor_in_fringe_p)
 35888     {
 35889       /* Cursor is in the fringe.  */
 35890       cr.x = window_box_right_offset (w,
 35891                                       (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
 35892                                        ? RIGHT_MARGIN_AREA
 35893                                        : TEXT_AREA));
 35894       cr.y = row->y;
 35895       cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
 35896       cr.height = row->height;
 35897       return gui_intersect_rectangles (&cr, r, &result);
 35898     }
 35899 
 35900   cursor_glyph = get_phys_cursor_glyph (w);
 35901   if (cursor_glyph)
 35902     {
 35903       /* r is relative to W's box, but w->phys_cursor.x is relative
 35904          to left edge of W's TEXT area.  Adjust it.  */
 35905       cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
 35906       cr.y = w->phys_cursor.y;
 35907       cr.width = cursor_glyph->pixel_width;
 35908       cr.height = w->phys_cursor_height;
 35909       /* ++KFS: W32 version used W32-specific IntersectRect here, but
 35910          I assume the effect is the same -- and this is portable.  */
 35911       return gui_intersect_rectangles (&cr, r, &result);
 35912     }
 35913   /* If we don't understand the format, pretend we're not in the hot-spot.  */
 35914   return false;
 35915 }
 35916 
 35917 
 35918 /* EXPORT:
 35919    Draw a vertical window border to the right of window W if W doesn't
 35920    have vertical scroll bars.  */
 35921 
 35922 void
 35923 gui_draw_vertical_border (struct window *w)
 35924 {
 35925   struct frame *f = XFRAME (WINDOW_FRAME (w));
 35926 
 35927   /* We could do better, if we knew what type of scroll-bar the adjacent
 35928      windows (on either side) have...  But we don't :-(
 35929      However, I think this works ok.  ++KFS 2003-04-25 */
 35930 
 35931   /* Redraw borders between horizontally adjacent windows.  Don't
 35932      do it for frames with vertical scroll bars because either the
 35933      right scroll bar of a window, or the left scroll bar of its
 35934      neighbor will suffice as a border.  */
 35935   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
 35936     return;
 35937 
 35938   /* Note: It is necessary to redraw both the left and the right
 35939      borders, for when only this single window W is being
 35940      redisplayed.  */
 35941   if (!WINDOW_RIGHTMOST_P (w)
 35942       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
 35943     {
 35944       int x0, x1, y0, y1;
 35945 
 35946       window_box_edges (w, &x0, &y0, &x1, &y1);
 35947       y1 -= 1;
 35948 
 35949       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 35950         x1 -= 1;
 35951 
 35952       FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
 35953     }
 35954 
 35955   if (!WINDOW_LEFTMOST_P (w)
 35956       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
 35957     {
 35958       int x0, x1, y0, y1;
 35959 
 35960       window_box_edges (w, &x0, &y0, &x1, &y1);
 35961       y1 -= 1;
 35962 
 35963       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 35964         x0 -= 1;
 35965 
 35966       FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
 35967     }
 35968 }
 35969 
 35970 
 35971 /* Draw window dividers for window W.  */
 35972 
 35973 void
 35974 gui_draw_right_divider (struct window *w)
 35975 {
 35976   struct frame *f = WINDOW_XFRAME (w);
 35977 
 35978   if (w->mini || w->pseudo_window_p)
 35979     return;
 35980   else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 35981     {
 35982       int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
 35983       int x1 = WINDOW_RIGHT_EDGE_X (w);
 35984       int y0 = WINDOW_TOP_EDGE_Y (w);
 35985       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 35986 
 35987       /* If W is horizontally combined and has a right sibling, don't
 35988          draw over any bottom divider.  */
 35989       if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
 35990           && !NILP (w->parent)
 35991           && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
 35992           && !NILP (w->next))
 35993         y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 35994 
 35995       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 35996     }
 35997 }
 35998 
 35999 static void
 36000 gui_draw_bottom_divider (struct window *w)
 36001 {
 36002   struct frame *f = XFRAME (WINDOW_FRAME (w));
 36003 
 36004   if (w->mini || w->pseudo_window_p)
 36005     return;
 36006   else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36007     {
 36008       int x0 = WINDOW_LEFT_EDGE_X (w);
 36009       int x1 = WINDOW_RIGHT_EDGE_X (w);
 36010       int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 36011       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 36012       struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : NULL;
 36013 
 36014       /* If W is vertically combined and has a sibling below, don't draw
 36015          over any right divider.  */
 36016       if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
 36017           && p
 36018           && ((WINDOW_VERTICAL_COMBINATION_P (p)
 36019                && !NILP (w->next))
 36020               || (WINDOW_HORIZONTAL_COMBINATION_P (p)
 36021                   && NILP (w->next)
 36022                   && !NILP (p->parent)
 36023                   && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
 36024                   && !NILP (XWINDOW (p->parent)->next))))
 36025         x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
 36026 
 36027       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 36028     }
 36029 }
 36030 
 36031 /* Redraw the part of window W intersection rectangle FR.  Pixel
 36032    coordinates in FR are frame-relative.  Call this function with
 36033    input blocked.  Value is true if the exposure overwrites
 36034    mouse-face.  */
 36035 
 36036 static bool
 36037 expose_window (struct window *w, const Emacs_Rectangle *fr)
 36038 {
 36039   struct frame *f = XFRAME (w->frame);
 36040   Emacs_Rectangle wr, r;
 36041   bool mouse_face_overwritten_p = false;
 36042 
 36043   /* If window is not yet fully initialized, do nothing.  This can
 36044      happen when toolkit scroll bars are used and a window is split.
 36045      Reconfiguring the scroll bar will generate an expose for a newly
 36046      created window.  */
 36047   if (w->current_matrix == NULL)
 36048     return false;
 36049 
 36050   /* When we're currently updating the window, display and current
 36051      matrix usually don't agree.  Arrange for a thorough display
 36052      later.  */
 36053   if (w->must_be_updated_p)
 36054     {
 36055       SET_FRAME_GARBAGED (f);
 36056       return false;
 36057     }
 36058 
 36059   /* Frame-relative pixel rectangle of W.  */
 36060   wr.x = WINDOW_LEFT_EDGE_X (w);
 36061   wr.y = WINDOW_TOP_EDGE_Y (w);
 36062   wr.width = WINDOW_PIXEL_WIDTH (w);
 36063   wr.height = WINDOW_PIXEL_HEIGHT (w);
 36064 
 36065   if (gui_intersect_rectangles (fr, &wr, &r))
 36066     {
 36067       int yb = window_text_bottom_y (w);
 36068       struct glyph_row *row;
 36069       struct glyph_row *first_overlapping_row, *last_overlapping_row;
 36070 
 36071       redisplay_trace ("expose_window (%d, %d, %u, %u)\n",
 36072                        r.x, r.y, r.width, r.height);
 36073 
 36074       /* Convert to window coordinates.  */
 36075       r.x -= WINDOW_LEFT_EDGE_X (w);
 36076       r.y -= WINDOW_TOP_EDGE_Y (w);
 36077 
 36078       /* Turn off the cursor.  */
 36079       bool cursor_cleared_p = (!w->pseudo_window_p
 36080                                && phys_cursor_in_rect_p (w, &r));
 36081       if (cursor_cleared_p)
 36082         gui_clear_cursor (w);
 36083 
 36084       /* If the row containing the cursor extends face to end of line,
 36085          then expose_area might overwrite the cursor outside the
 36086          rectangle and thus notice_overwritten_cursor might clear
 36087          w->phys_cursor_on_p.  We remember the original value and
 36088          check later if it is changed.  */
 36089       bool phys_cursor_on_p = w->phys_cursor_on_p;
 36090 
 36091       /* Use a signed int intermediate value to avoid catastrophic
 36092          failures due to comparison between signed and unsigned, when
 36093          y0 or y1 is negative (can happen for tall images).  */
 36094       int r_bottom = r.y + r.height;
 36095 
 36096       /* We must temporarily switch to the window's buffer, in case
 36097          the fringe face has been remapped in that buffer's
 36098          face-remapping-alist, so that draw_row_fringe_bitmaps,
 36099          called from expose_line, will use the right face.  */
 36100       bool buffer_changed = false;
 36101       struct buffer *oldbuf = current_buffer;
 36102       if (!w->pseudo_window_p)
 36103         {
 36104           set_buffer_internal_1 (XBUFFER (w->contents));
 36105           buffer_changed = true;
 36106         }
 36107 
 36108       /* Update lines intersecting rectangle R.  */
 36109       first_overlapping_row = last_overlapping_row = NULL;
 36110       for (row = w->current_matrix->rows;
 36111            row->enabled_p;
 36112            ++row)
 36113         {
 36114           int y0 = row->y;
 36115           int y1 = MATRIX_ROW_BOTTOM_Y (row);
 36116 
 36117           if ((y0 >= r.y && y0 < r_bottom)
 36118               || (y1 > r.y && y1 < r_bottom)
 36119               || (r.y >= y0 && r.y < y1)
 36120               || (r_bottom > y0 && r_bottom < y1))
 36121             {
 36122               /* A header line may be overlapping, but there is no need
 36123                  to fix overlapping areas for them.  KFS 2005-02-12 */
 36124               if (row->overlapping_p && !row->mode_line_p)
 36125                 {
 36126                   if (first_overlapping_row == NULL)
 36127                     first_overlapping_row = row;
 36128                   last_overlapping_row = row;
 36129                 }
 36130 
 36131               row->clip = fr;
 36132               if (expose_line (w, row, &r))
 36133                 mouse_face_overwritten_p = true;
 36134               row->clip = NULL;
 36135             }
 36136           else if (row->overlapping_p)
 36137             {
 36138               /* We must redraw a row overlapping the exposed area.  */
 36139               if (y0 < r.y
 36140                   ? y0 + row->phys_height > r.y
 36141                   : y0 + row->ascent - row->phys_ascent < r.y +r.height)
 36142                 {
 36143                   if (first_overlapping_row == NULL)
 36144                     first_overlapping_row = row;
 36145                   last_overlapping_row = row;
 36146                 }
 36147             }
 36148 
 36149           if (y1 >= yb)
 36150             break;
 36151         }
 36152 
 36153       if (buffer_changed)
 36154         set_buffer_internal_1 (oldbuf);
 36155 
 36156       /* Display the mode line if there is one.  */
 36157       if (window_wants_mode_line (w)
 36158           && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
 36159               row->enabled_p)
 36160           && row->y < r_bottom)
 36161         {
 36162           if (expose_line (w, row, &r))
 36163             mouse_face_overwritten_p = true;
 36164         }
 36165 
 36166       if (!w->pseudo_window_p)
 36167         {
 36168           /* Fix the display of overlapping rows.  */
 36169           if (first_overlapping_row)
 36170             expose_overlaps (w, first_overlapping_row, last_overlapping_row,
 36171                              fr);
 36172 
 36173           /* Draw border between windows.  */
 36174           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 36175             gui_draw_right_divider (w);
 36176           else
 36177             gui_draw_vertical_border (w);
 36178 
 36179           if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36180             gui_draw_bottom_divider (w);
 36181 
 36182           /* Turn the cursor on again.  */
 36183           if (cursor_cleared_p
 36184               || (phys_cursor_on_p && !w->phys_cursor_on_p))
 36185             update_window_cursor (w, true);
 36186         }
 36187     }
 36188 
 36189   return mouse_face_overwritten_p;
 36190 }
 36191 
 36192 
 36193 
 36194 /* Redraw (parts) of all windows in the window tree rooted at W that
 36195    intersect R.  R contains frame pixel coordinates.  Value is
 36196    true if the exposure overwrites mouse-face.  */
 36197 
 36198 static bool
 36199 expose_window_tree (struct window *w, const Emacs_Rectangle *r)
 36200 {
 36201   struct frame *f = XFRAME (w->frame);
 36202   bool mouse_face_overwritten_p = false;
 36203 
 36204   while (w && !FRAME_GARBAGED_P (f))
 36205     {
 36206       mouse_face_overwritten_p
 36207         |= (WINDOWP (w->contents)
 36208             ? expose_window_tree (XWINDOW (w->contents), r)
 36209             : expose_window (w, r));
 36210 
 36211       w = NILP (w->next) ? NULL : XWINDOW (w->next);
 36212     }
 36213 
 36214   return mouse_face_overwritten_p;
 36215 }
 36216 
 36217 
 36218 /* EXPORT:
 36219    Redisplay an exposed area of frame F.  X and Y are the upper-left
 36220    corner of the exposed rectangle.  W and H are width and height of
 36221    the exposed area.  All are pixel values.  W or H zero means redraw
 36222    the entire frame.  */
 36223 
 36224 void
 36225 expose_frame (struct frame *f, int x, int y, int w, int h)
 36226 {
 36227   Emacs_Rectangle r;
 36228   bool mouse_face_overwritten_p = false;
 36229 
 36230   if (FRAME_GARBAGED_P (f))
 36231     {
 36232       redisplay_trace ("expose_frame garbaged\n");
 36233       return;
 36234     }
 36235 
 36236   /* If basic faces haven't been realized yet, there is no point in
 36237      trying to redraw anything.  This can happen when we get an expose
 36238      event while Emacs is starting, e.g. by moving another window.  */
 36239   if (FRAME_FACE_CACHE (f) == NULL
 36240       || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
 36241     {
 36242       redisplay_trace ("expose_frame no faces\n");
 36243       return;
 36244     }
 36245 
 36246   if (w == 0 || h == 0)
 36247     {
 36248       r.x = r.y = 0;
 36249       r.width = FRAME_TEXT_WIDTH (f);
 36250       r.height = FRAME_TEXT_HEIGHT (f);
 36251     }
 36252   else
 36253     {
 36254       r.x = x;
 36255       r.y = y;
 36256       r.width = w;
 36257       r.height = h;
 36258     }
 36259 
 36260   redisplay_trace ("expose_frame (%d, %d, %u, %u)\n",
 36261                    r.x, r.y, r.width, r.height);
 36262   mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
 36263 
 36264   if (WINDOWP (f->tab_bar_window))
 36265     mouse_face_overwritten_p
 36266       |= expose_window (XWINDOW (f->tab_bar_window), &r);
 36267 
 36268 #ifndef HAVE_EXT_TOOL_BAR
 36269   if (WINDOWP (f->tool_bar_window))
 36270     mouse_face_overwritten_p
 36271       |= expose_window (XWINDOW (f->tool_bar_window), &r);
 36272 #endif
 36273 
 36274 #ifdef HAVE_X_WINDOWS
 36275 #ifndef MSDOS
 36276 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
 36277   if (WINDOWP (f->menu_bar_window))
 36278     mouse_face_overwritten_p
 36279       |= expose_window (XWINDOW (f->menu_bar_window), &r);
 36280 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 36281 #endif
 36282 #endif
 36283 
 36284   /* Some window managers support a focus-follows-mouse style with
 36285      delayed raising of frames.  Imagine a partially obscured frame,
 36286      and moving the mouse into partially obscured mouse-face on that
 36287      frame.  The visible part of the mouse-face will be highlighted,
 36288      then the WM raises the obscured frame.  With at least one WM, KDE
 36289      2.1, Emacs is not getting any event for the raising of the frame
 36290      (even tried with SubstructureRedirectMask), only Expose events.
 36291      These expose events will draw text normally, i.e. not
 36292      highlighted.  Which means we must redo the highlight here.
 36293      Subsume it under ``we love X''.  --gerd 2001-08-15  */
 36294   /* Included in Windows version because Windows most likely does not
 36295      do the right thing if any third party tool offers
 36296      focus-follows-mouse with delayed raise.  --jason 2001-10-12  */
 36297   if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
 36298     {
 36299       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 36300       if (f == hlinfo->mouse_face_mouse_frame)
 36301         {
 36302           int mouse_x = hlinfo->mouse_face_mouse_x;
 36303           int mouse_y = hlinfo->mouse_face_mouse_y;
 36304           clear_mouse_face (hlinfo);
 36305           note_mouse_highlight (f, mouse_x, mouse_y);
 36306         }
 36307     }
 36308 }
 36309 
 36310 
 36311 /* EXPORT:
 36312    Determine the intersection of two rectangles R1 and R2.  Return
 36313    the intersection in *RESULT.  Value is true if RESULT is not
 36314    empty.  */
 36315 
 36316 bool
 36317 gui_intersect_rectangles (const Emacs_Rectangle *r1, const Emacs_Rectangle *r2,
 36318                           Emacs_Rectangle *result)
 36319 {
 36320   const Emacs_Rectangle *left, *right;
 36321   const Emacs_Rectangle *upper, *lower;
 36322   bool intersection_p = false;
 36323 
 36324   /* Rearrange so that R1 is the left-most rectangle.  */
 36325   if (r1->x < r2->x)
 36326     left = r1, right = r2;
 36327   else
 36328     left = r2, right = r1;
 36329 
 36330   /* X0 of the intersection is right.x0, if this is inside R1,
 36331      otherwise there is no intersection.  */
 36332   if (right->x <= left->x + left->width)
 36333     {
 36334       result->x = right->x;
 36335 
 36336       /* The right end of the intersection is the minimum of
 36337          the right ends of left and right.  */
 36338       result->width = (min (left->x + left->width, right->x + right->width)
 36339                        - result->x);
 36340 
 36341       /* Same game for Y.  */
 36342       if (r1->y < r2->y)
 36343         upper = r1, lower = r2;
 36344       else
 36345         upper = r2, lower = r1;
 36346 
 36347       /* The upper end of the intersection is lower.y0, if this is inside
 36348          of upper.  Otherwise, there is no intersection.  */
 36349       if (lower->y <= upper->y + upper->height)
 36350         {
 36351           result->y = lower->y;
 36352 
 36353           /* The lower end of the intersection is the minimum of the lower
 36354              ends of upper and lower.  */
 36355           result->height = (min (lower->y + lower->height,
 36356                                  upper->y + upper->height)
 36357                             - result->y);
 36358           intersection_p = true;
 36359         }
 36360     }
 36361 
 36362   return intersection_p;
 36363 }
 36364 
 36365 #endif /* HAVE_WINDOW_SYSTEM */
 36366 
 36367 
 36368 /***********************************************************************
 36369                             Initialization
 36370  ***********************************************************************/
 36371 
 36372 void
 36373 syms_of_xdisp (void)
 36374 {
 36375   Vwith_echo_area_save_vector = Qnil;
 36376   staticpro (&Vwith_echo_area_save_vector);
 36377 
 36378   Vmessage_stack = Qnil;
 36379   staticpro (&Vmessage_stack);
 36380 
 36381   /* Non-nil means don't actually do any redisplay.  */
 36382   DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
 36383 
 36384   DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
 36385 
 36386   DEFVAR_BOOL ("scroll-minibuffer-conservatively",
 36387                scroll_minibuffer_conservatively,
 36388                doc: /* Non-nil means scroll conservatively in minibuffer windows.
 36389 When the value is nil, scrolling in minibuffer windows obeys the
 36390 settings of `scroll-conservatively'.  */);
 36391   scroll_minibuffer_conservatively = true; /* bug#44070 */
 36392 
 36393   DEFVAR_BOOL ("inhibit-message", inhibit_message,
 36394               doc:  /* Non-nil means calls to `message' are not displayed.
 36395 They are still logged to the *Messages* buffer.
 36396 
 36397 Do NOT set this globally to a non-nil value, as doing that will
 36398 disable messages everywhere, including in I-search and other
 36399 places where they are necessary.  This variable is intended to
 36400 be let-bound around code that needs to disable messages temporarily. */);
 36401   inhibit_message = false;
 36402 
 36403   message_dolog_marker1 = Fmake_marker ();
 36404   staticpro (&message_dolog_marker1);
 36405   message_dolog_marker2 = Fmake_marker ();
 36406   staticpro (&message_dolog_marker2);
 36407   message_dolog_marker3 = Fmake_marker ();
 36408   staticpro (&message_dolog_marker3);
 36409 
 36410   defsubr (&Sset_buffer_redisplay);
 36411 #ifdef GLYPH_DEBUG
 36412   defsubr (&Sdump_frame_glyph_matrix);
 36413   defsubr (&Sdump_glyph_matrix);
 36414   defsubr (&Sdump_glyph_row);
 36415   defsubr (&Sdump_tab_bar_row);
 36416   defsubr (&Sdump_tool_bar_row);
 36417   defsubr (&Strace_redisplay);
 36418   defsubr (&Strace_to_stderr);
 36419 #endif
 36420 #ifdef HAVE_WINDOW_SYSTEM
 36421   defsubr (&Stab_bar_height);
 36422   defsubr (&Stool_bar_height);
 36423   defsubr (&Slookup_image_map);
 36424 #endif
 36425   defsubr (&Sline_pixel_height);
 36426   defsubr (&Sformat_mode_line);
 36427   defsubr (&Sinvisible_p);
 36428   defsubr (&Scurrent_bidi_paragraph_direction);
 36429   defsubr (&Swindow_text_pixel_size);
 36430   defsubr (&Sbuffer_text_pixel_size);
 36431   defsubr (&Smove_point_visually);
 36432   defsubr (&Sbidi_find_overridden_directionality);
 36433   defsubr (&Sdisplay__line_is_continued_p);
 36434   defsubr (&Sget_display_property);
 36435   defsubr (&Slong_line_optimizations_p);
 36436 
 36437   DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
 36438   DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
 36439   DEFSYM (Qoverriding_local_map, "overriding-local-map");
 36440   DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
 36441   DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
 36442   DEFSYM (Qeval, "eval");
 36443   DEFSYM (QCdata, ":data");
 36444 
 36445   /* Names of text properties relevant for redisplay.  */
 36446   DEFSYM (Qdisplay, "display");
 36447   DEFSYM (Qspace_width, "space-width");
 36448   DEFSYM (Qraise, "raise");
 36449   DEFSYM (Qslice, "slice");
 36450   DEFSYM (Qspace, "space");
 36451   DEFSYM (Qmargin, "margin");
 36452   DEFSYM (Qpointer, "pointer");
 36453   DEFSYM (Qleft_margin, "left-margin");
 36454   DEFSYM (Qright_margin, "right-margin");
 36455   DEFSYM (Qcenter, "center");
 36456   DEFSYM (Qline_height, "line-height");
 36457   DEFSYM (QCalign_to, ":align-to");
 36458   DEFSYM (QCrelative_width, ":relative-width");
 36459   DEFSYM (QCrelative_height, ":relative-height");
 36460   DEFSYM (QCeval, ":eval");
 36461   DEFSYM (QCpropertize, ":propertize");
 36462   DEFSYM (QCfile, ":file");
 36463   DEFSYM (Qfontified, "fontified");
 36464   DEFSYM (Qfontification_functions, "fontification-functions");
 36465   DEFSYM (Qlong_line_optimizations_in_fontification_functions,
 36466           "long-line-optimizations-in-fontification-functions");
 36467 
 36468   /* Name of the symbol which disables Lisp evaluation in 'display'
 36469      properties.  This is used by enriched.el.  */
 36470   DEFSYM (Qdisable_eval, "disable-eval");
 36471 
 36472   /* Name of the face used to highlight trailing whitespace.  */
 36473   DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
 36474 
 36475   /* Names of the faces used to display line numbers.  */
 36476   DEFSYM (Qline_number, "line-number");
 36477   DEFSYM (Qline_number_current_line, "line-number-current-line");
 36478   DEFSYM (Qline_number_major_tick, "line-number-major-tick");
 36479   DEFSYM (Qline_number_minor_tick, "line-number-minor-tick");
 36480   /* Name of a text property which disables line-number display.  */
 36481   DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
 36482 
 36483   /* Name of the face used to display fill column indicator character.  */
 36484   DEFSYM (Qfill_column_indicator, "fill-column-indicator");
 36485 
 36486   /* Name and number of the face used to highlight escape glyphs.  */
 36487   DEFSYM (Qescape_glyph, "escape-glyph");
 36488 
 36489   /* Name and number of the face used to highlight non-breaking
 36490      spaces/hyphens.  */
 36491   DEFSYM (Qnobreak_space, "nobreak-space");
 36492   DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
 36493 
 36494   /* The symbol 'image' which is the car of the lists used to represent
 36495      images in Lisp.  Also a tool bar style.  */
 36496   DEFSYM (Qimage, "image");
 36497 
 36498   /* Tool bar styles.  */
 36499   DEFSYM (Qtext, "text");
 36500   DEFSYM (Qboth, "both");
 36501   DEFSYM (Qboth_horiz, "both-horiz");
 36502   DEFSYM (Qtext_image_horiz, "text-image-horiz");
 36503 
 36504   /* The image map types.  */
 36505   DEFSYM (QCmap, ":map");
 36506   DEFSYM (QCpointer, ":pointer");
 36507   DEFSYM (Qrect, "rect");
 36508   DEFSYM (Qcircle, "circle");
 36509   DEFSYM (Qpoly, "poly");
 36510 
 36511   DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
 36512 
 36513   DEFSYM (Qgrow_only, "grow-only");
 36514   DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
 36515   DEFSYM (Qposition, "position");
 36516   DEFSYM (Qbuffer_position, "buffer-position");
 36517   DEFSYM (Qobject, "object");
 36518 
 36519   /* Cursor shapes.  */
 36520   DEFSYM (Qbar, "bar");
 36521   DEFSYM (Qhbar, "hbar");
 36522   DEFSYM (Qbox, "box");
 36523   DEFSYM (Qhollow, "hollow");
 36524 
 36525   /* Pointer shapes.  */
 36526   DEFSYM (Qhand, "hand");
 36527   DEFSYM (Qarrow, "arrow");
 36528   /* also Qtext */
 36529 
 36530   DEFSYM (Qdragging, "dragging");
 36531   DEFSYM (Qdropping, "dropping");
 36532   DEFSYM (Qdrag_source, "drag-source");
 36533 
 36534   DEFSYM (Qdrag_with_mode_line, "drag-with-mode-line");
 36535   DEFSYM (Qdrag_with_header_line, "drag-with-header-line");
 36536   DEFSYM (Qdrag_with_tab_line, "drag-with-tab-line");
 36537 
 36538   DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
 36539 
 36540   list_of_error = list1 (Qerror);
 36541   staticpro (&list_of_error);
 36542 
 36543   /* Values of those variables at last redisplay are stored as
 36544      properties on 'overlay-arrow-position' symbol.  However, if
 36545      Voverlay_arrow_position is a marker, last-arrow-position is its
 36546      numerical position.  */
 36547   DEFSYM (Qlast_arrow_position, "last-arrow-position");
 36548   DEFSYM (Qlast_arrow_string, "last-arrow-string");
 36549 
 36550   /* Alternative overlay-arrow-string and overlay-arrow-bitmap
 36551      properties on a symbol in overlay-arrow-variable-list.  */
 36552   DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
 36553   DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
 36554 
 36555   echo_buffer[0] = echo_buffer[1] = Qnil;
 36556   staticpro (&echo_buffer[0]);
 36557   staticpro (&echo_buffer[1]);
 36558 
 36559   echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
 36560   staticpro (&echo_area_buffer[0]);
 36561   staticpro (&echo_area_buffer[1]);
 36562 
 36563   DEFVAR_LISP ("messages-buffer-name", Vmessages_buffer_name,
 36564     doc: /* The name of the buffer where messages are logged.
 36565 This is normally \"\*Messages*\", but can be rebound by packages that
 36566 wish to redirect messages to a different buffer.  (If the buffer
 36567 doesn't exist, it will be created and put into
 36568 `messages-buffer-mode'.)  */);
 36569   Vmessages_buffer_name = build_string ("*Messages*");
 36570 
 36571   mode_line_proptrans_alist = Qnil;
 36572   staticpro (&mode_line_proptrans_alist);
 36573   mode_line_string_list = Qnil;
 36574   staticpro (&mode_line_string_list);
 36575   mode_line_string_face = Qnil;
 36576   staticpro (&mode_line_string_face);
 36577   mode_line_string_face_prop = Qnil;
 36578   staticpro (&mode_line_string_face_prop);
 36579   Vmode_line_unwind_vector = Qnil;
 36580   staticpro (&Vmode_line_unwind_vector);
 36581 
 36582   DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
 36583 
 36584   help_echo_string = Qnil;
 36585   staticpro (&help_echo_string);
 36586   help_echo_object = Qnil;
 36587   staticpro (&help_echo_object);
 36588   help_echo_window = Qnil;
 36589   staticpro (&help_echo_window);
 36590   previous_help_echo_string = Qnil;
 36591   staticpro (&previous_help_echo_string);
 36592   help_echo_pos = -1;
 36593 
 36594   DEFSYM (Qright_to_left, "right-to-left");
 36595   DEFSYM (Qleft_to_right, "left-to-right");
 36596   defsubr (&Sbidi_resolved_levels);
 36597 
 36598 #ifdef HAVE_WINDOW_SYSTEM
 36599   DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
 36600     doc: /* Non-nil means draw block cursor as wide as the glyph under it.
 36601 For example, if a block cursor is over a tab, it will be drawn as
 36602 wide as that tab on the display.  */);
 36603   x_stretch_cursor_p = 0;
 36604 #endif
 36605 
 36606   DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
 36607     doc: /* Non-nil means highlight trailing whitespace.
 36608 The face used for trailing whitespace is `trailing-whitespace'.  */);
 36609   Vshow_trailing_whitespace = Qnil;
 36610 
 36611   DEFVAR_LISP ("mode-line-compact", Vmode_line_compact,
 36612     doc: /* Non-nil means that mode lines should be compact.
 36613 This means that repeating spaces will be replaced with a single space.
 36614 If this variable is `long', only mode lines that are wider than the
 36615 currently selected window are compressed. */);
 36616   Vmode_line_compact = Qnil;
 36617   DEFSYM (Qlong, "long");
 36618 
 36619   DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
 36620     doc: /* Control highlighting of non-ASCII space and hyphen chars.
 36621 If the value is t, Emacs highlights non-ASCII chars which have the
 36622 same appearance as an ASCII space or hyphen, using the `nobreak-space'
 36623 or `nobreak-hyphen' face respectively.
 36624 
 36625 All of the non-ASCII characters in the Unicode horizontal whitespace
 36626 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 36627 U+2011 (non-breaking hyphen) are affected.
 36628 
 36629 Any other non-nil value means to display these characters as an escape
 36630 glyph followed by an ordinary space or hyphen.
 36631 
 36632 A value of nil means no special handling of these characters.  */);
 36633   Vnobreak_char_display = Qt;
 36634 
 36635   DEFVAR_BOOL ("nobreak-char-ascii-display", nobreak_char_ascii_display,
 36636     doc: /* Control display of non-ASCII space and hyphen chars.
 36637 If the value of this variable is nil, the default, Emacs displays
 36638 non-ASCII chars which have the same appearance as an ASCII space
 36639 or hyphen as themselves, with the `nobreak-space' or `nobreak-hyphen'
 36640 face, respectively.
 36641 
 36642 If the value is t, these characters are displayed as their ASCII
 36643 counterparts: whitespace characters as ASCII space, hyphen characters
 36644 as ASCII hyphen (a.k.a. \"dash\"), using the `nobreak-space' or
 36645 the `nobreak-hyphen' face.
 36646 
 36647 This variable has effect only if `nobreak-char-display' is t;
 36648 otherwise it is ignored.
 36649 
 36650 All of the non-ASCII characters in the Unicode horizontal whitespace
 36651 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 36652 U+2011 (non-breaking hyphen) are affected.  */);
 36653   nobreak_char_ascii_display = false;
 36654 
 36655   DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
 36656     doc: /* The pointer shape to show in void text areas.
 36657 A value of nil means to show the text pointer.  Other options are
 36658 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
 36659 `hourglass'.  */);
 36660   Vvoid_text_area_pointer = Qarrow;
 36661 
 36662   DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
 36663     doc: /* Non-nil means don't actually do any redisplay.
 36664 This is used for internal purposes.  */);
 36665   Vinhibit_redisplay = Qnil;
 36666 
 36667   DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
 36668     doc: /* String (or mode line construct) included (normally) in `mode-line-misc-info'.  */);
 36669   Vglobal_mode_string = Qnil;
 36670 
 36671   DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
 36672     doc: /* Marker for where to display an arrow on top of the buffer text.
 36673 This must be the beginning of a line in order to work.
 36674 See also `overlay-arrow-string'.  */);
 36675   Voverlay_arrow_position = Qnil;
 36676 
 36677   DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
 36678     doc: /* String to display as an arrow in non-window frames.
 36679 See also `overlay-arrow-position'.  */);
 36680   Voverlay_arrow_string = build_pure_c_string ("=>");
 36681 
 36682   DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
 36683     doc: /* List of variables (symbols) which hold markers for overlay arrows.
 36684 The symbols on this list are examined during redisplay to determine
 36685 where to display overlay arrows.  */);
 36686   Voverlay_arrow_variable_list
 36687     = list1 (intern_c_string ("overlay-arrow-position"));
 36688 
 36689   DEFVAR_INT ("scroll-step", emacs_scroll_step,
 36690     doc: /* The number of lines to try scrolling a window by when point moves out.
 36691 If that fails to bring point back on frame, point is centered instead.
 36692 If this is zero, point is always centered after it moves off frame.
 36693 If you want scrolling to always be a line at a time, you should set
 36694 `scroll-conservatively' to a large value rather than set this to 1.  */);
 36695 
 36696   DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
 36697     doc: /* Scroll up to this many lines, to bring point back on screen.
 36698 If point moves off-screen, redisplay will scroll by up to
 36699 `scroll-conservatively' lines in order to bring point just barely
 36700 onto the screen again.  If that cannot be done, then redisplay
 36701 recenters point as usual.
 36702 
 36703 If the value is greater than 100, redisplay will never recenter point,
 36704 but will always scroll just enough text to bring point into view, even
 36705 if you move far away.
 36706 
 36707 A value of zero means always recenter point if it moves off screen.  */);
 36708   scroll_conservatively = 0;
 36709 
 36710   DEFVAR_INT ("scroll-margin", scroll_margin,
 36711     doc: /* Number of lines of margin at the top and bottom of a window.
 36712 Trigger automatic scrolling whenever point gets within this many lines
 36713 of the top or bottom of the window (see info node `Auto Scrolling').  */);
 36714   scroll_margin = 0;
 36715 
 36716   DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
 36717     doc: /* Maximum effective value of `scroll-margin'.
 36718 Given as a fraction of the current window's lines.  The value should
 36719 be a floating point number between 0.0 and 0.5.  The effective maximum
 36720 is limited to (/ (1- window-lines) 2).  Non-float values for this
 36721 variable are ignored and the default 0.25 is used instead.  */);
 36722   Vmaximum_scroll_margin = make_float (0.25);
 36723 
 36724   DEFVAR_LISP ("display-pixels-per-inch",  Vdisplay_pixels_per_inch,
 36725     doc: /* Pixels per inch value for non-window system displays.
 36726 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI).  */);
 36727   Vdisplay_pixels_per_inch = make_float (72.0);
 36728 
 36729 #ifdef GLYPH_DEBUG
 36730   DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask.  */);
 36731 #endif
 36732 
 36733   DEFVAR_LISP ("truncate-partial-width-windows",
 36734                Vtruncate_partial_width_windows,
 36735     doc: /* Non-nil means truncate lines in windows narrower than the frame.
 36736 For an integer value, truncate lines in each window narrower than the
 36737 full frame width, provided the total window width in column units is less
 36738 than that integer; otherwise, respect the value of `truncate-lines'.
 36739 The total width of the window is as returned by `window-total-width', it
 36740 includes the fringes, the continuation and truncation glyphs, the
 36741 display margins (if any), and the scroll bar
 36742 
 36743 For any other non-nil value, truncate lines in all windows that do
 36744 not span the full frame width.
 36745 
 36746 A value of nil means to respect the value of `truncate-lines'.
 36747 
 36748 If `word-wrap' is enabled, you might want to reduce the value of this.
 36749 
 36750 Don't set this to a non-nil value when `visual-line-mode' is
 36751 turned on, as it could produce confusing results.  */);
 36752   Vtruncate_partial_width_windows = make_fixnum (50);
 36753 
 36754   DEFVAR_BOOL("word-wrap-by-category", word_wrap_by_category, doc: /*
 36755     Non-nil means also wrap after characters of a certain category.
 36756 Normally when `word-wrap' is on, Emacs only breaks lines after
 36757 whitespace characters.  When this option is turned on, Emacs also
 36758 breaks lines after characters that have the "|" category (defined in
 36759 characters.el).  This is useful for allowing breaking after CJK
 36760 characters and improves the word-wrapping for CJK text mixed with
 36761 Latin text.
 36762 
 36763 If this variable is set using Customize, Emacs automatically loads
 36764 kinsoku.el.  When kinsoku.el is loaded, Emacs respects kinsoku rules
 36765 when breaking lines.  That means characters with the ">" category
 36766 don't appear at the beginning of a line (e.g., FULLWIDTH COMMA), and
 36767 characters with the "<" category don't appear at the end of a line
 36768 (e.g., LEFT DOUBLE ANGLE BRACKET).  */);
 36769   word_wrap_by_category = false;
 36770 
 36771   DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
 36772     doc: /* Maximum buffer size for which line number should be displayed.
 36773 If the buffer is bigger than this, the line number does not appear
 36774 in the mode line.  A value of nil means no limit.  */);
 36775   Vline_number_display_limit = Qnil;
 36776 
 36777   DEFVAR_INT ("line-number-display-limit-width",
 36778               line_number_display_limit_width,
 36779     doc: /* Maximum line width (in characters) for line number display.
 36780 If the average length of the lines near point is bigger than this, then the
 36781 line number may be omitted from the mode line.  */);
 36782   line_number_display_limit_width = 200;
 36783 
 36784   DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
 36785     doc: /* Non-nil means highlight active region even in nonselected windows.
 36786 When nil (the default), the active region is only highlighted when
 36787 the window is selected.  */);
 36788   highlight_nonselected_windows = false;
 36789 
 36790   DEFVAR_BOOL ("multiple-frames", multiple_frames,
 36791     doc: /* Non-nil if more than one frame is visible on this display.
 36792 Minibuffer-only frames don't count, but iconified frames do.
 36793 This variable is not guaranteed to be accurate except while processing
 36794 `frame-title-format' and `icon-title-format'.  */);
 36795 
 36796   DEFVAR_LISP ("frame-title-format", Vframe_title_format,
 36797     doc: /* Template for displaying the title bar of visible frames.
 36798 \(Assuming the window manager supports this feature.)
 36799 
 36800 This variable has the same structure as `mode-line-format', except that
 36801 the %c, %C, and %l constructs are ignored.  It is used only on frames for
 36802 which no explicit name has been set (see `modify-frame-parameters').  */);
 36803 
 36804   DEFVAR_LISP ("icon-title-format", Vicon_title_format,
 36805     doc: /* Template for displaying the title bar of an iconified frame.
 36806 \(Assuming the window manager supports this feature.)
 36807 If the value is a string, it should have the same structure
 36808 as `mode-line-format' (which see), and is used only on frames
 36809 for which no explicit name has been set \(see `modify-frame-parameters').
 36810 If the value is t, that means use `frame-title-format' for
 36811 iconified frames.  */);
 36812   /* Do not nest calls to pure_list.  This works around a bug in
 36813      Oracle Developer Studio 12.6.  */
 36814   Lisp_Object icon_title_name_format
 36815     = pure_list (empty_unibyte_string,
 36816                  build_pure_c_string ("%b - GNU Emacs at "),
 36817                  intern_c_string ("system-name"));
 36818   Vicon_title_format
 36819     = Vframe_title_format
 36820     = pure_list (intern_c_string ("multiple-frames"),
 36821                  build_pure_c_string ("%b"),
 36822                  icon_title_name_format);
 36823 
 36824   DEFVAR_LISP ("message-log-max", Vmessage_log_max,
 36825     doc: /* Maximum number of lines to keep in the message log buffer.
 36826 If nil, disable message logging.  If t, log messages but don't truncate
 36827 the buffer when it becomes large.  */);
 36828   Vmessage_log_max = make_fixnum (1000);
 36829 
 36830   DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
 36831     doc: /* List of functions to call before redisplaying a window with scrolling.
 36832 Each function is called with two arguments, the window and its new
 36833 display-start position.
 36834 These functions are called whenever the `window-start' marker is modified,
 36835 either to point into another buffer (e.g. via `set-window-buffer') or another
 36836 place in the same buffer.
 36837 When each function is called, the `window-start' marker of its window
 36838 argument has been already set to the new value, and the buffer which that
 36839 window will display is set to be the current buffer.
 36840 Note that the value of `window-end' is not valid when these functions are
 36841 called.
 36842 
 36843 Warning: Do not use this feature to alter the way the window
 36844 is scrolled.  It is not designed for that, and such use probably won't
 36845 work.  */);
 36846   Vwindow_scroll_functions = Qnil;
 36847 
 36848   DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
 36849      doc: /* Non-nil means autoselect window with mouse pointer.
 36850 If nil, do not autoselect windows.
 36851 A positive number means delay autoselection by that many seconds: a
 36852 window is autoselected only after the mouse has remained in that
 36853 window for the duration of the delay.
 36854 A negative number has a similar effect, but causes windows to be
 36855 autoselected only after the mouse has stopped moving.  (Because of
 36856 the way Emacs compares mouse events, you will occasionally wait twice
 36857 that time before the window gets selected.)
 36858 Any other value means to autoselect window instantaneously when the
 36859 mouse pointer enters it.
 36860 
 36861 Autoselection selects the minibuffer only if it is active, and never
 36862 unselects the minibuffer if it is active.
 36863 
 36864 If you want to use the mouse to autoselect a window on another frame,
 36865 make sure that (1) your window manager has focus follow the mouse and
 36866 (2) the value of the option `focus-follows-mouse' matches the policy
 36867 of your window manager.  */);
 36868   Vmouse_autoselect_window = Qnil;
 36869 
 36870   DEFVAR_LISP ("auto-resize-tab-bars", Vauto_resize_tab_bars,
 36871     doc: /* Non-nil means automatically resize tab-bars.
 36872 This dynamically changes the tab-bar's height to the minimum height
 36873 that is needed to make all tab-bar items visible.
 36874 If value is `grow-only', the tab-bar's height is only increased
 36875 automatically; to decrease the tab-bar height, use \\[recenter],
 36876 after setting `recenter-redisplay' to the value of t.  */);
 36877   Vauto_resize_tab_bars = Qt;
 36878 
 36879   DEFVAR_BOOL ("auto-raise-tab-bar-buttons", auto_raise_tab_bar_buttons_p,
 36880     doc: /* Non-nil means raise tab-bar buttons when the mouse moves over them.  */);
 36881   auto_raise_tab_bar_buttons_p = true;
 36882 
 36883   DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
 36884     doc: /* Non-nil means automatically resize tool-bars.
 36885 This dynamically changes the tool-bar's height to the minimum height
 36886 that is needed to make all tool-bar items visible.
 36887 If value is `grow-only', the tool-bar's height is only increased
 36888 automatically; to decrease the tool-bar height, use \\[recenter],
 36889 after setting `recenter-redisplay' to the value of t.  */);
 36890   Vauto_resize_tool_bars = Qt;
 36891 
 36892   DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
 36893     doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them.  */);
 36894   auto_raise_tool_bar_buttons_p = true;
 36895 
 36896   DEFVAR_LISP ("make-cursor-line-fully-visible", Vmake_cursor_line_fully_visible,
 36897     doc: /* Whether to scroll the window if the cursor line is not fully visible.
 36898 If the value is non-nil, Emacs scrolls or recenters the window to make
 36899 the cursor line fully visible.  The value could also be a function, which
 36900 is called with a single argument, the window to be scrolled, and should
 36901 return non-nil if the partially-visible cursor requires scrolling the
 36902 window, nil if it's okay to leave the cursor partially-visible.  */);
 36903   Vmake_cursor_line_fully_visible = Qt;
 36904   DEFSYM (Qmake_cursor_line_fully_visible, "make-cursor-line-fully-visible");
 36905 
 36906   DEFVAR_BOOL ("make-window-start-visible", make_window_start_visible,
 36907     doc: /* Whether to ensure `window-start' position is never invisible.  */);
 36908   make_window_start_visible = false;
 36909   DEFSYM (Qmake_window_start_visible, "make-window-start-visible");
 36910   Fmake_variable_buffer_local (Qmake_window_start_visible);
 36911 
 36912   DEFSYM (Qclose_tab, "close-tab");
 36913   DEFVAR_LISP ("tab-bar-border", Vtab_bar_border,
 36914     doc: /* Border below tab-bar in pixels.
 36915 If an integer, use it as the height of the border.
 36916 If it is one of `internal-border-width' or `border-width', use the
 36917 value of the corresponding frame parameter.
 36918 Otherwise, no border is added below the tab-bar.  */);
 36919   Vtab_bar_border = Qinternal_border_width;
 36920 
 36921   DEFVAR_LISP ("tab-bar-button-margin", Vtab_bar_button_margin,
 36922     doc: /* Margin around tab-bar buttons in pixels.
 36923 If an integer, use that for both horizontal and vertical margins.
 36924 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 36925 HORZ specifying the horizontal margin, and VERT specifying the
 36926 vertical margin.  */);
 36927   Vtab_bar_button_margin = make_fixnum (DEFAULT_TAB_BAR_BUTTON_MARGIN);
 36928 
 36929   DEFVAR_INT ("tab-bar-button-relief", tab_bar_button_relief,
 36930     doc: /* Relief thickness of tab-bar buttons.  */);
 36931   tab_bar_button_relief = DEFAULT_TAB_BAR_BUTTON_RELIEF;
 36932 
 36933   DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
 36934     doc: /* Border below tool-bar in pixels.
 36935 If an integer, use it as the height of the border.
 36936 If it is one of `internal-border-width' or `border-width', use the
 36937 value of the corresponding frame parameter.
 36938 Otherwise, no border is added below the tool-bar.  */);
 36939   Vtool_bar_border = Qinternal_border_width;
 36940 
 36941   DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
 36942     doc: /* Margin around tool-bar buttons in pixels.
 36943 If an integer, use that for both horizontal and vertical margins.
 36944 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 36945 HORZ specifying the horizontal margin, and VERT specifying the
 36946 vertical margin.  */);
 36947   Vtool_bar_button_margin = make_fixnum (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
 36948 
 36949   DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
 36950     doc: /* Relief thickness of tool-bar buttons.  */);
 36951   tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
 36952 
 36953   DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
 36954     doc: /* Tool bar style to use.
 36955 It can be one of
 36956  image            - show images only
 36957  text             - show text only
 36958  both             - show both, text below image
 36959  both-horiz       - show text to the right of the image
 36960  text-image-horiz - show text to the left of the image
 36961  any other        - use system default or image if no system default.
 36962 
 36963 This variable only affects the GTK+ toolkit version of Emacs.  */);
 36964   Vtool_bar_style = Qnil;
 36965 
 36966   DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
 36967     doc: /* Maximum number of characters a label can have to be shown.
 36968 The tool bar style must also show labels for this to have any effect, see
 36969 `tool-bar-style'.  */);
 36970   tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
 36971 
 36972   DEFVAR_LISP ("fontification-functions", Vfontification_functions,
 36973     doc: /* List of functions to call to fontify regions of text.
 36974 Each function is called with one argument POS.  Functions must
 36975 fontify a region starting at POS in the current buffer, and give
 36976 fontified regions the property `fontified' with a non-nil value.
 36977 
 36978 Note that, when `long-line-optimizations-p' is non-nil in the buffer,
 36979 these functions are called as if they were in a `with-restriction' form,
 36980 with a `long-line-optimizations-in-fontification-functions' label and
 36981 with the buffer narrowed to a portion around POS whose size is
 36982 specified by `long-line-optimizations-region-size'.  */);
 36983   Vfontification_functions = Qnil;
 36984   Fmake_variable_buffer_local (Qfontification_functions);
 36985 
 36986   DEFVAR_BOOL ("unibyte-display-via-language-environment",
 36987                unibyte_display_via_language_environment,
 36988     doc: /* Non-nil means display unibyte text according to language environment.
 36989 Specifically, this means that raw bytes in the range 160-255 decimal
 36990 are displayed by converting them to the equivalent multibyte characters
 36991 according to the current language environment.  As a result, they are
 36992 displayed according to the current fontset.
 36993 
 36994 Note that this variable affects only how these bytes are displayed,
 36995 but does not change the fact they are interpreted as raw bytes.  */);
 36996   unibyte_display_via_language_environment = false;
 36997 
 36998   DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
 36999     doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
 37000 If a float, it specifies the maximum height in units of the
 37001 mini-window frame's height.
 37002 If an integer, it specifies the maximum height in units of the
 37003 mini-window frame's default font's height.  */);
 37004   Vmax_mini_window_height = make_float (0.25);
 37005 
 37006   DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
 37007     doc: /* How to resize mini-windows (the minibuffer and the echo area).
 37008 A value of nil means don't automatically resize mini-windows.
 37009 A value of t means resize them to fit the text displayed in them.
 37010 A value of `grow-only', the default, means let mini-windows grow only;
 37011 they return to their normal size when the minibuffer is closed, or the
 37012 echo area becomes empty.
 37013 
 37014 This variable does not affect resizing of the minibuffer window of
 37015 minibuffer-only frames.  These are handled by `resize-mini-frames'
 37016 only.  */);
 37017   /* Contrary to the doc string, we initialize this to nil, so that
 37018      loading loadup.el won't try to resize windows before loading
 37019      window.el, where some functions we need to call for this live.
 37020      We assign the 'grow-only' value right after loading window.el
 37021      during loadup.  */
 37022   Vresize_mini_windows = Qnil;
 37023 
 37024   DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
 37025     doc: /* Alist specifying how to blink the cursor off.
 37026 Each element has the form (ON-STATE . OFF-STATE).  Whenever the
 37027 `cursor-type' frame-parameter or variable equals ON-STATE,
 37028 comparing using `equal', Emacs uses OFF-STATE to specify
 37029 how to blink it off.  ON-STATE and OFF-STATE are values for
 37030 the `cursor-type' frame parameter.
 37031 
 37032 If a frame's ON-STATE has no entry in this list,
 37033 the frame's other specifications determine how to blink the cursor off.  */);
 37034   Vblink_cursor_alist = Qnil;
 37035 
 37036   DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
 37037     doc: /* Allow or disallow automatic horizontal scrolling of windows.
 37038 The value `current-line' means the line displaying point in each window
 37039 is automatically scrolled horizontally to make point visible.
 37040 Any other non-nil value means all the lines in a window are automatically
 37041 scrolled horizontally to make point visible.  */);
 37042   automatic_hscrolling = Qt;
 37043   DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
 37044   DEFSYM (Qcurrent_line, "current-line");
 37045 
 37046   DEFVAR_INT ("hscroll-margin", hscroll_margin,
 37047     doc: /* How many columns away from the window edge point is allowed to get
 37048 before automatic hscrolling will horizontally scroll the window.  */);
 37049   hscroll_margin = 5;
 37050 
 37051   DEFVAR_LISP ("hscroll-step", Vhscroll_step,
 37052     doc: /* How many columns to scroll the window when point gets too close to the edge.
 37053 When point is less than `hscroll-margin' columns from the window
 37054 edge, automatic hscrolling will scroll the window by the amount of columns
 37055 determined by this variable.  If its value is a positive integer, scroll that
 37056 many columns.  If it's a positive floating-point number, it specifies the
 37057 fraction of the window's width to scroll.  If it's nil or zero, point will be
 37058 centered horizontally after the scroll.  Any other value, including negative
 37059 numbers, are treated as if the value were zero.
 37060 
 37061 Automatic hscrolling always moves point outside the scroll margin, so if
 37062 point was more than scroll step columns inside the margin, the window will
 37063 scroll more than the value given by the scroll step.
 37064 
 37065 Note that the lower bound for automatic hscrolling specified by `scroll-left'
 37066 and `scroll-right' overrides this variable's effect.  */);
 37067   Vhscroll_step = make_fixnum (0);
 37068 
 37069   DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
 37070     doc: /* If non-nil, messages are truncated when displaying the echo area.
 37071 Bind this around calls to `message' to let it take effect.  */);
 37072   message_truncate_lines = false;
 37073 
 37074   DEFVAR_LISP ("menu-bar-update-hook",  Vmenu_bar_update_hook,
 37075     doc: /* Normal hook run to update the menu bar definitions.
 37076 Redisplay runs this hook before it redisplays the menu bar.
 37077 This is used to update menus such as Buffers, whose contents depend on
 37078 various data.  */);
 37079   Vmenu_bar_update_hook = Qnil;
 37080 
 37081   DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
 37082                doc: /* Frame for which we are updating a menu.
 37083 The enable predicate for a menu binding should check this variable.  */);
 37084   Vmenu_updating_frame = Qnil;
 37085 
 37086   DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
 37087     doc: /* Non-nil means don't update menu bars.  Internal use only.  */);
 37088   inhibit_menubar_update = false;
 37089 
 37090   DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
 37091     doc: /* Prefix prepended to all continuation lines at display time.
 37092 The value may be a string, an image, or a stretch-glyph; it is
 37093 interpreted in the same way as the value of a `display' text property.
 37094 
 37095 This variable is overridden by any `wrap-prefix' text or overlay
 37096 property.
 37097 
 37098 To add a prefix to non-continuation lines, use `line-prefix'.  */);
 37099   Vwrap_prefix = Qnil;
 37100   DEFSYM (Qwrap_prefix, "wrap-prefix");
 37101   Fmake_variable_buffer_local (Qwrap_prefix);
 37102 
 37103   DEFVAR_LISP ("line-prefix", Vline_prefix,
 37104     doc: /* Prefix prepended to all non-continuation lines at display time.
 37105 The value may be a string, an image, or a stretch-glyph; it is
 37106 interpreted in the same way as the value of a `display' text property.
 37107 
 37108 This variable is overridden by any `line-prefix' text or overlay
 37109 property.
 37110 
 37111 To add a prefix to continuation lines, use `wrap-prefix'.  */);
 37112   Vline_prefix = Qnil;
 37113   DEFSYM (Qline_prefix, "line-prefix");
 37114   Fmake_variable_buffer_local (Qline_prefix);
 37115 
 37116   DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
 37117     doc: /* Non-nil means display line numbers.
 37118 
 37119 If the value is t, display the absolute number of each line of a buffer
 37120 shown in a window.  Absolute line numbers count from the beginning of
 37121 the current narrowing, or from buffer beginning.  The variable
 37122 `display-line-numbers-offset', if non-zero, is a signed offset added
 37123 to each absolute line number; it also forces line numbers to be counted
 37124 from the beginning of the buffer, as if `display-line-numbers-widen'
 37125 were non-nil.  It has no effect when line numbers are not absolute.
 37126 
 37127 If the value is `relative', display for each line not containing the
 37128 window's point its relative number instead, i.e. the number of the line
 37129 relative to the line showing the window's point.
 37130 
 37131 In either case, line numbers are displayed at the beginning of each
 37132 non-continuation line that displays buffer text, i.e. after each newline
 37133 character that comes from the buffer.  The value `visual' is like
 37134 `relative' but counts screen lines instead of buffer lines.  In practice
 37135 this means that continuation lines count as well when calculating the
 37136 relative number of a line.
 37137 
 37138 Lisp programs can disable display of a line number of a particular
 37139 buffer line by putting the `display-line-numbers-disable' text property
 37140 or overlay property on the first visible character of that line.  */);
 37141   Vdisplay_line_numbers = Qnil;
 37142   DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
 37143   Fmake_variable_buffer_local (Qdisplay_line_numbers);
 37144   DEFSYM (Qrelative, "relative");
 37145   DEFSYM (Qvisual, "visual");
 37146 
 37147   DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
 37148     doc: /* Minimum width of space reserved for line number display.
 37149 A positive number means reserve that many columns for line numbers,
 37150 even if the actual number needs less space.
 37151 The default value of nil means compute the space dynamically.
 37152 Any other value is treated as nil.  */);
 37153   Vdisplay_line_numbers_width = Qnil;
 37154   DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
 37155   Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
 37156 
 37157   DEFVAR_LISP ("display-line-numbers-current-absolute",
 37158                Vdisplay_line_numbers_current_absolute,
 37159     doc: /* Non-nil means display absolute number of current line.
 37160 This variable has effect only when `display-line-numbers' is
 37161 either `relative' or `visual'.  */);
 37162   Vdisplay_line_numbers_current_absolute = Qt;
 37163 
 37164   DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
 37165     doc: /* Non-nil means display line numbers disregarding any narrowing.  */);
 37166   display_line_numbers_widen = false;
 37167   DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
 37168   Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
 37169 
 37170   DEFVAR_INT ("display-line-numbers-offset", display_line_numbers_offset,
 37171     doc: /* A signed integer added to each absolute line number.
 37172 When this variable is non-zero, line numbers are always counted from
 37173 the beginning of the buffer even if `display-line-numbers-widen' is nil.
 37174 It has no effect when set to 0, or when line numbers are not absolute.  */);
 37175   display_line_numbers_offset = 0;
 37176   DEFSYM (Qdisplay_line_numbers_offset, "display-line-numbers-offset");
 37177   Fmake_variable_buffer_local (Qdisplay_line_numbers_offset);
 37178 
 37179   DEFVAR_BOOL ("display-fill-column-indicator", display_fill_column_indicator,
 37180     doc: /* Non-nil means display the fill column indicator.
 37181 If you set this non-nil, make sure `display-fill-column-indicator-character'
 37182 is also non-nil.
 37183 See Info node `Displaying Boundaries' for details.  */);
 37184   display_fill_column_indicator = false;
 37185   DEFSYM (Qdisplay_fill_column_indicator, "display-fill-column-indicator");
 37186   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator);
 37187 
 37188   DEFVAR_LISP ("display-fill-column-indicator-column", Vdisplay_fill_column_indicator_column,
 37189     doc: /* Column for indicator when `display-fill-column-indicator' is non-nil.
 37190 The default value is t which means that the indicator
 37191 will use the `fill-column' variable.  If it is set to an integer the
 37192 indicator will be drawn in that column.
 37193 See Info node `Displaying Boundaries' for details.  */);
 37194   Vdisplay_fill_column_indicator_column = Qt;
 37195   DEFSYM (Qdisplay_fill_column_indicator_column, "display-fill-column-indicator-column");
 37196   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_column);
 37197 
 37198   DEFVAR_LISP ("display-fill-column-indicator-character", Vdisplay_fill_column_indicator_character,
 37199     doc: /* Character to draw the indicator when `display-fill-column-indicator' is non-nil.
 37200 A good candidate is U+2502, and an alternative is (ascii 124) if the
 37201 font of `fill-column-indicator' face does not support Unicode characters.
 37202 See Info node `Displaying Boundaries' for details.  */);
 37203   Vdisplay_fill_column_indicator_character = Qnil;
 37204   DEFSYM (Qdisplay_fill_column_indicator_character, "display-fill-column-indicator-character");
 37205   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_character);
 37206 
 37207   DEFVAR_INT ("display-line-numbers-major-tick", display_line_numbers_major_tick,
 37208     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37209 The line number is shown with the `line-number-major-tick' face.
 37210 Otherwise, no special highlighting is done every Nth line.
 37211 Note that major ticks take precedence over minor ticks.  */);
 37212   display_line_numbers_major_tick = 0;
 37213 
 37214   DEFVAR_INT ("display-line-numbers-minor-tick", display_line_numbers_minor_tick,
 37215     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37216 The line number is shown with the `line-number-minor-tick' face.
 37217 Otherwise, no special highlighting is done every Nth line.
 37218 Note that major ticks take precedence over minor ticks.  */);
 37219   display_line_numbers_minor_tick = 0;
 37220 
 37221   DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
 37222     doc: /* Non-nil means don't eval Lisp during redisplay.  */);
 37223   inhibit_eval_during_redisplay = false;
 37224 
 37225   DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
 37226     doc: /* Non-nil means don't free realized faces.  Internal use only.  */);
 37227   inhibit_free_realized_faces = false;
 37228 
 37229   DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
 37230     doc: /* Non-nil means don't mirror characters even when bidi context requires that.
 37231 Intended for use during debugging and for testing bidi display;
 37232 see biditest.el in the test suite.  */);
 37233   inhibit_bidi_mirroring = false;
 37234 
 37235   DEFVAR_BOOL ("bidi-inhibit-bpa", bidi_inhibit_bpa,
 37236     doc: /* Non-nil means inhibit the Bidirectional Parentheses Algorithm.
 37237 Disabling the BPA makes redisplay faster, but might produce incorrect
 37238 display reordering of bidirectional text with embedded parentheses and
 37239 other bracket characters whose `paired-bracket' Unicode property is
 37240 non-nil, see `get-char-code-property'.  */);
 37241   bidi_inhibit_bpa = false;
 37242 
 37243 #ifdef GLYPH_DEBUG
 37244   DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
 37245                doc: /* Inhibit try_window_id display optimization.  */);
 37246   inhibit_try_window_id = false;
 37247 
 37248   DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
 37249                doc: /* Inhibit try_window_reusing display optimization.  */);
 37250   inhibit_try_window_reusing = false;
 37251 
 37252   DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
 37253                doc: /* Inhibit try_cursor_movement display optimization.  */);
 37254   inhibit_try_cursor_movement = false;
 37255 #endif /* GLYPH_DEBUG */
 37256 
 37257   DEFVAR_INT ("overline-margin", overline_margin,
 37258                doc: /* Space between overline and text, in pixels.
 37259 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
 37260 margin to the character height.  */);
 37261   overline_margin = 2;
 37262 
 37263   DEFVAR_INT ("underline-minimum-offset",
 37264                underline_minimum_offset,
 37265      doc: /* Minimum distance between baseline and underline.
 37266 This can improve legibility of underlined text at small font sizes,
 37267 particularly when using variable `x-use-underline-position-properties'
 37268 with fonts that specify an UNDERLINE_POSITION relatively close to the
 37269 baseline.  The default value is 1.  */);
 37270   underline_minimum_offset = 1;
 37271   DEFSYM (Qunderline_minimum_offset, "underline-minimum-offset");
 37272 
 37273   DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
 37274                doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
 37275 This feature only works when on a window system that can change
 37276 cursor shapes.  */);
 37277   display_hourglass_p = true;
 37278 
 37279   DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
 37280                doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy.  */);
 37281   Vhourglass_delay = make_fixnum (DEFAULT_HOURGLASS_DELAY);
 37282 
 37283 #ifdef HAVE_WINDOW_SYSTEM
 37284   hourglass_atimer = NULL;
 37285   hourglass_shown_p = false;
 37286 #endif /* HAVE_WINDOW_SYSTEM */
 37287 
 37288   /* Name of the face used to display glyphless characters.  */
 37289   DEFSYM (Qglyphless_char, "glyphless-char");
 37290 
 37291   /* Method symbols for Vglyphless_char_display.  */
 37292   DEFSYM (Qhex_code, "hex-code");
 37293   DEFSYM (Qempty_box, "empty-box");
 37294   DEFSYM (Qthin_space, "thin-space");
 37295   DEFSYM (Qzero_width, "zero-width");
 37296 
 37297   DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
 37298                doc: /* Function run just before redisplay.
 37299 It is called with one argument, which is the set of windows that are to
 37300 be redisplayed.  This set can be nil (meaning, only the selected window),
 37301 or t (meaning all windows).  */);
 37302   Vpre_redisplay_function = intern ("ignore");
 37303 
 37304   /* Symbol for the purpose of Vglyphless_char_display.  */
 37305   DEFSYM (Qglyphless_char_display, "glyphless-char-display");
 37306   Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_fixnum (1));
 37307 
 37308   DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
 37309                doc: /* Char-table defining glyphless characters.
 37310 Each element, if non-nil, should be one of the following:
 37311   an ASCII acronym string: display this string in a box
 37312   `hex-code':   display the hexadecimal code of a character in a box
 37313   `empty-box':  display as an empty box
 37314   `thin-space': display as 1-pixel width space
 37315   `zero-width': don't display
 37316 Any other value is interpreted as `empty-box'.
 37317 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
 37318 display method for graphical terminals and text terminals respectively.
 37319 GRAPHICAL and TEXT should each have one of the values listed above.
 37320 
 37321 The char-table has one extra slot to control the display of characters
 37322 for which no font is found on graphical terminals, and characters that
 37323 cannot be displayed by text-mode terminals.  Its value should be an
 37324 ASCII acronym string, `hex-code', `empty-box', or `thin-space'.  It
 37325 could also be a cons cell of any two of these, to specify separate
 37326 values for graphical and text terminals.  The default is `empty-box'.
 37327 
 37328 With the obvious exception of `zero-width', all the other representations
 37329 are displayed using the face `glyphless-char'.
 37330 
 37331 If a character has a non-nil entry in an active display table, the
 37332 display table takes effect; in this case, Emacs does not consult
 37333 `glyphless-char-display' at all.  */);
 37334   Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
 37335   Fset_char_table_extra_slot (Vglyphless_char_display, make_fixnum (0),
 37336                               Qempty_box);
 37337 
 37338   DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
 37339                doc: /* If non-nil, debug if a message matching this regexp is displayed.  */);
 37340   Vdebug_on_message = Qnil;
 37341 
 37342   DEFVAR_LISP ("set-message-function", Vset_message_function,
 37343                doc: /* If non-nil, function to handle display of echo-area messages.
 37344 The function is called with one argument that is the text of a message.
 37345 If this function returns nil, the message is displayed in the echo area
 37346 as usual.  If the function returns a string, the returned string is
 37347 displayed in the echo area.  If this function returns any other non-nil
 37348 value, this means that the message was already handled, and the original
 37349 message text will not be displayed in the echo area.
 37350 
 37351 Also see `clear-message-function' (which can be used to clear the
 37352 message displayed by this function), and `command-error-function'
 37353 (which controls how error messages are displayed).  */);
 37354   Vset_message_function = Qnil;
 37355 
 37356   DEFSYM (Qdont_clear_message, "dont-clear-message");
 37357   DEFVAR_LISP ("clear-message-function", Vclear_message_function,
 37358                doc: /* If non-nil, function to clear echo-area messages.
 37359 Usually this function is called when the next input event arrives.
 37360 It is expected to clear the message displayed by its counterpart
 37361 function specified by `set-message-function'.
 37362 
 37363 The function is called without arguments.
 37364 
 37365 If this function returns a value that isn't `dont-clear-message', the
 37366 message is cleared from the echo area as usual.  If this function
 37367 returns `dont-clear-message', this means that the message was already
 37368 handled, and the original message text will not be cleared from the
 37369 echo area.  */);
 37370   Vclear_message_function = Qnil;
 37371 
 37372   DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
 37373                doc: /* Code of the cause for redisplaying all windows.
 37374 Internal use only.  */);
 37375   Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
 37376 
 37377   DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
 37378                doc: /* Code of the cause for redisplaying mode lines.
 37379 Internal use only.  */);
 37380   Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
 37381 
 37382   DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
 37383      doc: /* Non-nil means it is not safe to attempt bidi reordering for display.  */);
 37384   /* Initialize to t, since we need to disable reordering until
 37385      loadup.el successfully loads charprop.el.  */
 37386   redisplay__inhibit_bidi = true;
 37387 
 37388   DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
 37389     doc: /* Non-nil means display raw bytes in hexadecimal format.
 37390 The default is to use octal format (\\200) whereas hexadecimal (\\x80)
 37391 may be more familiar to users.  */);
 37392   display_raw_bytes_as_hex = false;
 37393 
 37394   DEFVAR_BOOL ("mouse-fine-grained-tracking", mouse_fine_grained_tracking,
 37395     doc: /* Non-nil for pixel-wise mouse-movement.
 37396 When nil, mouse-movement events will not be generated as long as the
 37397 mouse stays within the extent of a single glyph (except for images).  */);
 37398   mouse_fine_grained_tracking = false;
 37399 
 37400   DEFVAR_BOOL ("tab-bar--dragging-in-progress", tab_bar__dragging_in_progress,
 37401     doc: /* Non-nil when maybe dragging tab bar item.  */);
 37402   tab_bar__dragging_in_progress = false;
 37403 
 37404   DEFVAR_BOOL ("redisplay-skip-initial-frame", redisplay_skip_initial_frame,
 37405     doc: /* Non-nil means skip redisplay of the initial frame.
 37406 The initial frame is the text-mode frame used by Emacs internally during
 37407 the early stages of startup.  That frame is not displayed anywhere, so
 37408 skipping it is best except in special circumstances such as running
 37409 redisplay tests in batch mode.   */);
 37410   redisplay_skip_initial_frame = true;
 37411 
 37412   DEFVAR_BOOL ("redisplay-skip-fontification-on-input",
 37413                redisplay_skip_fontification_on_input,
 37414     doc: /* Skip `fontification_functions` when there is input pending.
 37415 If non-nil and there was input pending at the beginning of the command,
 37416 the `fontification_functions` hook is not run.  This usually does not
 37417 affect the display because redisplay is completely skipped anyway if input
 37418 was pending, but it can make scrolling smoother by avoiding
 37419 unnecessary fontification.
 37420 It is similar to `fast-but-imprecise-scrolling' with similar tradeoffs,
 37421 but with the advantage that it should only affect the behavior when Emacs
 37422 has trouble keeping up with the incoming input rate.  */);
 37423   redisplay_skip_fontification_on_input = false;
 37424 
 37425   DEFVAR_BOOL ("redisplay-adhoc-scroll-in-resize-mini-windows",
 37426                redisplay_adhoc_scroll_in_resize_mini_windows,
 37427     doc: /* If nil always use normal scrolling in minibuffer windows.
 37428 Otherwise, use custom-tailored code after resizing minibuffer windows to try
 37429 and display the most important part of the minibuffer.   */);
 37430   /* See bug#43519 for some discussion around this.  */
 37431   redisplay_adhoc_scroll_in_resize_mini_windows = true;
 37432 
 37433   DEFVAR_BOOL ("composition-break-at-point", composition_break_at_point,
 37434     doc: /* If non-nil, prevent auto-composition of characters around point.
 37435 This makes it easier to edit character sequences that are
 37436 composed on display.  */);
 37437   composition_break_at_point = false;
 37438 
 37439   DEFVAR_INT ("max-redisplay-ticks", max_redisplay_ticks,
 37440     doc: /* Maximum number of redisplay ticks before aborting redisplay of a window.
 37441 
 37442 This allows to abort the display of a window if the amount of low-level
 37443 redisplay operations exceeds the value of this variable.  When display of
 37444 a window is aborted due to this reason, the buffer shown in that window
 37445 will not have its windows redisplayed until the buffer is modified or until
 37446 you type \\[recenter-top-bottom] with one of its windows selected.
 37447 You can also decide to kill the buffer and visit it in some
 37448 other way, like under `so-long-mode' or literally.
 37449 
 37450 The default value is zero, which disables this feature.
 37451 The recommended non-zero value is between 100000 and 1000000,
 37452 depending on your patience and the speed of your system.  */);
 37453   max_redisplay_ticks = 0;
 37454 }
 37455 
 37456 
 37457 /* Initialize this module when Emacs starts.  */
 37458 
 37459 void
 37460 init_xdisp (void)
 37461 {
 37462   CHARPOS (this_line_start_pos) = 0;
 37463 
 37464   echo_area_window = minibuf_window;
 37465 
 37466   if (!noninteractive)
 37467     {
 37468       struct window *m = XWINDOW (minibuf_window);
 37469       Lisp_Object frame = m->frame;
 37470       struct frame *f = XFRAME (frame);
 37471       Lisp_Object root = FRAME_ROOT_WINDOW (f);
 37472       struct window *r = XWINDOW (root);
 37473       int i;
 37474 
 37475       r->top_line = FRAME_TOP_MARGIN (f);
 37476       r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
 37477       r->total_cols = FRAME_COLS (f);
 37478       r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
 37479       r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
 37480       r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
 37481 
 37482       m->top_line = FRAME_TOTAL_LINES (f) - 1;
 37483       m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
 37484       m->total_cols = FRAME_COLS (f);
 37485       m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
 37486       m->total_lines = 1;
 37487       m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
 37488 
 37489       scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
 37490       scratch_glyph_row.glyphs[TEXT_AREA + 1]
 37491         = scratch_glyphs + MAX_SCRATCH_GLYPHS;
 37492 
 37493       /* The default ellipsis glyphs `...'.  */
 37494       for (i = 0; i < 3; ++i)
 37495         default_invis_vector[i] = make_fixnum ('.');
 37496     }
 37497 
 37498   {
 37499     /* Allocate the buffer for frame titles.
 37500        Also used for `format-mode-line'.  */
 37501     int size = 100;
 37502     mode_line_noprop_buf = xmalloc (size);
 37503     mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 37504     mode_line_noprop_ptr = mode_line_noprop_buf;
 37505     mode_line_target = MODE_LINE_DISPLAY;
 37506   }
 37507 
 37508   help_echo_showing_p = false;
 37509 }
 37510 
 37511 #ifdef HAVE_WINDOW_SYSTEM
 37512 
 37513 /* Platform-independent portion of hourglass implementation.  */
 37514 
 37515 /* Timer function of hourglass_atimer.  */
 37516 
 37517 static void
 37518 show_hourglass (struct atimer *timer)
 37519 {
 37520   /* The timer implementation will cancel this timer automatically
 37521      after this function has run.  Set hourglass_atimer to null
 37522      so that we know the timer doesn't have to be canceled.  */
 37523   hourglass_atimer = NULL;
 37524 
 37525   if (!hourglass_shown_p)
 37526     {
 37527       Lisp_Object tail, frame;
 37528 
 37529       block_input ();
 37530 
 37531       FOR_EACH_FRAME (tail, frame)
 37532         {
 37533           struct frame *f = XFRAME (frame);
 37534 
 37535           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37536               && FRAME_RIF (f)->show_hourglass)
 37537             FRAME_RIF (f)->show_hourglass (f);
 37538         }
 37539 
 37540       hourglass_shown_p = true;
 37541       unblock_input ();
 37542     }
 37543 }
 37544 
 37545 /* Cancel a currently active hourglass timer, and start a new one.  */
 37546 
 37547 void
 37548 start_hourglass (void)
 37549 {
 37550   struct timespec delay;
 37551 
 37552   cancel_hourglass ();
 37553 
 37554   if (FIXNUMP (Vhourglass_delay)
 37555       && XFIXNUM (Vhourglass_delay) > 0)
 37556     delay = make_timespec (min (XFIXNUM (Vhourglass_delay),
 37557                                   TYPE_MAXIMUM (time_t)),
 37558                            0);
 37559   else if (FLOATP (Vhourglass_delay)
 37560            && XFLOAT_DATA (Vhourglass_delay) > 0)
 37561     delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
 37562   else
 37563     delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
 37564 
 37565   hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
 37566                                    show_hourglass, NULL);
 37567 }
 37568 
 37569 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
 37570    shown.  */
 37571 
 37572 void
 37573 cancel_hourglass (void)
 37574 {
 37575   if (hourglass_atimer)
 37576     {
 37577       cancel_atimer (hourglass_atimer);
 37578       hourglass_atimer = NULL;
 37579     }
 37580 
 37581   if (hourglass_shown_p)
 37582     {
 37583       Lisp_Object tail, frame;
 37584 
 37585       block_input ();
 37586 
 37587       FOR_EACH_FRAME (tail, frame)
 37588         {
 37589           struct frame *f = XFRAME (frame);
 37590 
 37591           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37592               && FRAME_RIF (f)->hide_hourglass)
 37593             FRAME_RIF (f)->hide_hourglass (f);
 37594 #ifdef HAVE_NTGUI
 37595           /* No cursors on non GUI frames - restore to stock arrow cursor.  */
 37596           else if (!FRAME_W32_P (f))
 37597             w32_arrow_cursor ();
 37598 #endif
 37599         }
 37600 
 37601       hourglass_shown_p = false;
 37602       unblock_input ();
 37603     }
 37604 }
 37605 
 37606 /* Return a correction to be applied to G->pixel_width when it is
 37607    displayed in MOUSE_FACE.  This is needed for the first and the last
 37608    glyphs of text inside a face with :box when it is displayed with
 37609    MOUSE_FACE that has a different or no :box attribute.
 37610    ORIGINAL_FACE is the face G was originally drawn in, and MOUSE_FACE
 37611    is the face it will be drawn in now.  ROW is the G's glyph row and
 37612    W is its window.  */
 37613 static int
 37614 adjust_glyph_width_for_mouse_face (struct glyph *g, struct glyph_row *row,
 37615                                    struct window *w,
 37616                                    struct face *original_face,
 37617                                    struct face *mouse_face)
 37618 {
 37619   int sum = 0;
 37620 
 37621   bool do_left_box_p = g->left_box_line_p;
 37622   bool do_right_box_p = g->right_box_line_p;
 37623 
 37624   /* This is required because we test some parameters of the image
 37625      slice before applying the box in produce_image_glyph.  */
 37626   if (g->type == IMAGE_GLYPH)
 37627     {
 37628       if (!row->reversed_p)
 37629         {
 37630           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 37631                                              g->u.img_id);
 37632           do_left_box_p = g->left_box_line_p &&
 37633             g->slice.img.x == 0;
 37634           do_right_box_p = g->right_box_line_p &&
 37635             g->slice.img.x + g->slice.img.width == img->width;
 37636         }
 37637       else
 37638         {
 37639           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 37640                                              g->u.img_id);
 37641           do_left_box_p = g->left_box_line_p &&
 37642             g->slice.img.x + g->slice.img.width == img->width;
 37643           do_right_box_p = g->right_box_line_p &&
 37644             g->slice.img.x == 0;
 37645         }
 37646     }
 37647 
 37648   /* If the glyph has a left box line, subtract it from the offset.  */
 37649   if (do_left_box_p)
 37650     sum -= max (0, original_face->box_vertical_line_width);
 37651   /* Likewise with the right box line, as there may be a
 37652      box there as well.  */
 37653   if (do_right_box_p)
 37654     sum -= max (0, original_face->box_vertical_line_width);
 37655   /* Now add the line widths from the new face.  */
 37656   if (g->left_box_line_p)
 37657     sum += max (0, mouse_face->box_vertical_line_width);
 37658   if (g->right_box_line_p)
 37659     sum += max (0, mouse_face->box_vertical_line_width);
 37660 
 37661   return sum;
 37662 }
 37663 
 37664 /* Get the offset due to mouse-highlight to apply before drawing
 37665    phys_cursor, and return it in OFFSET.  ROW should be the row that
 37666    is under mouse face and contains the phys cursor.
 37667 
 37668    This is required because the produce_XXX_glyph series of functions
 37669    add the width of the various vertical box lines to the total width
 37670    of the glyphs, but that must be updated when the row is put under
 37671    mouse face, which can have different box dimensions.  */
 37672 static void
 37673 get_cursor_offset_for_mouse_face (struct window *w, struct glyph_row *row,
 37674                                   int *offset)
 37675 {
 37676   int sum = 0;
 37677   /* Return because the mode line can't possibly have a cursor. */
 37678   if (row->mode_line_p)
 37679     return;
 37680 
 37681   block_input ();
 37682 
 37683   struct frame *f = WINDOW_XFRAME (w);
 37684   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 37685   struct glyph *start, *end;
 37686   struct face *mouse_face = FACE_FROM_ID (f, hlinfo->mouse_face_face_id);
 37687   int hpos = w->phys_cursor.hpos;
 37688   end = &row->glyphs[TEXT_AREA][hpos];
 37689 
 37690   if (!row->reversed_p)
 37691     {
 37692       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 37693           hlinfo->mouse_face_beg_row)
 37694         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_beg_col];
 37695       else
 37696         start = row->glyphs[TEXT_AREA];
 37697     }
 37698   else
 37699     {
 37700       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 37701           hlinfo->mouse_face_end_row)
 37702         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_end_col];
 37703       else
 37704         start = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
 37705     }
 37706 
 37707   /* Calculate the offset by which to correct phys_cursor x if we are
 37708      drawing the cursor inside mouse-face highlighted text.  */
 37709 
 37710   for ( ; row->reversed_p ? start > end : start < end;
 37711           row->reversed_p ? --start : ++start)
 37712     sum += adjust_glyph_width_for_mouse_face (start, row, w,
 37713                                               FACE_FROM_ID (f, start->face_id),
 37714                                               mouse_face);
 37715 
 37716   if (row->reversed_p)
 37717     sum = -sum;
 37718 
 37719   *offset = sum;
 37720 
 37721   unblock_input ();
 37722 }
 37723 #endif /* HAVE_WINDOW_SYSTEM */

/* [<][>][^][v][top][bottom][index][help] */